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

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

public class StrategyAcceptor
{
  /**
   * Create an instance of StrategyAcceptor.
   *@param handlerFactory Svc Handler factory that is used to create
   * an instance of a Svc Handler when a connection is accepted.
   */
  public StrategyAcceptor (Class handlerFactory)
    {
      this (handlerFactory, null, null, null);
    }

  /**
   * Create an instance of StrategyAcceptor. Use the creation
   * strategy and the handlerFactory passed in to creae a new instance
   * of the Svc Handler.
   *@param handlerFactory Svc Handler factory that is used to create
   * an instance of a Svc Handler when a connection is accepted.
   *@param creStrategy Creation strategy to use to create a new
   * instance of the Svc Handler.
   *@param acceptStrategy Accept strategy to use to accept a new
   * connection into the Svc Handler.
   *@param activateStrategy Activate strategy to use to activate the
   * instance of the Svc Handler.
   */
  public StrategyAcceptor (Class handlerFactory,
			    CreationStrategy creStrategy,
			    AcceptStrategy acceptStrategy,
			    ActivateStrategy activateStrategy)
    {
      // Cache everything
      this.handlerFactory_ = handlerFactory;
      this.creStrategy_ = creStrategy;
      this.acceptStrategy_ = acceptStrategy;
      this.activateStrategy_ = activateStrategy;
    }

  /**
   * Initialize the Strategy Acceptor. The method creates the
   * appropriate strategies as needed.
   *@param port port number where the server will listen for connections
   *@exception IOException Socket level error
   */
  public void open (int port) throws IOException
    {
      if (this.creStrategy_ == null)
	this.creStrategy_ = new CreationStrategy (this.handlerFactory_);
      if (this.acceptStrategy_ == null)
	this.acceptStrategy_ = new AcceptStrategy (port);
      else
	this.acceptStrategy_.open (port);
      if (this.activateStrategy_ == null)
	this.activateStrategy_ = new ActivateStrategy ();
    }

  /**
   * Accept a connection using the appropriate strategies.
   *
   *@exception SocketException Socket level error
   *@exception InstantiationException Problem creating a handler
   *@exception IllegalAccessException No strategy available
   *@exception IOException Socket level error
   */
  public void accept () throws SocketException, 
      InstantiationException, 
      IllegalAccessException,
      IOException
  {
    // Create a Svc_Handler using the appropriate Creation_Strategy
    SvcHandler sh = this.makeSvcHandler ();

    // Accept a connection into the Svc_Handler
    this.acceptSvcHandler (sh);

    // Activate the Svc_Handler
    this.activateSvcHandler (sh);
  }

  /**
   * Bridge method for creating a SvcHandler.  The strategy for
   * creating a SvcHandler is configured into the Acceptor via it's
   * creStrategy_.  If no strategy is passed in, the default behavior
   * of this method is to use the default CreationStrategy.
   *@return a new instance of the Svc Handler
   *@exception InstantiationException Couldn't create SvcHandler
   *@exception IllegalAccessException No strategy available
   */
  protected SvcHandler makeSvcHandler () throws InstantiationException, IllegalAccessException
    {
      // Create a new handler for the connection
      return this.creStrategy_.makeSvcHandler ();
    }


  /**
   * Bridge method for accepting the new connection into the
   * <SvcHandler>.  The strategy for accepting into a SvcHandler is
   * configured into the Acceptor via it's acceptStrategy_.  If no
   * strategy is passed in, the default behavior of this method is to
   * use the default AcceptStrategy. 
   *@param sh Svc Handler in which to accept the connection
   *@return result of accepting a connection using the accept strategy
   *@exception SocketException Socket level error
   *@exception IOException Socket level error
   */
  protected int acceptSvcHandler (SvcHandler sh) throws SocketException, IOException
  {
    // Delegate responsibility to the appropriate strategy
    return this.acceptStrategy_.acceptSvcHandler (sh);
  }
  
  /**
   * Bridge method for activating a <SvcHandler>.  The strategy for
   * activating a SvcHandler is configured into the Acceptor via it's
   * activateStrategy_. If no strategy is passed in, the default
   * behavior of this method is to use the default ActivateStrategy.
   *@param sh Svc Handler to activate
   *@return result of activating the Svc Handler
   */
  protected int activateSvcHandler (SvcHandler sh)
    {
      // Delegate responsibility to the appropriate strategy
      return this.activateStrategy_.activateSvcHandler (sh);
    }

  // Handler class that should be instantiated when a connection is
  // made with a client
  private Class handlerFactory_;

  // Creation Strategy
  private CreationStrategy creStrategy_;

  // Accept Strategy
  private AcceptStrategy acceptStrategy_;

  // Activation Strategy
  private ActivateStrategy activateStrategy_;
}