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

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

/**
 * <hr>
 * <h2>SYNOPSIS</h2>
 *<blockquote>
 * Abstract factory for connecting a
 * (<a href="ACE.Connection.SvcHandler.html"><tt>SvcHandler</tt></a>),
 * to an application.
 *</blockquote>
 *
 * <h2>DESCRIPTION</h2>
 *<blockquote>
 * Implements the basic strategy for actively establishing connections
 * with applications.  The <tt>Connector</tt> establishes the connection,
 * passing it on to a <tt>SvcHandler</tt> instance, and handing over
 * control to that instance.
 *<p>
 * TCP is the transport mechanism used, via
 * <a href="ACE.SOCK_SAP.SOCKConnector.html#_top_"><tt>SOCKConnector</tt></a>.
 *</blockquote>
 *
 *<h2>NOTES</h2>
 *<blockquote>
 * This class, as currently implemented, does not work like its C++ counterpart.
 * Future versions are expected to rectify this discrepancy.
 *</blockquote>
 *
 *@see SOCKConnector,SvcHandler
 */
public class Connector extends ServiceObject
{
  /**
   * Create a Connector. Do nothing constructor. Allows user to
   * call <a href="#open(java.lang.String)">open</a>() later.
   */
  public Connector ()
    {
    }

  /**
   * Create a Connector passing in server hostname and port
   * number, effectively shorthand for calling
   * <a href="#open(java.lang.String)">open</a>().
   *@param hostname server hostname
   *@param port server port number
   */
  public Connector (String hostname, int port)
    {
      this.open (hostname, port);
    }

  /**
   * Initialize the Connector passing in server hostname and port
   * number.  Note that no connection attempt is made.
   *@param hostname server hostname
   *@param port server port number
   */
  public void open (String hostname, int port)
    {
      this.hostname_ = hostname;
      this.port_ = port;
    }

  /**
   * Connect to the server.
   *@param sh Svc Handler to use to handle the connection
   */
  public void connect (SvcHandler sh) throws UnknownHostException, 
      SocketException,
      InstantiationException, 
      IllegalAccessException, 
      IOException
  {
    // Make a connection using the appropriate Connection_Strategy
    this.connectSvcHandler (sh);

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

  /**
   * Bridge method for making a new connection. The default behavior
   * creates a new SOCKConnector and then calls setHandle() on the
   * <SvcHandler> that was passed in. Subclasses can override this
   * strategy, if needed.
   *@param sh Svc Handler to use to handle the connection
   *@return 0
   */
  protected int connectSvcHandler (SvcHandler sh) throws
  SocketException, IOException
  {
    // Create a new stream
    SOCKStream sockStream = new SOCKStream ();

    // Create a SOCK_Connector (note the constructor does the connect for us)
    this.sockConnector_ = new SOCKConnector (sockStream,
					       this.hostname_,
					       this.port_);
    ACE.DEBUG ("Connected to " +
	       sockStream.socket ().getInetAddress ());
    
    // Set the streams for the new handler
    sh.setHandle (sockStream);
    return 0;
  }
  
  /**
   * Bridge method for activating a <SvcHandler>.  The default
   * behavior of this method is to activate the <SvcHandler> by
   * calling its open() method (which allows the SVC_HANDLER to define
   * its own concurrency strategy).  However, subclasses can override
   * this strategy to do more sophisticated concurrency activations.
   *@param sh Svc Handler to activate
   *@return 0
   */
  protected int activateSvcHandler (SvcHandler sh)
    {
      sh.open (null);
      return 0;
    }

  
  // Port server is listening on
  private int port_;

  // Server hostname
  private String hostname_;

  // Our connection factory
  private SOCKConnector sockConnector_;
}