001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as published by the
008: * Free Software Foundation; either version 2.1 of the License, or any later
009: * version.
010: *
011: * This library is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this library; if not, write to the Free Software Foundation,
018: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
019: *
020: */package org.objectweb.jonas.discovery;
021:
022: import java.net.UnknownHostException;
023:
024: import javax.management.MBeanRegistration;
025: import javax.management.MBeanServer;
026: import javax.management.ObjectName;
027:
028: import org.objectweb.jonas.common.Log;
029: import org.objectweb.jonas.common.NetUtils;
030:
031: import org.objectweb.util.monolog.api.BasicLevel;
032: import org.objectweb.util.monolog.api.Logger;
033:
034: /**
035: * The DiscoveryClient is in charge of sending discovery messages on the LAN to
036: * discover resources. It waits during a Timeout period of time for the
037: * discovery answers (discovery messages containing a response).
038: *
039: * @author <a href="mailto:Takoua.Abdellatif@inria.fr">Takoua Abdellatif </a>
040: * @version 1.0
041: */
042: public class DiscoveryClient implements DiscoveryClientMBean,
043: MBeanRegistration {
044: /**
045: * Source port of the sent discovery message
046: */
047: private int sourcePort;
048: /**
049: * Source IP of the sent discovery message (address of the local host)
050: */
051: private String sourceIp;
052: /**
053: * My ObjectName
054: */
055: private ObjectName myOn;
056: /**
057: * Runnable object that sends a discovery message and than listens for
058: * response of servers in the management domain
059: */
060: private DiscoveryClientListener dcl = null;
061: /**
062: * Thread associated to dcl
063: */
064: private Thread discoveryClientListener = null;
065: private int ttl = 1;
066: /**
067: * Time to wait for a response
068: */
069: private int timeout = 1000;
070: private int listeningPort;
071: private String listeningIp;
072: private static Logger logger = Log
073: .getLogger(Log.JONAS_DISCOVERY_PREFIX);
074:
075: public DiscoveryClient(int listeningPort, String listeningIP,
076: int sourcePort) {
077: this .listeningIp = listeningIP;
078: this .listeningPort = listeningPort;
079: this .sourcePort = sourcePort;
080: }
081:
082: /**
083: *
084: * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#getTimeOut()
085: */
086: public int getTimeout() {
087: return timeout;
088: }
089:
090: /**
091: *
092: * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#setTimeout(int)
093: */
094: public void setTimeout(int timeout) {
095: this .timeout = timeout;
096: }
097:
098: /**
099: *
100: * @see org.objectweb.jonas.server.discovery.EnrollerMBean#getListeningPort()
101: */
102: public int getListeningPort() {
103: return listeningPort;
104: }
105:
106: /**
107: *
108: * @see org.objectweb.jonas.server.discovery.EnrollerMBean#setListeningPort(int)
109: */
110: public void setListeningPort(int listeningPort) {
111: this .listeningPort = listeningPort;
112:
113: }
114:
115: /**
116: * @see org.objectweb.jonas.server.discovery.EnrollerMBean#getListeningIp()
117: */
118: public String getListeningIp() {
119: return listeningIp;
120: }
121:
122: /**
123: *
124: * @see org.objectweb.jonas.server.discovery.EnrollerMBean#setListeningIP(java.lang.String)
125: */
126: public void setListeningIp(String ipAddress) {
127: this .listeningIp = ipAddress;
128:
129: }
130:
131: /**
132: * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#getSourcePort()
133: */
134: public int getSourcePort() {
135: return sourcePort;
136: }
137:
138: /**
139: * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#setSourcePort(int)
140: */
141: public void setSourcePort(int sourcePort) {
142: this .sourcePort = sourcePort;
143:
144: }
145:
146: /**
147: * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#getSourceIp()
148: */
149: public String getSourceIp() {
150: return sourceIp;
151: }
152:
153: /**
154: * @see org.objectweb.jonas.server.discovery.EnrollerMBean#getTimeToLive()
155: */
156: public int getTimeToLive() {
157: return ttl;
158: }
159:
160: /**
161: * @see org.objectweb.jonas.server.discovery.DiscoveryClientMBean#setSourceIp(java.lang.String)
162: */
163: public void setSourceIp(String sourceIp) {
164: this .sourceIp = sourceIp;
165:
166: }
167:
168: /**
169: *
170: * @see org.objectweb.jonas.server.discovery.EnrollerMBean#setTimeToLive(int)
171: */
172: public void setTimeToLive(int ttl) {
173: this .ttl = ttl;
174:
175: }
176:
177: /**
178: *
179: * @see org.objectweb.jonas.server.discovery.EnrollerMBean#start()
180: */
181: public void start() {
182: // create the thread in charge of sending the discovery message and waiting
183: // for the responses
184: // creates an instance of the DiscoveryClientListener class
185: try {
186: this .setSourceIp(NetUtils.getLocalAddress());
187: } catch (UnknownHostException e) {
188: logger.log(BasicLevel.ERROR,
189: "Unable to create a localhost.", e);
190: }
191: dcl = new DiscoveryClientListener(this );
192:
193: if (discoveryClientListener == null) {
194: discoveryClientListener = new Thread(dcl,
195: "discoveryClientListener");
196: }
197: discoveryClientListener.start();
198:
199: }
200:
201: public void stop() {
202:
203: }
204:
205: /**
206: *
207: * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
208: * javax.management.ObjectName)
209: */
210: public ObjectName preRegister(MBeanServer mbeanServer, ObjectName on) {
211: this .myOn = on;
212: return myOn;
213: }
214:
215: /**
216: *
217: * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
218: */
219: public void postRegister(Boolean arg0) {
220: start();
221: }
222:
223: /**
224: *
225: * @see javax.management.MBeanRegistration#preDeregister()
226: */
227: public void preDeregister() throws Exception {
228: dcl.stop();
229:
230: }
231:
232: /**
233: *
234: * @see javax.management.MBeanRegistration#postDeregister()
235: */
236: public void postDeregister() {
237: }
238:
239: }
|