]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/rendezvous/rpv/PeerViewElement.java
fab64a4dbc75f75f7ffe37483385b7ffd562ce07
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / rendezvous / rpv / PeerViewElement.java
1 /*
2  * Copyright (c) 2002-2007 Sun Micro//Systems, 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.rpv;
57
58 import java.io.IOException;
59
60 import net.jxta.endpoint.EndpointService;
61 import net.jxta.endpoint.Message;
62 import net.jxta.endpoint.Messenger;
63 import net.jxta.endpoint.OutgoingMessageEvent;
64 import net.jxta.endpoint.OutgoingMessageEventListener;
65 import net.jxta.impl.util.TimeUtils;
66 import net.jxta.protocol.RdvAdvertisement;
67
68 import java.util.logging.Level;
69 import net.jxta.logging.Logging;
70 import java.util.logging.Logger;
71
72 /**
73  * An element of the PeerView.
74  *
75  * <p/>The destination address (peerID) is part of PeerViewDestination, which implements the
76  * comparable interface. That makes it possible to sort and create ordered lists of
77  * PeerViewElements, and to search these lists while knowing only a destination address.
78  */
79 public final class PeerViewElement extends PeerViewDestination implements OutgoingMessageEventListener {
80
81     /**
82      *  Logger
83      */
84     private final static transient Logger LOG = Logger.getLogger(PeerViewElement.class.getName());
85
86     /**
87      * EndpointService that this PeerViewElement must use.
88      */
89     private final EndpointService endpoint;
90
91     /**
92      * Absolute time in milliseconds at which this element was created.
93      */
94     private final long created;
95
96     /**
97      * Absolute time in milliseconds at which this element was created.
98      */
99     private long lastUpdate = 0;
100
101     /**
102      * The encapsulated RdvAdvertisement for the Peer this instance
103      * represents.
104      */
105     private RdvAdvertisement radv = null;
106
107     /**
108      * True is the remote peer is known to be alive, false otherwise.
109      * It is always alive at birth. It may die soon after and we want to
110      * generate an event in that case.
111      */
112     private boolean alive = true;
113
114     /**
115      * If true then we are not accepting new messages until something unclogs.
116      */
117     private volatile boolean throttling = false;
118
119     /**
120      * PeerView that owns this PeerViewElement.
121      */
122     private PeerView peerview = null;
123
124     /**
125      *  A cached Messenger for sending to the destination peer.
126      */
127     private Messenger cachedMessenger = null;
128
129     /**
130      * Initialize from a RdvAdvertisement.
131      *
132      * @param endpoint The endpoint service.
133      * @param radv the RdvAdvertisement from which to initialize
134      */
135     PeerViewElement(EndpointService endpoint, RdvAdvertisement radv) {
136
137         super(radv.getPeerID());
138
139         this.endpoint = endpoint;
140         this.radv = radv;
141
142         created = TimeUtils.timeNow();
143         lastUpdate = created;
144     }
145
146     /**
147      *  {@inheritDoc}
148      *  <p/>
149      *  A simple implementation for debugging. Do not attempt to parse this value!
150      */
151     @Override
152     public String toString() {
153         StringBuilder asString = new StringBuilder();
154
155         asString.append('\"');
156         asString.append(radv.getName());
157         asString.append('\"');
158         asString.append(alive ? " A " : " a ");
159         asString.append(isInPeerView() ? " P " : " p ");
160         asString.append(throttling ? " T " : " t ");
161         asString.append(" [");
162         asString.append(TimeUtils.toRelativeTimeMillis(TimeUtils.timeNow(), created) / TimeUtils.ASECOND);
163         asString.append("/");
164         asString.append(TimeUtils.toRelativeTimeMillis(TimeUtils.timeNow(), lastUpdate) / TimeUtils.ASECOND);
165         asString.append("]");
166         return asString.toString();
167     }
168
169     /**
170      * {@inheritDoc}
171      */
172     public void messageSendSucceeded(OutgoingMessageEvent e) {
173
174         // As far as we know, connectivity is fine.
175         setAlive(true, true);
176
177         throttling = false;
178     }
179
180     /**
181      * {@inheritDoc}
182      */
183     public void messageSendFailed(OutgoingMessageEvent e) {
184
185         // As far as we know, connectivity is down.
186         // Except if failure is null; then it's just a queue overflow.
187
188         if (null != e.getFailure()) {
189             setAlive(false, true);
190         }
191
192         throttling = (e.getFailure() == null);
193     }
194
195     /**
196      * Return <code>true</code> if the remote peer is known to be alive,
197      * <code>false</code> otherwise.
198      *
199      * @return Return <code>true</code> if the remote peer is known to be
200      * alive, <code>false</code> otherwise.
201      */
202     public boolean isAlive() {
203         return alive;
204     }
205
206     /**
207      * Update the connection status based upon the result of the last message
208      * send.
209      *
210      * <p/>We track the current dead-alive state and If we're in a peerview
211      * notify it of the transitions from alive to dead.
212      *
213      * @param live The known liveness of our connection to this peer.
214      * @param doNotify {@code true} will cause failure notifications to be sent.
215      * {@code false} makes notifications the caller's responsibility.
216      * @return {@code true} if a failure notification needs to be sent otherwise
217      * {@code false}.
218      */
219     boolean setAlive(boolean live, boolean doNotify) {
220         boolean mustNotify;
221
222         synchronized (this) {
223             mustNotify = alive && !live;
224             alive = live;
225         }
226
227         // Since we do this out of sync, it is in theory possible that our alive
228         // status has already changed. It is rare but will only cause a little
229         // shake. So leave the sync behind, it causes a deadlock.
230         if (mustNotify && doNotify) {
231             PeerView temp = peerview;
232
233             if (null != temp) {
234                 temp.notifyFailure(this, true);
235             }
236         }
237         
238         return mustNotify;
239     }
240
241     boolean isInPeerView() {
242         return (null != peerview);
243     }
244
245     /**
246      *  Sets the peerview
247      */
248     synchronized void setPeerView(PeerView pv) {
249         if ((null != peerview) && (null != pv)) {
250             throw new IllegalStateException("Element already in " + peerview);
251         }
252
253         peerview = pv;
254     }
255
256     /**
257      *  Return the time in absolute milliseconds at which we last updated this peer.
258      */
259     long getLastUpdateTime() {
260         return lastUpdate;
261     }
262
263     /**
264      *  Sets the time in absolute milliseconds at which we last updated this peer.
265      */
266     void setLastUpdateTime(long last) {
267         lastUpdate = last;
268     }
269
270     /**
271      * Send a message to the peer which is represented by the current
272      * PeerViewElement.
273      *
274      * @param msg the message to send
275      *
276      * @param serviceName the service name on the destination peer to
277      * which the message will be demultiplexed
278      *
279      * @param serviceParam the service param on the destination peer
280      * to which the message will be demultiplexed
281      *
282      * @return true if the message was successfully handed off to the
283      * endpoint for delivery, false otherwise
284      */
285     public boolean sendMessage(Message msg, String serviceName, String serviceParam) {
286
287         if (throttling) {
288             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
289                 LOG.warning("Declining to send -- throttling on " + this);
290             }
291             return false;
292         }
293
294         Messenger sendVia = getCachedMessenger();
295
296         if (null == sendVia) {
297             // There is nothing really we can do.
298             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
299                 LOG.warning("Could not get messenger for " + getPeerID());
300             }
301
302             OutgoingMessageEvent event = new OutgoingMessageEvent(msg,
303                     new IOException("Couldn't get messenger for " + getPeerID()));
304
305             messageSendFailed(event);
306             return false;
307         }
308
309         sendVia.sendMessage(msg, serviceName, serviceParam, this);
310
311         return true;
312     }
313
314     /**
315      * Get the encapsulated Peer Advertisement.
316      *
317      * @return the Advertisement of the Peer represented by this
318      * object
319      */
320     public RdvAdvertisement getRdvAdvertisement() {
321         return radv;
322     }
323
324     /**
325      * Set the encapsulated Peer Advertisement.
326      *
327      * @param adv is the RdvAdvertisement to be set.
328      * @return RdvAdvertisement the old Advertisement of the Peer represented by this
329      * object
330      */
331     RdvAdvertisement setRdvAdvertisement(RdvAdvertisement adv) {
332
333         if (!radv.getPeerID().equals(adv.getPeerID())) {
334             if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
335                 LOG.severe("adv refers to a different peer");
336             }
337
338             throw new IllegalArgumentException("adv refers to a different peer");
339         }
340
341         RdvAdvertisement old = radv;
342
343         this.radv = adv;
344
345         setLastUpdateTime(TimeUtils.timeNow());
346
347         return old;
348     }
349
350     /**
351      *  Return a messenger suitable for sending to this peer.
352      *
353      *  @return a messenger to this PVE peer or if {@code null} if peer is
354      *  unreachable.
355      */
356     private Messenger getCachedMessenger() {
357
358         boolean mustNotify = false;
359         
360         synchronized (this) {
361             if ((null == cachedMessenger) || ((cachedMessenger.getState() & Messenger.USABLE) == 0)) {
362                 cachedMessenger = null;
363
364                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
365                     LOG.fine("Getting cached Messenger for " + radv.getName());
366                 }
367
368                 cachedMessenger = endpoint.getMessengerImmediate(getDestAddress(), radv.getRouteAdv());
369                 
370                 if (null == cachedMessenger) {
371                     mustNotify = setAlive(false, false);
372                 } else if ((cachedMessenger.getState() & Messenger.RESOLVED) != 0) {
373                     mustNotify = setAlive(true, false);
374                 }
375             }
376         }
377
378         // Since we do this out of sync, it is in theory possible that our alive
379         // status has already changed. It is rare but will only cause a little
380         // shake. So leave the sync behind, it causes a deadlock.
381         if (mustNotify) {
382             PeerView temp = peerview;
383
384             if (null != temp) {
385                 temp.notifyFailure(this, true);
386             }
387         }
388
389         return cachedMessenger;
390     }
391 }