KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > discovery > DiscoveryClient


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  */

21
22 package org.objectweb.jonas.discovery;
23
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26
27 import javax.management.MBeanRegistration JavaDoc;
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.NotificationBroadcasterSupport JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 import org.objectweb.jonas.common.Log;
33 import org.objectweb.jonas.common.NetUtils;
34 import org.objectweb.util.monolog.api.BasicLevel;
35 import org.objectweb.util.monolog.api.Logger;
36
37 /**
38  * The DiscoveryClient is in charge of sending discovery messages on the LAN to
39  * discover resources. It waits during a Timeout period of time for the
40  * discovery answers (discovery messages containing a response).
41  *
42  * @author <a HREF="mailto:Takoua.Abdellatif@inria.fr">Takoua Abdellatif </a>
43  * @version 1.0
44  */

45 public class DiscoveryClient extends NotificationBroadcasterSupport JavaDoc implements
46     DiscoveryClientMBean,
47     MBeanRegistration JavaDoc {
48     /**
49      * Source port of the sent discovery message
50      */

51     private int sourcePort;
52     /**
53      * Source IP of the sent discovery message (address of the local host)
54      */

55     private String JavaDoc sourceIp;
56     /**
57      * Current MBean Server
58      */

59     private MBeanServer JavaDoc mbeanServer;
60     /**
61      * My ObjectName
62      */

63     private ObjectName JavaDoc myOn;
64     /**
65      * Runnable object that sends a discovery message and than listens for
66      * response of servers in the management domain
67      */

68     private DiscoveryClientListener dcl = null;
69     /**
70      * Thread associated to dcl
71      */

72     private Thread JavaDoc discoveryClientListener = null;
73     private int ttl = 1;
74     /**
75      * Time to wait for a response
76      */

77     private int timeout = 1000;
78     private int listeningPort;
79     private String JavaDoc listeningIp;
80     private static Logger logger = Log.getLogger(Log.JONAS_DISCOVERY_PREFIX);
81
82     public DiscoveryClient(int listeningPort, String JavaDoc listeningIP, int sourcePort) {
83       this.listeningIp = listeningIP;
84       this.listeningPort = listeningPort;
85       this.sourcePort =sourcePort;
86     }
87
88     /**
89      *
90      * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#getTimeOut()
91      */

92     public int getTimeout() {
93         return timeout;
94     }
95
96     /**
97      *
98      * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#setTimeout(int)
99      */

100     public void setTimeout(int timeout) {
101         this.timeout = timeout;
102     }
103
104     /**
105      *
106      * @see org.objectweb.jonas.server.discovery.EnrollerMBean#getListeningPort()
107      */

108     public int getListeningPort() {
109         return listeningPort;
110     }
111
112     /**
113      *
114      * @see org.objectweb.jonas.server.discovery.EnrollerMBean#setListeningPort(int)
115      */

116     public void setListeningPort(int listeningPort) {
117         this.listeningPort = listeningPort;
118
119     }
120
121     /**
122      * @see org.objectweb.jonas.server.discovery.EnrollerMBean#getListeningIp()
123      */

124     public String JavaDoc getListeningIp() {
125         return listeningIp;
126     }
127
128     /**
129      *
130      * @see org.objectweb.jonas.server.discovery.EnrollerMBean#setListeningIP(java.lang.String)
131      */

132     public void setListeningIp(String JavaDoc ipAddress) {
133         this.listeningIp = ipAddress;
134
135     }
136
137     /**
138      * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#getSourcePort()
139      */

140     public int getSourcePort() {
141         return sourcePort;
142     }
143
144     /**
145      * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#setSourcePort(int)
146      */

147     public void setSourcePort(int sourcePort) {
148         this.sourcePort = sourcePort;
149
150     }
151
152     /**
153      * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#getSourceIp()
154      */

155     public String JavaDoc getSourceIp(){
156         return sourceIp;
157     }
158
159     /**
160      * @see org.objectweb.jonas.server.discovery.EnrollerMBean#getTimeToLive()
161      */

162     public int getTimeToLive() {
163         return ttl;
164     }
165
166     /**
167      * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#setSourceIp(java.lang.String)
168      */

169     public void setSourceIp(String JavaDoc sourceIp) {
170         this.sourceIp = sourceIp;
171
172     }
173
174     /**
175      *
176      * @see org.objectweb.jonas.server.discovery.EnrollerMBean#setTimeToLive(int)
177      */

178     public void setTimeToLive(int ttl) {
179         this.ttl = ttl;
180
181     }
182
183     /**
184      *
185      * @see org.objectweb.jonas.server.discovery.EnrollerMBean#start()
186      */

187     public void start() {
188         // create the thread in charge of sending the discovery message and waiting
189
// for the responses
190
// creates an instance of the DiscoveryClientListener class
191
try {
192             this.setSourceIp(NetUtils.getLocalAddress());
193         } catch (UnknownHostException JavaDoc e) {
194             logger.log(BasicLevel.ERROR, "Unable to create a localhost.", e);
195         }
196         dcl = new DiscoveryClientListener(this);
197
198         if (discoveryClientListener == null) {
199             discoveryClientListener = new Thread JavaDoc(dcl, "discoveryClientListener");
200         }
201         discoveryClientListener.start();
202
203     }
204
205     public void stop() {
206
207     }
208
209     /**
210      *
211      * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
212      * javax.management.ObjectName)
213      */

214     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc mbeanServer, ObjectName JavaDoc on) {
215         // set mbeanServer
216
this.mbeanServer = mbeanServer;
217         this.myOn = on;
218
219         return myOn;
220     }
221
222     /**
223      *
224      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
225      */

226     public void postRegister(Boolean JavaDoc arg0) {
227         start();
228     }
229
230     /**
231      *
232      * @see javax.management.MBeanRegistration#preDeregister()
233      */

234     public void preDeregister() throws Exception JavaDoc {
235         dcl.stop();
236
237     }
238
239     /**
240      *
241      * @see javax.management.MBeanRegistration#postDeregister()
242      */

243     public void postDeregister() {
244     }
245
246 }
Popular Tags