Code cleanup. Removed unnecessary methods from Connection interface

This commit is contained in:
nathan 2015-07-22 10:35:45 +02:00
parent f3fef6fcea
commit 61545f0235
19 changed files with 77 additions and 128 deletions

View File

@ -15,8 +15,6 @@
*/
package dorkbox.network.connection;
import org.bouncycastle.crypto.params.ParametersWithIV;
import dorkbox.network.connection.bridge.ConnectionBridge;
import dorkbox.network.connection.idle.IdleBridge;
import dorkbox.network.connection.idle.IdleSender;
@ -27,24 +25,6 @@ import dorkbox.util.exceptions.NetException;
@SuppressWarnings("unused")
public
interface Connection {
/**
* Initialize the connection with any extra info that is needed but was unavailable at the channel construction.
* <p/>
* This happens BEFORE prep.
*/
void init(Bridge bridge);
/**
* Prepare the channel wrapper, since it doesn't have access to certain fields during it's initialization.
* <p/>
* This happens AFTER init.
*/
void prep();
/**
* @return the AES key/IV, etc associated with this connection
*/
ParametersWithIV getCryptoParameters();
/**
* Has the remote ECC public key changed. This can be useful if specific actions are necessary when the key has changed.

View File

@ -103,9 +103,9 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements Connection,
/**
* Initialize the connection with any extra info that is needed but was unavailable at the channel construction.
* <p/>
* This happens BEFORE prep.
*/
@Override
public
void init(final Bridge bridge) {
if (bridge != null) {
this.sessionManager = bridge.sessionManager;
@ -126,10 +126,10 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements Connection,
}
/**
* Prepare the channel wrapper, since it doesn't have access to certain fields during it's construction.
* Prepare the channel wrapper, since it doesn't have access to certain fields during it's initialization.
* <p/>
* This happens AFTER init.
*/
@Override
public
void prep() {
if (this.channelWrapper != null) {
this.channelWrapper.init();
@ -140,8 +140,7 @@ class ConnectionImpl extends ChannelInboundHandlerAdapter implements Connection,
/**
* @return the AES key/IV, etc associated with this connection
*/
@Override
public final
final
ParametersWithIV getCryptoParameters() {
return this.channelWrapper.cryptoParameters();
}

View File

@ -77,7 +77,6 @@ class EndPoint {
// TODO: maybe some sort of STUN-like connection keep-alive??
// TODO: do we really need this? Maybe?
public static final String LOCAL_CHANNEL = "local_channel";
protected static final String shutdownHookName = "::SHUTDOWN_HOOK::";
protected static final String stopTreadName = "::STOP_THREAD::";
@ -417,14 +416,14 @@ class EndPoint {
/**
* This method allows the connections used by the client/server to be subclassed (custom implementations).
* <p/>
* As this is for the network stack, the new connection MUST subclass {@link Connection}
* As this is for the network stack, the new connection MUST subclass {@link ConnectionImpl}
* <p/>
* The parameters are ALL NULL when getting the base class, as this instance is just thrown away.
*
* @return a new network connection
*/
public
Connection newConnection(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) {
ConnectionImpl newConnection(final Logger logger, final EndPoint endPoint, final RmiBridge rmiBridge) {
return new ConnectionImpl(logger, endPoint, rmiBridge);
}
@ -437,7 +436,7 @@ class EndPoint {
*/
protected final
Connection connection0(MetaChannel metaChannel) {
Connection connection;
ConnectionImpl connection;
RmiBridge rmiBridge = null;
if (metaChannel != null && rmiEnabled) {
@ -490,7 +489,7 @@ class EndPoint {
* <p/>
* Only the CLIENT injects in front of this)
*/
void connectionConnected0(Connection connection) {
void connectionConnected0(ConnectionImpl connection) {
this.isConnected.set(true);
// prep the channel wrapper

View File

@ -131,7 +131,7 @@ class EndPointClient extends EndPoint implements Runnable {
*/
@Override
final
void connectionConnected0(Connection connection) {
void connectionConnected0(ConnectionImpl connection) {
// invokes the listener.connection() method, and initialize the connection channels with whatever extra info they might need.
super.connectionConnected0(connection);

View File

@ -737,7 +737,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/
@Override
public final
void writeWithCryptoTcp(Connection connection, ByteBuf buffer, Object message) {
void writeWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, Object message) {
if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!");
}
@ -752,7 +752,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/
@Override
public final
void writeWithCryptoUdp(Connection connection, ByteBuf buffer, Object message) {
void writeWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, Object message) {
if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!");
}
@ -770,7 +770,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/
@Override
public final
Object readWithCryptoTcp(Connection connection, ByteBuf buffer, int length) {
Object readWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, int length) {
if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!");
}
@ -788,7 +788,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/
@Override
public final
Object readWithCryptoUdp(Connection connection, ByteBuf buffer, int length) {
Object readWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, int length) {
if (connection == null) {
throw new NetException("Unable to perform crypto when NO network connection!");
}
@ -801,7 +801,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/
@SuppressWarnings("unchecked")
private
void write0(final Connection connection, final ByteBuf buffer, final Object message, final boolean doCrypto) {
void write0(final ConnectionImpl connection, final ByteBuf buffer, final Object message, final boolean doCrypto) {
final KryoExtra kryo = (KryoExtra) this.pool.takeUninterruptibly();
Logger logger2 = logger;
@ -911,7 +911,7 @@ class KryoCryptoSerializationManager implements CryptoSerializationManager {
*/
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
private
Object read0(final Connection connection, final ByteBuf buffer, final int length, final boolean doCrypto) {
Object read0(final ConnectionImpl connection, final ByteBuf buffer, final int length, final boolean doCrypto) {
final KryoExtra kryo = (KryoExtra) this.pool.takeUninterruptibly();
Logger logger2 = logger;

View File

@ -18,9 +18,9 @@ package dorkbox.network.connection;
import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.pipeline.KryoEncoder;
import dorkbox.network.pipeline.KryoEncoderCrypto;
import dorkbox.util.exceptions.SecurityException;
import dorkbox.util.collections.IntMap;
import dorkbox.util.crypto.Crypto;
import dorkbox.util.exceptions.SecurityException;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.slf4j.Logger;
@ -135,7 +135,7 @@ class RegistrationWrapper implements UdpServer {
* to the pipeline are finished.
*/
public
void connectionConnected0(Connection networkConnection) {
void connectionConnected0(ConnectionImpl networkConnection) {
this.endPoint.connectionConnected0(networkConnection);
}

View File

@ -15,7 +15,7 @@
*/
package dorkbox.network.connection.registration;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import io.netty.channel.Channel;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
@ -43,7 +43,7 @@ class MetaChannel {
public Channel udtChannel = null;
public Connection connection; // only needed until the connection has been notified.
public ConnectionImpl connection; // only needed until the connection has been notified.
public ECPublicKeyParameters publicKey; // used for ECC crypto + handshake on NETWORK (remote) connections. This is the remote public key.

View File

@ -15,7 +15,7 @@
*/
package dorkbox.network.connection.registration.local;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.RegistrationWrapper;
import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.connection.registration.Registration;
@ -87,7 +87,7 @@ class RegistrationLocalHandlerClient extends RegistrationLocalHandler {
// Event though a local channel is XOR with everything else, we still have to make the client clean up it's state.
registrationWrapper.registerNextProtocol0();
Connection connection = metaChannel.connection;
ConnectionImpl connection = metaChannel.connection;
registrationWrapper.connectionConnected0(connection);
}
else {

View File

@ -15,7 +15,7 @@
*/
package dorkbox.network.connection.registration.local;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.RegistrationWrapper;
import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.util.collections.IntMap;
@ -79,7 +79,7 @@ class RegistrationLocalHandlerServer extends RegistrationLocalHandler {
logger2.trace("Sent registration");
}
Connection connection = null;
ConnectionImpl connection = null;
try {
IntMap<MetaChannel> channelMap = this.registrationWrapper.getAndLockChannelMap();
MetaChannel metaChannel = channelMap.remove(channel.hashCode());

View File

@ -16,18 +16,17 @@
package dorkbox.network.connection.registration.remote;
import dorkbox.network.Broadcast;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.connection.RegistrationWrapper;
import dorkbox.network.connection.registration.MetaChannel;
import dorkbox.network.connection.registration.Registration;
import dorkbox.network.connection.wrapper.UdpWrapper;
import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import dorkbox.util.bytes.OptimizeUtilsByteArray;
import dorkbox.util.collections.IntMap;
import dorkbox.util.collections.IntMap.Entries;
import dorkbox.util.crypto.Crypto;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
@ -149,7 +148,7 @@ class RegistrationRemoteHandlerServerUDP extends MessageToMessageCodec<DatagramP
public final
void sendUDP(ChannelHandlerContext context, Object object, ByteBuf buffer, InetSocketAddress udpRemoteAddress) {
Connection networkConnection = this.registrationWrapper.getServerUDP(udpRemoteAddress);
ConnectionImpl networkConnection = this.registrationWrapper.getServerUDP(udpRemoteAddress);
if (networkConnection != null) {
// try to write data! (IT SHOULD ALWAYS BE ENCRYPTED HERE!)
this.serializationManager.writeWithCryptoUdp(networkConnection, buffer, object);

View File

@ -26,20 +26,20 @@ import java.util.List;
public
class KryoDecoder extends ByteToMessageDecoder {
private final OptimizeUtilsByteBuf optimize;
private final CryptoSerializationManager kryoWrapper;
private final CryptoSerializationManager serializationManager;
public
KryoDecoder(CryptoSerializationManager kryoWrapper) {
KryoDecoder(CryptoSerializationManager serializationManager) {
super();
this.kryoWrapper = kryoWrapper;
this.serializationManager = serializationManager;
this.optimize = OptimizeUtilsByteBuf.get();
}
@SuppressWarnings("unused")
protected
Object readObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext context, ByteBuf in, int length) {
Object readObject(CryptoSerializationManager serializationManager, ChannelHandlerContext context, ByteBuf in, int length) {
// no connection here because we haven't created one yet. When we do, we replace this handler with a new one.
return kryoWrapper.read(in, length);
return serializationManager.read(in, length);
}
@Override
@ -150,13 +150,13 @@ class KryoDecoder extends ByteToMessageDecoder {
length = optimize.readInt(in, true); // object LENGTH
// however many we need to
out.add(readObject(this.kryoWrapper, ctx, in, length));
out.add(readObject(this.serializationManager, ctx, in, length));
}
// the buffer reader index will be at the correct location, since the read object method advances it.
}
else {
// exactly one!
out.add(readObject(this.kryoWrapper, ctx, in, length));
out.add(readObject(this.serializationManager, ctx, in, length));
}
}
}

View File

@ -15,9 +15,8 @@
*/
package dorkbox.network.pipeline;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
@ -29,21 +28,15 @@ public
class KryoDecoderCrypto extends KryoDecoder {
public
KryoDecoderCrypto(CryptoSerializationManager kryoWrapper) {
super(kryoWrapper);
KryoDecoderCrypto(CryptoSerializationManager serializationManager) {
super(serializationManager);
}
@Override
protected
Object readObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext ctx, ByteBuf in, int length) {
Object readObject(CryptoSerializationManager serializationManager, ChannelHandlerContext ctx, ByteBuf in, int length) {
ChannelHandler last = ctx.pipeline()
.last();
if (last instanceof Connection) {
return kryoWrapper.readWithCryptoTcp((Connection) last, in, length);
}
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to READ an object with NO network connection!");
}
return serializationManager.readWithCryptoTcp((ConnectionImpl) last, in, length);
}
}

View File

@ -17,8 +17,8 @@ package dorkbox.network.pipeline;
import com.esotericsoftware.kryo.KryoException;
import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import dorkbox.util.bytes.OptimizeUtilsByteBuf;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
@ -28,14 +28,14 @@ import io.netty.handler.codec.MessageToByteEncoder;
public
class KryoEncoder extends MessageToByteEncoder<Object> {
private static final int reservedLengthIndex = 4;
private final CryptoSerializationManager kryoWrapper;
private final CryptoSerializationManager serializationManager;
private final OptimizeUtilsByteBuf optimize;
public
KryoEncoder(CryptoSerializationManager kryoWrapper) {
KryoEncoder(CryptoSerializationManager serializationManager) {
super();
this.kryoWrapper = kryoWrapper;
this.serializationManager = serializationManager;
this.optimize = OptimizeUtilsByteBuf.get();
}
@ -60,7 +60,7 @@ class KryoEncoder extends MessageToByteEncoder<Object> {
out.writeInt(0); // put an int in, which is the same size as reservedLengthIndex
try {
writeObject(this.kryoWrapper, ctx, msg, out);
writeObject(this.serializationManager, ctx, msg, out);
// now set the frame (if it's TCP)!
int length = out.readableBytes() - startIndex -

View File

@ -15,9 +15,8 @@
*/
package dorkbox.network.pipeline;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandler.Sharable;
@ -28,21 +27,15 @@ public
class KryoEncoderCrypto extends KryoEncoder {
public
KryoEncoderCrypto(CryptoSerializationManager kryoWrapper) {
super(kryoWrapper);
KryoEncoderCrypto(CryptoSerializationManager serializationManager) {
super(serializationManager);
}
@Override
protected
void writeObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) {
void writeObject(CryptoSerializationManager serializationManager, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) {
ChannelHandler last = ctx.pipeline()
.last();
if (last instanceof Connection) {
kryoWrapper.writeWithCryptoTcp((Connection) last, buffer, msg);
}
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to WRITE an object with NO network connection (or wrong connection type!)!");
}
serializationManager.writeWithCryptoTcp((ConnectionImpl) last, buffer, msg);
}
}

View File

@ -29,11 +29,11 @@ import java.util.List;
public
class KryoDecoderUdp extends MessageToMessageDecoder<DatagramPacket> {
private final CryptoSerializationManager kryoWrapper;
private final CryptoSerializationManager serializationManager;
public
KryoDecoderUdp(CryptoSerializationManager kryoWrapper) {
this.kryoWrapper = kryoWrapper;
KryoDecoderUdp(CryptoSerializationManager serializationManager) {
this.serializationManager = serializationManager;
}
@Override
@ -46,12 +46,12 @@ class KryoDecoderUdp extends MessageToMessageDecoder<DatagramPacket> {
// there is a REMOTE possibility that UDP traffic BEAT the TCP registration traffic, which means that THIS packet
// COULD be encrypted!
if (kryoWrapper.isEncrypted(data)) {
if (serializationManager.isEncrypted(data)) {
throw new NetException("Encrypted UDP packet received before registration complete. WHOOPS!");
}
// no connection here because we haven't created one yet. When we do, we replace this handler with a new one.
Object read = kryoWrapper.read(data, data.writerIndex());
Object read = serializationManager.read(data, data.writerIndex());
out.add(read);
}
}

View File

@ -15,9 +15,8 @@
*/
package dorkbox.network.pipeline.udp;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandler.Sharable;
@ -31,11 +30,11 @@ import java.util.List;
public
class KryoDecoderUdpCrypto extends MessageToMessageDecoder<DatagramPacket> {
private final CryptoSerializationManager kryoWrapper;
private final CryptoSerializationManager serializationManager;
public
KryoDecoderUdpCrypto(CryptoSerializationManager kryoWrapper) {
this.kryoWrapper = kryoWrapper;
KryoDecoderUdpCrypto(CryptoSerializationManager serializationManager) {
this.serializationManager = serializationManager;
}
@Override
@ -44,14 +43,8 @@ class KryoDecoderUdpCrypto extends MessageToMessageDecoder<DatagramPacket> {
ChannelHandler last = ctx.pipeline()
.last();
if (last instanceof Connection) {
ByteBuf data = in.content();
Object object = kryoWrapper.readWithCryptoUdp((Connection) last, data, data.readableBytes());
out.add(object);
}
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to READ an object with NO network connection!");
}
ByteBuf data = in.content();
Object object = serializationManager.readWithCryptoUdp((ConnectionImpl) last, data, data.readableBytes());
out.add(object);
}
}

View File

@ -36,21 +36,21 @@ public
class KryoEncoderUdp extends MessageToMessageEncoder<Object> {
private static final int maxSize = EndPoint.udpMaxSize;
private final CryptoSerializationManager kryoWrapper;
private final CryptoSerializationManager serializationManager;
public
KryoEncoderUdp(CryptoSerializationManager kryoWrapper) {
KryoEncoderUdp(CryptoSerializationManager serializationManager) {
super();
this.kryoWrapper = kryoWrapper;
this.serializationManager = serializationManager;
}
// the crypto writer will override this
@SuppressWarnings("unused")
protected
void writeObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext context, Object msg, ByteBuf buffer) {
void writeObject(CryptoSerializationManager serializationManager, ChannelHandlerContext context, Object msg, ByteBuf buffer) {
// no connection here because we haven't created one yet. When we do, we replace this handler with a new one.
kryoWrapper.write(buffer, msg);
serializationManager.write(buffer, msg);
}
@Override
@ -61,7 +61,7 @@ class KryoEncoderUdp extends MessageToMessageEncoder<Object> {
ByteBuf outBuffer = Unpooled.buffer(maxSize);
// no size info, since this is UDP, it is not segmented
writeObject(this.kryoWrapper, ctx, msg, outBuffer);
writeObject(this.serializationManager, ctx, msg, outBuffer);
// have to check to see if we are too big for UDP!

View File

@ -15,9 +15,8 @@
*/
package dorkbox.network.pipeline.udp;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.network.util.CryptoSerializationManager;
import dorkbox.util.exceptions.NetException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandler.Sharable;
@ -28,22 +27,16 @@ public
class KryoEncoderUdpCrypto extends KryoEncoderUdp {
public
KryoEncoderUdpCrypto(CryptoSerializationManager kryoWrapper) {
super(kryoWrapper);
KryoEncoderUdpCrypto(CryptoSerializationManager serializationManager) {
super(serializationManager);
}
@Override
protected
void writeObject(CryptoSerializationManager kryoWrapper, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) {
void writeObject(CryptoSerializationManager serializationManager, ChannelHandlerContext ctx, Object msg, ByteBuf buffer) {
ChannelHandler last = ctx.pipeline()
.last();
if (last instanceof Connection) {
kryoWrapper.writeWithCryptoUdp((Connection) last, buffer, msg);
}
else {
// SHOULD NEVER HAPPEN!
throw new NetException("Tried to use kryo to WRITE an object with NO network connection!");
}
serializationManager.writeWithCryptoUdp((ConnectionImpl) last, buffer, msg);
}
}

View File

@ -15,7 +15,7 @@
*/
package dorkbox.network.util;
import dorkbox.network.connection.Connection;
import dorkbox.network.connection.ConnectionImpl;
import dorkbox.util.SerializationManager;
import io.netty.buffer.ByteBuf;
@ -37,14 +37,14 @@ interface CryptoSerializationManager extends SerializationManager, RMISerializat
* <p/>
* There is a small speed penalty if there were no kryo's available to use.
*/
void writeWithCryptoTcp(Connection connection, ByteBuf buffer, Object message);
void writeWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, Object message);
/**
* Waits until a kryo is available to write, using CAS operations to prevent having to synchronize.
* <p/>
* There is a small speed penalty if there were no kryo's available to use.
*/
void writeWithCryptoUdp(Connection connection, ByteBuf buffer, Object message);
void writeWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, Object message);
/**
* Reads an object from the buffer.
@ -54,7 +54,7 @@ interface CryptoSerializationManager extends SerializationManager, RMISerializat
* @param connection can be NULL
* @param length should ALWAYS be the length of the expected object!
*/
Object readWithCryptoTcp(Connection connection, ByteBuf buffer, int length);
Object readWithCryptoTcp(ConnectionImpl connection, ByteBuf buffer, int length);
/**
* Reads an object from the buffer.
@ -64,5 +64,5 @@ interface CryptoSerializationManager extends SerializationManager, RMISerializat
* @param connection can be NULL
* @param length should ALWAYS be the length of the expected object!
*/
Object readWithCryptoUdp(Connection connection, ByteBuf buffer, int length);
Object readWithCryptoUdp(ConnectionImpl connection, ByteBuf buffer, int length);
}