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.
56 package net.jxta.impl.pipe;
58 import net.jxta.document.AdvertisementFactory;
59 import net.jxta.document.StructuredDocumentFactory;
60 import net.jxta.document.XMLDocument;
61 import net.jxta.endpoint.EndpointAddress;
62 import net.jxta.endpoint.EndpointListener;
63 import net.jxta.endpoint.EndpointService;
64 import net.jxta.endpoint.Message;
65 import net.jxta.endpoint.MessageElement;
66 import net.jxta.id.ID;
67 import net.jxta.logging.Logging;
68 import net.jxta.peergroup.PeerGroup;
69 import net.jxta.pipe.InputPipe;
70 import net.jxta.pipe.PipeMsgListener;
71 import net.jxta.pipe.PipeService;
72 import net.jxta.platform.Module;
73 import net.jxta.protocol.PipeAdvertisement;
74 import net.jxta.rendezvous.RendezVousService;
76 import java.io.IOException;
77 import java.util.ArrayList;
78 import java.util.Collection;
79 import java.util.HashMap;
82 import java.util.logging.Level;
83 import java.util.logging.Logger;
86 * The Wire (Propagated) Pipe Service.
88 public class WirePipeImpl implements EndpointListener {
93 private final static Logger LOG = Logger.getLogger(WirePipeImpl.class.getName());
96 * Service name we register our listener with.
98 final static String WIRE_SERVICE_NAME = "jxta.service.wirepipe";
101 * Service param we register our listener with.
103 * Including the wireParam as part of the destination endpoint address
104 * is needed only for backwards compatibility with legacy version of JXTA.
105 * The wireParam is no longer registered as part of the endpoint listener
108 private final String wireParam;
111 * The Message namespace we use for passing the wire header.
113 final static String WIRE_HEADER_ELEMENT_NAMESPACE = "jxta";
116 * The Message Element name we use for passing the wire header.
118 final static String WIRE_HEADER_ELEMENT_NAME = "JxtaWireHeader";
121 * The wire pipes we know of.
123 private final Map<ID, WirePipe> wirePipes = new HashMap<ID, WirePipe>();
125 private final PeerGroup group;
126 private final PipeResolver pipeResolver;
128 private EndpointService endpoint = null;
129 private RendezVousService rendezvous = null;
132 * @param group Description of the Parameter
133 * @param pipeResolver Description of the Parameter
135 WirePipeImpl(PeerGroup group, PipeResolver pipeResolver) {
137 this.pipeResolver = pipeResolver;
138 this.wireParam = group.getPeerGroupID().getUniqueValue().toString();
142 * To support WirePipe.send(Message, Enumeration)
144 * @return The serviceParameter value
146 public String getServiceParameter() {
151 * Supply arguments and starts this service if it hadn't started by itself.
153 * Currently this service does not expect arguments.
155 * @param arg A table of strings arguments.
156 * @return int status indication.
158 public int startApp(String[] arg) {
159 endpoint = group.getEndpointService();
161 if (null == endpoint) {
162 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
163 LOG.warning("Stalled until there is an endpoint service");
165 return Module.START_AGAIN_STALLED;
168 rendezvous = group.getRendezVousService();
169 if (null == rendezvous) {
170 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
171 LOG.warning("Stalled until there is a rendezvous service");
173 return Module.START_AGAIN_STALLED;
176 // Set our Endpoint Listener
178 endpoint.addIncomingMessageListener(this, WIRE_SERVICE_NAME, null);
179 } catch (Exception e) {
180 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
181 LOG.log(Level.WARNING, "Failed registering Endpoint Listener", e);
183 throw new IllegalStateException("Failed registering Endpoint Listener");
185 return Module.START_OK;
189 * Ask this service to stop.
191 public void stopApp() {
192 Collection<WirePipe> allWirePipes = new ArrayList<WirePipe>(wirePipes.values());
194 for (WirePipe aWirePipe : allWirePipes) {
195 // Close all of the wire pipes.
200 // Clear our listener
201 endpoint.removeIncomingMessageListener(WIRE_SERVICE_NAME, null);
208 * create an InputPipe from a pipe Advertisement
210 * @param adv is the advertisement of the PipeServiceImpl.
211 * @param listener PipeMsgListener to receive msgs.
212 * @return InputPipe InputPipe object created
213 * @throws IOException error creating input pipe
215 InputPipe createInputPipe(PipeAdvertisement adv, PipeMsgListener listener) throws IOException {
216 WirePipe wirePipe = getWirePipe(adv);
217 return new InputPipeImpl(wirePipe, adv, listener);
221 * create an OutputPipe from the pipe Advertisement giving a PeerId(s)
222 * where the corresponding InputPipe is supposed to be.
224 * @param adv is the advertisement of the NetPipe.
225 * @param peers is a set of the PeerId of the peers where to look
226 * for the corresponding Pipes
227 * @return OuputPipe corresponding OutputPipe
229 NonBlockingWireOutputPipe createOutputPipe(PipeAdvertisement adv, Set<? extends ID> peers) {
230 WirePipe wirePipe = getWirePipe(adv);
231 return new NonBlockingWireOutputPipe(group, wirePipe, adv, peers);
235 * PropagateType pipes
237 * @param adv the pipe adv
238 * @return the wire pipe
240 private WirePipe getWirePipe(PipeAdvertisement adv) {
243 synchronized (wirePipes) {
244 // First see if we have already a WirePipe for this pipe
245 wirePipe = wirePipes.get(adv.getPipeID());
247 if (null == wirePipe) {
248 // No.. There is none. Create a new one.
249 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
250 LOG.fine("Creating new wire pipe for " + adv.getPipeID());
252 wirePipe = new WirePipe(group, pipeResolver, this, adv);
253 wirePipes.put(adv.getPipeID(), wirePipe);
260 * PropagateType pipes
262 * @param pipeID Pipe ID
263 * @param create if true create one if one does not exist
264 * @return the wire pipe
266 private WirePipe getWirePipe(ID pipeID, boolean create) {
269 synchronized (wirePipes) {
270 // First see if we have already a WirePipe for this pipe
271 wirePipe = wirePipes.get(pipeID);
273 if ((null == wirePipe) && create) {
274 // No.. There is none. Create a new one.
275 // XXX 20031019 bondolo@jxta.org Check for the adv in local discovery maybe?
276 PipeAdvertisement adv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(
277 PipeAdvertisement.getAdvertisementType());
279 adv.setPipeID(pipeID);
280 adv.setType(PipeService.PropagateType);
282 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
283 LOG.fine("Creating new wire pipe for " + adv.getPipeID());
285 wirePipe = new WirePipe(group, pipeResolver, this, adv);
286 wirePipes.put(pipeID, wirePipe);
293 * Remove a wire pipe from our collection of wire pipes.
295 * @param pipeID The ID of the wire pipe to forget.
296 * @return {@code true} if the wire pipe had been registered otherwise
299 boolean forgetWirePipe(ID pipeID) {
300 synchronized (wirePipes) {
301 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
302 LOG.fine("Removing wire pipe for " + pipeID);
304 return null != wirePipes.remove(pipeID);
311 * Listener for "jxta.service.wirepipe" / <null>
313 public void processIncomingMessage(Message message, EndpointAddress srcAddr, EndpointAddress dstAddr) {
314 // Check if there is a JXTA-WIRE header
315 MessageElement elem = message.getMessageElement(WIRE_HEADER_ELEMENT_NAMESPACE, WIRE_HEADER_ELEMENT_NAME);
318 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
319 LOG.fine("No JxtaWireHeader element. Discarding " + message);
327 XMLDocument doc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(elem);
328 header = new WireHeader(doc);
329 } catch (Exception e) {
330 if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
331 LOG.log(Level.WARNING, "bad wire header for " + message, e);
336 WirePipe wirePipe = getWirePipe(header.getPipeID(), rendezvous.isRendezVous());
337 if (null != wirePipe) {
338 wirePipe.processIncomingMessage(message, header, srcAddr, dstAddr);
340 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
341 LOG.fine("Ignoring message " + message + " for id " + header.getPipeID());