summaryrefslogtreecommitdiff
path: root/java/src/AcceptStrategy.java
blob: cf35c4fb46046f9aae8f693d4b232fac06a48dc0 (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
/*************************************************
 *
 * = PACKAGE
 *    ACE.Connection
 *
 * = FILENAME
 *    AcceptStrategy.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package ACE.Connection;

import java.io.*;
import java.net.*;
import ACE.SOCK_SAP.*;

/**
 * <hr>
 * <h2>SYNOPSIS</h2>
 *<blockquote>
 * Interface for specifying a passive connection
 * acceptance strategy for a 
 * <a href="ACE.Connection.SvcHandler.html"><tt>SvcHandler</tt></a>
 * .
 *</blockquote>
 *
 * <h2>DESCRIPTION</h2>
 *
 *<blockquote>
 * This class provides a strategy that manages passive
 * connection setup for an application, and can be extended
 * to define new strategies.
 *</blockquote>
 *
 * @see SvcHandler
 * @see Acceptor
 */

public class AcceptStrategy
{
  /**
   * Create an instance of Accept Strategy.
   *@param port port number where the server will listen for connections
   *@exception IOException couldn't open port
   */
  AcceptStrategy (int port) throws IOException
  {
    this.open (port);
  }

  /**
   * Initialize AcceptStrategy.
   *@param port port number where the server will listen for connections
   *@exception IOException couldn't open port
   */
  public void open (int port) throws IOException
  {
    // Create a new SOCK_Acceptor to accept client connections
    this.sockAcceptor_ = new SOCKAcceptor (port);
  }

  /**
   * Accept connections into the SvcHandler. Note that subclasses
   * should overwrite this method to provide a different accept
   * strategy.
   *@param sh Svc Handler in which to accept the connection
   *@exception SocketException
   *@exception IOException
   *@return 0
   */
  public int acceptSvcHandler (SvcHandler sh) throws
  SocketException, IOException
    {
      // Create a new stream
      SOCKStream sockStream = new SOCKStream ();
      
      // Block in accept. Returns when a connection shows up
      this.sockAcceptor_.accept (sockStream);
      
      // Set the streams for the new handler
      sh.setHandle (sockStream);
      return 0;
    }

  // Our connection acceptance factory
  private SOCKAcceptor sockAcceptor_;

}