Package org.activecomponents.udp
Class Connection
- java.lang.Object
- 
- org.activecomponents.udp.Connection
 
- 
 public class Connection extends java.lang.Object
- 
- 
Field SummaryFields Modifier and Type Field Description protected static byteACKAcknowledgement of a message part.protected SymCipherManagementciphermgmtManagement object for symmetric ciphers.protected booleanconnectedFlag that defines if the connection is currently active.protected java.util.List<IConnectionListener>connectionlistenersListeners for new connections.static byteDATAData package after connection handshake.protected static booleanDEBUGDebug flag.protected static byteFINPacket type for closing a connection.protected static byteFINACKPacket type for confirming a closed connection.protected IIncomingListenerinclistenerListener for incoming messages and packets.protected java.util.RandominsecrandomInsecure PRNG.protected static byteINT_MESSAGE_PARTPart of an internal message.protected java.util.concurrent.LinkedBlockingQueue<byte[]>internalpacketsArrived packets that are used to manage the connectionprotected static byteKEEPALIVEKeepalive packet.protected longkeepalivereceivedTimestamp of the last received keepalive packet.protected longkeepalivesentTimestamp of the last sent keepalive packet.protected byte[]keypackageKey package built during connection establishment.protected java.util.List<IKeyVerifier>keyverifiersList of externally-provided key verifiers.protected longlatencyCurrent latency.protected static byteMESSAGE_PARTPart of a message.protected java.util.Queue<OutgoingMessage>messagequeueQueue of scheduled messages.protected java.util.Map<java.lang.Long,IncomingMessage>messagesIncoming messagesprotected static bytePACKETPacket with data requiring no confirmation.protected java.util.LinkedList<MessagePart>queuedmessagepartsParts of messages that have been queued and awaiting transmission.protected static byteRDYResponse to a SYNACK, confirming readiness by the initiator.protected static byteREKEYMessage type used to rekey the connection.protected IUdpCallback<java.lang.Boolean>rekeycallbackprotected java.net.SocketAddressremoteaddrThe remote peer this connection targets.protected java.util.Map<java.lang.Long,MessagePart>sentwindowMessage parts that have been sent and await confirmation.protected java.net.DatagramSocketsocketThe socket.protected static byteSYNPacket type for initiating a connection.protected static byteSYNACKResponse to a SYN, finalizing the key exchange.protected IThreadExecutortexecThe thread executor.protected java.util.concurrent.LinkedBlockingQueue<byte[]>userpacketsUser packets with a payload.protected intwindowsizeCurrent window size.protected intwindowthresWindow size threshold.
 - 
Constructor SummaryConstructors Constructor Description Connection(java.net.DatagramSocket socket, java.util.List<IConnectionListener> connectionlisteners, java.net.SocketAddress remoteaddr, IThreadExecutor texec, KeyExchangeGenerator keyexgen, int symcipherclassid, java.util.List<IKeyVerifier> keyverifiers, IIncomingListener inclistener)
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected byte[]buildSignedKeyPackage(byte[] localpubkey, byte[] remotepubkey)Builds a signed package of local and remote public key.protected voidclearQueues()Clears internal buffers.protected voidconnectionEstablished(UdpConnectionHandler handler, java.net.DatagramPacket confirmation)Called when the connection has been established.protected byte[]createUserPacket(byte[] packet)Creates a prepared packet as encrypted user packet.protected voiddisconnect()Notifies the remote host of a disconnect and terminates the connection.protected byte[]extractAndVerifyPubKey(byte[] localpubkey, byte[] buffer, int offset)Verifies a signed package of local and remote public key.java.net.SocketAddressgetRemoteAddress()Returns the remote address.protected voidinitiate(UdpConnectionHandler handler)Method for initiating the connection.booleanisConnected()Checks if the connection is working.protected voidprocessInternalMessage(byte[] incmsg)Processes an internal message.protected voidrawPacketReceived(byte[] data)Called when a package is received.byte[]receive()Receives a message / packet (blocking).byte[]receive(long timeout)Receives a message / packet (blocking).protected voidrespond(byte[] initial, UdpConnectionHandler handler)Response actions for an attempt to initate a connection.protected voidscheduleMessage(OutgoingMessage msg, boolean internal)protected voidsendLoop()Loop for dispatching packets to the remote host.voidsendMessage(byte[] msg)Send a message.voidsendMessage(byte[] msg, IUdpCallback<java.lang.Boolean> callback)Send a message.voidsendPacket(byte[] data)Sends a packet.protected voiduserPacketReceived(SymCipherSuite ciphersuite, byte[] data)Method for handling decrypted user-level packets.
 
