View Javadoc

1   /*
2      Copyright 2007 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package fulmine.distribution.connection.tcp;
17  
18  import static fulmine.distribution.connection.tcp.ProtocolMessageConstants.DELIMITER;
19  import fulmine.context.IFrameworkContext;
20  import fulmine.distribution.connection.AbstractConnectionDiscoverer;
21  import fulmine.distribution.connection.IConnectionBroker;
22  import fulmine.distribution.connection.IConnectionDiscoverer;
23  import fulmine.distribution.connection.IConnectionParameters;
24  import fulmine.util.log.AsyncLog;
25  
26  /**
27   * A discoverer for remote contexts that use TCP/IP.
28   * 
29   * @author Ramon Servadei
30   */
31  public final class TcpConnectionDiscoverer extends AbstractConnectionDiscoverer
32      implements IConnectionDiscoverer
33  {
34      final static AsyncLog LOG = new AsyncLog(TcpConnectionDiscoverer.class);
35  
36      /** The host address for this context */
37      private final String address;
38  
39      /** The TCP port for this context */
40      private final int port;
41  
42      /**
43       * Constructs the discoverer with specific UDP binding parameters and
44       * extracts the TCP connection details from the broker
45       * 
46       * @param context
47       *            the local context
48       * @param hostAddress
49       *            the host for the TCP connection the context exposes
50       * @param tcpPort
51       *            the TCP port for the connection the context exposes
52       * @param udpNetwork
53       *            the UDP network for discovery operations
54       * @param udpPort
55       *            the UDP port for discovery operations
56       * @param udpNic
57       *            the network interface card name to bind to, <code>null</code>
58       *            for default
59       */
60      public TcpConnectionDiscoverer(IFrameworkContext context,
61          String hostAddress, int tcpPort, String udpNetwork, int udpPort,
62          String udpNic)
63      {
64          super(context, udpNetwork, udpPort, udpNic);
65          this.address = hostAddress;
66          this.port = tcpPort;
67      }
68  
69      /**
70       * Standard constructor using parameters for the TCP address and port.
71       * 
72       * @param context
73       *            the local context
74       * @param address
75       *            the IP address of the local {@link IConnectionBroker}
76       * @param tcpPort
77       *            the TCP port of the local {@link IConnectionBroker}
78       */
79      TcpConnectionDiscoverer(IFrameworkContext context, String address,
80          int tcpPort)
81      {
82          super(context);
83          this.address = address;
84          this.port = tcpPort;
85      }
86  
87      @Override
88      protected AsyncLog getLog()
89      {
90          return LOG;
91      }
92  
93      @Override
94      protected final void doStart()
95      {
96          super.doStart();
97      }
98  
99      @Override
100     protected IConnectionParameters getConnectionParameters(String data)
101     {
102         final String[] values = data.split(DELIMITER);
103         if (values.length == 5)
104         {
105             final String identity = values[1];
106             final int hashCode = Integer.parseInt(values[2]);
107             final String inetAddr = values[3];
108             final int port = Integer.parseInt(values[4]);
109             return new TcpConnectionParameters(identity, hashCode, inetAddr,
110                 port);
111         }
112         if (getLog().isDebugEnabled())
113         {
114             getLog().debug("Could not decipher '" + data + "'");
115         }
116         return null;
117     }
118 
119     @Override
120     protected String getProtocolConnectionParameters()
121     {
122         return this.address + DELIMITER + this.port;
123     }
124 
125 }