|
|||||||||
| 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)| Field Summary | |
|---|---|
private int |
currentQuestionID
|
private java.io.InputStream |
in
|
private static java.util.logging.Logger |
logger
|
private MessageHandler |
messageHandler
|
private java.io.OutputStream |
out
|
private java.net.Socket |
socket
|
private ReceivingThread |
thread
|
private java.lang.String |
threadName
|
private static int |
TIMEOUT
|
private javax.xml.stream.XMLStreamWriter |
xmlOut
|
private javax.xml.transform.Transformer |
xmlTransformer
|
private javax.xml.stream.XMLOutputFactory |
xof
|
| Constructor Summary | |
|---|---|
protected |
Connection()
Dead constructor, for DummyConnection purposes. |
|
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, String). |
| 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. |
private static java.net.Socket |
createSocket(java.lang.String host,
int port)
|
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. |
private void |
releaseQuestionId()
Release a previously obtained question id. |
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. |
private void |
waitForAndSetNewQuestionId()
Wait until the previous question has been released, then install a new question id. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
private static final java.util.logging.Logger logger
private static final int TIMEOUT
private final java.io.OutputStream out
private final java.io.InputStream in
private final java.net.Socket socket
private final javax.xml.transform.Transformer xmlTransformer
private final ReceivingThread thread
private final javax.xml.stream.XMLOutputFactory xof
private MessageHandler messageHandler
private javax.xml.stream.XMLStreamWriter xmlOut
private int currentQuestionID
private java.lang.String threadName
| Constructor Detail |
|---|
protected Connection()
public Connection(java.lang.String host,
int port,
MessageHandler messageHandler,
java.lang.String threadName)
throws java.io.IOException
Connection(Socket, MessageHandler, String).
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 |
|---|
private static java.net.Socket createSocket(java.lang.String host,
int port)
throws java.io.IOException
java.io.IOException
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)private void releaseQuestionId()
waitForAndSetNewQuestionId()private void waitForAndSetNewQuestionId()
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 | ||||||||