summaryrefslogtreecommitdiff
path: root/java/JACE/netsvcs/Naming/NameAcceptor.java
blob: 7e7be457d70825a50fc33cc9e4a94b327158820a (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
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Naming
 *
 * = FILENAME
 *    NameAcceptor.java
 *
 *************************************************/
package JACE.netsvcs.Naming;

import java.io.*;
import java.net.*;
import java.util.*;
import JACE.OS.*;
import JACE.Misc.*;
import JACE.Connection.*;
import JACE.Reactor.*;
import JACE.ASX.TimeValue;
import JACE.netsvcs.Server;

/**
 * Server for the naming service.  
 * Listens on the specified port (command line option) and launches
 * NameHandlers when connections are made.  
 * <P>
 * The hash table for the mapping and a timer queue are created here.
 * Periodically, if it has been changed, the mapping is saved to a file.
 * If the data file exists at load time, it is read from disk.  Currently,
 * the service stores the entire mapping in one Hashtable (which is probably
 * kept in memory at all times).
 * <P>
 * <B>Valid command line arguments:</B>
 * <PRE>
 *    -f (file name)    File name of the database
 *                      (Default is namedata.dat)
 *    -p (port number)  Port to listen on for clients
 *    -d                Enable debugging
 *    -t (time sec)     How often to save the database (default 60 sec)
 *    -a (class name)   Specify ActivateStrategy
 *                      (Default is multi-threaded)
 * </PRE>
 *
 *@see NameHandler
 *
 *@author Everett Anderson
 *
 */
public class NameAcceptor extends Server
{
  /**
   * Constructor
   */
  public NameAcceptor ()
  {
    // Set the name in case we are not using the service
    // configurator
    name ("Naming Service");

    // Create the hash table and timer queue
    this.mapping_ = new Hashtable ();
    this.tq_ = new TimerQueue (true);
  }

  /**
   * Simple main program.  See the class description for more
   * information about command line arguments.
   */
  public static void main (String [] args)
  {
    // Simple main program to get things rolling
    NameAcceptor na = new NameAcceptor();

    na.init(args);
  }

  /**
   * Check to see if the mapping has been modified since the last
   * save.
   */
  synchronized boolean modifiedMapping ()
  {
    return mappingWasModified_;
  }

  /**
   * Set the modified state of the mapping.
   */
  synchronized void modifiedMapping (boolean value)
  {
    mappingWasModified_ = value;
  }

  /**
   * Cancels the timer which was used to save the mapping, then delegates
   * to Server.fini ().
   *
   *@return -1 on failure, 0 on success
   */
  public int fini ()
  {
    if (!done () && tq_ != null) 
      tq_.cancelTimer (this);

    return super.fini ();
  }

  /**
   * Read the data file (if it exists) and schedule a periodic timer
   * to save it at intervals.  At the end, this delegates to
   * Server.initialize () (which currently sets the default
   * activation scheme if it wasn't defined on the command line).
   *
   *@see Server#initialize
   *@return -1 on failure, 0 on success
   */
  protected int initialize ()
  {
    this.loadTable ();

    this.tq_.scheduleTimer (this,
			    null,
			    new TimeValue (this.updateInterval_),
			    new TimeValue (this.updateInterval_));

    // Use whatever default ActivateStrategy is defined in the
    // Server class (unless specified in parseArgs)
    return super.initialize ();
  }

  /**
   * Create a new NameHandler instance.
   */
  protected SvcHandler makeSvcHandler ()
  {
    return new NameHandler (mapping_);
  }

  /**
   * Prints out the valid command line arguments.  See the class
   * description for more information.  Called by Server.init when
   * parseArgs returns -1.
   */
  protected void printUsage ()
  {
    ACE.ERROR ("Valid options:\n");
    ACE.ERROR ("-f <file name>    File name of the database");
    ACE.ERROR ("                  (Default is namedata.dat)");
    ACE.ERROR ("-p <port number>  Port to listen on for clients");
    ACE.ERROR ("-d                Enable debugging");
    ACE.ERROR ("-t <time sec>     How often to save the database");
    ACE.ERROR ("                  (Default is 60 seconds)");
    ACE.ERROR ("-a <class name>   Specify ActivateStrategy");
    ACE.ERROR ("                  (Default is multi-threaded");
  }

  /**
   * Parses the command line arguments.  See the class description
   * for more information.
   *
   *@param args command line arguments
   *@return -1 on failure, 0 on success
   */
  protected int parseArgs (String [] args)
  {
    int c = 0;
    String s;
    GetOpt opt = new GetOpt (args, "p:f:t:da:", true);

    try {

      while ((c = opt.next ()) != -1) {
	switch (c)
	  {
	  case 'f':
	    this.filename_ = opt.optarg ();
	    break;
	  case 't':
	    try {
	      this.updateInterval_ = Integer.parseInt (opt.optarg ());
	    } catch (NumberFormatException e) {
	      ACE.ERROR ("Invalid interval specified: " + e.getMessage ());
	      return -1;
	    }
	    break;
	  case 'd':
	    ACE.enableDebugging ();
	    ACE.DEBUG ("Debugging is enabled");
	    break;
	  case 'p':
	    if (!port (opt.optarg ()))
	      return -1;
	    break;
	  case 'a':
	    Object strategy = newStrategyInstance (opt.optarg (),
						   "ActivateStrategy");
	    if (strategy == null)
	      return -1;
	    
	    activateStrategy ((ActivateStrategy) strategy);
	    break;	
	  default:
	    ACE.ERROR ("Unknown argument: " + (char)c);
	    return -1;
	  }
      }
    } catch (ArrayIndexOutOfBoundsException e) {
      ACE.ERROR ("Option -" + (char)c + " requires an argument");
      return -1;
    }
    
    return 0;
  }

  /**
   * Loads the hash table into memory from the specified
   * file.  Uses ObjectInputStream.
   */
  protected void loadTable ()
  {
    File file = new File(this.filename_);
    FileInputStream fis;
    ObjectInputStream ois;

    Hashtable ht = null;

    try {

      if ((file.exists()) && (file.canRead())) {

	fis = new FileInputStream (file);

	ois = new ObjectInputStream(fis);

	ht = (Hashtable)ois.readObject();
      } else 
	return;
    } catch (ClassNotFoundException e) {
      ACE.ERROR(e);
    } catch (StreamCorruptedException e) {
      ACE.ERROR(e);
    } catch (SecurityException e) {
      ACE.ERROR(e);
    } catch (IOException e) {
      ACE.ERROR(e);
    }

    if (ht != null) 
      this.mapping_ = ht;

  }

  /**
   * Writes the table out to the specified file if it has been modified.
   */
  protected void saveTable ()
  {
    if (!modifiedMapping ())
      return;

    FileOutputStream fos;
    ObjectOutputStream oos;

    try {

      fos = new FileOutputStream(this.filename_);
      oos = new ObjectOutputStream(fos);

      synchronized (this.mapping_) {
	oos.writeObject(this.mapping_);

	modifiedMapping (false);
      }

      oos.flush();

      oos.close();

    } catch (OptionalDataException e) {
      ACE.ERROR(e);
    } catch (NotSerializableException e) {
      ACE.ERROR(e);
    } catch (IOException e) {
      ACE.ERROR(e);
    }
  }

  /**
   * Call back for the TimerQueue.  This calls the method to save the
   * hash table.  The default time out is 60 seconds.
   */
  public int handleTimeout (TimeValue tv, Object obj)
  {
    this.saveTable();

    return 0;
  }

  // Mapping data structure
  Hashtable mapping_ = null;

  // Default file name
  String filename_ = "namedata.dat";

  // How often to save the table (seconds)
  int updateInterval_ = 60;

  // Calls handleTimeout at updateInterval_ intervals
  TimerQueue tq_ = null;

  boolean mappingWasModified_ = false;
}