KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > ServerDesc


1 /*
2  * Copyright (C) 2001 - 2004 ScalAgent Distributed Technologies
3  * Copyright (C) 2004 - France Telecom R&D
4  * Copyright (C) 1996 - 2000 BULL
5  * Copyright (C) 1996 - 2000 INRIA
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA.
21  *
22  * Initial developer(s): Dyade
23  * Contributor(s): ScalAgent Distributed Technologies
24  */

25 package fr.dyade.aaa.agent;
26
27 import java.io.*;
28 import java.net.InetAddress JavaDoc;
29 import java.util.Vector JavaDoc;
30 import java.util.Enumeration JavaDoc;
31
32 import fr.dyade.aaa.util.Strings;
33 import fr.dyade.aaa.util.SocketAddress;
34
35 /**
36  * Description of an agent server. It is used by <code>Channel</code> and
37  * <code>Network</code> objects. Be careful, this structure is initialized
38  * in AgentServer, but it can be viewed outside of the agent package, so
39  * it's very important to make all modifiers package.
40  */

41 public final class ServerDesc implements Serializable {
42   /** Server unique identifier. */
43   short sid;
44   /** Server name. */
45   String JavaDoc name;
46   /**
47    * The IP address of the server.
48    * It contains hostname and port ({@link fr.dyade.aaa.util.SocketAddress
49    * <code>SocketAddress</code>}) of remote server. The communication port
50    * is set only if the server is directly accessible from this node; in
51    * this case it corresponds to the communication port of the server in the
52    * adjoining domain.
53    * The descriptor of an HA server contains one <code>SocketAddress</code>
54    * for each of its constituent.
55    */

56   private Vector JavaDoc sockAddrs = null;
57   /**
58    * Description of services running on this server.
59    */

60   transient ServiceDesc[] services = null;
61   /**
62    * Server Id. of a gateway server for this server if it is not in an
63    * adjoining domain.
64    */

65   short gateway = -1;
66   /**
67    * Domain description of this server.
68    */

69   transient MessageConsumer domain = null;
70
71   /** True if there is no waiting messages for this server. */
72   transient volatile boolean active = true;
73   /** Date of the last unsuccessful connection to this server. */
74   transient volatile long last = 0L;
75   /** Number of unsuccessful connection to this server. */
76   transient volatile int retry = 0;
77     
78   /**
79    * Constructs a new node for a persistent agent server.
80    * @param sid the server unique id
81    * @param name the server name
82    * @param hostname the server hostname
83    * @param port the server port
84    */

85   public ServerDesc(short sid,
86             String JavaDoc name,
87             String JavaDoc hostname,
88                     int port) {
89     this.sid = sid;
90     this.name = name;
91     sockAddrs = new Vector JavaDoc();
92     sockAddrs.addElement(new SocketAddress(hostname,port));
93   }
94
95   /**
96    * Gets server id. for this server.
97    *
98    * @return the server id.
99    */

100   public short getServerId() {
101     return sid;
102   }
103
104   /**
105    * Gets server name for this server.
106    *
107    * @return the server name.
108    */

109   public String JavaDoc getServerName() {
110     return name;
111   }
112
113   /**
114    * Gets hostname for this server.
115    *
116    * @return the hostname.
117    */

118   public String JavaDoc getHostname() {
119     return ((SocketAddress) sockAddrs.firstElement()).getHostname();
120   }
121
122   /**
123    * Gets port for this server.
124    *
125    * @return the port.
126    */

127   public int getPort() {
128     return ((SocketAddress) sockAddrs.firstElement()).getPort();
129   }
130
131   /**
132    * Returns an IP address for its server.
133    *
134    * @return an IP address for this server.
135    */

136   public InetAddress JavaDoc getAddr() {
137     return ((SocketAddress) sockAddrs.firstElement()).getAddress();
138   }
139
140   /**
141    * Resolves an IP address for its server, don't use an eventually caching
142    * address.
143    *
144    * @return an IP address for this server.
145    */

146   public InetAddress JavaDoc resetAddr() {
147     ((SocketAddress) sockAddrs.firstElement()).resetAddr();
148     return getAddr();
149   }
150
151   void addSockAddr(String JavaDoc hostname, int port) {
152     sockAddrs.addElement(new SocketAddress(hostname, port));
153   }
154
155   void updateSockAddr(String JavaDoc hostname, int port) {
156     sockAddrs.remove(0);
157     sockAddrs.insertElementAt(new SocketAddress(hostname,port), 0);
158   }
159
160   /**
161    * In case of an HA server, selects the IP address as this of the master
162    * component of the HA configuration.
163    */

164   void moveToFirst(SocketAddress addr) {
165     if (sockAddrs.indexOf(addr) > 0) {
166       if (sockAddrs.remove(addr))
167         sockAddrs.insertElementAt(addr,0);
168     }
169   }
170
171   /**
172    * In case of an HA server, gets the IP address of all the components
173    * of the HA configuration.
174    */

175   Enumeration JavaDoc getSockAddrs() {
176     return sockAddrs.elements();
177   }
178   
179   /**
180    * Gets the description of services running on this server.
181    *
182    * @return the description of services.
183    */

184   public ServiceDesc[] getServices() {
185     return services;
186   }
187
188   public short getGateway() {
189     return gateway;
190   }
191
192   public String JavaDoc getDomainName() {
193     return domain.getDomainName();
194   }
195
196
197   public Class JavaDoc getDomainType() {
198     return domain.getClass();
199   }
200
201   /**
202    * Provides a string image for this object.
203    *
204    * @return printable image of this object
205    */

206   public String JavaDoc toString() {
207     StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
208     strBuf.append("(").append(super.toString());
209     strBuf.append(",sid=").append(sid);
210     strBuf.append(",name=").append(name);
211     strBuf.append(",services=");
212     Strings.toString(strBuf, services);
213     strBuf.append(",active=").append(active);
214     strBuf.append(",last=").append(last);
215     strBuf.append(",gateway=").append(gateway);
216     strBuf.append(",sockAddrs=").append(sockAddrs);
217     strBuf.append(",domain=").append(domain);
218     strBuf.append(")");
219     return strBuf.toString();
220   }
221 }
222
Popular Tags