summaryrefslogtreecommitdiff
path: root/java/netsvcs/Naming/NameAcceptor.java
blob: f704504ee3ca917c6d8b01a714f6130f08a1688f (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
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Naming
 *
 * = FILENAME
 *    NameAcceptor.java
 *
 * Listens on the specified port (command line option) and launches
 * NameHandlers when connections are made.  Each NameHandler runs in
 * its own thread.
 *
 * The hash table for the mapping and a timer queue are created here.
 * Periodically the mapping is written out to a file.
 *
 * A small main program is included to start things off.  If the
 * data file exists, it is read into memory.  Currently the service
 * stores the entire mapping in memory at all times.  The mapping is
 * dumped to a file at regular intervals.
 *
 *@see netsvcs.Naming.NameHandler
 *
 *@author Everett Anderson
 *
 *************************************************/
package 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;

public class NameAcceptor extends Acceptor implements Runnable
{
  /**
   * Constructor
   */
  public NameAcceptor ()
    {
      super();

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

  /**
   * Simple main program.  Command line options are
   * described under parseArgs.
   */
  public static void main (String [] args)
    {
      // Simple main program to get things rolling
      NameAcceptor na = new NameAcceptor();

      na.init(args);
    }


  /**
   * Close the socket when shutting down
   */
  public int fini ()
  {
    try 
      {
	this.done_ = true;
	this.sockAcceptor_.close();
      } 
    catch (IOException e) 
      {
	ACE.ERROR("" + e);
	return -1;
      }

    return 0;
  }

  /**
   * Stops accepting when suspended 
   */
  public int suspend()
  {
    this.suspended_ = true;
    return 0;
  }

  /**
   * Resumes accepting 
   */
  public int resume()
  {
    this.suspended_ = false;
    return 0;
  }


  /**
   * Runs this instance in its own thread
   */
  public int init (String [] args)
    {
      // Parse arguments
      this.parseArgs (args);

      System.out.println("Starting naming service on port: " + this.port_);

      // Run in own thread of control so that we don't block the caller
      new Thread (this).start();

      return 0;
    }

  /**
   * 
   * Main loop:  launches NameHandlers in separate threads whenever a
   * connection request is made
   */
  public void run ()
  {
    // Load the hash table from disk
    this.loadTable();

    // Schedule to write out the memory copy of the hash table at regular
    // intervals
    this.tq_.scheduleTimer(this, 
			   null, 
			   new TimeValue(this.updateInterval_), 
			   new TimeValue(this.updateInterval_));

    try
      {
	// Create new NameHandlers as requests come in
	this.open (this.port_);
	while (!this.done_) {

	  if (!this.suspended_)
	    this.accept ();
	}
      }
    catch (SocketException e)
      {
	ACE.ERROR ("Socket Exception: " + e);
      }
    catch (InstantiationException e)
      {
	ACE.ERROR (e);
      }
    catch (IllegalAccessException e)
      {
	ACE.ERROR (e);
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }
  }

  /**
   * Create a new NameHandler
   */
  protected SvcHandler makeSvcHandler ()
    throws InstantiationException, IllegalAccessException
    {
      return new netsvcs.Naming.NameHandler (this.mapping_);
    }

  /** 
   * Process the command line.  The following options are available:
   *
   * -p <port>       Port number for listening
   * -f <filename>   Name of the database file
   * -t <time>       Mapping write-out time interval (in seconds)
   *
   */
  protected void parseArgs (String args[])
  {
    String s;
    GetOpt opt = new GetOpt (args, "p:f:t:");
    for (int c; (c = opt.next ()) != -1; )
      {
	switch (c)
	  {
	    // Specify port
	  case 'p':
	    s = opt.optarg ();
	    this.port_ = (new Integer (s)).intValue ();
	    break;
	    // Specify file name of the database
	  case 'f':
	    s = opt.optarg ();
	    this.filename_ = new String(s);
	    break;
	    // Specify time interval to write out the table
	  case 't':
	    s = opt.optarg ();
	    this.updateInterval_ = (new Integer (s)).intValue();
	    break;
	  default:
	    ACE.ERROR ("Unknown argument: " + c);
	    break;
	  }
      }
  }

  /**
   * 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.
   */
  protected void saveTable ()
    {
      FileOutputStream fos;
      ObjectOutputStream oos;

      try {

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

	oos.writeObject(this.mapping_);

	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;
    }

  // Port to listen on
  private int port_ = ACE.DEFAULT_SERVER_PORT;

  // 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 done_ = false;
  boolean suspended_ = false;

}