megamek.common.net
Class AbstractConnection

java.lang.Object
  extended by megamek.common.net.AbstractConnection
All Implemented Interfaces:
IConnection
Direct Known Subclasses:
DataStreamConnection, ObjectStreamConnection

public abstract class AbstractConnection
extends java.lang.Object
implements IConnection

Generic bidirectional connection between client and server


Nested Class Summary
protected static interface AbstractConnection.INetworkPacket
          Connection layer data packet.
(package private) static class AbstractConnection.SendQueue
           
 
Field Summary
(package private)  long bytesSent
          Bytes send during the connection lifecycle
(package private)  PacketMarshaller marshaller
          Marshaller used to send packets
protected  int marshallingType
          Type of marshalling used to represent sent packets
(package private)  boolean zipData
          Indicates the need to compress sent data
 
Constructor Summary
AbstractConnection(java.net.Socket socket, int id)
          Creates new Server connection
AbstractConnection(java.lang.String host, int port, int id)
          Creates new client (connection from client to server) connection
 
Method Summary
 void addConnectionListener(ConnectionListener listener)
          Adds the specified connection listener to receive connection events from connection.
 long bytesReceived()
          Returns a very approximate count of how many bytes were received
 long bytesSent()
          Returns a very approximate count of how many bytes were sent
 void close()
          Closes the socket and shuts down the receiver and sender threads
protected  void doFlush()
           
 void flush()
          this is the method that should be overridden
protected  java.lang.String getConnectionTypeAbbrevation()
          Returns the the connection type abbrevation (client/server) that used in the debug messages and so on.
 int getId()
          Returns the connection ID
 java.lang.String getInetAddress()
           
protected  java.io.InputStream getInputStream()
          Returns an input stream
protected  int getMarshallingType()
          Returns the type of the marshalling used to send packets
protected  java.io.OutputStream getOutputStream()
          Returns an output stream
 boolean hasPending()
          Returns true if there are pending packets
 boolean isCompressed()
          Returns true if this connection compress the sent data
 boolean isServer()
          Returns true if it's the Server connection
 boolean open()
          Opens the connection
protected  void processConnectionEvent(ConnectionEvent event)
          Processes game events occurring on this connection by dispatching them to any registered GameListener objects.
protected  void processPacket(AbstractConnection.INetworkPacket np)
          process a received packet
protected  void processPacket(megamek.common.net.AbstractConnection.SendPacket packet)
          process a packet to be sent
protected abstract  AbstractConnection.INetworkPacket readNetworkPacket()
          Reads a complete NetworkPacket must not block, must return null instead
 void removeConnectionListener(ConnectionListener listener)
          Removes the specified connection listener.
protected  void reportLastCommands()
          Appends the last commands sent/received to the given StringBuffer
protected  void reportLastCommands(boolean sent)
          Appends the last commands sent or received to the given StringBuffer dependig on the sent parameter
protected  void reportReceiveException(java.lang.Exception ex)
          Reports receive exception to the System.err
protected  void reportReceiveException(java.lang.Exception ex, java.lang.StringBuffer buffer)
          Appends the receive exception report to the given StringBuffer
protected  void reportSendException(java.lang.Exception ex, megamek.common.net.AbstractConnection.SendPacket packet)
          Reports receive exception to the System.err
 void send(Packet packet)
          Adds a packet to the send queue to be send on a seperate thread.
protected abstract  void sendNetworkPacket(byte[] data, boolean zipped)
          Sends the data must not block for too long
 void sendNow(megamek.common.net.AbstractConnection.SendPacket packet)
          Send packet now; This is the blocking call.
 void setCompression(boolean compress)
          Sets the compression
 void setId(int id)
          Sets the connection ID
protected  void setMarshallingType(int marshallingType)
          Sets the type of the marshalling used to send packets
 long update()
          checks if there is anything to send or receive and sends or receives that stuff.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bytesSent

long bytesSent
Bytes send during the connection lifecycle


marshallingType

protected int marshallingType
Type of marshalling used to represent sent packets


marshaller

PacketMarshaller marshaller
Marshaller used to send packets


zipData

boolean zipData
Indicates the need to compress sent data

Constructor Detail

AbstractConnection

public AbstractConnection(java.lang.String host,
                          int port,
                          int id)
Creates new client (connection from client to server) connection

Parameters:
host - target host
port - target port
id - connection ID

AbstractConnection

public AbstractConnection(java.net.Socket socket,
                          int id)
Creates new Server connection

Parameters:
socket - accepted socket
id - connection ID
Method Detail

isServer

public boolean isServer()
Returns true if it's the Server connection

Returns:
true if it's the Server connection

getMarshallingType

protected int getMarshallingType()
Returns the type of the marshalling used to send packets

Returns:
the type of the marshalling used to send packets

setMarshallingType

protected void setMarshallingType(int marshallingType)
Sets the type of the marshalling used to send packets

Parameters:
marshallingType - new marhalling type

open

public boolean open()
Opens the connection

