KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > plugins > discovery > ADControlSender


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat and Yuval Lubowich.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.plugins.discovery;
47
48
49 import java.net.InetAddress JavaDoc;
50 import java.net.NetworkInterface JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.Collection JavaDoc;
53 import java.util.Enumeration JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56
57
58 import org.mr.MantaAgent;
59
60 import org.mr.MantaException;
61 import org.mr.core.groups.GroupsException;
62 import org.mr.core.groups.MutlicastGroupManager;
63 import org.apache.commons.logging.Log;
64 import org.apache.commons.logging.LogFactory;
65 import org.mr.core.net.LocalTransport;
66 import org.mr.core.net.TransportInfo;
67 import org.mr.core.protocol.MantaBusMessage;
68 import org.mr.core.protocol.MantaBusMessageConsts;
69 import org.mr.core.util.byteable.ByteableList;
70 import org.mr.core.util.byteable.ByteableMap;
71 import org.mr.kernel.control.ControlSignalMessageSender;
72 import org.mr.kernel.services.ServiceActor;
73 import org.mr.kernel.services.ServiceActorInfoContainer;
74 import org.mr.kernel.services.ServiceProducer;
75 import org.mr.kernel.world.WorldModeler;
76
77 /**
78  * Overwrites the gControlSignalMessageSender sends information about this agent via
79  * multicast groups.
80  * @author Amir Shevat and Yuval Lubowich
81  *
82  */

83 public class ADControlSender extends ControlSignalMessageSender implements Runnable JavaDoc {
84
85     private boolean running = true;
86    
87     private Log log = null;
88     private MantaAgent agent = null;
89     private WorldModeler worldModeler = null;
90     private ByteableMap infoMap = null;
91     public static ServiceProducer serviceProducer = null;
92
93     private String JavaDoc agentName = null;
94     private ByteableList serviceActorList = null;
95     private ByteableList removedDurableList = null;
96
97     private MutlicastGroupManager groupsManager = null;
98     private ByteableList sendInfoTempList;
99
100     public ADControlSender() throws GroupsException {
101         agent = MantaAgent.getInstance();
102         agentName = agent.getAgentName();
103         serviceProducer = new ServiceProducer(agentName, "AD",(byte)0);
104         worldModeler = agent.getSingletonRepository().getWorldModeler();
105         groupsManager = agent.getSingletonRepository().getGroupsManager();
106         serviceActorList = new ByteableList();
107         removedDurableList = new ByteableList();
108         log=LogFactory.getLog("ADControlSender");
109         infoMap = new ByteableMap();
110         infoMap.put(AutoDiscoveryPlugin.AGENT_NAME, agentName);
111         sendInfoTempList = new ByteableList();
112     }//ADControlSender
113

114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.mr.kernel.control.ControlSignalMessageSender#advertiseService(org.mr.kernel.services.ServiceActor,
119      * org.mr.MantaAgent)
120      */

121     public void advertiseService(ServiceActor serviceActor, MantaAgent agent) throws MantaException {
122         //Update the info and invoke the SendInfo method
123
ServiceActorInfoContainer serviceActorInfoContainer = new ServiceActorInfoContainer();
124         serviceActorInfoContainer.setActor(serviceActor);
125         synchronized(serviceActorList){
126             serviceActorList.add(serviceActorInfoContainer);
127             removedDurableList.remove(serviceActorInfoContainer);
128         }
129         
130         
131         try {
132             sendInfo();
133         }
134         catch (Exception JavaDoc e) {
135             e.printStackTrace();
136             throw new MantaException(e.toString(), MantaException.ID_RECEIVE_GENERAL_FAIL);
137         }
138
139
140     }//advertiseService
141

142
143     /*
144      * (non-Javadoc)
145      *
146      * @see org.mr.kernel.control.ControlSignalMessageSender#recallService(org.mr.kernel.services.ServiceActor,
147      * org.mr.MantaAgent)
148      */

149     public void recallService(ServiceActor serviceActor, MantaAgent agent) throws MantaException {
150         //Update the info and invoke the SendInfo method
151
ArrayList JavaDoc tempList = null;
152         synchronized(serviceActorList){
153             tempList = new ArrayList JavaDoc();
154             tempList.addAll(serviceActorList);
155         }
156         Iterator JavaDoc actors = tempList.iterator();
157         while(actors.hasNext()){
158             ServiceActorInfoContainer saic = (ServiceActorInfoContainer) actors.next();
159             if(saic.equals(serviceActor)){
160                 synchronized(serviceActorList){
161                     serviceActorList.remove(saic);
162                 }
163             }
164                 
165         }
166         try {
167             sendInfo();
168         }
169         catch (Exception JavaDoc e) {
170             throw new MantaException(e.toString(), MantaException.ID_RECEIVE_GENERAL_FAIL);
171         }
172     }//recallService
173

