1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.server.service;
22
23 //import java.io.BufferedInputStream;
24 //import java.io.BufferedOutputStream;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.net.Socket;
28 import java.lang.InterruptedException;
29
30 import autohit.common.AutohitErrorCodes;
31 import autohit.common.AutohitLogInjectorWrapper;
32
33 /**
34 * Socket Relay Helper. Basically, this a bound relay.
35 *
36 * @author Erich P. Gatejen
37 * @version 1.0 <i>Version History</i><code>EPG - Initial - 15Sep03</code>
38 */
39 public class SocketRelayHelper extends Thread {
40
41 final private static int BUFFER_SIZE = 512;
42
43 /**
44 * Server side */
45 public Socket up;
46
47 /**
48 * Client side */
49 public Socket down;
50
51 // Props
52 private String outAddress;
53 private int outPort;
54
55 // Links
56 private SocketRelayServiceHelper uplink;
57 private SocketRelayServiceHelper downlink;
58
59 /**
60 * Logging mechinism */
61 public AutohitLogInjectorWrapper myLog;
62 public boolean wireFlag;
63
64 /**
65 * Default constructor */
66 public SocketRelayHelper() {
67 super();
68 }
69
70 /**
71 * Complete construction. This will be called when the VM is initialized. */
72 public void init(Socket ins, String addr, int port, AutohitLogInjectorWrapper logger, boolean wire) {
73 down = ins;
74 outAddress = addr;
75 outPort = port;
76 myLog = logger;
77 wireFlag = wire;
78 }
79
80 /**
81 * Run the context */
82 public void run() {
83
84 try {
85
86 // Try to construct
87 up = new Socket(outAddress, outPort);
88
89 // links
90 if (wireFlag) {
91 SocketRelayServiceHelperLink tuplink = new SocketRelayServiceHelperLink();
92 tuplink.init(down.getInputStream(), up.getOutputStream());
93 uplink = (SocketRelayServiceHelper) tuplink;
94 SocketRelayServiceHelperLink tdownlink = new SocketRelayServiceHelperLink();
95 tdownlink.init(up.getInputStream(), down.getOutputStream());
96 downlink = (SocketRelayServiceHelper) tdownlink;
97 } else {
98 SocketRelayServiceHelperLinkWIRE tuplink = new SocketRelayServiceHelperLinkWIRE();
99 tuplink.init(down.getInputStream(), up.getOutputStream(), "uplink");
100 uplink = (SocketRelayServiceHelper) tuplink;
101 SocketRelayServiceHelperLinkWIRE tdownlink = new SocketRelayServiceHelperLinkWIRE();
102 tdownlink.init(up.getInputStream(), down.getOutputStream(), "downlink");
103 downlink = (SocketRelayServiceHelper) tdownlink;
104 }
105
106 // go dog go
107 uplink.start();
108 downlink.start();
109 myLog.info(
110 "SocketRelayServiceHelper: Uplink and downlink established.",
111 AutohitErrorCodes.CODE_INFORMATIONAL_OK);
112
113 // the end... This assumes that the uplink will come down if the
114 // downlink socket has a problem, since the socket IO should cause
115 // an exception. If this is a bad assumption, this code may ghost
116 // or
117 // deadlock.
118 uplink.join();
119 downlink.interrupt();
120 myLog.info("SocketRelayServiceHelper:Relay to " + outAddress + " interrupted. Killing link.");
121
122 } catch (InterruptedException e) {
123
124 try {
125
126 uplink.interrupt();
127 } catch (Exception eee) { // dont care
128 }
129 try {
130 downlink.interrupt();
131 } catch (Exception eee) { // dont care
132 }
133
134 } catch (Exception e) {
135
136 // Generally, don't care why.
137 myLog.info(
138 "SocketRelayServiceHelper:Connection to " + outAddress + " died. reason=" + e.getMessage(),
139 AutohitErrorCodes.CODE_INFORMATIONAL_OK);
140
141 } finally {
142
143 // Brute force close these guys
144 try {
145 up.close();
146 } catch (Exception eee) {
147 // Don't care
148 }
149 try {
150 down.close();
151 } catch (Exception eee) {
152 // Don't care
153 }
154 }
155 }
156
157 // IMBEDDED CLASS - LINK DRAIN
158 class SocketRelayServiceHelper extends Thread {
159
160 }
161
162 // IMBEDDED CLASS - LINK DRAIN
163 class SocketRelayServiceHelperLink extends SocketRelayServiceHelper {
164
165 //BufferedInputStream in;
166 //BufferedOutputStream out;
167 InputStream in;
168 OutputStream out;
169
170 public void init(InputStream i, OutputStream o) {
171 //in = new BufferedInputStream(i);
172 //out = new BufferedOutputStream(o);
173 in = i;
174 out = o;
175 }
176
177 public void run() {
178 int thing;
179
180 try {
181
182 // turn and burn
183 while ((thing = in.read()) != -1) {
184 out.write(thing);
185 }
186
187 } catch (Exception e) {
188 // don't care. may be an interrupt.
189 } finally {
190 //
191 try {
192 out.close();
193 in.close();
194 } catch (Exception e) {
195 // dont care
196 }
197 }
198 } // end run()
199
200 } // end IMBEDDED CLASS - LINK DRAIN
201
202 // IMBEDDED CLASS - LINK DRAIN
203 class SocketRelayServiceHelperLinkWIRE extends SocketRelayServiceHelper {
204
205 InputStream in;
206 OutputStream out;
207 String wname;
208
209 public void init(InputStream i, OutputStream o, String name) {
210
211 in = i;
212 out = o;
213 wname = "SocketRelayService WIRE:" + name + ":";
214 }
215
216 public void run() {
217 int thing;
218
219 try {
220
221 // turn and burn
222 while ((thing = in.read()) != -1) {
223 out.write(thing);
224 myLog.debug(wname + thing);
225 }
226
227 } catch (Exception e) {
228 // don't care. may be an interrupt.
229 } finally {
230 //
231 try {
232 out.close();
233 in.close();
234 } catch (Exception e) {
235 // dont care
236 }
237 }
238 } // end run()
239
240 } // end IMBEDDED CLASS - LINK DRAIN
241 }
|