View Javadoc

1   package org.sourceforge.jemmrpc.example.echo;
2   
3   import java.io.IOException;
4   
5   import org.apache.log4j.Logger;
6   import org.sourceforge.jemmrpc.client.RPCClient;
7   
8   /**
9    * A simple test client for sending echo messages to an echo server.
10   */
11  public class EchoClient implements EchoClientIF
12  {
13      protected static final Logger logger = Logger.getLogger(EchoClient.class);
14  
15      String hostname;
16  
17      int port;
18  
19      RPCClient client;
20  
21      EchoServerIF echoServerIF;
22  
23      /**
24       * Creates an EchoClient with the given parameters.
25       * 
26       * @param hostname The target echo server host name.
27       * @param port The target echo server port.
28       * @throws IOException On connection error.
29       */
30      public EchoClient(String hostname, int port) throws IOException
31      {
32          this.hostname = hostname;
33          this.port = port;
34          client = new RPCClient(hostname, port);
35          client.registerIF(EchoClientIF.class, this);
36          client.connect();
37          echoServerIF = (EchoServerIF) client.getServerIF(EchoServerIF.class);
38      }
39  
40      /**
41       * Send an message to the echo server.
42       * 
43       * @param message The message to send to the echo server
44       * @return The reply from the echo server
45       */
46      public String sendEcho(String message)
47      {
48          logger.debug("Synchronous client message send");
49          return echoServerIF.echo(message);
50      }
51  
52      /**
53       * Send an echo message asynchronously. Replies are received by processServerMessage method.
54       * 
55       * @param message The message to send.
56       */
57      public void sendAsyncEcho(String message)
58      {
59          logger.debug("Async client message send");
60          echoServerIF.asyncEcho(message);
61      }
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see org.sourceforge.jemm.comm.example.echo.EchoClientIF#asyncEchoReply(java.lang.String)
67       */
68      public void asyncEchoReply(String reply)
69      {
70          logger.info("Got async echo reply: " + reply);
71      }
72  
73      /**
74       * Close the echo client/server connection.
75       */
76      public void close()
77      {
78          client.close();
79      }
80  }