]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/endpoint/LoopbackMessenger.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / endpoint / LoopbackMessenger.java
1 /*
2  * Copyright (c) 2001-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
57 package net.jxta.impl.endpoint;
58
59
60 import net.jxta.endpoint.EndpointAddress;
61 import net.jxta.endpoint.EndpointService;
62 import net.jxta.endpoint.Message;
63 import java.util.logging.Level;
64 import net.jxta.logging.Logging;
65 import java.util.logging.Logger;
66
67 import java.io.IOException;
68 import java.util.concurrent.locks.Lock;
69 import java.util.concurrent.locks.ReentrantLock;
70 import net.jxta.impl.peergroup.GenericPeerGroup;
71 import net.jxta.peergroup.PeerGroup;
72
73
74 /**
75  * This class implements local delivery of messages (for example when the
76  * InputPipe and the OutputPipe are located on the same peer)
77  * <p/>
78  * The reason this class is useful is that it may not always be possible to
79  * connect to oneself without actually through the relay. i.e. A peer with outgoing
80  * only http transport, can not possibly connect to self through the transport.
81  * <p/>
82  * Since transports cannot be relied on to perform a loopback, some layer
83  * above has to figure out that a message is looping back.
84  * Since peerid loopback does not explicitly request to go through a real
85  * transport, and since peerid addressing is the job of the router, it is
86  * the router that performs loopback.
87  * <p/>
88  * The router could probably perform the loopback by delivering the message
89  * to its own input queue, that would take a special transport instead of a
90  * special messenger, which is the same kind of deal but would imply some
91  * incoming message processing by the router for every message. In
92  * contrast, the loopback messenger is setup once and the router will never
93  * sees the messages. That's a good optimization.
94  * <p/>
95  * Alternatively, the endpoint service itself could figure out the
96  * loopback, but since the API wants to give a messenger to the requestor
97  * rather than just sending a message, the endpoint would have to setup a
98  * loopback messenger anyway. So it is pretty much the same.
99  */
100 public class LoopbackMessenger extends BlockingMessenger {
101     
102     /**
103      *  Logger
104      */
105     private final static transient Logger LOG = Logger.getLogger(LoopbackMessenger.class.getName());
106     
107     /**
108      * The peergroup we are working for, ie. that we will loop back to.
109      */
110     private final PeerGroup group;
111     
112     /**
113      * The endpoint we are working for, ie. that we will loop back to.
114      */
115     private final EndpointService endpoint;
116     
117     /**
118      * The source address of messages sent on this messenger.
119      */
120     private final EndpointAddress srcAddress;
121     
122     /**
123      * The location destination of this messenger.
124      */
125     private final EndpointAddress logicalDestination;
126     
127     /**
128      *  Used to ensure that only a single message is demuxed at a time.
129      */
130     private final Lock orderingLock = new ReentrantLock(true);
131     
132     /**
133      * Create a new loopback messenger.
134      *
135      * @param group       The group context.
136      * @param ep          where messages go
137      * @param src         who messages should be addressed from
138      * @param dest        who messages should be addressed to
139      * @param logicalDest The logical destination address.
140      */
141     public LoopbackMessenger(PeerGroup group, EndpointService ep, EndpointAddress src, EndpointAddress dest, EndpointAddress logicalDest) {
142         super(group.getPeerGroupID(), dest, false);
143         
144         this.group = group;
145         endpoint = ep;
146         srcAddress = src;
147         logicalDestination = logicalDest;
148     }
149     
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public EndpointAddress getLogicalDestinationImpl() {
155         return logicalDestination;
156     }
157     
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public long getMTU() {
163         return Long.MAX_VALUE;
164     }
165     
166     /**
167      * {@inheritDoc}
168      */
169     @Override
170     public boolean isIdleImpl() {
171         return false;
172     }
173     
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public void closeImpl() {}
179     
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public void sendMessageBImpl(final Message message, final String service, final String serviceParam) throws IOException {
185         
186         if (isClosed()) {
187             IOException failure = new IOException("Messenger was closed, it cannot be used to send messages.");
188             
189             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
190                 LOG.log(Level.WARNING, failure.getMessage(), failure);
191             }
192             throw failure;
193         }
194         
195         orderingLock.lock();
196         try {
197             // Process the message with the appropriate src and dest address
198             ((GenericPeerGroup)group).getExecutor().execute( new Runnable() {
199                 public void run() {
200                     try {
201                         endpoint.processIncomingMessage(message, srcAddress, getDestAddressToUse(service, serviceParam));
202                     } catch(Throwable uncaught) {
203                         if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
204                             LOG.log(Level.WARNING, "Uncaught Throwable in Loopback Messenger ", uncaught);
205                         }
206                     }
207                 }
208             });
209         } finally {
210             orderingLock.unlock();
211         }
212     }
213 }