Code cleanup

This commit is contained in:
nathan 2017-09-25 21:56:45 +02:00
parent 4f7acde039
commit 37adecd68a
5 changed files with 42 additions and 43 deletions

View File

@ -38,10 +38,10 @@ import dorkbox.network.connection.ping.PingTuple;
import dorkbox.network.connection.wrapper.ChannelNetworkWrapper; import dorkbox.network.connection.wrapper.ChannelNetworkWrapper;
import dorkbox.network.connection.wrapper.ChannelNull; import dorkbox.network.connection.wrapper.ChannelNull;
import dorkbox.network.connection.wrapper.ChannelWrapper; import dorkbox.network.connection.wrapper.ChannelWrapper;
import dorkbox.network.rmi.RMI;
import dorkbox.network.rmi.RemoteObject; import dorkbox.network.rmi.RemoteObject;
import dorkbox.network.rmi.RemoteObjectCallback; import dorkbox.network.rmi.RemoteObjectCallback;
import dorkbox.network.rmi.RmiImplHandler; import dorkbox.network.rmi.Rmi;
import dorkbox.network.rmi.RmiBridge;
import dorkbox.network.rmi.RmiRegistration; import dorkbox.network.rmi.RmiRegistration;
import dorkbox.network.util.CryptoSerializationManager; import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.collections.IntMap; import dorkbox.util.collections.IntMap;
@ -108,7 +108,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
// //
// RMI fields // RMI fields
// //
private final RmiImplHandler rmiImplHandler; private final RmiBridge rmiBridge;
private final Map<Integer, RemoteObject> proxyIdCache = new WeakHashMap<Integer, RemoteObject>(8); private final Map<Integer, RemoteObject> proxyIdCache = new WeakHashMap<Integer, RemoteObject>(8);
private final IntMap<RemoteObjectCallback> rmiRegistrationCallbacks = new IntMap<>(); private final IntMap<RemoteObjectCallback> rmiRegistrationCallbacks = new IntMap<>();
private int rmiRegistrationID = 0; // protected by synchronized (rmiRegistrationCallbacks) private int rmiRegistrationID = 0; // protected by synchronized (rmiRegistrationCallbacks)
@ -118,10 +118,10 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
*/ */
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
public public
ConnectionImpl(final Logger logger, final EndPoint endPoint, final RmiImplHandler rmiImplHandler) { ConnectionImpl(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) {
this.logger = logger; this.logger = logger;
this.endPoint = endPoint; this.endPoint = endPoint;
this.rmiImplHandler = rmiImplHandler; this.rmiBridge = rmiBridge;
} }
/** /**
@ -1097,7 +1097,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
// we use kryo to create a new instance - so only return it on error or when it's done creating a new instance // we use kryo to create a new instance - so only return it on error or when it's done creating a new instance
manager.returnKryo(kryo); manager.returnKryo(kryo);
rmiImplHandler.register(rmiImplHandler.nextObjectId(), remotePrimaryObject); rmiBridge.register(rmiBridge.nextObjectId(), remotePrimaryObject);
LinkedList<ClassObject> remoteClasses = new LinkedList<ClassObject>(); LinkedList<ClassObject> remoteClasses = new LinkedList<ClassObject>();
remoteClasses.add(new ClassObject(implementationClass, remotePrimaryObject)); remoteClasses.add(new ClassObject(implementationClass, remotePrimaryObject));
@ -1106,7 +1106,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
while ((remoteClassObject = remoteClasses.pollFirst()) != null) { while ((remoteClassObject = remoteClasses.pollFirst()) != null) {
// we have to check for any additional fields that will have proxy information // we have to check for any additional fields that will have proxy information
for (Field field : remoteClassObject.clazz.getDeclaredFields()) { for (Field field : remoteClassObject.clazz.getDeclaredFields()) {
if (field.getAnnotation(RMI.class) != null) { if (field.getAnnotation(Rmi.class) != null) {
boolean prev = field.isAccessible(); boolean prev = field.isAccessible();
field.setAccessible(true); field.setAccessible(true);
final Object o = field.get(remoteClassObject.object); final Object o = field.get(remoteClassObject.object);
@ -1114,7 +1114,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
final Class<?> type = field.getType(); final Class<?> type = field.getType();
rmiImplHandler.register(rmiImplHandler.nextObjectId(), o); rmiBridge.register(rmiBridge.nextObjectId(), o);
remoteClasses.offerLast(new ClassObject(type, o)); remoteClasses.offerLast(new ClassObject(type, o));
} }
} }
@ -1126,7 +1126,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
connection.TCP(new RmiRegistration(rmiID)).flush(); connection.TCP(new RmiRegistration(rmiID)).flush();
} }
} }
else if (remoteRegistration.remoteObjectId > RmiImplHandler.INVALID_RMI) { else if (remoteRegistration.remoteObjectId > RmiBridge.INVALID_RMI) {
// THIS IS ON THE REMOTE CONNECTION (where the object will really exist) // THIS IS ON THE REMOTE CONNECTION (where the object will really exist)
// //
// GET a LOCAL rmi object, if none get a specific, GLOBAL rmi object (objects that are not bound to a single connection). // GET a LOCAL rmi object, if none get a specific, GLOBAL rmi object (objects that are not bound to a single connection).
@ -1167,15 +1167,15 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
public public
<T> int getRegisteredId(final T object) { <T> int getRegisteredId(final T object) {
// always check local before checking global, because less contention on the synchronization // always check local before checking global, because less contention on the synchronization
RmiImplHandler globalRmiImplHandler = endPoint.globalRmiImplHandler; RmiBridge globalRmiBridge = endPoint.globalRmiBridge;
if (globalRmiImplHandler == null) { if (globalRmiBridge == null) {
throw new NullPointerException("Unable to call 'getRegisteredId' when the globalRmiImplHandler is null!"); throw new NullPointerException("Unable to call 'getRegisteredId' when the globalRmiBridge is null!");
} }
int object1 = globalRmiImplHandler.getRegisteredId(object); int object1 = globalRmiBridge.getRegisteredId(object);
if (object1 == Integer.MAX_VALUE) { if (object1 == Integer.MAX_VALUE) {
return rmiImplHandler.getRegisteredId(object); return rmiBridge.getRegisteredId(object);
} else { } else {
return object1; return object1;
} }
@ -1196,7 +1196,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
if (remoteObject == null) { if (remoteObject == null) {
// duplicates are fine, as they represent the same object (as specified by the ID) on the remote side. // duplicates are fine, as they represent the same object (as specified by the ID) on the remote side.
remoteObject = rmiImplHandler.createProxyObject(this, objectID, type); remoteObject = rmiBridge.createProxyObject(this, objectID, type);
proxyIdCache.put(objectID, remoteObject); proxyIdCache.put(objectID, remoteObject);
} }
@ -1210,16 +1210,16 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements ICryptoConn
@Override @Override
public public
Object getImplementationObject(final int objectID) { Object getImplementationObject(final int objectID) {
if (RmiImplHandler.isGlobal(objectID)) { if (RmiBridge.isGlobal(objectID)) {
RmiImplHandler globalRmiImplHandler = endPoint.globalRmiImplHandler; RmiBridge globalRmiBridge = endPoint.globalRmiBridge;
if (globalRmiImplHandler == null) { if (globalRmiBridge == null) {
throw new NullPointerException("Unable to call 'getRegisteredId' when the gloablRmiBridge is null!"); throw new NullPointerException("Unable to call 'getRegisteredId' when the gloablRmiBridge is null!");
} }
return globalRmiImplHandler.getRegisteredObject(objectID); return globalRmiBridge.getRegisteredObject(objectID);
} else { } else {
return rmiImplHandler.getRegisteredObject(objectID); return rmiBridge.getRegisteredObject(objectID);
} }
} }
} }

View File

@ -42,12 +42,11 @@ import dorkbox.network.connection.wrapper.ChannelNetworkWrapper;
import dorkbox.network.connection.wrapper.ChannelWrapper; import dorkbox.network.connection.wrapper.ChannelWrapper;
import dorkbox.network.pipeline.KryoEncoder; import dorkbox.network.pipeline.KryoEncoder;
import dorkbox.network.pipeline.KryoEncoderCrypto; import dorkbox.network.pipeline.KryoEncoderCrypto;
import dorkbox.network.rmi.RmiImplHandler; import dorkbox.network.rmi.RmiBridge;
import dorkbox.network.store.NullSettingsStore; import dorkbox.network.store.NullSettingsStore;
import dorkbox.network.store.SettingsStore; import dorkbox.network.store.SettingsStore;
import dorkbox.util.OS; import dorkbox.util.OS;
import dorkbox.util.Property; import dorkbox.util.Property;
import dorkbox.util.SerializationManager;
import dorkbox.util.crypto.CryptoECC; import dorkbox.util.crypto.CryptoECC;
import dorkbox.util.entropy.Entropy; import dorkbox.util.entropy.Entropy;
import dorkbox.util.exceptions.InitializationException; import dorkbox.util.exceptions.InitializationException;
@ -164,7 +163,7 @@ class EndPoint<C extends Connection> {
final ECPublicKeyParameters publicKey; final ECPublicKeyParameters publicKey;
final SecureRandom secureRandom; final SecureRandom secureRandom;
final RmiImplHandler globalRmiImplHandler; final RmiBridge globalRmiBridge;
private final CountDownLatch blockUntilDone = new CountDownLatch(1); private final CountDownLatch blockUntilDone = new CountDownLatch(1);
@ -322,10 +321,10 @@ class EndPoint<C extends Connection> {
if (this.rmiEnabled) { if (this.rmiEnabled) {
// these register the listener for registering a class implementation for RMI (internal use only) // these register the listener for registering a class implementation for RMI (internal use only)
this.connectionManager.add(new RegisterRmiSystemListener()); this.connectionManager.add(new RegisterRmiSystemListener());
this.globalRmiImplHandler = new RmiImplHandler(logger, options.rmiExecutor, true); this.globalRmiBridge = new RmiBridge(logger, options.rmiExecutor, true);
} }
else { else {
this.globalRmiImplHandler = null; this.globalRmiBridge = null;
} }
serializationManager.finishInit(); serializationManager.finishInit();
@ -420,7 +419,7 @@ class EndPoint<C extends Connection> {
* Returns the serialization wrapper if there is an object type that needs to be added outside of the basics. * Returns the serialization wrapper if there is an object type that needs to be added outside of the basics.
*/ */
public public
SerializationManager getSerialization() { dorkbox.network.util.CryptoSerializationManager getSerialization() {
return this.serializationManager; return this.serializationManager;
} }
@ -434,8 +433,8 @@ class EndPoint<C extends Connection> {
* @return a new network connection * @return a new network connection
*/ */
protected protected
ConnectionImpl newConnection(final Logger logger, final EndPoint<C> endPoint, final RmiImplHandler rmiImplHandler) { ConnectionImpl newConnection(final Logger logger, final EndPoint<C> endPoint, final RmiBridge rmiBridge) {
return new ConnectionImpl(logger, endPoint, rmiImplHandler); return new ConnectionImpl(logger, endPoint, rmiBridge);
} }
/** /**
@ -450,9 +449,9 @@ class EndPoint<C extends Connection> {
Connection connection0(MetaChannel metaChannel) { Connection connection0(MetaChannel metaChannel) {
ConnectionImpl connection; ConnectionImpl connection;
RmiImplHandler rmiImplHandler = null; RmiBridge rmiBridge = null;
if (metaChannel != null && rmiEnabled) { if (metaChannel != null && rmiEnabled) {
rmiImplHandler = new RmiImplHandler(logger, rmiExecutor, false); rmiBridge = new RmiBridge(logger, rmiExecutor, false);
} }
// setup the extras needed by the network connection. // setup the extras needed by the network connection.
@ -461,7 +460,7 @@ class EndPoint<C extends Connection> {
if (metaChannel != null) { if (metaChannel != null) {
ChannelWrapper<C> wrapper; ChannelWrapper<C> wrapper;
connection = newConnection(logger, this, rmiImplHandler); connection = newConnection(logger, this, rmiBridge);
metaChannel.connection = connection; metaChannel.connection = connection;
if (metaChannel.localChannel != null) { if (metaChannel.localChannel != null) {
@ -479,10 +478,10 @@ class EndPoint<C extends Connection> {
// now initialize the connection channels with whatever extra info they might need. // now initialize the connection channels with whatever extra info they might need.
connection.init(wrapper, (ConnectionManager<Connection>) this.connectionManager); connection.init(wrapper, (ConnectionManager<Connection>) this.connectionManager);
if (rmiImplHandler != null) { if (rmiBridge != null) {
// notify our remote object space that it is able to receive method calls. // notify our remote object space that it is able to receive method calls.
connection.listeners() connection.listeners()
.add(rmiImplHandler.getListener()); .add(rmiBridge.getListener());
} }
} }
else { else {
@ -800,8 +799,8 @@ class EndPoint<C extends Connection> {
*/ */
public public
<T> int createGlobalObject(final T globalObject) { <T> int createGlobalObject(final T globalObject) {
int globalObjectId = globalRmiImplHandler.nextObjectId(); int globalObjectId = globalRmiBridge.nextObjectId();
globalRmiImplHandler.register(globalObjectId, globalObject); globalRmiBridge.register(globalObjectId, globalObject);
return globalObjectId; return globalObjectId;
} }
} }

