MessageBroker.java

Go to the documentation of this file.
00001 /*
00002  * MessageBroker.java
00003  *
00004  * Created on 21 octobre 2003, 12:30
00005  *  Copyright (C) 2003 Philippe MOULIN
00006  *  www.net2map.org
00007  *
00008  *  This program is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU General Public License
00010  *  as published by the Free Software Foundation; either version 2
00011  *  of the License, or (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program; if not, write to the Free Software
00020  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021  */
00022 
00023 package org.net2map.pov.p2p;
00024 
00025 import java.beans.*;
00026 import java.util.Hashtable;
00027 import java.util.Enumeration;
00028 import net.jxta.endpoint.Message;
00029 import net.jxta.endpoint.Message.ElementIterator;
00030 import net.jxta.endpoint.StringMessageElement;
00031 
00032 
00037 public class MessageBroker 
00038        extends Object 
00039        implements java.io.Serializable
00040 {
00042     public static org.apache.log4j.Logger logger = 
00043     org.apache.log4j.Logger.getLogger(MessageBroker.class.getName());
00044     
00048     private Hashtable colMessageTypes;
00049     
00050         
00052 
00054     private static MessageBroker instance = null;
00061     public static MessageBroker getInstance()
00062     {
00063         if (instance == null)
00064         {
00065             synchronized(LocalNode.class)
00066             {
00067                 if (instance == null)
00068                 {
00069                     instance = new MessageBroker();
00070                 }
00071             }      
00072         }
00073         return instance;
00074     }
00076     private MessageBroker()
00077     {
00078         logger.setLevel(org.apache.log4j.Level.DEBUG);
00079         //propertySupport = new PropertyChangeSupport( this );
00080         colMessageTypes = new Hashtable();
00081     }
00082     
00086     public synchronized void addPOV_MessageReceiver(POV_MessageReceiver theReceiver)
00087     {
00088         colMessageTypes.put(theReceiver.getAssociatedMessageTag(), theReceiver);
00089         logger.warn("" + 
00090                      theReceiver.getClass().getName() + 
00091                      " is now a MessageReceiver for " + 
00092                      theReceiver.getAssociatedMessageTag() + 
00093                      " messages");
00094     }
00098     public synchronized void removePOV_MessageReceiver(String messageTag)
00099     {
00100         colMessageTypes.remove(messageTag);
00101     }
00105     public synchronized void removePOV_MessageReceiver(POV_MessageReceiver theReceiver)
00106     {
00107         colMessageTypes.remove(theReceiver.getAssociatedMessageTag());
00108     }
00113     public synchronized void handleIncomingMessage(Message theMessage, RemoteNode theSender)
00114     {
00115         logger.warn("Received a message from " + theSender.getPeerName());
00116         POV_MessageReceiver currentReceiver = null;
00117         Enumeration registeredReceivers = colMessageTypes.elements();
00118         Message.ElementIterator currentElementIterator = null;
00119         net.jxta.endpoint.MessageElement currentElement = null;
00120         while(registeredReceivers.hasMoreElements())
00121         {
00122             currentReceiver = (POV_MessageReceiver)registeredReceivers.nextElement();
00123             logger.warn("Looking for " + currentReceiver.getAssociatedMessageTag() + " messages...");
00124             currentElementIterator = theMessage.getMessageElements(currentReceiver.getAssociatedMessageTag());
00125             while(currentElementIterator.hasNext())
00126             {
00127                 currentElement = (net.jxta.endpoint.MessageElement)currentElementIterator.next();
00128                 logger.warn("Sending " + 
00129                              currentElement.getElementName() + 
00130                              " to " + 
00131                              currentReceiver.getClass().toString());
00132                 currentReceiver.processIncomingMessage(currentElement, theSender);
00133             }
00134         }
00135     }
00136     
00141     public synchronized void sendRequestToRemoteNodes(String theRequest,
00142                                                       String theTag)
00143     {
00144         logger.warn("Sending request:"  + theRequest);
00145                 
00146         Message theMessage;
00147         StringMessageElement theStringMessageElement;
00148         
00149         theMessage = new Message();
00150         theStringMessageElement = new StringMessageElement( theTag, //Tag 
00151                                                             theRequest,
00152                                                             null //No signature
00153                                                             );
00154         theMessage.addMessageElement(theStringMessageElement);
00155         /*theStringMessageElement = new StringMessageElement( "POV_Sender", //Tag 
00156                                                             getMyPeerID().toString(),
00157                                                             null //No signature
00158                                                             );
00159         theMessage.addMessageElement(theStringMessageElement);*/
00160         RemoteNodeList.getInstance().sendMessageToRemoteNodes(theMessage);
00161         logger.warn("has sent a request !");
00162     }
00163     
00169     public synchronized void sendResponseToRemoteNode(String                         theResponse, 
00170                                                       org.net2map.pov.p2p.RemoteNode theRequester,
00171                                                       String                         theTag)
00172     {
00173         Message theMessage;
00174         StringMessageElement theStringMessageElement;
00175         
00176         theMessage = new Message();
00177         theStringMessageElement = new StringMessageElement( theTag, //Tag 
00178                                                             theResponse,
00179                                                             null //No signature
00180                                                             );
00181         theMessage.addMessageElement(theStringMessageElement);
00182 
00183         theRequester.receiveMessage(theMessage);
00184         logger.warn("has sent a response to " + theRequester.getPeerName());
00185     }
00186 }
Accueil | Téléchargement | Manuel | Doc. technique | Sources CVS | Faq | Nous contacter
©2003 - All Rights Reserved