-
Notifications
You must be signed in to change notification settings - Fork 131
/
DCapDoor.java
346 lines (307 loc) · 10.5 KB
/
DCapDoor.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// $Id: DCapDoor.java,v 1.17 2007-04-05 19:44:07 podstvkv Exp $
package diskCacheV111.doors ;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.security.auth.Subject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.concurrent.TimeUnit;
import dmg.cells.nucleus.CellAdapter;
import dmg.cells.nucleus.CellMessage;
import dmg.cells.nucleus.CellNucleus;
import dmg.util.CommandExitException;
import dmg.util.KeepAliveListener;
import dmg.util.StreamEngine;
import org.dcache.auth.Subjects;
import org.dcache.util.Args;
import org.dcache.util.Transfer;
/**
* @author Patrick Fuhrmann
* @version 1.0, Aug 04 2001
*
* Simple Template for a Door. It runs the DiskCacheV111
* Protocol V3 version.
*
*
*
*/
public class DCapDoor
extends CellAdapter
implements Runnable, KeepAliveListener {
private final static Logger _log =
LoggerFactory.getLogger(DCapDoor.class);
private StreamEngine _engine;
private BufferedReader _in;
private PrintWriter _out;
private String _host;
private Subject _subject;
private Thread _workerThread;
private int _commandCounter;
private String _lastCommand = "<init>";
private Reader _reader;
private CellNucleus _nucleus;
private boolean _dcapLock = true ;
/**
* DCAP command interpreter.
*/
private final DcapProtocolInterpreter _interpreter;
/////////////////////////////////////////////////////////////////////
//
// the constructor
//
public DCapDoor( String name , StreamEngine engine , Args args )
throws Exception {
//
// the cell stuff
//
super( name , DCapDoor.class.getName(), args , false );
_nucleus = getNucleus() ;
try{
//
// all we need, to talk to the client.
//
_engine = engine;
_reader = engine.getReader();
_in = new BufferedReader( _reader );
_out = new PrintWriter(engine.getWriter(), true);
_subject = engine.getSubject();
_host = engine.getInetAddress().toString();
_interpreter = new DCapDoorInterpreterV3(this, _out, _subject, engine.getInetAddress());
addCommandListener(_interpreter);
}catch(Exception ee ){
start() ;
kill() ;
throw ee ;
}
//
// we have to use CellAdapapter.newThread instead of
// new Thread because we want to have the worker
// thread to be a member of the cell ThreadGroup.
//
_workerThread = _nucleus.newThread( this , "worker" );
_workerThread.start();
start() ;
}
@Override
public void keepAlive(){
if( _interpreter instanceof KeepAliveListener ) {
((KeepAliveListener) _interpreter).keepAlive();
}
}
//
// possible ways to bail out
//
// i) The connection is broken or closed by the
// client ( which is a protocolException because
// we expect a 'byebye' first.
// ii) The client sends a 'byebye' and closes the
// connection.
// iii) We are killed, which means, that all threads
// ( including the client communicationThread )
// will be interrupted.
//
@Override
public void run()
{
//
// check for lock
//
Transfer.initSession();
_log.info("Checking DCap lock");
try {
while (true) {
String lock = (String) _nucleus.getDomainContext().get("dcapLock");
if (lock == null) {
break;
}
TimeUnit.SECONDS.sleep(5);
}
} catch (InterruptedException iee) {
_log.info("Interrupted the 'dcap' lock");
_log.info("ComThread : Client communication Thread finished");
_stateChanged(__connectionLostEvent);
return;
}
_log.info("DCapLock released");
_dcapLock = false;
try {
while (true) {
if ((_lastCommand = _in.readLine()) == null) {
break;
}
if (_lastCommand.length() == 0) {
continue;
}
_commandCounter++;
_log.info("Executing command: " + _lastCommand);
VspArgs args;
try {
args = new VspArgs(_lastCommand);
} catch (IllegalArgumentException e) {
println("protocol violation: " + e.getMessage());
_log.debug("protocol violation [{}] from {}", e.getMessage(), _engine.getInetAddress());
break;
}
if (execute(args) > 0) {
println("0 0 server byebye");
_log.info("ComThread : protocol ended");
break;
}
}
} catch (IOException e) {
_log.warn("Got IO exception " + e + " from: " + _engine.getInetAddress());
} catch (Exception e) {
_log.warn("ComThread : got " + e, e);
} finally {
_out.close();
}
_log.info("ComThread : Client communication Thread finished");
_stateChanged(__connectionLostEvent);
}
private static final int __connectionLostEvent = 1 ;
private static final int __weWereKilledEvent = 2 ;
private static final int __abortCacheFinishedEvent = 3 ;
private static final int __NormalOperation = 1 ;
private static final int __AbortCacheProtOnBye = 2 ;
private static final int __WeAreFinished = 3 ;
private static final int __AbortCacheProtOnKill = 4 ;
private boolean _connectionLost;
private boolean _abortCacheFinished;
private int _state = __NormalOperation ;
private synchronized void _stateChanged( int event ){
_log.info( "_stateChanged : state = "+_state+" ; event = "+event ) ;
switch( _state ){
case __NormalOperation :
switch( event ){
case __connectionLostEvent :
//
// this is the usual case
//
_state = __AbortCacheProtOnBye ;
_stateChanged(__abortCacheFinishedEvent);
break ;
case __weWereKilledEvent :
println( "0 0 server shutdown" ) ;
_out.close();
_state = __AbortCacheProtOnKill ;
_stateChanged(__abortCacheFinishedEvent);
break ;
}
break ;
case __AbortCacheProtOnBye :
switch( event ){
case __abortCacheFinishedEvent :
_state = __WeAreFinished ;
kill() ;
break ;
}
break ;
case __AbortCacheProtOnKill :
//
// this state can only become WeAreFinished if
// two events have been accured.
// __abortCacheFinished and __connectionLost
//
switch( event ){
case __abortCacheFinishedEvent :
_abortCacheFinished = true ;
if( _connectionLost ) {
_state = __WeAreFinished;
}
break ;
case __connectionLostEvent :
_connectionLost = true ;
if( _abortCacheFinished ) {
_state = __WeAreFinished;
}
break ;
}
break ;
case __WeAreFinished :
switch( event ){
case __weWereKilledEvent :
_log.info( "Done" ) ;
break ;
}
break ;
}
_log.info( "_stateChanged : new state = "+_state ) ;
notifyAll() ;
}
private synchronized void waitForFinish( long timeout )
throws InterruptedException {
long end = System.currentTimeMillis() + timeout ;
while( _state != __WeAreFinished ){
long rest = end - System.currentTimeMillis() ;
_log.info( "waitForFinish : waiting for "+rest+" seconds" ) ;
if( rest <=0 ) {
break;
}
wait( rest ) ;
}
}
@Override
public void cleanUp(){
_log.info( "CleanUp : starting" );
_stateChanged( __weWereKilledEvent ) ;
try{
_log.info("CleanUp : waiting for final gate" ) ;
waitForFinish( 2000 ) ;
}catch(InterruptedException ie ){
_log.info("CleanUp : PANIC : interrupted (system left in an undefined state)" ) ;
}
if( _state != __WeAreFinished ) {
_log.info("CleanUp : PANIC : timeout (system left in an undefined state)");
}
_log.info( "CleanUp : finished" );
_interpreter.close();
_out.close();
try {
if (!_engine.getSocket().isClosed()) {
_log.info("Close socket");
_engine.getSocket().close();
}
} catch (IOException e) {
_log.warn("DcapDoor: got I/O exception closing socket:" + e.getMessage());
}
}
private synchronized void println( String str ){
_log.info( "toclient(println) : "+str ) ;
_out.println(str);
}
private int execute( VspArgs args ) throws Exception {
try{
String answer = _interpreter.execute(args);
if( answer != null ){
_log.info( "Our answer : "+answer ) ;
println( answer ) ;
}
}catch(CommandExitException e) {
return 1;
}
return 0 ;
}
///////////////////////////////////////////////////////////////
//
// the stuff which makes us a cell
//
@Override
public String toString(){
return Subjects.getDisplayName(_subject)+"@"+_host+( _dcapLock ? " (LOCKED)" : "" ) ;
}
@Override
public void getInfo( PrintWriter pw ){
pw.println( " DCapDoor" +( _dcapLock ? " (LOCKED)" : "" ));
pw.println( " User : "+Subjects.getDisplayName(_subject) );
pw.println( " Host : "+_host );
pw.println( " Last Command : "+_lastCommand );
pw.println( " Command Count : "+_commandCounter );
_interpreter.getInfo(pw);
}
@Override
public void messageArrived( CellMessage msg ){
_interpreter.messageArrived(msg);
}
}