View File

@ -25,7 +25,7 @@ class RegisterRmiSystemListener implements Listener.OnMessageReceived<Connection
@Override @Override
public public
void received(final ConnectionImpl connection, final RmiRegistration registerClass) { void received(final ConnectionImpl connection, final RmiRegistration registerClass) {
// register this into the RmiImplHandler // register this into the RmiBridge
connection.registerInternal(connection, registerClass); connection.registerInternal(connection, registerClass);
} }
} }

View File

@ -99,7 +99,7 @@ class RegistrationWrapper<C extends Connection> implements UdpServer {
*/ */
public public
boolean rmiEnabled() { boolean rmiEnabled() {
return endPoint.globalRmiImplHandler != null; return endPoint.globalRmiBridge != null;
} }
public public

View File

@ -36,7 +36,7 @@ import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.EndPoint; import dorkbox.network.connection.EndPoint;
import dorkbox.network.connection.Listener; import dorkbox.network.connection.Listener;
import dorkbox.network.connection.ListenerBridge; import dorkbox.network.connection.ListenerBridge;
import dorkbox.network.rmi.RmiImplHandler; import dorkbox.network.rmi.RmiBridge;
import dorkbox.util.exceptions.InitializationException; import dorkbox.util.exceptions.InitializationException;
import dorkbox.util.exceptions.SecurityException; import dorkbox.util.exceptions.SecurityException;
@ -64,8 +64,8 @@ class ListenerTest extends BaseTest {
// quick and dirty test to also test connection sub-classing // quick and dirty test to also test connection sub-classing
class TestConnectionA extends ConnectionImpl { class TestConnectionA extends ConnectionImpl {
public public
TestConnectionA(final Logger logger, final EndPoint endPoint, final RmiImplHandler rmiImplHandler) { TestConnectionA(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) {
super(logger, endPoint, rmiImplHandler); super(logger, endPoint, rmiBridge);
} }
public public
@ -77,8 +77,8 @@ class ListenerTest extends BaseTest {
class TestConnectionB extends TestConnectionA { class TestConnectionB extends TestConnectionA {
public public
TestConnectionB(final Logger logger, final EndPoint endPoint, final RmiImplHandler rmiImplHandler) { TestConnectionB(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) {
super(logger, endPoint, rmiImplHandler); super(logger, endPoint, rmiBridge);
} }
@Override @Override
@ -109,7 +109,7 @@ class ListenerTest extends BaseTest {
Server server = new Server(configuration) { Server server = new Server(configuration) {
@Override @Override
public public
TestConnectionA newConnection(final Logger logger, final EndPoint endPoint, final RmiImplHandler rmiBridge) { TestConnectionA newConnection(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) {
return new TestConnectionA(logger, endPoint, rmiBridge); return new TestConnectionA(logger, endPoint, rmiBridge);
} }
}; };