Specified by:
open in interface IConnection
Returns:
true on success, false otherwise

close

public void close()
Closes the socket and shuts down the receiver and sender threads

Specified by:
close in interface IConnection

getId

public int getId()
Returns the connection ID

Specified by:
getId in interface IConnection
Returns:
the connection ID

setId

public void setId(int id)
Sets the connection ID

Specified by:
setId in interface IConnection
Parameters:
id - new connection ID Be careful with this...

getInetAddress

public java.lang.String getInetAddress()
Specified by:
getInetAddress in interface IConnection
Returns:
what?

isCompressed

public boolean isCompressed()
Returns true if this connection compress the sent data

Returns:
true if this connection compress the sent data

setCompression

public void setCompression(boolean compress)
Sets the compression

Parameters:
compress -

send

public void send(Packet packet)
Adds a packet to the send queue to be send on a seperate thread.

Specified by:
send in interface IConnection

sendNow

public void sendNow(megamek.common.net.AbstractConnection.SendPacket packet)
Send packet now; This is the blocking call.


hasPending

public boolean hasPending()
Returns true if there are pending packets

Specified by:
hasPending in interface IConnection
Returns:
true if there are pending packets

bytesSent

public long bytesSent()
Returns a very approximate count of how many bytes were sent

Specified by:
bytesSent in interface IConnection
Returns:
a very approximate count of how many bytes were sent

bytesReceived

public long bytesReceived()
Returns a very approximate count of how many bytes were received

Specified by:
bytesReceived in interface IConnection
Returns:
a very approximate count of how many bytes were received

addConnectionListener

public void addConnectionListener(ConnectionListener listener)
Adds the specified connection listener to receive connection events from connection.

Specified by:
addConnectionListener in interface IConnection
Parameters:
listener - the connection listener.

removeConnectionListener

public void removeConnectionListener(ConnectionListener listener)
Removes the specified connection listener.

Specified by:
removeConnectionListener in interface IConnection
Parameters:
listener - the connection listener.

reportSendException

protected void reportSendException(java.lang.Exception ex,
                                   megamek.common.net.AbstractConnection.SendPacket packet)
Reports receive exception to the System.err

Parameters:
ex - Exception
packet - Packet

reportReceiveException

protected void reportReceiveException(java.lang.Exception ex)
Reports receive exception to the System.err

Parameters:
ex - Exception

reportReceiveException

protected void reportReceiveException(java.lang.Exception ex,
                                      java.lang.StringBuffer buffer)
Appends the receive exception report to the given StringBuffer

Parameters:
ex - Exception

reportLastCommands

protected void reportLastCommands()
Appends the last commands sent/received to the given StringBuffer

Parameters:
buffer - StringBuffer to add the report to

reportLastCommands

protected void reportLastCommands(boolean sent)
Appends the last commands sent or received to the given StringBuffer dependig on the sent parameter

Parameters:
buffer - StringBuffer to add the report to
sent - indicates which commands (sent/received) should be reported

getConnectionTypeAbbrevation

protected java.lang.String getConnectionTypeAbbrevation()
Returns the the connection type abbrevation (client/server) that used in the debug messages and so on.

Returns:

getInputStream

protected java.io.InputStream getInputStream()
                                      throws java.io.IOException
Returns an input stream

Returns:
an input stream
Throws:
java.io.IOException

getOutputStream

protected java.io.OutputStream getOutputStream()
                                        throws java.io.IOException
Returns an output stream

Returns:
an output stream
Throws:
java.io.IOException

update

public long update()
checks if there is anything to send or receive and sends or receives that stuff. Should not block and will not flush the actual socket, just the packet sendqueue should not block for too long at a time, instead should return 0 and hope to be called soon again

Specified by:
update in interface IConnection
Returns:
the amount of milliseconds that we assume we should be called again in. must return >=0 always

flush

public void flush()
this is the method that should be overridden

Specified by:
flush in interface IConnection

doFlush

protected void doFlush()

processPacket

protected void processPacket(AbstractConnection.INetworkPacket np)
                      throws java.lang.Exception
process a received packet

Throws:
java.lang.Exception

processPacket

protected void processPacket(megamek.common.net.AbstractConnection.SendPacket packet)
                      throws java.lang.Exception
process a packet to be sent

Throws:
java.lang.Exception

readNetworkPacket

protected abstract AbstractConnection.INetworkPacket readNetworkPacket()
                                                                throws java.lang.Exception
Reads a complete NetworkPacket must not block, must return null instead

Returns:
the NetworkPacket that was sent.
Throws:
java.lang.Exception

sendNetworkPacket

protected abstract void sendNetworkPacket(byte[] data,
                                          boolean zipped)
                                   throws java.lang.Exception
Sends the data must not block for too long

Parameters:
data - data to send
zipped - should the data be compressed
Throws:
java.lang.Exception

processConnectionEvent

protected void processConnectionEvent(ConnectionEvent event)
Processes game events occurring on this connection by dispatching them to any registered GameListener objects.

Parameters:
event - the game event.