me.edwards.des.net
Class Connection

java.lang.Object
  extended by me.edwards.des.net.Connection

public class Connection
extends java.lang.Object

Data structure to handle the connection between Nodes. A Connection tracks the ping and status of a connection between Nodes and ensures that connection stay alive.

Created on: Oct 17, 2015 at 10:21:02 AM

Author:
Matthew Edwards

Field Summary
private  java.net.InetAddress address
           
private static int CONNECT_TIMEOUT
           
private  boolean connected
           
static int CONNECTION_BOTH
          The connection is agreed upon by both nodes
static int CONNECTION_NODE_ONLY
          A connection was initiated by this node
static int CONNECTION_PEER_ONLY
          A connection was initiated by a peer node
private  int connectionStatus
           
private static java.util.logging.Logger logger
           
private  java.lang.String name
           
private  Node node
           
private  long ping
           
private static int PING_TIMEOUT
           
private  boolean pingSent
           
private  long pingValue
           
private  int port
           
private  java.net.Socket socket
           
 
Constructor Summary
Connection(Node node, java.net.Socket socket)
          Creates new Connection using the local Node and the socket connecting to the remote Node.
 
Method Summary
 void connect()
          Connects the local Node to the remote Node through this Connection.
 void disconnect()
          Disconnects the local and remote Nodes, closes the connection socket and closes node communication.
 java.net.InetAddress getAddress()
          Returns the destination address of this connection.
 int getConnectionStatus()
          Returns the current connection status.

CONNECTION_NODE_ONLY A connection was initiated by this node CONNECTION_PEER_ONLY A connection was initiated by a peer node CONNECTION_BOTH The connection is agreed upon by both nodes
 java.lang.String getHostName()
          Returns this connection's host name in the format "/ADDRESS:PORT".
 java.lang.String getName()
          Returns this connection's common name (if any).
 int getPort()
          Returns the destination port of this connection.
 java.net.Socket getSocket()
          Returns the socket used by this connection.
 boolean isConnected()
          Returns true if the connection is currently connected.
 void pong(long pong)
          Notifies the connection that the Pong was received.
 void send(Packet packet)
          Sends a Packet through this connection to the remote Node.
 void setConnectionStatus(int status)
          Sets the current connection status.

CONNECTION_NODE_ONLY A connection was initiated by this node CONNECTION_PEER_ONLY A connection was initiated by a peer node CONNECTION_BOTH The connection is agreed upon by both nodes
 void setName(java.lang.String name)
          Sets this connection's common name.
 void setPort(int port)
          Sets the port of this connection.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONNECTION_NODE_ONLY

public static int CONNECTION_NODE_ONLY
A connection was initiated by this node


CONNECTION_PEER_ONLY

public static int CONNECTION_PEER_ONLY
A connection was initiated by a peer node


CONNECTION_BOTH

public static int CONNECTION_BOTH
The connection is agreed upon by both nodes


logger

private static final java.util.logging.Logger logger

CONNECT_TIMEOUT

private static final int CONNECT_TIMEOUT
See Also:
Constant Field Values

PING_TIMEOUT

private static final int PING_TIMEOUT
See Also:
Constant Field Values

node

private Node node

socket

private java.net.Socket socket

name

private java.lang.String name

address

private java.net.InetAddress address

port

private int port

connected

private boolean connected

connectionStatus

private int connectionStatus

ping

private long ping

pingValue

private long pingValue

pingSent

private boolean pingSent
Constructor Detail

Connection

public Connection(Node node,
                  java.net.Socket socket)
Creates new Connection using the local Node and the socket connecting to the remote Node.

Parameters:
node - Local Node which owns this connection
socket - Socket to remote Node which is used by this connection
Method Detail

getSocket

public java.net.Socket getSocket()
Returns the socket used by this connection.

Returns:
Socket connecting to remote Node

getName

public java.lang.String getName()
Returns this connection's common name (if any).

Returns:
This connection's human-readable common name

setName

public void setName(java.lang.String name)
Sets this connection's common name.

Parameters:
name - Human-readable common name of this connection

getHostName

public java.lang.String getHostName()
Returns this connection's host name in the format "/ADDRESS:PORT".

Returns:
The connection host name in the format "/ADDRESS:PORT"

getAddress

public java.net.InetAddress getAddress()
Returns the destination address of this connection.

Returns:
Address of remote Node as InetAddress

getPort

public int getPort()
Returns the destination port of this connection.

Returns:
Port of remote Node

setPort

public void setPort(int port)
Sets the port of this connection. Port is initially set to connecting port. Should only be used to reset the port to the remote Node's server port by Node.parse(byte[], Connection).

Parameters:
port - Port of remote Node

isConnected

public boolean isConnected()
Returns true if the connection is currently connected.

Returns:
True if connected, False otherwise

getConnectionStatus

public int getConnectionStatus()
Returns the current connection status.

CONNECTION_NODE_ONLY A connection was initiated by this node
CONNECTION_PEER_ONLY A connection was initiated by a peer node
CONNECTION_BOTH The connection is agreed upon by both nodes

Returns:
Current connection status

setConnectionStatus

public void setConnectionStatus(int status)
Sets the current connection status.

CONNECTION_NODE_ONLY A connection was initiated by this node
CONNECTION_PEER_ONLY A connection was initiated by a peer node
CONNECTION_BOTH The connection is agreed upon by both nodes

Parameters:
status - New connection status

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

send

public void send(Packet packet)
Sends a Packet through this connection to the remote Node.

Parameters:
packet - Packet to send

pong

public void pong(long pong)
Notifies the connection that the Pong was received. This method is used by Node.parse(byte[], Connection).

Parameters:
pong - Payload of the Pong received (Must be one more than the sent Ping value to be valid)

connect

public void connect()
Connects the local Node to the remote Node through this Connection.


disconnect

public void disconnect()
Disconnects the local and remote Nodes, closes the connection socket and closes node communication.