You can subscribe to this list here.
2002 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
(1) |
Dec
(12) |
---|
From: <rw...@us...> - 2002-12-31 23:37:01
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv20756/org/wettp2p/communicationslayer Modified Files: CommunicationsHandler.java Log Message: This version tests the DiscoveryManagers. Index: CommunicationsHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/communicationslayer/CommunicationsHandler.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** CommunicationsHandler.java 17 Dec 2002 06:15:58 -0000 1.6 --- CommunicationsHandler.java 31 Dec 2002 23:36:55 -0000 1.7 *************** *** 14,17 **** --- 14,27 ---- * 8 - (Done - rben) Add Initialization method to be used in initializing * communications. + * + * Update 0.6: 12/31/2002 + * Moved the start() and stop() methods to be right after initialize(). It's + * easier to modify the file for testing that way. + * + * start() now creates two DiscoveryManagers on netPeerGroup. One looks for + * PeerAdvertisements and the other looks for PeerGroups. The DiscoveryManager + * classes are derived from Hashtable and you can retrieve the contents of their + * cache by calling get(String key). You'll have to cast the returned object + * into either a PeerAdvertisement or a PeerGroup. */ *************** *** 45,48 **** --- 55,59 ---- import net.jxta.protocol.PeerGroupAdvertisement; import net.jxta.protocol.PeerAdvertisement; + import net.jxta.protocol.PipeAdvertisement; import net.jxta.protocol.ModuleImplAdvertisement; *************** *** 80,84 **** * @author Robert Wei * @author Ray Benjamin ! * @version 0.5, 12/14/2002 * * $Author$ --- 91,95 ---- * @author Robert Wei * @author Ray Benjamin ! * @version 0.6, 12/31/2002 * * $Author$ *************** *** 115,127 **** private PipeService currentPipeService; public static void main(String args[]) { ! CommunicationsHandler myApp = CommunicationsHandler.getInstance(); ! try ! { myApp.initialize(); myApp.start(); } ! finally ! { myApp.stop(); System.exit(0); --- 126,156 ---- private PipeService currentPipeService; + private GroupDiscoveryManager gdm; + private PeerDiscoveryManager pdm; + public static void main(String args[]) { ! CommunicationsHandler myApp = CommunicationsHandler.getInstance(); ! ! try { myApp.initialize(); myApp.start(); + + Thread.sleep(300000); + + System.out.println("Peers found"); + Enumeration keys = myApp.pdm.keys(); + while ( keys.hasMoreElements() ) { + System.out.println((String) keys.nextElement()); + } + + System.out.println("Groups found"); + keys = myApp.gdm.keys(); + while ( keys.hasMoreElements() ) { + System.out.println((String) keys.nextElement()); + } } ! catch ( InterruptedException ie ) { ! } ! finally { myApp.stop(); System.exit(0); *************** *** 154,182 **** /** - * Initializes the JXTA platform and joins the default netPeer group. - **/ - private void startJXTA() { - logger.entering(classStr, "startJXTA"); - try { - logger.fine("Attempting to initialize JXTA"); - netPeerGroup = PeerGroupFactory.newNetPeerGroup(); - logger.fine("JXTA started"); - logger.fine("Joined Peergroup: " + netPeerGroup.getPeerGroupName()); - } - catch (Exception e) { - // Failed to initialize JXTA, print error message and quit. - String str = "Error: JXTA initialization failed due to exception: "; - logger.severe(str); - System.err.println(str); - logger.severe(e.toString()); - System.err.println(e.toString()); - e.printStackTrace(); - // NOTE: should find someway to insure that stacktrace is written into - // log... - System.exit(1); - } - } - - /** * This routine is used to start the CommunicationHandler processes that * receive, respond to, and forward messages. --- 183,186 ---- *************** *** 184,187 **** --- 188,195 ---- private void start() { logger.entering(classStr,"start"); + + pdm = new PeerDiscoveryManager(netPeerGroup); + gdm = new GroupDiscoveryManager(netPeerGroup); + // create input pipe // discover peers *************** *** 198,201 **** --- 206,213 ---- private void stop() { logger.entering(classStr,"stop"); + + pdm.stop(); + gdm.stop(); + // Notify peers of pending shutdown // shutdown propogate pipe *************** *** 203,206 **** --- 215,243 ---- // At this point, the CommunicationsHandler is shutdown and may be // stopped cleanly. + } + + /** + * Initializes the JXTA platform and joins the default netPeer group. + **/ + private void startJXTA() { + logger.entering(classStr, "startJXTA"); + try { + logger.fine("Attempting to initialize JXTA"); + netPeerGroup = PeerGroupFactory.newNetPeerGroup(); + logger.fine("JXTA started"); + logger.fine("Joined Peergroup: " + netPeerGroup.getPeerGroupName()); + } + catch (Exception e) { + // Failed to initialize JXTA, print error message and quit. + String str = "Error: JXTA initialization failed due to exception: "; + logger.severe(str); + System.err.println(str); + logger.severe(e.toString()); + System.err.println(e.toString()); + e.printStackTrace(); + // NOTE: should find someway to insure that stacktrace is written into + // log... + System.exit(1); + } } |
From: <rw...@us...> - 2002-12-31 23:36:12
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv20208/org/wettp2p/communicationslayer Added Files: BasicDiscoveryManager.java Log Message: --- NEW FILE: BasicDiscoveryManager.java --- /* * DiscoveryThread.java * * Created on December 17, 2002, 11:02 AM * * <p>Copyright (C) 2002, Robert Wei</p> * * <p>This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version.</p> * * <p>This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details.</p> * * <p>You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</p> * * Update 12/31/2002 * This class has been overhauled to serve as the base class for caching * discovery threads. * * Notes: * The base Advertisement class is unable to call the getAdvertisementType() method * without throwing an exception. It is necessary to cast all advertisements * into the correct type before trying to get its type. * * When performing discovery, you will almost certainly recieve more than one * copy of an advertisement per query. It is advisable to set up a cache for * these found advertisements using a well-known and fairly unique key (like * the peer name for peer advertisements) and storing them into the provided * Hashtable. * * It's probably bad object-oriented practice to extend Hashtable in this class * just so that its subclasses get the functionality for less code but hey, * it sounded like a good idea at the time... */ package org.wettp2p.communicationslayer; import java.io.*; import java.util.*; import java.util.logging.*; import org.wettp2p.communicationslayer.event.*; import org.wettp2p.peer.*; import net.jxta.discovery.DiscoveryService; import net.jxta.discovery.DiscoveryEvent; import net.jxta.discovery.DiscoveryListener; import net.jxta.document.AdvertisementFactory; import net.jxta.document.Advertisement; import net.jxta.document.MimeMediaType; import net.jxta.peergroup.PeerGroup; import net.jxta.protocol.DiscoveryResponseMsg; import net.jxta.protocol.PeerAdvertisement; /** * The BasicDiscoveryManager forms the basis for more specialized classes that * are keyed to a certain form of discovery. The discovery parameters are * supplied via one of the two constructors. BasicDiscoveryManager can send out * single discovery messages or be configured to send out messages at a set * interval. In addition to retrieving adverisements into the local cache, * BasicDiscoveryManager is also a DiscoveryListener that can be configured to * process advertisements through subclassing. * * The three major methods of controlling execution are doLocal(), doRemote() * and setInterval(). The thread object can be obtained by calling getThread() * and the associated peergroup can be obtained through getPeerGroup. * * Subclasses of BasicDiscoveryManager should contain a call to super() in their * constructors as the discovery parameters cannot be set any other way and the * background thread capability is also activated through the constructor. * You may also want to call doLocal(), doRemote(), or setInterval() from the * constructor of a subclass depending on the application. The <code>name</code> * variable is protected and defines the type of advertisements that the * associated instance of DiscoveryManager looks for. * * Though BasicDiscoveryManager does not use Hashtable, its subclasses can make * significant use of it as an advertisement cache. * * The BasicDiscoveryManager searches for generic advertisements by default and * stores them into the local cache where other classes can find them. The type * of discovery can be changed but it is still highly recommended that this * class be subclassed to provide specialized handling for specific advertisements. * * @author Robert Wei * @version 1.0 12/31/2002 */ public class BasicDiscoveryManager extends Hashtable implements Runnable, DiscoveryListener { /* Logger */ static Logger logger = Logger.getLogger(Peer.TOP_LOG_NAME + ".communicationshandler"); private static String classStr = "BasicDiscoveryManager"; // DiscoveryService Constants public static final int PEER = DiscoveryService.PEER; public static final int GROUP = DiscoveryService.GROUP; public static final int ADV = DiscoveryService.ADV; public static final int DEFAULT_PRIORITY = 4; protected MimeMediaType mimeXML = new MimeMediaType("text/xml"); protected String name = null; /* Parameters for the getRemoteAdvertisements() method * To be overridden in subclasses that define different DiscoveryThreads */ String peerID = null; int type = ADV; String attribute = null; String value = null; int threshold = 10; private PeerGroup home; // Thread and thread-control variables private volatile Thread myThread; private boolean doLocal = false; private boolean doRemote = false; // The interval between discovery requests. Defaults to -1 private int interval = -1; // Event Listeners // private Vector advertisementListeners = new Vector(); /** * Creates a new DiscoveryThread. * * @param pg The peergroup in which discovery will be performed * @param type Discovery type PEER, GROUP, ADV */ BasicDiscoveryManager(PeerGroup pg, int type) { super(); home = pg; this.type = type; createName(); start(); } /** * Creates a new DiscoveryThread * * @param pg The peergroup in which discovery will be performed * @param type Discovery type PEER, GROUP, ADV * @param attribute Attribute name to narrow disocvery to Valid values for * this parameter are null (don't care), or exact element name in the * advertisement of interest (e.g. "Name") * @param value Value of attribute to narrow disocvery to valid values for * this parameter are null (don't care), Exact value, or use of wild * card(s) (e.g. if a Advertisement defines FooBar , a value of "*bar", * "foo*", or "*ooB*", will return the Advertisement */ BasicDiscoveryManager(PeerGroup pg, int type, String attribute, String value) { this(pg, type); this.attribute = attribute; this.value = value; } /** * Shuts down the thread before destroying this class. */ protected void finalize() throws Throwable { stop(); super.finalize(); } String getPeerID() { return peerID; } int getThreshold() { return threshold; } int getInterval() { return interval; } int getType() { return type; } String getAttribute() { return attribute; } String getValue() { return value; } String getName() { return name; } /** * Generates the name of the object instance based on the type of * advertisement searched for. */ private void createName() { StringBuffer str = new StringBuffer(home.getPeerGroupName()); switch ( type ) { case 0: str.append(" <Peers" ); break; case 1: str.append(" <Groups" ); break; default: str.append(" <Advertisements"); } if ( attribute!=null ) { str.append( attribute ); str.append( "=" ); str.append( value ); } str.append( ">" ); name = str.toString(); } /** * Returns the handle to the thread that runs this class. */ Thread getThread() { logger.entering(classStr,"getThread"); return myThread; } /** * Returns the bound peergroup */ PeerGroup getPeerGroup() { logger.entering(classStr,"getPeerGroup"); return home; } /** * Sets the peerID of a specific peer to query */ void setPeerID(String peerID) { this.peerID = peerID; } /** * Sets the maximum number of discoveries that can be recieved from one peer */ void setThreshold(int threshold) { this.threshold = threshold; } /** * Sets the interval between discovery requests in thread mode. Time in * seconds. If the value is negative, the thread will perform remote * discovery only when it is explicitly told to do so through the doRemote() * method. * * Local discovery can only be called explicitly through doLocal(). */ void setInterval(int interval) { this.interval = interval; if ( interval > 0 ) doRemote(); else doRemote = false; } /** * Tells the thread to perform Remote Discovery on its next execution cycle. */ void doRemote() { logger.entering(classStr,"doRemote"); doRemote = true; } /** * Tells the thread to perform Local Discovery on its next execution cycle. */ void doLocal() { logger.entering(classStr,"doLocal"); doLocal = true; } /** * Starts the thread */ void start() { logger.entering(classStr,"start"); if ( myThread == null ) { myThread = new Thread(this, classStr + name); myThread.setPriority(DEFAULT_PRIORITY); myThread.start(); } } /** * Stops the thread */ void stop() { logger.entering(classStr,"stop"); myThread = null; } /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() * */ public void run() { logger.entering(classStr,"run"); int seconds = interval; Thread thisThread = Thread.currentThread(); while ( thisThread == myThread ) { seconds++; try { // Check to see if there are commands to be executed. if ( seconds > interval ) { seconds = 0; // Sends a remoteDiscoveryMessage and deflags doRemote if // the thread is not in continuous mode. if ( doRemote ) { if ( interval <= 0 ) { doRemote = false; } sendDiscoveryMessage(); } // Performs one local discovery if doLocal is flagged if ( doLocal ) { doLocal = false; discoverLocalAdvertisements(); } } // Wait 1 second Thread.sleep(1000); } catch (InterruptedException ie) { String str = "Thread [" + name + "] Interrupted"; logger.warning(str); System.err.println(str); return; } } } /** * Called by DiscoveryService when a discovery is made. This method * repackages the DiscoveryEvent as an AdvertisementEvent and calls * <code>fireAdvertisementEvent()</code> to send it to all registered * listeners. * * @param discoveryEvent the event message recieved from DiscoveryService */ public void discoveryEvent(net.jxta.discovery.DiscoveryEvent discoveryEvent) { logger.entering(classStr,"discoveryEvent"); DiscoveryResponseMsg res = discoveryEvent.getResponse(); PeerAdvertisement peerAdv; // Find out where the advertisement came from String peerString = res.getPeerAdv(); // Deserialize, cast, and report try { InputStream is = new ByteArrayInputStream( peerString.getBytes() ); peerAdv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(mimeXML, is); String str = name + ": Got a Discovery Response [" + res.getResponseCount() + " elements] from peer : " + peerAdv.getName(); logger.finest(str); } catch (IOException e) { String str = name + ": Cannot deserialize incoming PeerAdvertisement"; logger.warning(str); return; } // Obtain the enumeration of advertisements Enumeration enum = res.getResponses(); while ( enum.hasMoreElements() ) { String advString = (String) enum.nextElement(); Advertisement adv = null; // Deserialize advertisements try { InputStream is = new ByteArrayInputStream( advString.getBytes() ); adv = AdvertisementFactory.newAdvertisement(mimeXML, is); processAdvertisement(adv); } catch (IOException e) { String str = name + ": Cannot deserialize incoming Advertisement"; logger.warning(str); } } } /** * This method is called by the DiscoveryListener event handler after intial * processing and reporting has been done. The method is empty in the basic * implementation but subclasses can override this method to provide * specialized handling for the advertisements recieved. * * @param Advertisement the advertisement recieved from the * <code>DiscoveryListener</code>. */ protected void processAdvertisement(Advertisement adv) { logger.entering(classStr,"processAdvertisement"); } /** * This method can be called to send a discovery message. If this class is * run as a thread, the run method will call this method at the specified * interval. * * @param Advertisement the advertisement recieved from the * <code>DiscoveryListener</code>. */ void sendDiscoveryMessage() { logger.entering(classStr,"sendDiscoveryMessage"); DiscoveryService disco = home.getDiscoveryService(); disco.getRemoteAdvertisements(peerID, type, attribute, value, threshold, this); logger.finer(name + ": Sending Discovery Request" ); } /** * This method is not called by default in the base class but may be called * from subclasses to process the local cache. * * @param Advertisement the advertisement recieved from the * <code>DiscoveryListener</code>. */ void discoverLocalAdvertisements() { logger.entering(classStr,"discoverLocalAdvertisements"); DiscoveryService disco = home.getDiscoveryService(); try { Enumeration advs = disco.getLocalAdvertisements(type, attribute, value); logger.finer(name + ": Executing Local Discovery" ); while ( advs.hasMoreElements() ) { Advertisement adv = (Advertisement) advs.nextElement(); processAdvertisement(adv); } } catch (IOException ie) { String str = name + ": Cannot deserialize local Advertisement"; logger.warning(str); } } } |
From: <rw...@us...> - 2002-12-31 23:35:40
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv19895/org/wettp2p/communicationslayer Added Files: GroupDiscoveryManager.java Log Message: --- NEW FILE: GroupDiscoveryManager.java --- /* * GroupDiscoveryManager.java * <p>Copyright (C) 2002, Robert Wei</p> * * <p>This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version.</p> * * <p>This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details.</p> * * <p>You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</p> * * Created on December 31, 2002, 4:01 AM */ package org.wettp2p.communicationslayer; import java.util.*; import java.util.logging.*; import org.wettp2p.peer.*; import net.jxta.document.Advertisement; import net.jxta.exception.PeerGroupException; import net.jxta.peergroup.PeerGroup; import net.jxta.protocol.PeerGroupAdvertisement; /** * The GroupDiscoveryManager is a specialization of BasicDiscoveryManager that * looks for PeerGroups and caches them. This class can be used like a Hashtable * to access the cache of stored PeerGroup objects. * * @author Robert Wei * @version 1.0 12/31/2002 */ class GroupDiscoveryManager extends BasicDiscoveryManager { /* Logger */ static Logger logger = Logger.getLogger(Peer.TOP_LOG_NAME + ".communicationshandler"); private static String classStr = "GroupDiscoveryManager"; /** Creates a new instance of GroupDiscoveryManager */ public GroupDiscoveryManager(PeerGroup pg) { super(pg, GROUP); // Instructs the thread to perform one local discovery doLocal(); // Sets the thread to rebroadcast its discovery request every 60 seconds. setInterval(60); } /** * This method is called by the DiscoveryListener event handler after intial * processing and reporting has been done. The method is empty in the basic * implementation but subclasses can override this method to provide * specialized handling for the advertisements recieved. * * @param Advertisement the advertisement recieved from the * <code>DiscoveryListener</code>. * */ protected void processAdvertisement(Advertisement adv) { logger.entering(classStr, "processAdvertisement"); if ( adv instanceof PeerGroupAdvertisement ) { // Cast the advertisement as a PeerGroupAdvertisement PeerGroupAdvertisement pgadv = (PeerGroupAdvertisement) adv; try { // Create a PeerGroup object from the advertisement and get its name PeerGroup npg = getPeerGroup().newGroup(pgadv); String npgName = npg.getPeerGroupName(); // Add this PeerGroup to the cache if it does not already exist if ( !containsKey(npgName) ) { put(npgName, npg); logger.finer("New PeerGroup [" + npgName + "] discovered and added"); } else { logger.finest("PeerGroup [" + npgName + "] already exists"); } } catch (PeerGroupException pge) { String str = name + ": Unable to create PeerGroup from advertisement"; logger.warning(str); } } else { logger.warning(name + ": Advertisement is not a PeerGroupAdvertisement"); return; } } } |
From: <rw...@us...> - 2002-12-31 23:35:05
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv19550/org/wettp2p/communicationslayer Added Files: PeerDiscoveryManager.java Log Message: --- NEW FILE: PeerDiscoveryManager.java --- /* * PeerDiscoveryManager.java * <p>Copyright (C) 2002, Robert Wei</p> * * <p>This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version.</p> * * <p>This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details.</p> * * <p>You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</p> * * Created on December 31, 2002, 4:01 AM */ package org.wettp2p.communicationslayer; import java.util.*; import java.util.logging.*; import org.wettp2p.peer.*; import net.jxta.document.Advertisement; import net.jxta.exception.PeerGroupException; import net.jxta.peergroup.PeerGroup; import net.jxta.protocol.PeerAdvertisement; /** * The PeerDiscoveryManager is a specialization of BasicDiscoveryManager that * looks for PeerAdvertisements and caches them. This class can be used like a * Hashtable to access the cache of stored PeerAdvertisement objects. * * @author Robert Wei * @version 1.0 12/31/2002 */ class PeerDiscoveryManager extends BasicDiscoveryManager { /* Logger */ static Logger logger = Logger.getLogger(Peer.TOP_LOG_NAME + ".communicationshandler"); private static String classStr = "PeerDiscoveryManager"; /** Creates a new instance of PeerDiscoveryManager */ public PeerDiscoveryManager(PeerGroup pg) { super(pg, PEER); // Instructs the thread to perform one local discovery doLocal(); // Sets the thread to rebroadcast its discovery request every 60 seconds. setInterval(60); } /** * This method is called by the DiscoveryListener event handler after intial * processing and reporting has been done. The method is empty in the basic * implementation but subclasses can override this method to provide * specialized handling for the advertisements recieved. * * @param Advertisement the advertisement recieved from the * <code>DiscoveryListener</code>. */ protected void processAdvertisement(Advertisement adv) { logger.entering(classStr, "processAdvertisement"); if ( adv instanceof PeerAdvertisement ) { // Cast the advertisement as a PeerAdvertisement PeerAdvertisement padv = (PeerAdvertisement) adv; String pName = padv.getName(); // Add this Peer to the cache if it does not already exist if ( !containsKey(pName) ) { put(pName, padv); logger.finer("New Peer [" + pName + "] discovered and added"); } else { logger.finest("Peer [" + pName + "] already exists"); } } else { logger.warning(name + ": Advertisement is not a PeerAdvertisement"); return; } } } |
From: <rw...@us...> - 2002-12-17 23:37:15
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv26972/org/wettp2p/communicationslayer Added Files: DiscoveryThread.java Log Message: A dedicated DiscoveryListener that runs in its own thread. --- NEW FILE: DiscoveryThread.java --- /* * DiscoveryThread.java * * Created on December 17, 2002, 11:02 AM * * <p>Copyright (C) 2002, Robert Wei</p> * * <p>This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version.</p> * * <p>This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details.</p> * * <p>You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</p> * * Comments and Usage Notes * This class basically wraps the DiscoveryService.getRemoteAdvertisements() * method into a thread and to fire off an Event that could be recieved by more * than one listener. It is intended to be run as a low-priority thread that * occasionally scans its peergroup context for new advertisements. It can be * extended to discover peergroups or peers as well as to find specific * advertisement types. * * Problems and Issues * There has to be some way to make this class fire an AdvertisementEvent only * when it receives an advertisement that has not already been cached. * * Future Work * Add a logger and error-reporting code. * With slight changes, instances of this class might be configured to discover * any Advertisement without the need to extend it. */ package org.wettp2p.communicationslayer; import java.io.*; import java.util.*; import org.wettp2p.communicationslayer.event.*; import net.jxta.discovery.DiscoveryService; import net.jxta.discovery.DiscoveryEvent; import net.jxta.discovery.DiscoveryListener; import net.jxta.document.AdvertisementFactory; import net.jxta.document.Advertisement; import net.jxta.document.MimeMediaType; import net.jxta.peergroup.PeerGroup; import net.jxta.protocol.DiscoveryResponseMsg; import net.jxta.protocol.PeerAdvertisement; /** * DiscoveryThread. A thread class that continually searches for * advertisements (of a specific type) in one peergroup. Does initial * processing of the Discovery * * @author Robert Wei * @version 1.0 12/17/2002 */ public class DiscoveryThread implements Runnable, DiscoveryListener { MimeMediaType mimeXML = new MimeMediaType("text/xml"); // The interval between discovery requests. Defaults to 10 seconds protected int requestInterval = 10000; /* Parameters for the getRemoteAdvertisements() method * To be overridden in subclasses that define different DiscoveryThreads */ protected String peerID = null; protected int type = DiscoveryService.ADV; protected String attribute = null; protected String value = null; protected int threshold = 10; private PeerGroup home; // Event Listeners private Vector advertisementListeners = new Vector(); /** * Creates a new DiscoveryThread * * @param <code>pg</code> The peergroup in which discovery will be performed */ public DiscoveryThread(PeerGroup pg) { home = pg; } /** * Changes the time between discovery requests. * * @param <code>interval</code> the new request interval. */ public synchronized void setRequestInterval(int interval) { requestInterval = interval; } /** * @return the current requestInterval */ public int getRequestInterval() { return requestInterval; } /** * @return the name of the peergroup */ public String getPeerGroupName() { return home.getPeerGroupName(); } /** * Package-level access only * * @return the peergroup's reference */ PeerGroup getPeerGroup() { return home; } /** * Sends a copy of the event to each registered listener. */ private synchronized void fireAdvertisementEvent(AdvertisementEvent ev) { Iterator i = advertisementListeners.iterator(); while ( i.hasNext() ) { AdvertisementListener listener = (AdvertisementListener) i.next(); listener.advertisementEvent((AdvertisementEvent) ev.clone()); } } /** * Registers a class as an AdvertisementListener */ public synchronized void addAdvertisementListener(AdvertisementListener listener) { advertisementListeners.add(listener); } /** * Creates a new <code>AdvertisementEvent</code> * from a <code>PeerAdvertisement</code> (originating peer) and * an <code>Advertisement<code> (message content) */ protected AdvertisementEvent createAdvertisementEvent(PeerAdvertisement peerAdv, Advertisement adv) { AdvertisementEvent ev = new AdvertisementEvent(this); ev.setOrigin(peerAdv); ev.setAdvertisement(adv); return ev; } /** * Called by DiscoveryService when a discovery is made. This method * repackages the DiscoveryEvent as an AdvertisementEvent and calls * <code>fireAdvertisementEvent()</code> to send it to all registered * listeners. * * @param discoveryEvent the event message recieved from DiscoveryService */ public void discoveryEvent(net.jxta.discovery.DiscoveryEvent discoveryEvent) { DiscoveryResponseMsg res = discoveryEvent.getResponse(); PeerAdvertisement peerAdv; // Get the responding peer's advertisement String peerString = res.getPeerAdv(); // Deserialize the advertisement. try { InputStream is = new ByteArrayInputStream( peerString.getBytes() ); peerAdv = (PeerAdvertisement) AdvertisementFactory.newAdvertisement(mimeXML, is); String logMsg = "Got a Discovery Response [" + res.getResponseCount() + " elements] from peer : " + peerAdv.getName(); } catch (IOException e) { // Invalid peer, skip this message and log a warning return; } // Obtain the enumeration of advertisements Enumeration enum = res.getResponses(); while ( enum.hasMoreElements() ) { String advString = (String) enum.nextElement(); Advertisement adv = null; // Deserialize advertisements try { InputStream is = new ByteArrayInputStream( advString.getBytes() ); adv = AdvertisementFactory.newAdvertisement(mimeXML, is); String logMsg = "Discovered Advertisement: " + adv.getAdvertisementType(); // Wrap the advertisement into an event and fire it AdvertisementEvent event = createAdvertisementEvent(peerAdv, adv); fireAdvertisementEvent(event); } catch (IOException e) { // Unable to read advertisement, log a warning and skip it } } } /** When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see java.lang.Thread#run() * */ public void run() { while ( true ) { DiscoveryService disco = home.getDiscoveryService(); disco.getRemoteAdvertisements(peerID, type, attribute, value, threshold, this); Thread.sleep(requestInterval); } } } |
From: <rw...@us...> - 2002-12-17 06:16:02
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv8956/org/wettp2p/communicationslayer Modified Files: CommunicationsHandler.java Log Message: Fixed a bug that prevented the program from properly constructing the WETT group's URL. Modified the main method to properly shut down the system. Index: CommunicationsHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/communicationslayer/CommunicationsHandler.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** CommunicationsHandler.java 17 Dec 2002 03:39:25 -0000 1.5 --- CommunicationsHandler.java 17 Dec 2002 06:15:58 -0000 1.6 *************** *** 100,108 **** // The globally unique ID for the WETT peergroup (generated on 11/21/2002 by Robert Wei) ! private final String WETT_GID = "urn:jxta:uuid-ECFFFA4E82E348AF9043A8A0844FE5CB02"; private static final String WETT_GROUP_NAME = "WETT"; private static final String WETT_GROUP_DESCRIPTION = "Web Enabled Table Top - Peer-to-Peer"; private static final int TIMEOUT = 3000; private PeerGroup netPeerGroup; private PeerGroup wettPeerGroup; --- 100,110 ---- // The globally unique ID for the WETT peergroup (generated on 11/21/2002 by Robert Wei) ! private final String WETT_GID = "jxta:uuid-ECFFFA4E82E348AF9043A8A0844FE5CB02"; private static final String WETT_GROUP_NAME = "WETT"; private static final String WETT_GROUP_DESCRIPTION = "Web Enabled Table Top - Peer-to-Peer"; private static final int TIMEOUT = 3000; + private MimeMediaType mimeXML = new MimeMediaType("text/xml"); + private PeerGroup netPeerGroup; private PeerGroup wettPeerGroup; *************** *** 114,120 **** public static void main(String args[]) { ! CommunicationsHandler myApp = CommunicationsHandler.getInstance(); ! myApp.startJXTA(); ! myApp.initialize(); } --- 116,130 ---- public static void main(String args[]) { ! CommunicationsHandler myApp = CommunicationsHandler.getInstance(); ! try ! { ! myApp.initialize(); ! myApp.start(); ! } ! finally ! { ! myApp.stop(); ! System.exit(0); ! } } *************** *** 134,137 **** --- 144,148 ---- String str = "ERROR: Unable to join or create Wett peergroup."; logger.severe(str); + e.printStackTrace(System.err); System.err.println(str); System.exit(1); // fail *************** *** 214,218 **** try { ! // get the diescvoer and pipe services for the Wett peergroup wettDiscoveryService = wettPeerGroup.getDiscoveryService(); wettPipeService = wettPeerGroup.getPipeService(); --- 225,229 ---- try { ! // get the discovery and pipe services for the Wett peergroup wettDiscoveryService = wettPeerGroup.getDiscoveryService(); wettPipeService = wettPeerGroup.getPipeService(); *************** *** 420,427 **** } /** - * A default-level access method for other communicationslayer classes to - * obtain a reference to the WETT peergroup. - * * @return the WETT peergroup */ --- 431,439 ---- } + // + // Package level accessors for Communications Layer classes + // + /** * @return the WETT peergroup */ |
From: <rw...@us...> - 2002-12-17 04:00:38
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv8001/org/wettp2p/communicationslayer Added Files: OutboundMessageQueue.java Log Message: The default implementation of an OutboundMessageQueue. Logging code has been included. --- NEW FILE: OutboundMessageQueue.java --- /* * OutboundMessageQueue.java * * Created on December 16, 2002, 6:03 PM */ package org.wettp2p.communicationslayer; import org.wettp2p.messagehandler.Message; import org.wettp2p.messagehandler.MessageQueue; import org.wettp2p.messagehandler.MessageConsumer; import net.jxta.document.MimeMediaType; import net.jxta.peergroup.PeerGroup; import net.jxta.pipe.PipeService; import java.util.*; import java.io.*; import org.wettp2p.peer.*; import java.util.logging.*; /** * This is a message queue (similar to Paulo Mouat's <code>MessageQueue</code> * class) that holds * JXTA Messages (class: <code>net.jxta.endpoint.Message</code>) * instead of * WETT Messages (class: <code>org.wettp2p.messagehandler.Message</code>). * It only recieves * input via its <code>MessageConsumer</code> interface. This default * implementation serializes the entire message into an element titled * <code><Wett:SerialMessage></code>. By overriding the processMessage() * method, it is possible to create JXTA Messages in other ways. * * Suggested Usage: * 1. Create the OutboundMessageQueue around a PeerGroup. The queue represents * the queue of messages associated with that particular peergroup. This * implementation defaults to using the WETT peergroup. * * 2. Subscribe the queue to the desired messagetypes or MessageProducers using * one of the the subscribe() methods. * * 3. Pass the queue as an argument to the constructor of a * <code>Transmitter</code> class that will attempt to send messages off of the * queue. * * * To Do: * 1. Add a method to <code>CommunicationsHandler</code> to allow retrieval of * the WETT peergroup. * * 2. Test this class to make sure that the messages it sends are recognized by * the MessageHandler of the recieving peer. * * 2a. If not, reimplement processMessage to get a valid message. * * * Future Work: * 1. Provide facilities to name instances of this class and possibly retrieve * them by name from a queue. * * * Copyright (C) 2002, Robert Wei * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * @author Robert Wei * @version 1.0, 12/16/2002 **/ public class OutboundMessageQueue extends MessageConsumer.DefaultImpl { /* Logger */ static Logger logger = Logger.getLogger(Peer.TOP_LOG_NAME + ".communicationshandler"); private static String classStr = "OutboundMessageQueue"; private PeerGroup group; private LinkedList queue; /** * Default constructor. * This creates an OutboundMessageQueue for the default WettPeerGroup **/ public OutboundMessageQueue() { // Call the constructor on the WETT peergroup this( CommunicationsHandler.getInstance().getWettGroup() ); } /** * Default constructor. * This creates an OutboundMessageQueue for the default WettPeerGroup **/ public OutboundMessageQueue( PeerGroup pg ) { // Associates this MessageQueue with the supplied peergroup. group = pg; } /** * Accessor method on the peergroup for use by other classes in * communicationslayer. * * @return the <code>PeerGroup</code> that this object is associated with. **/ PeerGroup getPeerGroup() { logger.entering(classStr, "getPeerGroup"); return group; } /** * Public accessor method on the peergroup. * * @return the name of the <code>PeerGroup</code> that this object is * associated with. **/ public String getPeerGroupName() { logger.entering(classStr, "getPeerGroupName"); return group.getPeerGroupName(); } /** * Processes the message into a form that can be transmitted over the JXTA * network. * * Override this method in subclasses to process the message in a different * manner. * * @param message the <code>Message</code>. **/ protected net.jxta.endpoint.Message processMessage( Message message ) throws Exception { logger.entering(classStr, "processMessage"); // Serialize the message into a byte array ByteArrayOutputStream bstream = new ByteArrayOutputStream(); try { ObjectOutputStream ostream = new ObjectOutputStream(bstream); ostream.writeObject(message); } catch (IOException ie) { throw new Exception("Failed to serialize message", ie); } byte[] bytes = bstream.toByteArray(); // Create the MimeType MimeMediaType mimeType = new MimeMediaType("application/octet-stream"); // Create a new message using the group's PipeService net.jxta.endpoint.Message msg = group.getPipeService().createMessage(); // Add the bytes as a new element in the message msg.addElement(msg.newMessageElement("Wett:SerialMessage", mimeType, bytes)); return msg; } /** * The MessageConsumer's DefaultImpl calls this method to process messages * it recieves via the MessageHandler. * * @param message the <code>Message</code>. **/ protected synchronized void doIt( Message wettMessage ) { logger.entering(classStr, "doIt"); net.jxta.endpoint.Message jxtaMessage = null; // Process the message if( wettMessage == null ) // disallow nulls return; try { jxtaMessage = processMessage(wettMessage); } catch (Exception e) { String str = "Message failed to process: Not entered into queue"; logger.warning(str); System.err.println(str); return; } queue.add( jxtaMessage ); } /** * Retrieves and removes the first message from the queue or null if there * are none. * * @return the <code>Message</code> at the start of * the queue or <code>null</code> if the queue is empty. **/ public synchronized net.jxta.endpoint.Message get() { logger.entering(classStr, "get"); net.jxta.endpoint.Message first = null; try { first = (net.jxta.endpoint.Message) queue.removeFirst(); } catch( java.util.NoSuchElementException e ) { System.out.println( this + ": get(), queue is empty!" ); // ignore exception silently } return first; } /** * Retrieves (without removing) the <code>Message</code> * at the start of the queue. This method is synchronized. * @return the <code>Message</code> at the start of * the queue or <code>null</code> if the queue is empty. **/ public synchronized net.jxta.endpoint.Message peek() { logger.entering(classStr, "peek"); net.jxta.endpoint.Message first = null; try { first = (net.jxta.endpoint.Message) queue.getFirst(); } catch( java.util.NoSuchElementException e ) { System.out.println( this + ": peek(), queue is empty!" ); // ignore exception silently } return first; } } |
From: <rw...@us...> - 2002-12-17 03:39:28
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv2830/org/wettp2p/communicationslayer Modified Files: CommunicationsHandler.java Log Message: Added: <default> PeerGroup getWettPeerGroup() Allows other members of the communicationslayer to obtain a reference to the WETT peergroup. Removed: MessageConsumer interface and all associated methods The MessageConsumer is now implemented by OutboundMessageQueue and its subclasses. Index: CommunicationsHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/communicationslayer/CommunicationsHandler.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** CommunicationsHandler.java 15 Dec 2002 00:30:30 -0000 1.4 --- CommunicationsHandler.java 17 Dec 2002 03:39:25 -0000 1.5 *************** *** 6,12 **** * * Phase 1 goals: ! * 1 (Done) - Initialize JXTA / join peergroup: netpeer ! * 2 (Done) - Join peergroup: WETT ! * 3 - Implement the MessageConsumer interface * 4 - Implement the MessageProducer interface * 5 - Send messages to every member of the peergroup --- 6,12 ---- * * Phase 1 goals: ! * 1 (Done - rwei) - Initialize JXTA / join peergroup: netpeer ! * 2 (Done - rwei) - Join peergroup: WETT ! * 3 (Done in <code>OutboundMessageQueue</code> - rwei) - Implement the MessageConsumer interface * 4 - Implement the MessageProducer interface * 5 - Send messages to every member of the peergroup *************** *** 90,94 **** * */ ! public class CommunicationsHandler implements MessageConsumer, MessageProducer { /* Logger */ --- 90,94 ---- * */ ! public class CommunicationsHandler implements MessageProducer { /* Logger */ *************** *** 399,411 **** new URL("urn", "", WETT_GID)); } ! ! public void subscribe(Class type) { ! logger.entering(classStr,"subscribe", type); ! } ! ! public void subscribe(String producerName) { ! logger.entering(classStr,"subscribe", producerName); ! } ! private void createTransmitter(OutputPipe pipe, Message message) { logger.entering(classStr,"createTransmitter"); --- 399,403 ---- new URL("urn", "", WETT_GID)); } ! private void createTransmitter(OutputPipe pipe, Message message) { logger.entering(classStr,"createTransmitter"); *************** *** 417,436 **** /** - * Receives the passed <code>Message</code> from the MessageHandler, - * the module that handles communications within the Wett-p2p - * application. - * - * There should be two types of messages we are concerned with: messages - * bound for other peers and messages that are commands to the - * CommunicationsHander. - * - * @param message the <code>Message</code> to be received. - * - */ - public void receive(Message message) { - logger.entering(classStr,"receive", message); - } - - /** * Sends the passed <code>Message</code> to the MessageHandler which * sends the message to it's appropriate destination based on the class --- 409,412 ---- *************** *** 442,445 **** --- 418,431 ---- public void send(Message message) { logger.entering(classStr,"send", message); + } + + /** + * A default-level access method for other communicationslayer classes to + * obtain a reference to the WETT peergroup. + * + * @return the WETT peergroup + */ + PeerGroup getWettGroup() { + return wettPeerGroup; } |
From: <rb...@us...> - 2002-12-15 00:30:32
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv29583 Modified Files: CommunicationsHandler.java Log Message: I've finished refactoring the original code so that we now have more flexible memethods which will be needed when we use peer groups other than wettPeerGroup. I also added variables to track the currentPeerGroup, currentDiscoveryService and currentPipeService. We still need to add processes, probably seperate classes that will handle messages comming in and going out through the MessageHandler interface and messages going out and coming in through the JXTA interface. Index: CommunicationsHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/communicationslayer/CommunicationsHandler.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** CommunicationsHandler.java 11 Dec 2002 04:28:59 -0000 1.3 --- CommunicationsHandler.java 15 Dec 2002 00:30:30 -0000 1.4 *************** *** 62,83 **** * CommunicationsHandler class. Manages and controls network traffic. * ! * Copyright (C) 2002, Robert Wei, Ray Benjamin * ! * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 ! * of the License, or (at your option) any later version. * ! * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! * GNU General Public License for more details. * ! * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software ! * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * @author Robert Wei ! * @version 0.3, 2002/11/21 * * $Author$ --- 62,84 ---- * CommunicationsHandler class. Manages and controls network traffic. * ! * <p>Copyright (C) 2002, Robert Wei, Ray Benjamin</p> * ! * <p>This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 ! * of the License, or (at your option) any later version.</p> * ! * <p>This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ! * GNU General Public License for more details.</p> * ! * <p>You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software ! * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</p> * * @author Robert Wei ! * @author Ray Benjamin ! * @version 0.5, 12/14/2002 * * $Author$ *************** *** 106,116 **** private PeerGroup netPeerGroup; private PeerGroup wettPeerGroup; private DiscoveryService wettDiscoveryService; private PipeService wettPipeService; public static void main(String args[]) { CommunicationsHandler myApp = CommunicationsHandler.getInstance(); myApp.startJXTA(); ! myApp.createWettGroup(); } --- 107,120 ---- private PeerGroup netPeerGroup; private PeerGroup wettPeerGroup; + private PeerGroup currentPeerGroup; private DiscoveryService wettDiscoveryService; + private DiscoveryService currentDiscoveryService; private PipeService wettPipeService; + private PipeService currentPipeService; public static void main(String args[]) { CommunicationsHandler myApp = CommunicationsHandler.getInstance(); myApp.startJXTA(); ! myApp.initialize(); } *************** *** 123,127 **** startJXTA(); // try to find the WettGroup ! // if it can't be found, create it try { joinWettGroup(); --- 127,131 ---- startJXTA(); // try to find the WettGroup ! // if it can't be found, create it and join it try { joinWettGroup(); *************** *** 133,137 **** System.exit(1); // fail } ! // Join the Wett group. } --- 137,143 ---- System.exit(1); // fail } ! // Create a propogate pipe and a input pipe and advertise them ! // Create the process that searches for and connects to other members ! // of the group we are currently subscribed to. } *************** *** 162,165 **** --- 168,198 ---- /** + * This routine is used to start the CommunicationHandler processes that + * receive, respond to, and forward messages. + */ + private void start() { + logger.entering(classStr,"start"); + // create input pipe + // discover peers + // discover peer input pipes + // construct output pipe connected to peer input pipes + // send greeting message + // forward peer information to main application + } + + /** + * This methods stops the CommunicationHander processes that receive, + * respond to, and forward messages. + */ + private void stop() { + logger.entering(classStr,"stop"); + // Notify peers of pending shutdown + // shutdown propogate pipe + // shutdown input pipe + // At this point, the CommunicationsHandler is shutdown and may be + // stopped cleanly. + } + + /** * Joins the WettGroup if it exists and creates and joins the group if it * does not exist. *************** *** 170,176 **** logger.info("Attempting to Discover the WettGroup."); // get the discovery service. ! DiscoveryService discoveryService = netPeerGroup.getDiscoveryService(); ! Enumeration ae = null; // holds discovered peers // Loop until we discover WettGroup or until we've exhausted the --- 203,308 ---- logger.info("Attempting to Discover the WettGroup."); + wettPeerGroup = findPeerGroup(netPeerGroup,WETT_GROUP_NAME); + if (wettPeerGroup == null) { + wettPeerGroup = createPeerGroup(netPeerGroup, + WETT_GROUP_NAME, + WETT_GROUP_DESCRIPTION, + mkWettGroupID()); + } + + currentPeerGroup = wettPeerGroup; + + try { + // get the diescvoer and pipe services for the Wett peergroup + wettDiscoveryService = wettPeerGroup.getDiscoveryService(); + wettPipeService = wettPeerGroup.getPipeService(); + + currentDiscoveryService = wettDiscoveryService; + currentPipeService = wettPipeService; + } + catch (Exception e) { + String str = "Error getting services from Wett peergroup"; + logger.severe(str); + System.err.println(str); + throw e; + } + + logger.info("Web Enabled Table Top - Peer-to-Peer is on-line!"); + } // end joinWettGroup() + + /** + * This routine will search for the given group and join it if found or + * create it and join it if it's not found. + * + * @param groupName A <code>String</code> containing the name of the + * desired group. + * + * @param groupDescription A <code>String</code> describing the group. + * + * @param groupID A <code>PeerGroupID</code> associated with the group. + * + * @return A <code>PeerGroup</code> object containing the information about the + * peer group. + */ + private PeerGroup findOrCreateGroup(PeerGroup parentGroup, + String groupName, + String groupDescription, + PeerGroupID groupID) + throws Exception + { + PeerGroup peerGroup = null; + logger.entering(classStr, "findOrCreateGroup"); + + peerGroup = findPeerGroup(parentGroup,groupName); + + if (peerGroup == null) { + peerGroup = createPeerGroup(parentGroup, groupName, groupDescription, groupID); + } + + return peerGroup; + } // end method + + private void moveToPeerGroup(PeerGroup peerGroup) throws Exception { + logger.entering(classStr, "moveToPeerGroup"); + + logger.info("Moving from " + currentPeerGroup.getPeerGroupName() + + " to " + peerGroup.getPeerGroupName() + "."); + currentPeerGroup = peerGroup; + try { + // get the diescvoer and pipe services for the Wett peergroup + currentDiscoveryService = currentPeerGroup.getDiscoveryService(); + currentPipeService = currentPeerGroup.getPipeService(); + } + catch (Exception e) { + String str = "Error getting services from Peer Group " + + currentPeerGroup.getPeerGroupName() + "."; + logger.severe(str); + System.err.println(str); + throw e; + } + } + + /** + * This method searches for the peer group named in the argument + * and returns the group if found or null if the group is not found. + * + * @param <code>groupName</code> a <code>String</code> containing the name + * of the group we are looking for. + * + * @return A reference to the <code>PeerGroup</code> found, it it's found + * or <code>null</code>. + */ + private PeerGroup findPeerGroup(PeerGroup parentGroup, + String groupName) + throws Exception + { + logger.entering(classStr, "findPeerGroup"); + PeerGroup peerGroup = null; + int count = DEFAULT_RETRY_COUNT; + logger.info("Attempting to Discover the " + groupName + " peer group."); + // get the discovery service. ! DiscoveryService discoveryService = parentGroup.getDiscoveryService(); ! Enumeration ae = null; // holds discovered groups // Loop until we discover WettGroup or until we've exhausted the *************** *** 181,185 **** // the Wett peergroup advertisement ae = discoveryService.getLocalAdvertisements( ! DiscoveryService.GROUP, "Name", WETT_GROUP_NAME); // If we found the advetisement, we are done --- 313,317 ---- // the Wett peergroup advertisement ae = discoveryService.getLocalAdvertisements( ! DiscoveryService.GROUP, "Name", groupName); // If we found the advetisement, we are done *************** *** 189,193 **** // If we did not find it, we send a discovery request discoveryService.getRemoteAdvertisements(null, ! DiscoveryService.GROUP, "Name", WETT_GROUP_NAME, 1, null); // Sleep to allow time for peers to respond to the discovery --- 321,325 ---- // If we did not find it, we send a discovery request discoveryService.getRemoteAdvertisements(null, ! DiscoveryService.GROUP, "Name", groupName, 1, null); // Sleep to allow time for peers to respond to the discovery *************** *** 205,319 **** } // end while - PeerGroupAdvertisement wettGroupAdv = null; - - // See if we found the Wett Group advertisement. If we didn't, - // then either we are the first peer to join or no other peers are up. - // In either case, we must create the group. if (ae == null || !ae.hasMoreElements()) { ! logger.info("Could not find the Wett peergroup; creating one"); ! ! try { ! ! // create a new, all-purpose peergroup ! ModuleImplAdvertisement implAdv = ! netPeerGroup.getAllPurposePeerGroupImplAdvertisement(); ! ! wettPeerGroup = netPeerGroup.newGroup(mkGroupID(), ! implAdv, ! WETT_GROUP_NAME, ! WETT_GROUP_DESCRIPTION); ! ! // Get the peer group advertisement ! wettGroupAdv = netPeerGroup.getPeerGroupAdvertisement(); ! } ! catch (Exception e) { ! String str = "Error in creating WETT peergroup."; ! logger.severe(str); ! System.err.println(str); ! throw e; ! } } else { ! // we found the advertisement in the cache; ! // We can join the existing wettPeerGroup. ! ! try { ! wettGroupAdv = (PeerGroupAdvertisement) ae.nextElement(); ! wettPeerGroup = netPeerGroup.newGroup(wettGroupAdv); ! logger.info("Found the WETT Peer Group advertisement; " + ! "joined the existing group."); ! } ! catch (Exception e) { ! String str = "Error in creating Wett peergroup " + ! "from existing advertisement."; ! logger.severe(str); ! System.err.println(str); ! throw e; ! } ! } // end if (wett group adv found) ! ! try { ! // get the diescvoer and pipe services for the Wett peergroup ! wettDiscoveryService = wettPeerGroup.getDiscoveryService(); ! wettPipeService = wettPeerGroup.getPipeService(); ! } ! catch (Exception e) { ! String str = "Error getting services from Wett peergroup"; ! logger.severe(str); ! System.err.println(str); ! throw e; } ! ! logger.info("Web Enabled Table Top - Peer-to-Peer is on-line!"); ! } // end joinWettGroup() ! ! /** ! * Creates and joins the WettPeerGroup. ! */ ! private void createWettGroup() { ! logger.entering(classStr,"createWettGroup"); ! PeerGroupAdvertisement adv = null; ! logger.info("Attempting to create the WETT peergroup"); ! try { ! // Creates an all purpose peergroup advertisement ! ModuleImplAdvertisement implAdv = netPeerGroup.getAllPurposePeerGroupImplAdvertisement(); ! ! // Create the WETT peergroup as a child to netPeer ! wettPeerGroup = netPeerGroup.newGroup((PeerGroupID) IDFactory.fromURL(new URL(WETT_GID)), implAdv, "WETT", "Web-Enabled Table Top"); ! ! // Retrieve the advertisement for the wett peergroup ! adv = wettPeerGroup.getPeerGroupAdvertisement(); ! ! logger.info("WETT peergroup successfully created"); ! } ! catch (Exception e) { ! String str = "Failed to create the WETT group: " + e; ! logger.severe(str); ! System.err.println(str); ! e.printStackTrace(); ! System.exit(1); ! } ! try { ! // Publish the advertisement using the discovery service of netPeer ! DiscoveryService disco = netPeerGroup.getDiscoveryService(); ! disco.remotePublish(adv, DiscoveryService.GROUP); ! logger.info("WETT peergroup successfully published"); } ! catch (Exception ee) { ! String str = "Failed to publish the WETT group: " + ee; logger.severe(str); System.err.println(str); ! ee.printStackTrace(); ! System.exit(1); } } ! private PeerGroupID mkGroupID() throws Exception { return (PeerGroupID) IDFactory.fromURL( new URL("urn", "", WETT_GID)); --- 337,399 ---- } // end while if (ae == null || !ae.hasMoreElements()) { ! // The group wasn't found ! logger.fine("Group " + groupName + " not found."); ! peerGroup = null; } else { ! PeerGroupAdvertisement peerGroupAdv = (PeerGroupAdvertisement) ae.nextElement(); ! peerGroup = parentGroup.newGroup(peerGroupAdv); ! logger.fine("Group " + groupName + " created."); } ! return peerGroup; ! } ! private PeerGroup createPeerGroup(PeerGroup parentGroup, ! String groupName, ! String groupDescription, ! PeerGroupID groupID) ! throws Exception ! { ! logger.entering(classStr, "createPeerGroup"); ! PeerGroupAdvertisement peerGroupAdv = null; ! PeerGroup peerGroup = null; ! logger.info("Creating the " + groupName + " Peer Group."); ! if (groupID == null) { ! // create a group id ! groupID = IDFactory.newPeerGroupID(); // create new random peer group ID ! logger.finer("Generated GroupID " + groupID.toString() + ! " created for Group " + groupName + "."); ! } ! try { ! ! // create a new, all-purpose peergroup ! ModuleImplAdvertisement implAdv = ! parentGroup.getAllPurposePeerGroupImplAdvertisement(); ! ! peerGroup = parentGroup.newGroup(groupID, ! implAdv, ! groupName, ! groupDescription); ! ! // Get the peer group advertisement ! peerGroupAdv = parentGroup.getPeerGroupAdvertisement(); } ! catch (Exception e) { ! String str = "Error in creating peergroup."; logger.severe(str); System.err.println(str); ! throw e; } + logger.info("Peer Group, " + groupName + ", created."); + return peerGroup; } ! private PeerGroupID mkWettGroupID() throws Exception { return (PeerGroupID) IDFactory.fromURL( new URL("urn", "", WETT_GID)); |
From: <rb...@us...> - 2002-12-15 00:27:55
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv29120 Added Files: CommunicationsHandlerDesign.html Log Message: Start of initial draft of a continuing design for the CommunicationHandler class and classes that will work with it. It will document the control messages that are used by the application to control the CommunicationsLayer. --- NEW FILE: CommunicationsHandlerDesign.html --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <TITLE>CommunicationsHandler Design</TITLE> </HEAD> <BODY> <h1>CommunicationsHandler Design</H1> <h2>Introduction</h2> <p>The CommunicationsHandler (CH) provides an interface between the JXTA communications layer and the MessageHandler which handles messaging for the rest of the application. The CH sets up and controls the peer groups, pipes and other JXTA structures and presents a simplified view of the network to the rest of the application. The rest of the appication just sees a module that accepts and produces messages just like any other part of the application. The CH itself is controled via messages sent to it through the MessageHandler.</p> <h2>CommunicationsHandler Controls</h2> <p>The CH only has a very few public methods: a getInstance() method, an initialize() method, and a few others. Almost all interaction with the CH is done via messages that are sent via the MessageHandler.</p> <h3>Control Messages</h3> <ul> <li>Start Communications Processes</li> <li>Stop Communications Processes</li> <li>Change Peer Group</li> <li>Leave Peer Group</li> <li>Retransmit Message or Message Block</li> </ul> </BODY> </HTML> |
From: <rb...@us...> - 2002-12-11 04:29:02
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv25492 Modified Files: CommunicationsHandler.java Log Message: Changed the logic used to join the WETT peer group so that the application will join the group if it already exists or create the group if it doesn't. Index: CommunicationsHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/communicationslayer/CommunicationsHandler.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** CommunicationsHandler.java 10 Dec 2002 21:21:16 -0000 1.2 --- CommunicationsHandler.java 11 Dec 2002 04:28:59 -0000 1.3 *************** *** 53,56 **** --- 53,59 ---- import net.jxta.id.IDFactory; + import java.io.IOException; + import java.net.*; + import java.util.*; import org.wettp2p.peer.*; import java.util.logging.*; *************** *** 77,80 **** --- 80,91 ---- * @author Robert Wei * @version 0.3, 2002/11/21 + * + * $Author$ + * $Date$ + * $Name$ + * $Locker$ + * $Revision$ + * $State$ + * */ public class CommunicationsHandler implements MessageConsumer, MessageProducer { *************** *** 83,93 **** --- 94,111 ---- static Logger logger = Logger.getLogger(Peer.TOP_LOG_NAME + ".communicationshandler"); + private static final int DEFAULT_RETRY_COUNT = 3; + private static String classStr = "CommunicationsHandler"; // The globally unique ID for the WETT peergroup (generated on 11/21/2002 by Robert Wei) private final String WETT_GID = "urn:jxta:uuid-ECFFFA4E82E348AF9043A8A0844FE5CB02"; + private static final String WETT_GROUP_NAME = "WETT"; + private static final String WETT_GROUP_DESCRIPTION = "Web Enabled Table Top - Peer-to-Peer"; + private static final int TIMEOUT = 3000; private PeerGroup netPeerGroup; private PeerGroup wettPeerGroup; + private DiscoveryService wettDiscoveryService; + private PipeService wettPipeService; public static void main(String args[]) { *************** *** 104,108 **** logger.entering(classStr, "initialize"); startJXTA(); ! createWettGroup(); } --- 122,137 ---- logger.entering(classStr, "initialize"); startJXTA(); ! // try to find the WettGroup ! // if it can't be found, create it ! try { ! joinWettGroup(); ! } ! catch (Exception e) { ! String str = "ERROR: Unable to join or create Wett peergroup."; ! logger.severe(str); ! System.err.println(str); ! System.exit(1); // fail ! } ! // Join the Wett group. } *************** *** 113,126 **** logger.entering(classStr, "startJXTA"); try { ! System.out.println("Attempting to initialize JXTA"); netPeerGroup = PeerGroupFactory.newNetPeerGroup(); ! System.out.println("JXTA started"); ! System.out.println("Joined Peergroup: " + netPeerGroup.getPeerGroupName()); } catch (Exception e) { // Failed to initialize JXTA, print error message and quit. ! System.out.print("JXTA initialization failed due to exception: "); ! System.out.println(e); e.printStackTrace(); System.exit(1); } --- 142,160 ---- logger.entering(classStr, "startJXTA"); try { ! logger.fine("Attempting to initialize JXTA"); netPeerGroup = PeerGroupFactory.newNetPeerGroup(); ! logger.fine("JXTA started"); ! logger.fine("Joined Peergroup: " + netPeerGroup.getPeerGroupName()); } catch (Exception e) { // Failed to initialize JXTA, print error message and quit. ! String str = "Error: JXTA initialization failed due to exception: "; ! logger.severe(str); ! System.err.println(str); ! logger.severe(e.toString()); ! System.err.println(e.toString()); e.printStackTrace(); + // NOTE: should find someway to insure that stacktrace is written into + // log... System.exit(1); } *************** *** 128,131 **** --- 162,273 ---- /** + * Joins the WettGroup if it exists and creates and joins the group if it + * does not exist. + */ + private void joinWettGroup() throws Exception { + logger.entering(classStr, "joinWettGroup"); + int count = DEFAULT_RETRY_COUNT; + logger.info("Attempting to Discover the WettGroup."); + + // get the discovery service. + DiscoveryService discoveryService = netPeerGroup.getDiscoveryService(); + Enumeration ae = null; // holds discovered peers + + // Loop until we discover WettGroup or until we've exhausted the + // desired number of attempts + while(count-- > 0) { + try { + // Search first in the peer's local cache to find + // the Wett peergroup advertisement + ae = discoveryService.getLocalAdvertisements( + DiscoveryService.GROUP, "Name", WETT_GROUP_NAME); + + // If we found the advetisement, we are done + if ((ae != null) && ae.hasMoreElements()) + break; + + // If we did not find it, we send a discovery request + discoveryService.getRemoteAdvertisements(null, + DiscoveryService.GROUP, "Name", WETT_GROUP_NAME, 1, null); + + // Sleep to allow time for peers to respond to the discovery + // request. + try { + Thread.sleep(TIMEOUT); + } + catch (InterruptedException ie) { + // do nothing + } + } + catch (IOException e) { + // found nothing + } + } // end while + + PeerGroupAdvertisement wettGroupAdv = null; + + // See if we found the Wett Group advertisement. If we didn't, + // then either we are the first peer to join or no other peers are up. + // In either case, we must create the group. + + if (ae == null || !ae.hasMoreElements()) { + logger.info("Could not find the Wett peergroup; creating one"); + + try { + + // create a new, all-purpose peergroup + ModuleImplAdvertisement implAdv = + netPeerGroup.getAllPurposePeerGroupImplAdvertisement(); + + wettPeerGroup = netPeerGroup.newGroup(mkGroupID(), + implAdv, + WETT_GROUP_NAME, + WETT_GROUP_DESCRIPTION); + + // Get the peer group advertisement + wettGroupAdv = netPeerGroup.getPeerGroupAdvertisement(); + } + catch (Exception e) { + String str = "Error in creating WETT peergroup."; + logger.severe(str); + System.err.println(str); + throw e; + } + } + else { + // we found the advertisement in the cache; + // We can join the existing wettPeerGroup. + + try { + wettGroupAdv = (PeerGroupAdvertisement) ae.nextElement(); + wettPeerGroup = netPeerGroup.newGroup(wettGroupAdv); + logger.info("Found the WETT Peer Group advertisement; " + + "joined the existing group."); + } + catch (Exception e) { + String str = "Error in creating Wett peergroup " + + "from existing advertisement."; + logger.severe(str); + System.err.println(str); + throw e; + } + } // end if (wett group adv found) + + try { + // get the diescvoer and pipe services for the Wett peergroup + wettDiscoveryService = wettPeerGroup.getDiscoveryService(); + wettPipeService = wettPeerGroup.getPipeService(); + } + catch (Exception e) { + String str = "Error getting services from Wett peergroup"; + logger.severe(str); + System.err.println(str); + throw e; + } + + logger.info("Web Enabled Table Top - Peer-to-Peer is on-line!"); + } // end joinWettGroup() + + /** * Creates and joins the WettPeerGroup. */ *************** *** 135,139 **** PeerGroupAdvertisement adv = null; ! System.out.println("Attempting to create the WETT peergroup"); try { --- 277,281 ---- PeerGroupAdvertisement adv = null; ! logger.info("Attempting to create the WETT peergroup"); try { *************** *** 147,154 **** adv = wettPeerGroup.getPeerGroupAdvertisement(); ! System.out.println("WETT peergroup successfully created"); } catch (Exception e) { ! System.out.println("Failed to create the WETT group: " + e); e.printStackTrace(); System.exit(1); --- 289,298 ---- adv = wettPeerGroup.getPeerGroupAdvertisement(); ! logger.info("WETT peergroup successfully created"); } catch (Exception e) { ! String str = "Failed to create the WETT group: " + e; ! logger.severe(str); ! System.err.println(str); e.printStackTrace(); System.exit(1); *************** *** 160,167 **** disco.remotePublish(adv, DiscoveryService.GROUP); ! System.out.println("WETT peergroup successfully published"); } catch (Exception ee) { ! System.out.println("Failed to publish the WETT group: " + ee); ee.printStackTrace(); System.exit(1); --- 304,313 ---- disco.remotePublish(adv, DiscoveryService.GROUP); ! logger.info("WETT peergroup successfully published"); } catch (Exception ee) { ! String str = "Failed to publish the WETT group: " + ee; ! logger.severe(str); ! System.err.println(str); ee.printStackTrace(); System.exit(1); *************** *** 169,172 **** --- 315,323 ---- } + private PeerGroupID mkGroupID() throws Exception { + return (PeerGroupID) IDFactory.fromURL( + new URL("urn", "", WETT_GID)); + } + public void subscribe(Class type) { logger.entering(classStr,"subscribe", type); *************** *** 236,238 **** --- 387,390 ---- { } + } |
From: <rb...@us...> - 2002-12-10 21:21:19
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv16607 Modified Files: CommunicationsHandler.java Log Message: I added some logging code to CommunicationsHandler and an initialize method which is called by Peer to initialize the module. Index: CommunicationsHandler.java =================================================================== RCS file: /cvsroot/wett-p2p/org/wettp2p/communicationslayer/CommunicationsHandler.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CommunicationsHandler.java 21 Nov 2002 19:27:20 -0000 1.1 --- CommunicationsHandler.java 10 Dec 2002 21:21:16 -0000 1.2 *************** *** 2,5 **** --- 2,8 ---- * CommunicationsHandler.java * + * This module handles communications between the MessageHandler and the JXTA + * network and all other peers. + * * Phase 1 goals: * 1 (Done) - Initialize JXTA / join peergroup: netpeer *************** *** 9,12 **** --- 12,17 ---- * 5 - Send messages to every member of the peergroup * 6 - Expose an input pipe to recieve messages from the peergroup + * 8 - (Done - rben) Add Initialization method to be used in initializing + * communications. */ *************** *** 48,55 **** import net.jxta.id.IDFactory; /** * CommunicationsHandler class. Manages and controls network traffic. * ! * Copyright (C) 2002, Paulo Mouat * * This program is free software; you can redistribute it and/or --- 53,63 ---- import net.jxta.id.IDFactory; + import org.wettp2p.peer.*; + import java.util.logging.*; + /** * CommunicationsHandler class. Manages and controls network traffic. * ! * Copyright (C) 2002, Robert Wei, Ray Benjamin * * This program is free software; you can redistribute it and/or *************** *** 72,75 **** --- 80,88 ---- public class CommunicationsHandler implements MessageConsumer, MessageProducer { + /* Logger */ + static Logger logger = Logger.getLogger(Peer.TOP_LOG_NAME + ".communicationshandler"); + + private static String classStr = "CommunicationsHandler"; + // The globally unique ID for the WETT peergroup (generated on 11/21/2002 by Robert Wei) private final String WETT_GID = "urn:jxta:uuid-ECFFFA4E82E348AF9043A8A0844FE5CB02"; *************** *** 85,91 **** --- 98,115 ---- /** + * This routine performs initialization of the communications interface. + * + */ + public void initialize() { + logger.entering(classStr, "initialize"); + startJXTA(); + createWettGroup(); + } + + /** * Initializes the JXTA platform and joins the default netPeer group. **/ private void startJXTA() { + logger.entering(classStr, "startJXTA"); try { System.out.println("Attempting to initialize JXTA"); *************** *** 107,110 **** --- 131,135 ---- */ private void createWettGroup() { + logger.entering(classStr,"createWettGroup"); PeerGroupAdvertisement adv = null; *************** *** 145,171 **** public void subscribe(Class type) { } public void subscribe(String producerName) { } private void createTransmitter(OutputPipe pipe, Message message) { } private void createReceiver(InputPipe pipe) { } ! /** Receives the passed <code>Message</code>. * @param message the <code>Message</code> to be received. * */ public void receive(Message message) { } ! /** Sends the passed <code>Message</code>. * @param message the <code>Message</code> to be sent. * */ public void send(Message message) { } --- 170,214 ---- public void subscribe(Class type) { + logger.entering(classStr,"subscribe", type); } public void subscribe(String producerName) { + logger.entering(classStr,"subscribe", producerName); } private void createTransmitter(OutputPipe pipe, Message message) { + logger.entering(classStr,"createTransmitter"); } private void createReceiver(InputPipe pipe) { + logger.entering(classStr,"createReceiver", pipe); } ! /** ! * Receives the passed <code>Message</code> from the MessageHandler, ! * the module that handles communications within the Wett-p2p ! * application. ! * ! * There should be two types of messages we are concerned with: messages ! * bound for other peers and messages that are commands to the ! * CommunicationsHander. ! * * @param message the <code>Message</code> to be received. * */ public void receive(Message message) { + logger.entering(classStr,"receive", message); } ! /** ! * Sends the passed <code>Message</code> to the MessageHandler which ! * sends the message to it's appropriate destination based on the class ! * of the message. ! * * @param message the <code>Message</code> to be sent. * */ public void send(Message message) { + logger.entering(classStr,"send", message); } |
From: <rw...@us...> - 2002-11-21 19:27:23
|
Update of /cvsroot/wett-p2p/org/wettp2p/communicationslayer In directory sc8-pr-cvs1:/tmp/cvs-serv12095/org/wettp2p/communicationslayer Added Files: CommunicationsHandler.java Log Message: CommunicationsHandler.java Implemented as a singleton class startJXTA() - Starts the NetPeerGroup and the JXTA platform createWettGroup() - Creates and publishes the unique WETT peergroup. Much work still needs to be done implementing the messagehandler interfaces as well as actual communication over JXTA. --- NEW FILE: CommunicationsHandler.java --- /* * CommunicationsHandler.java * * Phase 1 goals: * 1 (Done) - Initialize JXTA / join peergroup: netpeer * 2 (Done) - Join peergroup: WETT * 3 - Implement the MessageConsumer interface * 4 - Implement the MessageProducer interface * 5 - Send messages to every member of the peergroup * 6 - Expose an input pipe to recieve messages from the peergroup */ package org.wettp2p.communicationslayer; import org.wettp2p.messagehandler.MessageConsumer; import org.wettp2p.messagehandler.MessageProducer; import org.wettp2p.messagehandler.Message; import java.io.InputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.URL; import java.util.Enumeration; import net.jxta.peergroup.PeerGroup; import net.jxta.peergroup.PeerGroupID; import net.jxta.peergroup.PeerGroupFactory; import net.jxta.exception.PeerGroupException; import net.jxta.document.Advertisement; import net.jxta.document.AdvertisementFactory; import net.jxta.document.MimeMediaType; import net.jxta.discovery.DiscoveryEvent; import net.jxta.discovery.DiscoveryListener; import net.jxta.discovery.DiscoveryService; import net.jxta.protocol.DiscoveryResponseMsg; import net.jxta.protocol.PeerGroupAdvertisement; import net.jxta.protocol.PeerAdvertisement; import net.jxta.protocol.ModuleImplAdvertisement; import net.jxta.pipe.PipeService; import net.jxta.pipe.InputPipe; import net.jxta.pipe.OutputPipe; import net.jxta.id.IDFactory; /** * CommunicationsHandler class. Manages and controls network traffic. * * Copyright (C) 2002, Paulo Mouat * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * @author Robert Wei * @version 0.3, 2002/11/21 */ public class CommunicationsHandler implements MessageConsumer, MessageProducer { // The globally unique ID for the WETT peergroup (generated on 11/21/2002 by Robert Wei) private final String WETT_GID = "urn:jxta:uuid-ECFFFA4E82E348AF9043A8A0844FE5CB02"; private PeerGroup netPeerGroup; private PeerGroup wettPeerGroup; public static void main(String args[]) { CommunicationsHandler myApp = CommunicationsHandler.getInstance(); myApp.startJXTA(); myApp.createWettGroup(); } /** * Initializes the JXTA platform and joins the default netPeer group. **/ private void startJXTA() { try { System.out.println("Attempting to initialize JXTA"); netPeerGroup = PeerGroupFactory.newNetPeerGroup(); System.out.println("JXTA started"); System.out.println("Joined Peergroup: " + netPeerGroup.getPeerGroupName()); } catch (Exception e) { // Failed to initialize JXTA, print error message and quit. System.out.print("JXTA initialization failed due to exception: "); System.out.println(e); e.printStackTrace(); System.exit(1); } } /** * Creates and joins the WettPeerGroup. */ private void createWettGroup() { PeerGroupAdvertisement adv = null; System.out.println("Attempting to create the WETT peergroup"); try { // Creates an all purpose peergroup advertisement ModuleImplAdvertisement implAdv = netPeerGroup.getAllPurposePeerGroupImplAdvertisement(); // Create the WETT peergroup as a child to netPeer wettPeerGroup = netPeerGroup.newGroup((PeerGroupID) IDFactory.fromURL(new URL(WETT_GID)), implAdv, "WETT", "Web-Enabled Table Top"); // Retrieve the advertisement for the wett peergroup adv = wettPeerGroup.getPeerGroupAdvertisement(); System.out.println("WETT peergroup successfully created"); } catch (Exception e) { System.out.println("Failed to create the WETT group: " + e); e.printStackTrace(); System.exit(1); } try { // Publish the advertisement using the discovery service of netPeer DiscoveryService disco = netPeerGroup.getDiscoveryService(); disco.remotePublish(adv, DiscoveryService.GROUP); System.out.println("WETT peergroup successfully published"); } catch (Exception ee) { System.out.println("Failed to publish the WETT group: " + ee); ee.printStackTrace(); System.exit(1); } } public void subscribe(Class type) { } public void subscribe(String producerName) { } private void createTransmitter(OutputPipe pipe, Message message) { } private void createReceiver(InputPipe pipe) { } /** Receives the passed <code>Message</code>. * @param message the <code>Message</code> to be received. * */ public void receive(Message message) { } /** Sends the passed <code>Message</code>. * @param message the <code>Message</code> to be sent. * */ public void send(Message message) { } // // singleton interface // /** the one and only instance of this class */ private static CommunicationsHandler instance = new CommunicationsHandler(); /** * Static method that returns the single instance of this class. * @return the single instance of this class. **/ public static CommunicationsHandler getInstance() { return instance; } /** * Singleton class, so the default constructor * is made private. **/ private CommunicationsHandler() { } } |