174     public void recallDurableSubscription(ServiceActor serviceActor, MantaAgent agent) throws MantaException {
175          ServiceActorInfoContainer serviceActorInfoContainer = new ServiceActorInfoContainer();
176          serviceActorInfoContainer.setActor(serviceActor);
177          
178          removedDurableList.add(serviceActorInfoContainer);
179          
180          try {
181              sendInfo();
182          }
183          catch (Exception JavaDoc e) {
184             e.printStackTrace();
185              throw new MantaException(e.toString(), MantaException.ID_RECEIVE_GENERAL_FAIL);
186          }
187
188     }
189
190     
191     private synchronized void sendInfo() throws Exception JavaDoc {
192         sendInfoTempList.clear();
193         Collection JavaDoc localTransports = MantaAgent.getInstance().getSingletonRepository().getNetworkManager().getLocalTransports();
194         List JavaDoc updatedTransportsInfo = new ArrayList JavaDoc();
195         Iterator JavaDoc i = localTransports.iterator();
196         while (i.hasNext()) {
197             TransportInfo info =((LocalTransport) i.next()).getInfo();
198             String JavaDoc myIp = info.getIp().getHostAddress();
199             if(myIp.equals("0.0.0.0") ){
200                 String JavaDoc validLocalAddress = getValidLocalAddress();
201                 if(validLocalAddress != null){
202                     info= new TransportInfo(validLocalAddress,info.getPort(), info.getTransportInfoType().toString());
203                 }
204                 
205             }
206             updatedTransportsInfo.add(info);
207             
208         }
209             // check if transport is set to ip 0.0.0.0 and change it to real IP
210

211         
212         
213         if(localTransports == null){
214             if(log.isFatalEnabled()){
215                 log.fatal("There are no transports for this layer. Please check the configuration and the log files for errors.");
216             }//if
217
return;
218         }
219         ByteableList list = new ByteableList(updatedTransportsInfo);
220
221
222         infoMap.put(AutoDiscoveryPlugin.TRANSPORT_INFO, list);
223         synchronized(serviceActorList){
224             sendInfoTempList.addAll(serviceActorList);
225         }
226         infoMap.put(AutoDiscoveryPlugin.SERVICE_ACTOR, sendInfoTempList);
227         infoMap.put(AutoDiscoveryPlugin.REMOVED_DURABLE, removedDurableList);
228
229         MantaBusMessage msg = MantaBusMessage.getInstance();
230         msg.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CONTROL);
231
232         // insert the Map payload
233
msg.setPayload(infoMap);
234         msg.setSource(serviceProducer);
235         
236         groupsManager.sendMessageToGroup(AutoDiscoveryPlugin.groupKey, AutoDiscoveryPlugin.MANTA_AD_SUBJECT_NAME+worldModeler.getDefaultDomainName(), msg);
237         removedDurableList.clear();
238     }//sendInfo
239

240     
241     static String JavaDoc validAddress = null;
242     public static synchronized String JavaDoc getValidLocalAddress() {
243         
244 // if(validAddress !=null){
245
// return validAddress;
246
// }
247
try {
248             Enumeration JavaDoc ifs = NetworkInterface.getNetworkInterfaces();
249             while (ifs.hasMoreElements()) {
250                 NetworkInterface JavaDoc iface = (NetworkInterface JavaDoc)
251                     ifs.nextElement();
252                 Enumeration JavaDoc ips = iface.getInetAddresses();
253                 while (ips.hasMoreElements()) {
254                     InetAddress JavaDoc ip = (InetAddress JavaDoc) ips.nextElement();
255                     if (!ip.getHostAddress().equals("127.0.0.1")) {
256                         validAddress =ip.getHostAddress();
257                         return validAddress;
258                     }
259                 }
260             }
261         } catch (Throwable JavaDoc t) {}
262         validAddress ="127.0.0.1";
263         return validAddress; // won't happen, trust me.
264
}
265
266     /*
267      * (non-Javadoc)
268      *
269      * @see java.lang.Runnable#run()
270      */

271     public void run() {
272         while (running) {
273             try {
274                 sendInfo();
275                 Thread.sleep(AutoDiscoveryPlugin.refreshInterval);
276             }
277             catch (InterruptedException JavaDoc e) {
278                 if(log.isErrorEnabled()){
279                     log.error("InterruptedException at run().", e);
280                 }//if
281
}//catch
282
catch (Exception JavaDoc e) {
283                 if(log.isErrorEnabled()){
284                     log.error("Could not send message to AutoDiscovery Group.", e);
285                 }//if
286
}//catch
287
}//while
288
}//run
289

290     /**
291      * @return Returns the running.
292      */

293     public boolean isRunning() {
294         return running;
295     }//isRunning
296

297     /**
298      * @param running
299      * The running to set.
300      */

301     public void setRunning(boolean running) {
302         this.running = running;
303     }//setRunning
304

305
306 }//ADControlSender
307
Popular Tags