📄 gossipservice.java
字号:
/* * Copyright (c) 2004-2007 Sun Microsystems, Inc. All rights reserved. * * The Sun Project JXTA(TM) Software License * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: "This product includes software * developed by Sun Microsystems, Inc. for JXTA(TM) technology." * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must * not be used to endorse or promote products derived from this software * without prior written permission. For written permission, please contact * Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", nor may * "JXTA" appear in their name, without prior written permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * JXTA is a registered trademark of Sun Microsystems, Inc. in the United * States and other countries. * * Please see the license information page at : * <http://www.jxta.org/project/www/license.html> for instructions on use of * the license in source files. * * ==================================================================== * * This software consists of voluntary contributions made by many individuals * on behalf of Project JXTA. For more information on Project JXTA, please see * http://www.jxta.org. * * This license is based on the BSD license adopted by the Apache Foundation. */package tutorial.customgroupservice;import java.io.IOException;import java.net.URI;import java.util.NoSuchElementException;import java.util.Timer;import java.util.TimerTask;import java.util.logging.Level;import java.util.logging.Logger;import net.jxta.document.Advertisement;import net.jxta.document.AdvertisementFactory;import net.jxta.document.XMLDocument;import net.jxta.endpoint.EndpointAddress;import net.jxta.endpoint.EndpointService;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.StringMessageElement;import net.jxta.exception.PeerGroupException;import net.jxta.id.ID;import net.jxta.logging.Logging;import net.jxta.platform.ModuleSpecID;import net.jxta.peergroup.PeerGroup;import net.jxta.platform.Module;import net.jxta.platform.ModuleClassID;import net.jxta.protocol.ConfigParams;import net.jxta.protocol.ModuleImplAdvertisement;import net.jxta.service.Service;/** * A very simple Peer Group Service. * <p/> * This service sends JXTA an annoucement message every few seconds via * JXTA endpoint service <tt>propagate()</tt>. It also listens for announcement * messages from other peers and prints a message on the console whenever it * receives one. * <p/> * The protocol for this service consists of JXTA messages sent via Endpoint * propagation. This gossip service implementation uses the <tt>assignedID</tt> * which is initialized in the <tt>init()</tt> method as the endpoint address * for the messages it sends and receives. Use of the <tt>assignedID</tt> as * the <tt>serviceParam</tt> is a common choice because it is gauranteed to be * unique within the PeerGroup and the <tt>assignedID</tt> * <tt>serviceParam</tt> is informally reserved for the service with that * <tt>assignedID</tt>. * <p/>The messages exchanged by the gossip service contain two message * elements in the "<tt>gossip</tt>" namespace. "<tt>sender</tt>" contains a * <tt>String</tt> of the peer id of the message sender. "<tt>gossip</tt>" * contains a <tt>String</tt> of the gossip text which is being shared by the * sender. */public class GossipService implements net.jxta.service.Service, net.jxta.endpoint.EndpointListener { /** * Logger */ private static final transient Logger LOG = Logger.getLogger(GossipService.class.getName()); /** * The module class ID for Gossip services. All Gossip services regardless * of the protocol used share this same module class id. */ public static final ModuleClassID GOSSIP_SERVICE_MCID = ModuleClassID.create(URI.create("urn:jxta:uuid-4CD1574ABA614A5FA242B613D8BAA30F05")); /** * The module spec ID for our Gossip service. The module spec id contains * the {@code GOSSIP_SERVICE_MCID}. All implementations which use the * same messaging protocol as this implementation will share this same * module spec id. */ public static final ModuleSpecID GOSSIP_SERVICE_MSID = ModuleSpecID.create(URI.create("urn:jxta:uuid-4CD1574ABA614A5FA242B613D8BAA30FD0A45F5F0E1A450DA912BB01585AB0FC06")); /** * The default gossip text we will send to other peers. */ public static final String DEFAULT_GOSSIP_TEXT = "JXTA is cool. Pass it on!"; /** * Whether we should show our own gossip text default. */ public static final boolean DEFAULT_SHOW_OWN = false; /** * The default interval in milliseconds at which we will send our gossip * text. */ public static final long GOSSIP_INTERVAL_DEFAULT = 10 * 1000L; /** * The name of the message namespace for all gossip service messages. */ public static final String GOSSIP_NAMESPACE = "gossip"; /** * The name of the message element identifying the gossip sender. */ public static final String GOSSIP_SENDER_ELEMENT_NAME = "sender"; /** * The name of the message element containing the gossip text. */ public static final String GOSSIP_GOSSIP_ELEMENT_NAME = "gossip"; /** * A Timer shared between all Gossip service instances that we use for * sending our gossip messages. */ public static final Timer SHARED_TIMER = new Timer("Gossip Services Timer", true); /** * The peer group in which this instance is running. */ private PeerGroup group; /** * Our assigned service ID. Usually this is our MCID but may also be our * MCID with a role id if there are multiple gossip services within the * peer group. */ private ID assignedID; /** * The module implementation advertisement for our instance. */ private ModuleImplAdvertisement implAdv; /** * The "gossip" message we read from our configuration. */ private String gossip = DEFAULT_GOSSIP_TEXT; /** * If {@code true} then we show our own gossip messages; */ private boolean showOwn = DEFAULT_SHOW_OWN; /** * The interval in milliseconds at which we will send our gossip message. */ private long gossip_interval = GOSSIP_INTERVAL_DEFAULT; /** * The endpoint service with which we send our gossips and register our * listener. */ private EndpointService endpoint = null; /** * The timer task we use to send our gossip messages. */ private TimerTask sendTask = null; /** * {@inheritDoc} * <p/> * This implementation doesn't currently use interface objects so it just * returns itself. We would use an interface object if we needed to maintain * state for each caller of the Gossip Service or wished to attach a * security context to the callers of this service. ie. different callers * might have different sercurity privleges. */ public Service getInterface() { return this; } /** * Return our assigned ID. * * @return Our assigned ID. */ public ID getAssignedID() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -