View Javadoc

1   package fr.ove.clientserver;
2   
3   import fr.ove.utils.Connection;
4   import fr.ove.utils.ConnectionHandler;
5   import fr.ove.openmath.mathematica.ResponseReader;
6   import java.io.IOException;
7   
8   /***
9   * A ConnectionHandler implementation.<BR>
10  * This connection handler is used with a server which connects to an another one.
11  * The objects sent from a connected client are sent to the server the server whose
12  * instance deals the conections is connected.
13  */
14  public class MuxConnectionHandler implements ConnectionHandler {    
15      /***
16      * The connection with the server the server that the instance handles the connection
17      * is connected.
18      */
19      private Connection serverConnection;
20      
21      /***
22      * The constructor.
23      * @param hostName the name of the host the instance connects to.
24      * @param portNumber the port number through the instance is connected.
25      */
26      public MuxConnectionHandler(String hostName, int portNumber) {
27          serverConnection = new Connection(hostName, portNumber);
28          try {
29              serverConnection.open();
30          }
31          catch (IOException ioe) {
32              System.out.println("Failed to open a connection to the server");
33              ioe.printStackTrace();
34          }
35      }
36      
37      // #################################
38      // ### Interfaces Implementation ###
39      // #################################    
40  
41      // ConnectionHandler interface
42  
43      /***
44      * Handles the specified connection.
45      * @param connection the connection to handle.
46      */
47      public void handleConnection(Connection connection) {
48          new LinkConnection(connection, serverConnection);
49      }
50      
51      /***
52      *
53      */
54      private class LinkConnection implements Runnable {
55          private Connection clientConnection;
56          private Connection serverConnection;
57          private ResponseReader reader = new ResponseReader();
58          
59          public LinkConnection(Connection clientConnection, Connection serverConnection) {
60              this.clientConnection = clientConnection;
61              this.serverConnection = serverConnection;
62              
63              new Thread(this).start();
64          }
65          // Runnable interface
66  
67          /***
68          *
69          */
70          public void run() {
71              String object = null;
72              
73              while (true) {
74                  try {Thread.sleep(100);} catch (Exception e) {e.printStackTrace();}
75                  if (clientConnection != null) {
76                      // Read the object sent by the client.
77                      object = reader.read(clientConnection.getInputStream());
78                      // if we got it, send it to the server
79                      if (object != null) {
80                          try {
81                              serverConnection.getOutputStream().write(object.getBytes());
82                              // Wait for the response from the server
83                              object = reader.read(serverConnection.getInputStream());
84                              // if we got something, send it to the client
85                              if (object != null) {
86                                  try {
87                                      clientConnection.getOutputStream().write(object.getBytes());
88                                  }
89                                  catch (IOException ioe) {
90                                      System.out.println("Failed to send back response to the client");
91                                      ioe.printStackTrace();
92                                  }
93                              }
94                          }
95                          catch (IOException ioe) {
96                              System.out.println("Failed to pipe the object to the server");
97                              ioe.printStackTrace();
98                          }
99                      }
100                 }
101             }
102         }
103     }
104 }