2 * Copyright (c) 2001-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.
57 package net.jxta.impl.endpoint;
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;
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;
75 * This class implements local delivery of messages (for example when the
76 * InputPipe and the OutputPipe are located on the same peer)
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.
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.
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.
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.
100 public class LoopbackMessenger extends BlockingMessenger {
105 private final static transient Logger LOG = Logger.getLogger(LoopbackMessenger.class.getName());
108 * The peergroup we are working for, ie. that we will loop back to.
110 private final PeerGroup group;
113 * The endpoint we are working for, ie. that we will loop back to.
115 private final EndpointService endpoint;
118 * The source address of messages sent on this messenger.
120 private final EndpointAddress srcAddress;
123 * The location destination of this messenger.
125 private final EndpointAddress logicalDestination;
128 * Used to ensure that only a single message is demuxed at a time.
130 private final Lock orderingLock = new ReentrantLock(true);
133 * Create a new loopback messenger.
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.
141 public LoopbackMessenger(PeerGroup group, EndpointService ep, EndpointAddress src, EndpointAddress dest, EndpointAddress logicalDest) {
142 super(group.getPeerGroupID(), dest, false);
147 logicalDestination = logicalDest;
154 public EndpointAddress getLogicalDestinationImpl() {
155 return logicalDestination;
162 public long getMTU() {
163 return Long.MAX_VALUE;
170 public boolean isIdleImpl() {
178 public void closeImpl() {}
184 public void sendMessageBImpl(final Message message, final String service, final String serviceParam) throws IOException {
187 IOException failure = new IOException("Messenger was closed, it cannot be used to send messages.");
189 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
190 LOG.log(Level.WARNING, failure.getMessage(), failure);
197 // Process the message with the appropriate src and dest address
198 ((GenericPeerGroup)group).getExecutor().execute( new Runnable() {
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);
210 orderingLock.unlock();