1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| private EventDispachThread edt = null;
private IOSession session = null;
/** Creates a new instance of Main */
public Main() {
try {
createNetworkService();
}
catch(Exception e) {
// Do job
}
this.session = connectToServer();
if( this.session == null ) {
// Do Job
}
Req requete = new Req("GET_INFO", dest, date);
/** Create a tool class for using synchronous communication (blocking reads)
*/
Conversation conversation = new Conversation(this.session);
/** Send the request (asynchronous write)
* This method returns a Future for tracking task (and cancelation purpose)
*/
conversation.write( requete );
/** Wait the response (blocking reads)
*/
try {
Object response = conversation.read();
}
catch(InterruptedException ie) {
// Do Job
}
/** Close the conversation tool (but not the underlying session)
* NB: If the session is closed by the peer, this conversation will be closed also automatically
*/
conversation.close();
/** Close the session
*/
this.session.close();
/** Stop the service
*/
this.edt.stop();
this.edt = null;
}
/** Create a network Service
*/
private void createNetworkService() throws NotStartedException {
if( this.edt != null ) throw new IllegalStateException("Service already started");
/** Start a network service (as client mode)
*/
this.edt = EventDispachThread.start();
}
/** Connect to the server
*/
private IOSession connectToServer() {
if( this.edt == null ) throw new IllegalStateException("Service must be started");
return this.edt.connect( InetAddress.getByName("you.server.ip") , 4555 );
} |