- 
- 
- 
Field Detail- 
DEBUGprotected static final boolean DEBUG Debug flag.- See Also:
- Constant Field Values
 
 - 
SYNprotected static final byte SYN Packet type for initiating a connection.- See Also:
- Constant Field Values
 
 - 
SYNACKprotected static final byte SYNACK Response to a SYN, finalizing the key exchange.- See Also:
- Constant Field Values
 
 - 
RDYprotected static final byte RDY Response to a SYNACK, confirming readiness by the initiator.- See Also:
- Constant Field Values
 
 - 
DATApublic static final byte DATA Data package after connection handshake.- See Also:
- Constant Field Values
 
 - 
KEEPALIVEprotected static final byte KEEPALIVE Keepalive packet.- See Also:
- Constant Field Values
 
 - 
FINprotected static final byte FIN Packet type for closing a connection.- See Also:
- Constant Field Values
 
 - 
FINACKprotected static final byte FINACK Packet type for confirming a closed connection.- See Also:
- Constant Field Values
 
 - 
ACKprotected static final byte ACK Acknowledgement of a message part.- See Also:
- Constant Field Values
 
 - 
PACKETprotected static final byte PACKET Packet with data requiring no confirmation.- See Also:
- Constant Field Values
 
 - 
MESSAGE_PARTprotected static final byte MESSAGE_PART Part of a message.- See Also:
- Constant Field Values
 
 - 
INT_MESSAGE_PARTprotected static final byte INT_MESSAGE_PART Part of an internal message.- See Also:
- Constant Field Values
 
 - 
REKEYprotected static final byte REKEY Message type used to rekey the connection.- See Also:
- Constant Field Values
 
 - 
socketprotected java.net.DatagramSocket socket The socket.
 - 
connectionlistenersprotected java.util.List<IConnectionListener> connectionlisteners Listeners for new connections.
 - 
keyverifiersprotected java.util.List<IKeyVerifier> keyverifiers List of externally-provided key verifiers.
 - 
texecprotected IThreadExecutor texec The thread executor.
 - 
remoteaddrprotected java.net.SocketAddress remoteaddr The remote peer this connection targets.
 - 
messagequeueprotected java.util.Queue<OutgoingMessage> messagequeue Queue of scheduled messages.
 - 
queuedmessagepartsprotected java.util.LinkedList<MessagePart> queuedmessageparts Parts of messages that have been queued and awaiting transmission.
 - 
sentwindowprotected java.util.Map<java.lang.Long,MessagePart> sentwindow Message parts that have been sent and await confirmation.
 - 
messagesprotected java.util.Map<java.lang.Long,IncomingMessage> messages Incoming messages
 - 
windowsizeprotected volatile int windowsize Current window size.
 - 
windowthresprotected volatile int windowthres Window size threshold.
 - 
latencyprotected volatile long latency Current latency.
 - 
insecrandomprotected java.util.Random insecrandom Insecure PRNG.
 - 
internalpacketsprotected java.util.concurrent.LinkedBlockingQueue<byte[]> internalpackets Arrived packets that are used to manage the connection
 - 
userpacketsprotected java.util.concurrent.LinkedBlockingQueue<byte[]> userpackets User packets with a payload.
 - 
inclistenerprotected IIncomingListener inclistener Listener for incoming messages and packets.
 - 
keypackageprotected volatile byte[] keypackage Key package built during connection establishment.
 - 
ciphermgmtprotected SymCipherManagement ciphermgmt Management object for symmetric ciphers.
 - 
rekeycallbackprotected IUdpCallback<java.lang.Boolean> rekeycallback 
 - 
connectedprotected volatile boolean connected Flag that defines if the connection is currently active.
 - 
keepalivereceivedprotected volatile long keepalivereceived Timestamp of the last received keepalive packet.
 - 
