View Javadoc

1   /*
2    * Copyright 2000 Computer System Services, Inc.
3    *
4    * Permission to use this software for any purpose is granted provided that
5    * this copyright notice is preserved.
6    *
7    * This software is provided as-is and without warranty as to its
8    * fitness for any purpose.  In other words, Computer System Services,
9    * Inc. does not guarantee that this software works.  It is provided
10   * only in the hope that it may be found useful by someone.
11   *
12   * Please e-mail tttaylor@cssassociates.com if you find any errors
13   * or want to request changes/enhancements.
14   */
15  package twcsckernel.projectbase.rmi.socketfactory;
16  
17  import java.io.*;
18  import java.net.*;
19  
20  /***
21   * Provides a signalling channel used to set up callback sockets between the
22   * client and server.
23   * 
24   * @author Tim Taylor -- tttaylor@cssassociates.com
25   */
26  public class SignallingChannel extends Thread {
27    private String directAddress;
28    private int directPort;
29  
30    private String destinationAddress;
31    private int destinationPort;
32  
33    private Socket socket;
34    private DataInputStream in;
35    private DataOutputStream out;
36  
37    public SignallingChannel(final String directAddress, final int directPort)
38        throws IOException {
39      this.directAddress = directAddress;
40      this.directPort = directPort;
41      socket = new Socket(directAddress, directPort);
42  
43      in = new DataInputStream(socket.getInputStream());
44      out = new DataOutputStream(socket.getOutputStream());
45  
46      // Register with server
47      out.writeInt(TwoWay.PROTOCOL_MAGIC);
48      out.writeInt(TwoWay.REGISTER_CALLBACK_SOCKET_SOURCE);
49      out.write(socket.getLocalAddress().getAddress());
50      out.flush();
51  
52      // Get back server endpoint info
53      in.readInt(); /*
54                     * int opcode = in.readInt(); - zmienna opcode nie byla
55                     * czytana-widocznie jakis smiec do wyciagniecia z kanalu;)
56                     * nizej podobnie
57                     */
58      byte[] address = new byte[4];
59      in.read(address, 0, 4);
60      destinationAddress = TwoWay.getAddressString(address);
61      destinationPort = in.readInt();
62    }
63  
64    public String getDirectAddress() {
65      return directAddress;
66    }
67  
68    public int getDirectPort() {
69      return directPort;
70    }
71  
72    public String getDestinationAddress() {
73      return destinationAddress;
74    }
75  
76    public int getDestinationPort() {
77      return destinationPort;
78    }
79  
80    public DataOutputStream getOutputStream() {
81      return out;
82    }
83  
84    public void run() {
85      try {
86        for (;;) {
87          in.readInt();// int magic = in.readInt(); //j.w.
88          in.readInt();// int opcode = in.readInt(); //j.w.
89          int port = in.readInt();
90  
91          byte[] localAddress = InetAddress.getLocalHost().getAddress();
92  
93          Socket localSocket = null;
94  
95          try {
96            localSocket = new Socket(TwoWay.getAddressString(localAddress), port);
97          } catch (IOException e) {
98            // Usually means client terminating while server
99            // requesting callback socket.
100         }
101 
102         Socket remoteSocket = new Socket(directAddress, directPort);
103         new SocketAdapter(localSocket, remoteSocket);
104 
105         DataOutputStream remoteOut = new DataOutputStream(remoteSocket
106             .getOutputStream());
107 
108         remoteOut.writeInt(TwoWay.PROTOCOL_MAGIC);
109         remoteOut.writeInt(TwoWay.RETURN_CALLBACK_SOCKET);
110         remoteOut.write(localAddress);
111         remoteOut.writeInt(port);
112         remoteOut.flush();
113       }
114     } catch (EOFException e) {
115       // Assume the server disconnected. We could add shutdown
116       // signalling to the protocol, but we would still find
117       // ourselves here if the server abends.
118     } catch (IOException e) {
119       e.printStackTrace();
120     }
121   }
122 }