]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/pipe/WirePipeImpl.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / pipe / WirePipeImpl.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 package net.jxta.impl.pipe;
57
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;
75
76 import java.io.IOException;
77 import java.util.ArrayList;
78 import java.util.Collection;
79 import java.util.HashMap;
80 import java.util.Map;
81 import java.util.Set;
82 import java.util.logging.Level;
83 import java.util.logging.Logger;
84
85 /**
86  * The Wire (Propagated) Pipe Service.
87  */
88 public class WirePipeImpl implements EndpointListener {
89
90     /**
91      * Logger
92      */
93     private final static Logger LOG = Logger.getLogger(WirePipeImpl.class.getName());
94
95     /**
96      * Service name we register our listener with.
97      */
98     final static String WIRE_SERVICE_NAME = "jxta.service.wirepipe";
99
100     /**
101      * Service param we register our listener with.
102      * <p/>
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
106      * address.
107      */
108     private final String wireParam;
109
110     /**
111      * The Message namespace we use for passing the wire header.
112      */
113     final static String WIRE_HEADER_ELEMENT_NAMESPACE = "jxta";
114
115     /**
116      * The Message Element name we use for passing the wire header.
117      */
118     final static String WIRE_HEADER_ELEMENT_NAME = "JxtaWireHeader";
119
120     /**
121      * The wire pipes we know of.
122      */
123     private final Map<ID, WirePipe> wirePipes = new HashMap<ID, WirePipe>();
124
125     private final PeerGroup group;
126     private final PipeResolver pipeResolver;
127
128     private EndpointService endpoint = null;
129     private RendezVousService rendezvous = null;
130
131     /**
132      * @param group        Description of the Parameter
133      * @param pipeResolver Description of the Parameter
134      */
135     WirePipeImpl(PeerGroup group, PipeResolver pipeResolver) {
136         this.group = group;
137         this.pipeResolver = pipeResolver;
138         this.wireParam = group.getPeerGroupID().getUniqueValue().toString();
139     }
140
141     /**
142      * To support WirePipe.send(Message, Enumeration)
143      *
144      * @return The serviceParameter value
145      */
146     public String getServiceParameter() {
147         return wireParam;
148     }
149
150     /**
151      * Supply arguments and starts this service if it hadn't started by itself.
152      * <p/>
153      * Currently this service does not expect arguments.
154      *
155      * @param arg A table of strings arguments.
156      * @return int status indication.
157      */
158     public int startApp(String[] arg) {
159         endpoint = group.getEndpointService();
160
161         if (null == endpoint) {
162             if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {
163                 LOG.warning("Stalled until there is an endpoint service");
164             }
165             return Module.START_AGAIN_STALLED;
166         }
167
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");
172             }
173             return Module.START_AGAIN_STALLED;
174         }
175
176         // Set our Endpoint Listener
177         try {
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);
182             }
183             throw new IllegalStateException("Failed registering Endpoint Listener");
184         }
185         return Module.START_OK;
186     }
187
188     /**
189      * Ask this service to stop.
190      */
191     public void stopApp() {
192         Collection<WirePipe> allWirePipes = new ArrayList<WirePipe>(wirePipes.values());
193
194         for (WirePipe aWirePipe : allWirePipes) {
195             // Close all of the wire pipes.
196             aWirePipe.close();
197         }
198         wirePipes.clear();
199
200         // Clear our listener
201         endpoint.removeIncomingMessageListener(WIRE_SERVICE_NAME, null);
202
203         endpoint = null;
204         rendezvous = null;
205     }
206
207     /**
208      * create an InputPipe from a pipe Advertisement
209      *
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
214      */
215     InputPipe createInputPipe(PipeAdvertisement adv, PipeMsgListener listener) throws IOException {
216         WirePipe wirePipe = getWirePipe(adv);
217         return new InputPipeImpl(wirePipe, adv, listener);
218     }
219
220     /**
221      * create an OutputPipe from the pipe Advertisement giving a PeerId(s)
222      * where the corresponding InputPipe is supposed to be.
223      *
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
228      */
229     NonBlockingWireOutputPipe createOutputPipe(PipeAdvertisement adv, Set<? extends ID> peers) {
230         WirePipe wirePipe = getWirePipe(adv);
231         return new NonBlockingWireOutputPipe(group, wirePipe, adv, peers);
232     }
233
234     /**
235      * PropagateType pipes
236      *
237      * @param adv the pipe adv
238      * @return the wire pipe
239      */
240     private WirePipe getWirePipe(PipeAdvertisement adv) {
241         WirePipe wirePipe;
242
243         synchronized (wirePipes) {
244             // First see if we have already a WirePipe for this pipe
245             wirePipe = wirePipes.get(adv.getPipeID());
246
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());
251                 }
252                 wirePipe = new WirePipe(group, pipeResolver, this, adv);
253                 wirePipes.put(adv.getPipeID(), wirePipe);
254             }
255         }
256         return wirePipe;
257     }
258
259     /**
260      * PropagateType pipes
261      *
262      * @param pipeID Pipe ID
263      * @param create if true create one if one does not exist
264      * @return the wire pipe
265      */
266     private WirePipe getWirePipe(ID pipeID, boolean create) {
267         WirePipe wirePipe;
268
269         synchronized (wirePipes) {
270             // First see if we have already a WirePipe for this pipe
271             wirePipe = wirePipes.get(pipeID);
272
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());
278
279                 adv.setPipeID(pipeID);
280                 adv.setType(PipeService.PropagateType);
281
282                 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
283                     LOG.fine("Creating new wire pipe for " + adv.getPipeID());
284                 }
285                 wirePipe = new WirePipe(group, pipeResolver, this, adv);
286                 wirePipes.put(pipeID, wirePipe);
287             }
288         }
289         return wirePipe;
290     }
291
292     /**
293      * Remove a wire pipe from our collection of wire pipes.
294      *
295      * @param pipeID The ID of the wire pipe to forget.
296      * @return {@code true} if the wire pipe had been registered otherwise
297      *         {@code false}.
298      */
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);
303             }
304             return null != wirePipes.remove(pipeID);
305         }
306     }
307
308     /**
309      * {@inheritDoc}
310      * <p/>
311      * Listener for "jxta.service.wirepipe" / &lt;null&gt;
312      */
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);
316
317         if (null == elem) {
318             if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
319                 LOG.fine("No JxtaWireHeader element. Discarding " + message);
320             }
321             return;
322         }
323
324         WireHeader header;
325
326         try {
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);
332             }
333             return;
334         }
335
336         WirePipe wirePipe = getWirePipe(header.getPipeID(), rendezvous.isRendezVous());
337         if (null != wirePipe) {
338             wirePipe.processIncomingMessage(message, header, srcAddr, dstAddr);
339         } else {
340             if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
341                 LOG.fine("Ignoring message " + message + " for id " + header.getPipeID());
342             }
343         }
344     }
345 }