/
Transport.java
274 lines (246 loc) · 9.02 KB
/
Transport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package jcifs.smb1.util.transport;
import java.io.*;
import java.net.*;
import java.util.*;
import jcifs.smb1.util.LogStream;
/**
* This class simplifies communication for protocols that support
* multiplexing requests. It encapsulates a stream and some protocol
* knowledge (provided by a concrete subclass) so that connecting,
* disconnecting, sending, and receiving can be syncronized
* properly. Apparatus is provided to send and receive requests
* concurrently.
*/
public abstract class Transport implements Runnable {
static int id = 0;
static LogStream log = LogStream.getInstance();
public static int readn( InputStream in,
byte[] b,
int off,
int len ) throws IOException {
int i = 0, n = -5;
while (i < len) {
n = in.read( b, off + i, len - i );
if (n <= 0) {
break;
}
i += n;
}
return i;
}
/* state values
* 0 - not connected
* 1 - connecting
* 2 - run connected
* 3 - connected
* 4 - error
*/
int state = 0;
String name = "Transport" + id++;
Thread thread;
TransportException te;
protected HashMap response_map = new HashMap( 4 );
protected abstract void makeKey( Request request ) throws IOException;
protected abstract Request peekKey() throws IOException;
protected abstract void doSend( Request request ) throws IOException;
protected abstract void doRecv( Response response ) throws IOException;
protected abstract void doSkip() throws IOException;
public synchronized void sendrecv( Request request,
Response response,
long timeout ) throws IOException {
makeKey( request );
response.isReceived = false;
try {
response_map.put( request, response );
doSend( request );
response.expiration = System.currentTimeMillis() + timeout;
while (!response.isReceived) {
wait( timeout );
timeout = response.expiration - System.currentTimeMillis();
if (timeout <= 0) {
throw new TransportException( name +
" timedout waiting for response to " +
request );
}
}
} catch( IOException ioe ) {
if (log.level > 2)
ioe.printStackTrace( log );
try {
disconnect( true );
} catch( IOException ioe2 ) {
ioe2.printStackTrace( log );
}
throw ioe;
} catch( InterruptedException ie ) {
throw new TransportException( ie );
} finally {
response_map.remove( request );
}
}
private void loop() {
while( thread == Thread.currentThread() ) {
try {
Request key = peekKey();
if (key == null)
throw new IOException( "end of stream" );
synchronized (this) {
Response response = (Response)response_map.get( key );
if (response == null) {
if (log.level >= 4)
log.println( "Invalid key, skipping message" );
doSkip();
} else {
doRecv( response );
response.isReceived = true;
notifyAll();
}
}
} catch( Exception ex ) {
String msg = ex.getMessage();
boolean timeout = msg != null && msg.equals( "Read timed out" );
/* If just a timeout, try to disconnect gracefully
*/
boolean hard = timeout == false;
if (!timeout && log.level >= 3)
ex.printStackTrace( log );
try {
disconnect( hard );
} catch( IOException ioe ) {
ioe.printStackTrace( log );
}
}
}
}
/* Build a connection. Only one thread will ever call this method at
* any one time. If this method throws an exception or the connect timeout
* expires an encapsulating TransportException will be thrown from connect
* and the transport will be in error.
*/
protected abstract void doConnect() throws Exception;
/* Tear down a connection. If the hard parameter is true, the diconnection
* procedure should not initiate or wait for any outstanding requests on
* this transport.
*/
protected abstract void doDisconnect( boolean hard ) throws IOException;
public synchronized void connect( long timeout ) throws TransportException {
try {
switch (state) {
case 0:
break;
case 3:
return; // already connected
case 4:
state = 0;
throw new TransportException( "Connection in error", te );
default:
TransportException te = new TransportException( "Invalid state: " + state );
state = 0;
throw te;
}
state = 1;
te = null;
thread = new Thread( this, name );
thread.setDaemon( true );
synchronized (thread) {
thread.start();
thread.wait( timeout ); /* wait for doConnect */
switch (state) {
case 1: /* doConnect never returned */
state = 0;
thread = null;
throw new TransportException( "Connection timeout" );
case 2:
if (te != null) { /* doConnect throw Exception */
state = 4; /* error */
thread = null;
throw te;
}
state = 3; /* Success! */
return;
}
}
} catch( InterruptedException ie ) {
state = 0;
thread = null;
throw new TransportException( ie );
} finally {
/* This guarantees that we leave in a valid state
*/
if (state != 0 && state != 3 && state != 4) {
if (log.level >= 1)
log.println("Invalid state: " + state);
state = 0;
thread = null;
}
}
}
public synchronized void disconnect( boolean hard ) throws IOException {
IOException ioe = null;
switch (state) {
case 0: /* not connected - just return */
return;
case 2:
hard = true;
case 3: /* connected - go ahead and disconnect */
if (response_map.size() != 0 && !hard) {
break; /* outstanding requests */
}
try {
doDisconnect( hard );
} catch (IOException ioe0) {
ioe = ioe0;
}
case 4: /* in error - reset the transport */
thread = null;
state = 0;
break;
default:
if (log.level >= 1)
log.println("Invalid state: " + state);
thread = null;
state = 0;
break;
}
if (ioe != null)
throw ioe;
}
public void run() {
Thread run_thread = Thread.currentThread();
Exception ex0 = null;
try {
/* We cannot synchronize (run_thread) here or the caller's
* thread.wait( timeout ) cannot reaquire the lock and
* return which would render the timeout effectively useless.
*/
doConnect();
} catch( Exception ex ) {
ex0 = ex; // Defer to below where we're locked
return;
} finally {
synchronized (run_thread) {
if (run_thread != thread) {
/* Thread no longer the one setup for this transport --
* doConnect returned too late, just ignore.
*/
if (ex0 != null) {
if (log.level >= 2)
ex0.printStackTrace(log);
}
return;
}
if (ex0 != null) {
te = new TransportException( ex0 );
}
state = 2; // run connected
run_thread.notify();
}
}
/* Proccess responses
*/
loop();
}
public String toString() {
return name;
}
}