|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.freecol.common.networking.Connection
public class Connection
A network connection. Responsible for both sending and receiving network messages.
send(Element)
,
sendAndWait(Element)
,
ask(Element)
Constructor Summary | |
---|---|
Connection(java.net.Socket socket,
MessageHandler messageHandler,
java.lang.String threadName)
Creates a new Connection with the specified
Socket and MessageHandler . |
|
Connection(java.lang.String host,
int port,
MessageHandler messageHandler,
java.lang.String threadName)
Sets up a new socket with specified host and port and uses #Connection(Socket, MessageHandler) . |
Method Summary | |
---|---|
javax.xml.stream.XMLStreamWriter |
ask()
Starts a session for asking a question using streaming. |
org.w3c.dom.Element |
ask(org.w3c.dom.Element element)
Sends a message to the other peer and returns the reply. |
void |
close()
Sends a "disconnect"-message and closes this connection. |
void |
endTransmission(javax.xml.stream.XMLStreamReader in)
Ends the transmission of a message or a ask/get-reply session. |
MessageHandler |
getMessageHandler()
Gets the MessageHandler for this Connection. |
javax.xml.stream.XMLStreamReader |
getReply()
Gets the reply being received after sending a question. |
java.net.Socket |
getSocket()
Gets the socket. |
void |
handleAndSendReply(java.io.BufferedInputStream in)
Handles a message using the registered MessageHandler . |
void |
reallyClose()
Closes this connection. |
javax.xml.stream.XMLStreamWriter |
send()
Starts a session for sending a message using streaming. |
void |
send(org.w3c.dom.Element element)
Sends the given message over this Connection. |
void |
sendAndWait(org.w3c.dom.Element element)
Sends the given message over this Connection and waits for
confirmation of receiveval before returning. |
void |
setMessageHandler(MessageHandler mh)
Sets the MessageHandler for this Connection. |
java.lang.String |
toString()
Override the default and return socket details. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Connection(java.lang.String host, int port, MessageHandler messageHandler, java.lang.String threadName) throws java.io.IOException
#Connection(Socket, MessageHandler)
.
host
- The host to connect to.port
- The port to connect to.messageHandler
- The MessageHandler to call for each message
received.
java.io.IOException
public Connection(java.net.Socket socket, MessageHandler messageHandler, java.lang.String threadName) throws java.io.IOException
Connection
with the specified
Socket
and MessageHandler
.
socket
- The socket to the client.messageHandler
- The MessageHandler to call for each message
received.
java.io.IOException
Method Detail |
---|
public void close() throws java.io.IOException
java.io.IOException
public void reallyClose() throws java.io.IOException
java.io.IOException
public void send(org.w3c.dom.Element element) throws java.io.IOException
element
- The element (root element in a DOM-parsed XML tree) that
holds all the information
java.io.IOException
- If an error occur while sending the message.sendAndWait(Element)
,
ask(Element)
public org.w3c.dom.Element ask(org.w3c.dom.Element element) throws java.io.IOException
element
- The question for the other peer.
java.io.IOException
- If an error occur while sending the message.send(Element)
,
sendAndWait(Element)
public javax.xml.stream.XMLStreamWriter ask() throws java.io.IOException
XML Elements
that can be used when streaming is not required (that is: when the
messages to be transmitted are small).
try { XMLStreamWriter out = ask(); // Write XML here XMLStreamReader in = connection.getReply(); // Read XML here connection.endTransmission(in); } catch (IOException e) { logger.warning("Could not send XML."); }
XMLStreamWriter
for sending the question. The
method getReply()
should be called when the message has
been written and the reply is required.
java.io.IOException
- if thrown by the underlying network stream.getReply()
,
endTransmission(XMLStreamReader)
public javax.xml.stream.XMLStreamWriter send() throws java.io.IOException
XML Elements
that can be used when streaming is not required (that is: when the
messages to be transmitted are small).
try { XMLStreamWriter out = send(); // Write XML here connection.endTransmission(in); } catch (IOException e) { logger.warning("Could not send XML."); }
XMLStreamWriter
for sending the question. The
method endTransmission(XMLStreamReader)
should be called
when the message has been written.
java.io.IOException
- if thrown by the underlying network stream.getReply()
,
endTransmission(XMLStreamReader)
public javax.xml.stream.XMLStreamReader getReply() throws java.io.IOException
XMLStreamReader
for reading the incoming data.
java.io.IOException
- if thrown by the underlying network stream.ask()
public void endTransmission(javax.xml.stream.XMLStreamReader in) throws java.io.IOException
java.io.IOException
- if thrown by the underlying network stream.ask()
,
send()
public void sendAndWait(org.w3c.dom.Element element) throws java.io.IOException
Connection
and waits for
confirmation of receiveval before returning.
element
- The element (root element in a DOM-parsed XML tree) that
holds all the information
java.io.IOException
- If an error occur while sending the message.send(Element)
,
ask(Element)
public void setMessageHandler(MessageHandler mh)
mh
- The new MessageHandler for this Connection.public MessageHandler getMessageHandler()
public void handleAndSendReply(java.io.BufferedInputStream in)
MessageHandler
.
in
- The stream containing the message.public java.net.Socket getSocket()
Socket
used while communicating with the other
peer.public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |