summaryrefslogtreecommitdiff
path: root/java/netsvcs/Naming/NameHandler.java
blob: a619eab0733829a2d29157926b3f69b373c012d8 (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Naming
 *
 * = FILENAME
 *    NameHandler.java
 *
 * An instance of this class is created in a separate thread for each connection 
 * request received by the NameAcceptor.  All interaction between the
 * client's requests and the database are handled here.
 *
 * In general, the user binds a name to a (value, type) pair.  The type is just
 * treated as just another String (in the C++ version the name and value are
 * arrays of 16 bit data types and the type is an array of 8 bit chars).
 *
 * For this to work in the hash table scheme, the type and value are wrapped in
 * a ValueType class defined at the end of this file.
 *
 * This is compatible with the C++ ACE remote name service. 
 *
 *@see netsvcs.Naming.NameAcceptor
 *@see netsvcs.Naming.NameRequest
 *@see netsvcs.Naming.NameReply
 *
 *@author Everett Anderson
 *
 *************************************************/
package netsvcs.Naming;

import java.io.*;
import java.util.*;
import JACE.OS.*;
import JACE.Connection.*;
import JACE.Reactor.*;
import JACE.SOCK_SAP.*;

public class NameHandler extends SvcHandler
{
  /**
   * Constructor
   *
   * @param mapping     Hash table created in NameAcceptor
   */
  public NameHandler (Hashtable mapping)
  {
    super();

    this.mapping_ = mapping;
  }
  
  /**
   * Starts this handler in its own thread
   *
   */
  public int open (Object obj)
  {
    new Thread (this).start ();
    return 0;
  }

  /**
   * Main loop that this thread executes.  Waits for connection requests and
   * creates a NameHandler thread for each.
   *
   */
  public void run ()
  {
    ACE.DEBUG("NameHandler instance running");
    
    // Can't assume the SOCKStream uses DataInputStream, so put one
    // over its OutputStream 
    DataInputStream dis = new DataInputStream (this.peer().inputStream());

    // The NameRequest is the how all requests come in to the naming service.
    NameRequest nameRequest = new NameRequest();

    // Main loop -- wait for requests
    int msgLen;
    try
      {
	while (!this.done_)
	  {
	    // Read a NameRequest from the stream
	    nameRequest.streamInFrom(dis);
	    
	    // Decide what to do based on the request type
	    this.dispatch(nameRequest);

	  }
      }
    catch (NullPointerException e)
      {
	ACE.ERROR ("Connection reset by peer");
      }
    catch (EOFException e)
      {
	/* The client has shut down the connection */
	
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }
    finally
      {
	try 
	  {
	    this.peer ().close ();
	  }
	catch (IOException e)
	  {
	  }
      }
  }


  /**
   *
   * This is the point at which a request is sent to the various methods
   * that fulfill it.  Switches on the request type -- bind, rebind, resolve,
   * etc.
   *
   *@param nameRequest     The request to fill
   */
 void dispatch(NameRequest nameRequest) throws IOException
    {

      // Call the various other member functions based on the
      // message type of the request -- bind, rebind, etc.
      switch (nameRequest.requestType()) 
	{
	case NameRequest.BIND:
	  this.bind(nameRequest, false);
	  break;
	case NameRequest.REBIND:
	  this.bind(nameRequest, true);
	  break;
	case NameRequest.RESOLVE:
	  this.resolve(nameRequest);
	  break;
	case NameRequest.UNBIND:
	  this.unbind(nameRequest);
	  break;
	case NameRequest.LIST_NAMES:
	  this.listByName(nameRequest.name(), false);
	  break;
	case NameRequest.LIST_VALUES:
	  this.listByValue(nameRequest.name(), false);
	  break;
	case NameRequest.LIST_TYPES:
	  this.listByType(nameRequest.name(), false);
	  break;
	case NameRequest.LIST_NAME_ENTRIES:
	  this.listByName(nameRequest.name(), true);
	  break;
	case NameRequest.LIST_VALUE_ENTRIES:
	  this.listByValue(nameRequest.name(), true);
	  break;
	case NameRequest.LIST_TYPE_ENTRIES:
	  this.listByType(nameRequest.name(), true);
	  break;
	default:
	  System.err.println("unknown type");

	  ACE.ERROR("Unknown type: " + nameRequest.requestType());

	  // Send a failure message.  This will only work if the other
	  // side is expecting something like a NameReply rather than
	  // a NameRequest.  It would've been better to have everything
	  // use NameRequests to avoid this kind of thing.
	  NameReply reply = new NameReply(NameReply.FAILURE, 0);
	  reply.streamOutTo(this.peer());

	  break;
	}

    }

  /**
   * 
   * Bind a name and a (value, type) pair.  All this data is given in the
   * NameRequest from the client.  Returns a NameReply back to the client
   * with either Reply.SUCCESS or Reply.FAILURE as the type.
   *
   *@param request        NameRequest given by the client
   *@param rebind         Is this a rebind or not?
   */
  void bind (NameRequest request, boolean rebind) throws IOException
    {
      // The hash table entries consists of (String name, ValueType data) pairs, so
      // create the appropriate ValueType
      ValueType vt = new ValueType(request.type(),
				   request.value());

      // Reply to tell sender of success or failure
      NameReply reply = new NameReply();

      // If it's a rebind request, overwrite the old entry.  If the key doesn't
      // exist, add it.  If it does exist and it's not a bind request, return
      // a failure code via a NameReply.
      if ((rebind) || (!this.mapping_.containsKey(request.name()))) {

	System.err.println("Binding: " + request.name() + " and " + vt.value_);

	// Add/Update the entry in the hash table
	this.mapping_.put(request.name(), vt);

	// Set the reply code to success
	reply.type(NameReply.SUCCESS);

      } else {

	ACE.DEBUG("Key " + request.name() + " already exists");

	// Set reply code to failure
	reply.type(NameReply.FAILURE);

	// reply error code unused as far as I know
      }
	
      reply.streamOutTo(this.peer());
    }

  /**
   * Given a name, this looks up and returns the type and value.  This is
   * done by sending back a full NameRequest with the correct info.  If
   * there is a problem, an "empty" NameRequest is returned -- it has no
   * name, type, or value fields.
   *
   *@param request     NameRequest sent by the client (has the name to lookup)
   */
  void resolve (NameRequest request) throws IOException
    {
      // A NameRequest is also used in response
      NameRequest result;

      // Wrap a DataOutputStream around the socket's output stream
      // (the socket should already have at least a BufferedOutputStream)
      DataOutputStream dos = new DataOutputStream(this.peer().outputStream());

      // If the requested name is in the hash table, return the data
      if (this.mapping_.containsKey(request.name())) {

	// Get the data pair based on the name
	ValueType vt = (ValueType)this.mapping_.get(request.name());

	ACE.DEBUG("Good resolve: " + vt.value_);

	// Fill the reply structure 
	result = new NameRequest(NameRequest.RESOLVE,
				 null,
				 vt.value_,
				 vt.type_,
				 null);

      } else {

	// Otherwise return a null response 
	result = new NameRequest(NameRequest.RESOLVE,
			      null,
			      null,
			      null,
			      null);

      }

      // Send the result to the socket
      //      result.streamOutTo(dos);

      result.streamOutTo(this.peer());

    }

  /**
   * 
   * Given a name, remove its entry in the mapping.  Returns a NameReply
   * to the client with NameReply.SUCCESS or NameReply.FAILURE.
   *
   *@param request    NameRequest from the client (has the name to remove)
   */
  void unbind (NameRequest request) throws IOException
    {
      NameReply reply = new NameReply();

      // If the given key isn't in the table, return an error
      // Otherwise remove it.  Uses a NameReply to respond.
      if (!this.mapping_.containsKey(request.name())) 
	reply.type(NameReply.FAILURE);
      else {
	this.mapping_.remove(request.name());
	reply.type(NameReply.SUCCESS);
      }

      // Send the reply out to the socket
      reply.streamOutTo(this.peer());
    }

  /**
   *
   * Given a pattern string (given in NameRequest's name field), this 
   * finds all the entries in the mapping which have a name that begins with
   * the string.  Each one is sent back separately via a NameRequest, and this
   * sequence is followed by a blank NameRequest.
   *
   *@param pattern           Pattern to find (what result names should begin with)
   *@param completeLookup    Should the value and type be returned as well?
   */
  void listByName (String pattern, boolean completeLookup) throws IOException
    {
      // Get a listing of all the keys in the hash table
      Enumeration enum = this.mapping_.keys();

      // References used in the loop
      String name;
      ValueType vt;

      // A NameRequest is used to return each item corresponding to the pattern.
      NameRequest result = new NameRequest((completeLookup ? NameRequest.LIST_NAMES :
					                  NameRequest.LIST_NAME_ENTRIES),
					null,
					null,
					null,
					null);

      // Keep ourselves safe from null pointer exceptions
      if (pattern == null)
	pattern = new String("");
      
      // Scan through all the elements
      while (enum.hasMoreElements()) {

	// Get a key
	name = (String)enum.nextElement();

	// Does it fit the pattern?
	if (name.startsWith(pattern)) {

	  // Set the result name
	  result.name(name);

	  // Only make another hash table request if the user
	  // wants all the data
	  if (completeLookup) {

	    // Get data from the hash table
	    vt = (ValueType)mapping_.get(name);

	    // Set the rest of the data
	    result.type(vt.type_);
	    result.value(vt.value_);
	  }
	  
	  // Send it to the socket
	  result.streamOutTo(this.peer());
	}
      }

      // Send final null message
      result.name(null);
      result.type(null);
      result.value(null);
      result.requestType(NameRequest.MAX_ENUM);
      result.streamOutTo(this.peer());
    }

  /**
   *
   * Given a pattern string (given in NameRequest's name field), this 
   * finds all the entries in the mapping which have a type that begins with
   * the string.  Each one is sent back separately via a NameRequest, and this
   * sequence is followed by a blank NameRequest.
   *
   *@param pattern           Pattern to find (what result types should begin with)
   *@param completeLookup    Should the value be returned as well?  This is only
   *                         used to decide between LIST_TYPES and LIST_TYPE_ENTRIES
   *                         since we might as well send back both if we look them up
   *                         together.
   */
  void listByType (String pattern, boolean completeLookup) throws IOException
    {
      // Get a listing of all the keys in the hash table
      Enumeration enum = this.mapping_.keys();

      // References used in the loop
      String name;
      ValueType vt;

      // A NameRequest is used to return each item corresponding to the pattern.
      NameRequest result = new NameRequest((completeLookup ? NameRequest.LIST_TYPES :
					                  NameRequest.LIST_TYPE_ENTRIES),
					null,
					null,
					null,
					null);
      // Keep ourselves safe from null pointer exceptions
      if (pattern == null)
	pattern = new String("");

      // Scan through all the elements
      while (enum.hasMoreElements()) {

	// Get a key
	name = (String)enum.nextElement();

	// Have to get all the data for this entry to compare
	vt = (ValueType)mapping_.get(name);

	// Does it fit the pattern?
	if (vt.type_ != null)
	  if (vt.type_.startsWith(pattern)) {

	    // Set the result values
	    result.name(name);
	    result.type(vt.type_);
	    result.value(vt.value_);
      
	    // Send it out to the socket
	    result.streamOutTo(this.peer());
	  }
      }

      // Send final null message
      result.name(null);
      result.type(null);
      result.value(null);
      result.requestType(NameRequest.MAX_ENUM);
      result.streamOutTo(this.peer());
    }
  /**
   *
   * Given a pattern string (given in NameRequest's name field), this 
   * finds all the entries in the mapping which have a value that begins with
   * the string.  Each one is sent back separately via a NameRequest, and this
   * sequence is followed by a blank NameRequest.
   *
   *@param pattern           Pattern to find (what result values should begin with)
   *@param completeLookup    Should the type be returned as well?  This is only
   *                         used to decide between LIST_VALUES and LIST_VALUE_ENTRIES
   *                         since we might as well send back both if we look them up
   *                         together.
   */

  void listByValue (String pattern, boolean completeLookup) throws IOException
    {
      // Get a listing of all the keys in the hash table
      Enumeration enum = this.mapping_.keys();

      // References used in the loop
      String name;
      ValueType vt;

      // A NameRequest is used to return each item corresponding to the pattern.
      NameRequest result = new NameRequest((completeLookup ? NameRequest.LIST_VALUES :
					                  NameRequest.LIST_VALUE_ENTRIES),
					null,
					null,
					null,
					null);
      // Keep ourselves safe from null pointer exceptions
      if (pattern == null)
	pattern = new String("");

      // Scan through all the elements
      while (enum.hasMoreElements()) {

	// Get a key
	name = (String)enum.nextElement();

	// Have to get all the data for this entry to compare
	vt = (ValueType)mapping_.get(name);

	// Does it fit the pattern?
	if (vt.value_ != null)
	  if (vt.value_.startsWith(pattern)) {

	    // Set the result values
	    result.name(name);
	    result.type(vt.type_);
	    result.value(vt.value_);
      
	    // Send it out to the socket
	    result.streamOutTo(this.peer());
	  }
      }

      // Send final null message
      result.name(null);
      result.type(null);
      result.value(null);
      result.requestType(NameRequest.MAX_ENUM);
      result.streamOutTo(this.peer());
    }

  boolean done_ = false;
 

  // References to the hash table and the timer queue
  Hashtable mapping_;
}


/**
 * A simple wrapper to keep the type and value together in
 * the hash table.
 */
class ValueType implements Serializable
{
  /**
   * Constructor
   *
   *@param type   Type string to include
   *@param value  Value string to include
   */
  ValueType(String type, String value)
    { this.type_ = type; this.value_ = value; }

  public String type_;
  public String value_;
}