Class 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 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_ARRAY

        private static final char[] END_OF_STREAM_ARRAY
      • NETWORK_REPLY_ID_TAG

        public static final java.lang.String NETWORK_REPLY_ID_TAG
        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.
      • outputLock

        private final java.lang.Object outputLock
        A lock for the output side.
      • 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
        Creates a new Connection with the specified Socket and MessageHandler.
        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 uses Connection(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 new Socket.
      • 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 new MessageHandler 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.
      • 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 - The Message to send.
        timeout - A timeout in milliseconds, after which a TimeoutException 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 - The Message 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 - The Message 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 - The Message 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 - A Message 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 - A Message 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 interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • toString

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