summaryrefslogtreecommitdiff
path: root/java/src/SOCKAcceptor.java
blob: aaca725426a04c9a03cb9332356fa6a0e6b2d807 (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
/*************************************************
 *
 * = PACKAGE
 *    JACE.SOCK_SAP
 *
 * = FILENAME
 *    SOCKAcceptor.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package JACE.SOCK_SAP;

import java.io.*;
import java.net.*;
import JACE.OS.*;

/**
 * <hr>
 * <p><b>TITLE</b><br>
 *     Defines the format and interface for an ACE.SOCKAcceptor.
 */
public class SOCKAcceptor
{
  // = Initialization

  /**
   * Create a SOCKAcceptor. Do nothing constructor. Allows user to
   * call open() later and pass in the port number.
   */
  public SOCKAcceptor ()
    {
    }

  /**
   * Create a SOCKAcceptor.
   *@param port port number where the server will listen for connections
   */
  public SOCKAcceptor (int port) throws IOException
    {
      this.open (port);
    }

  /**
   * Create socket to listen for connections on.
   *@param port port number where the server will listen for connections
   */
  public void open(int port) throws IOException
    {
      // Close old socket (if there is one)
      this.close ();

      // Create a new server socket
      this.listenSocket_ = new ServerSocket (port);
      //      ACE.DEBUG ("Server listening on port " + port);
    }

  /**
   * Close the socket and do any necessary cleanup.
   */
  public void close () throws IOException
    {
      if (this.listenSocket_ != null)
	{
	  this.listenSocket_.close ();
	  this.listenSocket_ = null;
	}
    }

  /**
   * Accept a connection. The streams are set when the method returns.
   *@param sockStream SOCK Stream to use for the connection
   */
  public void accept (SOCKStream sockStream) throws SocketException, IOException
    {
      // Block in accept. Returns when a connection shows up and sets
      // the streams
      sockStream.socket (this.listenSocket_.accept ());
      ACE.DEBUG ("Accepted connection from " +
		 sockStream.socket ().getInetAddress ());
    }

  /**
   * Get the underlying listen socket.
   *@return the underlying listen socket
   */
  public ServerSocket listenSocket ()
  {
    return this.listenSocket_;
  }

  /**
   * Set the underlying listen socket.
   *@param s the underlying listen socket
   */
  public void listenSocket (ServerSocket s)
  {
    this.listenSocket_ = s;
  }

  /**
   * Clean up when the garbage collector gets run (if at all). Note
   * that there is no guarantee that finalize () will get called.
   */
  protected void finalize () throws Throwable
    {
      super.finalize ();
      this.close ();
    }

  // Socket on which listen for connections (by default initialized to
  // null)
  private ServerSocket listenSocket_;
}