summaryrefslogtreecommitdiff
path: root/java/netsvcs/Time/TSServerAcceptor.java
blob: 2d54f7b740ba5b09cd8eaa4d01e108e317834b8c (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
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Time
 *
 * = FILENAME
 *    TS_Server_Acceptor.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package netsvcs.Time;

import java.io.*;
import java.net.*;
import java.util.*;
import JACE.OS.*;
import JACE.Misc.*;
import JACE.Connection.*;
import JACE.Reactor.*;

/**
 * <hr>
 * <p><h2>DESCRIPTION</h2>
 *
 * Acceptor: listens to a port and launches TSServerHandlers
 * when connections are made.
 *
 * @see netsvcs.Time.TSServerHandler, JACE.Connection.Acceptor
 */
public class TSServerAcceptor extends Acceptor implements Runnable
{
  // Run this in its own thread
  public int init (String [] args)
    {
      // Parse arguments
      this.parseArgs (args);

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

  // Create a TSServerHandler for each client that wants to connect
  public void run ()
  {
    try
      {
	this.setHandlerFactory (Class.forName ("netsvcs.Time.TSServerHandler"));
	this.open (this.port_);
	while (true)
	  this.accept ();
      }
    catch (ClassNotFoundException e)
      {
	ACE.ERROR (e);
      }
    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);
      }

    System.err.println("Stopped accepting");
  }

  // Process the command line
  protected void parseArgs (String args[])
  {
    String s;
    GetOpt opt = new GetOpt (args, "p:");
    for (int c; (c = opt.next ()) != -1; )
      {
	switch (c)
	  {
	  case 'p':
	    s = opt.optarg ();
	    this.port_ = (new Integer (s)).intValue ();
	    break;
	  default:
	    ACE.ERROR ("Unknown argument: " + c);
	    break;
	  }
      }
  }

  private int port_ = 7989;
}