summaryrefslogtreecommitdiff
path: root/java/netsvcs/Time/TSRequestAcceptor.java
blob: e02b0b261ba94a83e3c887aceca7eb1f661cba74 (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
/*************************************************
 *
 * = PACKAGE
 *    netsvcs.Time
 *
 * = FILENAME
 *    TSRequestAcceptor.java
 *
 *@author Prashant Jain, Everett Anderson
 *
 *************************************************/
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>SYNOPSIS</h2>
 *
 * <blockquote>Monitors a specified port (default 7990) and launches 
 * TSRequestHandlers when connections are made.  The handlers
 * report the average deviation from the local time when input
 * is received to their sockets.</blockquote>
 * 
 * <p><h2>DESCRIPTION</h2>
 * <blockquote>This is in place of the shared memory system used in C++ ACE.  
 * The clients need to request the correct time range from the Clerk, so
 * they can do it with <a href="ACE.SOCK_SAP.SOCKStream.html#_top_">
 * <tt>sockets</tt></a>.  An instance of TSRequestAcceptor is created
 * and initialized in TSClerkProcessor init().  This should be the
 * only place it's used.</blockquote>
 *
 * @see ACE.SOCK_SAP.SOCKAcceptor,ACE.netsvcs.Time.TSClerkProcessor
 */

public class TSRequestAcceptor extends Acceptor implements Runnable
{
  /**
   * Create an instance of TSRequestAcceptor.  Default constructor.
   */
  public TSRequestAcceptor (TSClerkProcessor parent) 
    {
      this.parent_ = parent;
    }

  /**
   *
   * Process command line arguments (port), and start this instance
   * in its own thread.
   *
   */
  public int init(String [] args)
    {
      this.parseArgs (args);

      new Thread (this).start();
      return 0;
    }

  /**
   *
   * Called when the thread starts.  Open the port and accept
   * connections.
   */
  public void run ()
    {
      try {
	this.open (this.port_);
	while (true)
	  this.accept();
      }
    catch (SocketException e)
      {
	ACE.ERROR (e);
      }
    catch (InstantiationException e)
      {
	ACE.ERROR (e);
      }
    catch (IllegalAccessException e)
      {
	ACE.ERROR (e);
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }

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

  /**
   *
   * Parse the command line.  This only looks for -p <port number>.
   *
   */
  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("Invalid argument: " + c);
	      break;
	    }
	}
    }

  /**
   *
   * Modifies to behavior of Acceptor accept() so the TSClerkProcessor
   * reference can be passed to the TSRequestHandler.
   *
   */

  protected SvcHandler makeSvcHandler ()
        throws InstantiationException, IllegalAccessException
  {
    return (SvcHandler) new TSRequestHandler(parent_);
  }

  // Port to monitor
  private int port_ = 7990;

  // Reference to the Clerk Processor (which holds the time value)
  private TSClerkProcessor parent_;
};