1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
53 in.readInt();
54
55
56
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();
88 in.readInt();
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
99
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
116
117
118 } catch (IOException e) {
119 e.printStackTrace();
120 }
121 }
122 }