]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/rendezvous/limited/LimitedRangeGreeter.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / rendezvous / limited / LimitedRangeGreeter.java
1 /*
2  * Copyright (c) 2002-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56 package net.jxta.impl.rendezvous.limited;
57
58 import java.io.IOException;
59
60 import java.util.logging.Level;
61
62 import net.jxta.logging.Logging;
63
64 import java.util.logging.Logger;
65
66 import net.jxta.endpoint.EndpointAddress;
67 import net.jxta.endpoint.EndpointListener;
68 import net.jxta.endpoint.EndpointService;
69 import net.jxta.endpoint.Message;
70
71 import net.jxta.impl.protocol.LimitedRangeRdvMsg;
72 import net.jxta.impl.rendezvous.RdvGreeter;
73 import net.jxta.impl.rendezvous.rpv.PeerViewElement;
74
75 /**
76  * The limited range rendezvous peer greeter.
77  *
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
82  */
83 public class LimitedRangeGreeter implements EndpointListener, RdvGreeter {
84
85     /**
86      * Logger
87      */
88     private static final Logger LOG = Logger.getLogger(LimitedRangeGreeter.class.getName());
89
90     /**
91      * The walk we are associated with.
92      */
93     private final LimitedRangeWalk walk;
94
95     /**
96      * XXX It would be nice to avoid making another link to the endpoint.
97      */
98     private final EndpointService endpoint;
99
100     /**
101      * Constructor
102      *
103      * @param walk The walk we will be associated with.
104      */
105     public LimitedRangeGreeter(LimitedRangeWalk walk) {
106         this.walk = walk;
107
108         this.endpoint = walk.getPeerGroup().getEndpointService();
109
110         if (!endpoint.addIncomingMessageListener(this, walk.getWalkServiceName(), walk.getWalkServiceParam())) {
111             throw new IllegalStateException("Could not register endpoint listener for greeter.");
112         }
113
114         if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
115             LOG.info("Listening on " + walk.getWalkServiceName() + "/" + walk.getWalkServiceParam());
116         }
117     }
118
119     /**
120      * {@inheritDoc}
121      */
122     public synchronized void stop() {
123         endpoint.removeIncomingMessageListener(walk.getWalkServiceName(), walk.getWalkServiceParam());
124     }
125
126     /**
127      * {@inheritDoc}
128      * <p/>
129      *  Listens on "LR-Greeter"&lt;groupid>/&lt;walkSvc>&lt;walkParam>
130      * <p/>
131      * Currently, all this method has to do, is to invoke the upper layer.
132      */
133     public void processIncomingMessage(Message message, EndpointAddress srcAddr, EndpointAddress dstAddr) {
134
135         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
136             LOG.fine("Processing " + message + " from " + srcAddr);
137         }
138
139         LimitedRangeRdvMsg rdvMsg = LimitedRangeWalk.getRdvMessage(message);
140
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.");
146             }
147             return;
148         }
149
150         if (rdvMsg.getTTL() <= 0) {
151             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
152                 LOG.warning("No TTL remaining for " + message + ". Dropping it.");
153             }
154             return;
155         }
156
157         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
158             LOG.fine("Limited Range Greeter calling listener");
159         }
160
161         try {
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);
166             }
167         }
168     }
169
170     /**
171      * {@inheritDoc}
172      */
173     public void replyMessage(Message msg, Message reply) throws IOException {
174         LimitedRangeRdvMsg rdvMsg = LimitedRangeWalk.getRdvMessage(msg);
175
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");
179         }
180
181         PeerViewElement pve = walk.getPeerView().getPeerViewElement(rdvMsg.getSrcPeerID());
182
183         if (null == pve) {
184             throw new IOException("LimitedRangeWalker was not able to send message" + ": no pve");
185         }
186
187         if (!pve.sendMessage(msg, rdvMsg.getSrcSvcName(), rdvMsg.getSrcSvcParams())) {
188             throw new IOException("LimitedRangeWalker was not able to send message" + ": send failed");
189         }
190     }
191 }