keepalivesentprotected volatile long keepalivesent Timestamp of the last sent keepalive packet.
 
- 
 - 
Constructor Detail- 
Connectionpublic Connection(java.net.DatagramSocket socket, java.util.List<IConnectionListener> connectionlisteners, java.net.SocketAddress remoteaddr, IThreadExecutor texec, KeyExchangeGenerator keyexgen, int symcipherclassid, java.util.List<IKeyVerifier> keyverifiers, IIncomingListener inclistener)
 
- 
 - 
Method Detail- 
isConnectedpublic boolean isConnected() Checks if the connection is working.- Returns:
- True, if data can be send.
 
 - 
sendPacketpublic void sendPacket(byte[] data) Sends a packet. Maximum size is at least 60000 bytes, delivery is not guaranteed.- Parameters:
- data- The data being send.
 
 - 
sendMessagepublic void sendMessage(byte[] msg) Send a message. Size is not limited, delivery is guaranteed unless a failure occurs.- Parameters:
- msg- The message.
 
 - 
sendMessagepublic void sendMessage(byte[] msg, IUdpCallback<java.lang.Boolean> callback)Send a message. Size is not limited, delivery is guaranteed unless a failure occurs.- Parameters:
- msg- The message.
- callback- Callback invoked with result of the send operation, true if the message was received, false on failure.
 
 - 
receivepublic byte[] receive() Receives a message / packet (blocking).- Returns:
- The message/packet.
 
 - 
receivepublic byte[] receive(long timeout) Receives a message / packet (blocking).- Returns:
- The message/packet.
 
 - 
getRemoteAddresspublic java.net.SocketAddress getRemoteAddress() Returns the remote address.- Returns:
- Remote address.
 
 - 
initiateprotected void initiate(UdpConnectionHandler handler) Method for initiating the connection.- Parameters:
- handler- The handler.
 
 - 
respondprotected void respond(byte[] initial, UdpConnectionHandler handler)Response actions for an attempt to initate a connection.- Parameters:
- initial- Initiation packaet data received that caused the response activity.
- handler- The handler.
 
 - 
connectionEstablishedprotected void connectionEstablished(UdpConnectionHandler handler, java.net.DatagramPacket confirmation) Called when the connection has been established.- Parameters:
- confirmation- Confirmation packet.
 
 - 
disconnectprotected void disconnect() Notifies the remote host of a disconnect and terminates the connection.
 - 
rawPacketReceivedprotected void rawPacketReceived(byte[] data) Called when a package is received.- Parameters:
- data- Raw package data.
 
 - 
userPacketReceivedprotected void userPacketReceived(SymCipherSuite ciphersuite, byte[] data) throws java.lang.Exception Method for handling decrypted user-level packets.- Parameters:
- data- Packet data.
- Throws:
- java.lang.Exception- Any exception that may occur and needs to be handled on a higher level.
 
 - 
sendLoopprotected void sendLoop() Loop for dispatching packets to the remote host.
 - 
createUserPacketprotected byte[] createUserPacket(byte[] packet) Creates a prepared packet as encrypted user packet.- Parameters:
- packet- The inner packet.
 
 - 
buildSignedKeyPackageprotected byte[] buildSignedKeyPackage(byte[] localpubkey, byte[] remotepubkey)Builds a signed package of local and remote public key.- Parameters:
- localpubkey- Local public key.
- remotepubkey- Remote public key.
- Returns:
- Signed package.
 
 - 
extractAndVerifyPubKeyprotected byte[] extractAndVerifyPubKey(byte[] localpubkey, byte[] buffer, int offset)Verifies a signed package of local and remote public key.- Parameters:
- localpubkey- Local public key for comparison
- buffer- Buffer containing package.
- offset- Offset of the package.
- Returns:
- Extracted remote public key if verified, null if verification failed.
 
 - 
processInternalMessageprotected void processInternalMessage(byte[] incmsg) Processes an internal message.- Parameters:
- incmsg- The message.
 
 - 
scheduleMessageprotected void scheduleMessage(OutgoingMessage msg, boolean internal) 
 - 
clearQueuesprotected void clearQueues() Clears internal buffers.
 
- 
 
-