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.util.Utils.COLON;
19  import static fulmine.util.Utils.string;
20  import fulmine.AbstractLifeCycle;
21  import fulmine.Domain;
22  import fulmine.IDomain;
23  import fulmine.ILifeCycle;
24  import fulmine.IType;
25  import fulmine.Type;
26  import fulmine.context.IFrameworkContext;
27  import fulmine.distribution.connection.IConnectionBroker;
28  import fulmine.distribution.connection.IConnectionParameters;
29  import fulmine.util.reference.is;
30  
31  /**
32   * Encapsulates the parameters for the TCP/IP server socket of a remote
33   * {@link IFrameworkContext} to connect to.
34   * <p>
35   * A note on the hierarchy; this only extends {@link AbstractLifeCycle} because
36   * the {@link TcpConnection} sub class requires the {@link ILifeCycle} method
37   * implementations provided by the {@link AbstractLifeCycle}. This is a conceded
38   * design choice.
39   * 
40   * @author Ramon Servadei
41   */
42  public class TcpConnectionParameters extends AbstractLifeCycle implements
43      IConnectionParameters
44  {
45      /**
46       * The IP remoteHostAddress of the host of the remote
47       * {@link IConnectionBroker}
48       */
49      private final String remoteHostAddress;
50  
51      /** The TCP remoteHostTcpPort of the remote {@link IConnectionBroker} */
52      private final int remoteHostTcpPort;
53  
54      /**
55       * The identity of the remote {@link IConnectionBroker} this connection is
56       * for
57       */
58      private String identity;
59  
60      /** The hashcode of the connection */
61      private int connectionHashCode;
62  
63      /**
64       * Standard constructor for the parameters of a TCP/IP connection to a
65       * remote {@link IConnectionBroker}.
66       * 
67       * @param identity
68       *            the connection identity (the identity of the remote context)
69       * @param connectionHashCode
70       *            the hashcode of the connection to the remote context
71       * @param remoteHostAddress
72       *            the IP remoteHostAddress or resolvable host name
73       * @param remoteHostTcpPort
74       *            the remoteHostTcpPort for the socket connection
75       */
76      public TcpConnectionParameters(String identity, int connectionHashCode,
77          String address, int port)
78      {
79          this.identity = identity;
80          this.remoteHostAddress = address;
81          this.remoteHostTcpPort = port;
82          this.connectionHashCode = connectionHashCode;
83      }
84  
85      public final String getRemoteContextIdentity()
86      {
87          return this.identity;
88      }
89  
90      /**
91       * @return The IP address of the host of the remote
92       *         {@link IConnectionBroker}
93       */
94      protected String getRemoteHostAddress()
95      {
96          return this.remoteHostAddress;
97      }
98  
99      /**
100      * @return The TCP port of the remote {@link IConnectionBroker}
101      */
102     protected int getRemoteHostTcpPort()
103     {
104         return this.remoteHostTcpPort;
105     }
106 
107     protected void setIdentity(String identity)
108     {
109         this.identity = identity;
110     }
111 
112     public final String getIdentity()
113     {
114         return this.identity;
115     }
116 
117     public IType getType()
118     {
119         return Type.SYSTEM;
120     }
121 
122     public IDomain getDomain()
123     {
124         return Domain.FRAMEWORK;
125     }
126 
127     public String getAddress()
128     {
129         return getIdentity() + COLON + getType() + COLON + getDomain();
130     }
131 
132     protected void setRemoteContextHashCode(int hashCode)
133     {
134         this.connectionHashCode = hashCode;
135     }
136 
137     public int getRemoteContextHashCode()
138     {
139         return this.connectionHashCode;
140     }
141 
142     public boolean isEqual(IConnectionParameters connectionParameters)
143     {
144         return equals(connectionParameters);
145     }
146 
147     @Override
148     protected void doDestroy()
149     {
150     }
151 
152     @Override
153     protected void doStart()
154     {
155     }
156 
157     @Override
158     public String toString()
159     {
160         return string(this, getIdentity() + ", Socket="
161             + getRemoteHostAddress() + COLON + getRemoteHostTcpPort()
162             + ", connectionHashCode=" + getRemoteContextHashCode());
163     }
164 
165     @Override
166     public int hashCode()
167     {
168         return this.connectionHashCode;
169     }
170 
171     @Override
172     public boolean equals(Object obj)
173     {
174         if (is.same(this, obj))
175         {
176             return true;
177         }
178         if (!(obj instanceof TcpConnectionParameters))
179         {
180             return false;
181         }
182         final TcpConnectionParameters other = (TcpConnectionParameters) obj;
183         return is.eq(this.connectionHashCode, other.connectionHashCode)
184             && is.eq(this.remoteHostAddress, other.remoteHostAddress)
185             && is.eq(this.identity, other.identity)
186             && is.eq(this.remoteHostTcpPort, other.remoteHostTcpPort);
187     }
188 }