Package net.sf.freecol.common.networking
Class Connection
- java.lang.Object
-
- net.sf.freecol.common.networking.Connection
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
DummyConnection
public class Connection extends java.lang.Object implements java.io.Closeable
A network connection. Responsible for both sending and receiving network messages.
-
-
Field Summary
Fields Modifier and Type Field Description private java.io.BufferedReader
br
The wrapped version of the input side of the socket.static int
BUFFER_SIZE
private boolean
connected
Is there an active connection.static long
DEFAULT_REPLY_TIMEOUT
The default timeout when waiting for a reply from the server.static char
END_OF_STREAM
private static char[]
END_OF_STREAM_ARRAY
private java.lang.Object
inputLock
A lock for the input side.private static java.util.logging.Logger
logger
private FreeColXMLWriter
lw
The FreeColXMLWriter to write logging messages to.private MessageHandler
messageHandler
The message handler to process incoming messages with.private java.lang.String
name
The name of the connection.static java.lang.String
NETWORK_REPLY_ID_TAG
private java.lang.Object
outputLock
A lock for the output side.static java.lang.String
QUESTION_TAG
private ReceivingThread
receivingThread
The subthread to read the input.static java.lang.String
REPLY_SUFFIX
static java.lang.String
REPLY_TAG
static java.lang.String
SEND_SUFFIX
private java.net.Socket
socket
The socket connected to the other end of the connection.private java.lang.Object
socketLock
A lock for access to the socket.private static int
TIMEOUT
private FreeColXMLReader
xr
An XML stream wrapping of an input line.private FreeColXMLWriter
xw
Main message writer.
-
Constructor Summary
Constructors Modifier Constructor Description protected
Connection(java.lang.String name)
Trivial constructor.Connection(java.lang.String host, int port, java.lang.String name)
Sets up a new socket with specified host and port and usesConnection(Socket, String)
.Connection(java.net.Socket socket, java.lang.String name)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Message
askMessage(Message message, long timeout)
Send a message, and return the response.void
close()
Close this connection.private void
closeInputStream()
Close and clear the input stream.private void
closeOutputStream()
Close and clear the output stream.private void
closeSocket()
Close and clear the socket.private static java.net.Socket
createSocket(java.lang.String host, int port)
Creates a socket to communication with a given host, port pair.void
disconnect()
Disconnect this connection.void
endListen()
FreeColXMLReader
getFreeColXMLReader()
java.lang.String
getHostAddress()
Get the host address of this connection.MessageHandler
getMessageHandler()
Gets the message handler for this connection.java.lang.String
getName()
Gets the connection name.int
getPort()
Get the port for this connection.int
getReplyId()
java.net.Socket
getSocket()
Get the socket.java.lang.String
getSocketName()
Get the printable description of the socket.Message
handle(Message message)
Handle a message using the MessageHandler.boolean
isAlive()
Is this connection alive?protected void
logMessage(Message message, boolean send)
Log a message.Message
reader()
Read a message using the MessageHandler.void
request(Message message)
Client request.void
send(Message message)
Client send.void
sendDisconnect()
Signal that this connection is disconnecting.void
sendMessage(Message message)
Send a message, do not consider a response.void
sendReconnect()
Send a reconnect message.Connection
setCommsLogging(boolean log)
Set the logging state of this connection.Connection
setMessageHandler(MessageHandler messageHandler)
Sets the message handler for this connection.private void
setSocket(java.net.Socket socket)
Set the socket.void
setWriteScope(FreeColXMLWriter.WriteScope ws)
Set the output write scope.java.lang.String
startListen()
void
startReceiving()
Start the recieving thread.java.lang.String
toString()
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
DEFAULT_REPLY_TIMEOUT
public static final long DEFAULT_REPLY_TIMEOUT
The default timeout when waiting for a reply from the server. Defined in milliseconds. Setting 30s for now since there was no timeout earlier when waiting for å reply.- See Also:
- Constant Field Values
-
END_OF_STREAM
public static final char END_OF_STREAM
- See Also:
- Constant Field Values
-
END_OF_STREAM_ARRAY
private static final char[] END_OF_STREAM_ARRAY
-
BUFFER_SIZE
public static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
NETWORK_REPLY_ID_TAG
public static final java.lang.String NETWORK_REPLY_ID_TAG
- See Also:
- Constant Field Values
-
QUESTION_TAG
public static final java.lang.String QUESTION_TAG
- See Also:
- Constant Field Values
-
REPLY_TAG
public static final java.lang.String REPLY_TAG
- See Also:
- Constant Field Values
-
SEND_SUFFIX
public static final java.lang.String SEND_SUFFIX
- See Also:
- Constant Field Values
-
REPLY_SUFFIX
public static final java.lang.String REPLY_SUFFIX
- See Also:
- Constant Field Values
-
TIMEOUT
private static final int TIMEOUT
- See Also:
- Constant Field Values
-
name
private java.lang.String name
The name of the connection.
-
socketLock
private final java.lang.Object socketLock
A lock for access to the socket.
-
socket
private java.net.Socket socket
The socket connected to the other end of the connection.
-
inputLock
private final java.lang.Object inputLock
A lock for the input side.
-
br
private java.io.BufferedReader br
The wrapped version of the input side of the socket.
-
xr
private FreeColXMLReader xr
An XML stream wrapping of an input line.
-
outputLock
private final java.lang.Object outputLock
A lock for the output side.
-
xw
private FreeColXMLWriter xw
Main message writer.
-
lw
private FreeColXMLWriter lw
The FreeColXMLWriter to write logging messages to.
-
receivingThread
private ReceivingThread receivingThread
The subthread to read the input.
-
messageHandler
private MessageHandler messageHandler
The message handler to process incoming messages with.
-
connected
private boolean connected
Is there an active connection.
-
-
Constructor Detail
-
Connection
protected Connection(java.lang.String name)
Trivial constructor.- Parameters:
name
- The name of the connection.
-
Connection
public Connection(java.net.Socket socket, java.lang.String name) throws java.io.IOException
- Parameters:
socket
- The socket to the client.name
- The connection name.- Throws:
java.io.IOException
- if streams can not be derived from the socket.
-
Connection
public Connection(java.lang.String host, int port, java.lang.String name) throws java.io.IOException
Sets up a new socket with specified host and port and usesConnection(Socket, String)
.- Parameters:
host
- The host to connect to.port
- The port to connect to.name
- The name for the connection.- Throws:
java.io.IOException
- if the socket creation is problematic.
-
-
Method Detail
-
createSocket
private static java.net.Socket createSocket(java.lang.String host, int port) throws java.io.IOException
Creates a socket to communication with a given host, port pair.- Parameters:
host
- The host to connect to.port
- The port to connect to.- Returns:
- A new socket.
- Throws:
java.io.IOException
- on failure to create/connect the socket.
-
startReceiving
public void startReceiving()
Start the recieving thread.
-
getSocket
public java.net.Socket getSocket()
Get the socket.- Returns:
- The current
Socket
.
-
setSocket
private void setSocket(java.net.Socket socket)
Set the socket.- Parameters:
socket
- The newSocket
.
-
closeSocket
private void closeSocket()
Close and clear the socket.
-
closeOutputStream
private void closeOutputStream()
Close and clear the output stream.
-
closeInputStream
private void closeInputStream()
Close and clear the input stream.
-
isAlive
public boolean isAlive()
Is this connection alive?- Returns:
- True if the connection is alive.
-
getHostAddress
public java.lang.String getHostAddress()
Get the host address of this connection.- Returns:
- The host address, or an empty string on error.
-
getPort
public int getPort()
Get the port for this connection.- Returns:
- The port number, or negative on error.
-
getSocketName
public java.lang.String getSocketName()
Get the printable description of the socket.- Returns:
- *host-address*:*port-number* or an empty string on error.
-
getMessageHandler
public MessageHandler getMessageHandler()
Gets the message handler for this connection.- Returns:
- The
MessageHandler
for this Connection.
-
setMessageHandler
public Connection setMessageHandler(MessageHandler messageHandler)
Sets the message handler for this connection.- Parameters:
messageHandler
- The newMessageHandler
for this Connection.- Returns:
- This
Connection
.
-
setWriteScope
public void setWriteScope(FreeColXMLWriter.WriteScope ws)
Set the output write scope.- Parameters:
ws
- The new write scope.
-
getName
public java.lang.String getName()
Gets the connection name.- Returns:
- The connection name.
-
setCommsLogging
public final Connection setCommsLogging(boolean log)
Set the logging state of this connection.- Parameters:
log
- If true, enable logging.- Returns:
- This
Connection
.
-
sendDisconnect
public void sendDisconnect()
Signal that this connection is disconnecting.
-
sendReconnect
public void sendReconnect()
Send a reconnect message.
-
disconnect
public void disconnect()
Disconnect this connection.
-
getFreeColXMLReader
public FreeColXMLReader getFreeColXMLReader()
-
startListen
public java.lang.String startListen() throws javax.xml.stream.XMLStreamException
- Throws:
javax.xml.stream.XMLStreamException
-
getReplyId
public int getReplyId()
-
endListen
public void endListen()
-
askMessage
public Message askMessage(Message message, long timeout) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException, java.util.concurrent.TimeoutException
Send a message, and return the response. Log both.- Parameters:
message
- TheMessage
to send.timeout
- A timeout in milliseconds, after which aTimeoutException
gets thrown when waiting for a reply.- Returns:
- The response.
- Throws:
FreeColException
- on extreme confusion.java.io.IOException
- on failure to send.javax.xml.stream.XMLStreamException
- on stream write error.java.util.concurrent.TimeoutException
- when the timeout is reached.
-
sendMessage
public void sendMessage(Message message) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Send a message, do not consider a response. Public as this is called from ReceivingThread.- Parameters:
message
- TheMessage
to send.- Throws:
FreeColException
- on extreme confusion.java.io.IOException
- on failure to send.javax.xml.stream.XMLStreamException
- on stream problem.
-
logMessage
protected final void logMessage(Message message, boolean send)
Log a message.- Parameters:
message
- TheMessage
to log.send
- True if this is a send, false if a reply.
-
handle
public Message handle(Message message) throws FreeColException
Handle a message using the MessageHandler.- Parameters:
message
- TheMessage
to handle.- Returns:
- The result of the handler.
- Throws:
FreeColException
- if the message is malformed.
-
reader
public Message reader() throws FreeColException, javax.xml.stream.XMLStreamException
Read a message using the MessageHandler.- Returns:
- The
Message
found, if any. - Throws:
FreeColException
- there is a problem creating the message.javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
request
public void request(Message message) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Client request.- Parameters:
message
- AMessage
to process.- Throws:
FreeColException
- on handler failure.java.io.IOException
- if there is a problem sending messages.javax.xml.stream.XMLStreamException
- if there is a message format problem.
-
send
public void send(Message message) throws FreeColException, java.io.IOException, javax.xml.stream.XMLStreamException
Client send.- Parameters:
message
- AMessage
to send.- Throws:
FreeColException
- on extreme confusion.java.io.IOException
- on write error.javax.xml.stream.XMLStreamException
- if there is a message format problem.
-
close
public void close()
Close this connection.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-