2 * Copyright (c) 2002-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
56 package net.jxta.impl.rendezvous.limited;
58 import java.io.IOException;
60 import java.util.logging.Level;
62 import net.jxta.logging.Logging;
64 import java.util.logging.Logger;
66 import net.jxta.endpoint.EndpointAddress;
67 import net.jxta.endpoint.EndpointListener;
68 import net.jxta.endpoint.EndpointService;
69 import net.jxta.endpoint.Message;
71 import net.jxta.impl.protocol.LimitedRangeRdvMsg;
72 import net.jxta.impl.rendezvous.RdvGreeter;
73 import net.jxta.impl.rendezvous.rpv.PeerViewElement;
76 * The limited range rendezvous peer greeter.
78 * @see net.jxta.impl.rendezvous.RdvGreeter
79 * @see net.jxta.impl.rendezvous.RdvWalk
80 * @see net.jxta.impl.rendezvous.RdvWalker
81 * @see net.jxta.impl.protocol.LimitedRangeRdvMsg
83 public class LimitedRangeGreeter implements EndpointListener, RdvGreeter {
88 private static final Logger LOG = Logger.getLogger(LimitedRangeGreeter.class.getName());
91 * The walk we are associated with.
93 private final LimitedRangeWalk walk;
96 * XXX It would be nice to avoid making another link to the endpoint.
98 private final EndpointService endpoint;
103 * @param walk The walk we will be associated with.
105 public LimitedRangeGreeter(LimitedRangeWalk walk) {
108 this.endpoint = walk.getPeerGroup().getEndpointService();
110 if (!endpoint.addIncomingMessageListener(this, walk.getWalkServiceName(), walk.getWalkServiceParam())) {
111 throw new IllegalStateException("Could not register endpoint listener for greeter.");
114 if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
115 LOG.info("Listening on " + walk.getWalkServiceName() + "/" + walk.getWalkServiceParam());
122 public synchronized void stop() {
123 endpoint.removeIncomingMessageListener(walk.getWalkServiceName(), walk.getWalkServiceParam());
129 * Listens on "LR-Greeter"<groupid>/<walkSvc><walkParam>
131 * Currently, all this method has to do, is to invoke the upper layer.
133 public void processIncomingMessage(Message message, EndpointAddress srcAddr, EndpointAddress dstAddr) {
135 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
136 LOG.fine("Processing " + message + " from " + srcAddr);
139 LimitedRangeRdvMsg rdvMsg = LimitedRangeWalk.getRdvMessage(message);
141 // Check and update the Limited Range Rdv Message
142 if (null == rdvMsg) {
143 // Message is invalid, drop it
144 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
145 LOG.warning("Limited Range Greeter received invalid " + message + ". Dropping it.");
150 if (rdvMsg.getTTL() <= 0) {
151 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
152 LOG.warning("No TTL remaining for " + message + ". Dropping it.");
157 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
158 LOG.fine("Limited Range Greeter calling listener");
162 walk.getListener().processIncomingMessage(message, srcAddr, dstAddr);
163 } catch (Throwable ignored) {
164 if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
165 LOG.log(Level.SEVERE, "Uncaught Throwable in listener (" + walk.getListener() + ")", ignored);
173 public void replyMessage(Message msg, Message reply) throws IOException {
174 LimitedRangeRdvMsg rdvMsg = LimitedRangeWalk.getRdvMessage(msg);
176 if (rdvMsg == null) {
177 // No RdvMessage. This message was not received by this Greeter.
178 throw new IOException("LimitedRangeWalker was not able to send message" + ": not from this greeter");
181 PeerViewElement pve = walk.getPeerView().getPeerViewElement(rdvMsg.getSrcPeerID());
184 throw new IOException("LimitedRangeWalker was not able to send message" + ": no pve");
187 if (!pve.sendMessage(msg, rdvMsg.getSrcSvcName(), rdvMsg.getSrcSvcParams())) {
188 throw new IOException("LimitedRangeWalker was not able to send message" + ": send failed");