]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/proxy/Requestor.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / proxy / Requestor.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.proxy;
58
59
60 import net.jxta.document.Advertisement;
61 import net.jxta.endpoint.*;
62 import net.jxta.peergroup.PeerGroup;
63 import net.jxta.protocol.PeerAdvertisement;
64 import net.jxta.protocol.PeerGroupAdvertisement;
65 import net.jxta.protocol.PipeAdvertisement;
66 import java.util.logging.Level;
67 import net.jxta.logging.Logging;
68 import java.util.logging.Logger;
69
70 import java.io.IOException;
71
72
73 public class Requestor {
74     private final static Logger LOG = Logger.getLogger(Requestor.class.getName());
75
76     private PeerGroup group;
77     private EndpointAddress address;
78     private MessageElement requestId;
79     private Messenger messenger;
80     private int threshold = 1;
81
82     public boolean send(Message message) {
83         int count;
84
85         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
86             LOG.fine("send to " + address.toString());
87         }
88
89         try {
90             synchronized (this) {
91                 if ((null == messenger) || messenger.isClosed()) {
92                     messenger = null;
93                     count = 0;
94                     // Add a retry in case we did not obtain a new messenger.
95                     // Due to the heavy polling of the client, it seems that
96                     // we can run in a race condition where we don't get
97                     // a new messenger.
98                     while (count < 2 && messenger == null) {
99                         messenger = group.getEndpointService().getMessengerImmediate(address, null);
100                         if (messenger != null) {
101                             break;
102                         }
103                         try {
104                             Thread.sleep(500);
105                         } catch (InterruptedException e) {
106                             Thread.interrupted();
107                             if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
108                                 LOG.fine("Retry getting a messenger" + e);
109                             }
110                         }
111                         count++;
112                     }
113
114                     if (null == messenger) {
115                         LOG.warning("Could not get messenger for " + address);
116                         return false;
117                     }
118                 }
119             }
120             messenger.sendMessage(message);
121         } catch (IOException e) {
122             LOG.log(Level.WARNING, "Could not send message to requestor for " + address, e);
123             return false;
124         }
125
126         ProxyService.logMessage(message, LOG);
127
128         return true;
129     }
130
131     public boolean send(Advertisement adv, String resultType) {
132         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
133             LOG.fine("send " + adv);
134         }
135
136         Message message = new Message();
137
138         if (resultType == null) {
139             resultType = "";
140         }
141         setString(message, ProxyService.RESPONSE_TAG, resultType);
142
143         if (requestId != null) {
144             message.addMessageElement(ProxyService.PROXYNS, requestId);
145         }
146
147         if (adv instanceof PeerAdvertisement) {
148             PeerAdvertisement peerAdv = (PeerAdvertisement) adv;
149
150             message.addMessageElement(ProxyService.PROXYNS
151                     ,
152                     new StringMessageElement(ProxyService.TYPE_TAG, ProxyService.TYPE_PEER, null));
153
154             message.addMessageElement(ProxyService.PROXYNS
155                     ,
156                     new StringMessageElement(ProxyService.NAME_TAG, peerAdv.getName(), null));
157
158             message.addMessageElement(ProxyService.PROXYNS
159                     ,
160                     new StringMessageElement(ProxyService.ID_TAG, peerAdv.getPeerID().toString(), null));
161
162             if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
163                 LOG.fine("send PeerAdvertisement name=" + peerAdv.getName() + " id=" + peerAdv.getPeerID().toString());
164             }
165
166         } else if (adv instanceof PeerGroupAdvertisement) {
167             PeerGroupAdvertisement groupAdv = (PeerGroupAdvertisement) adv;
168
169             message.addMessageElement(ProxyService.PROXYNS
170                     ,
171                     new StringMessageElement(ProxyService.TYPE_TAG, ProxyService.TYPE_GROUP, null));
172
173             message.addMessageElement(ProxyService.PROXYNS
174                     ,
175                     new StringMessageElement(ProxyService.NAME_TAG, groupAdv.getName(), null));
176
177             message.addMessageElement(ProxyService.PROXYNS
178                     ,
179                     new StringMessageElement(ProxyService.ID_TAG, groupAdv.getPeerGroupID().toString(), null));
180
181             LOG.fine("send GroupAdvertisement name=" + groupAdv.getName() + " id=" + groupAdv.getPeerGroupID().toString());
182
183         } else if (adv instanceof PipeAdvertisement) {
184             PipeAdvertisement pipeAdv = (PipeAdvertisement) adv;
185
186             message.addMessageElement(ProxyService.PROXYNS
187                     ,
188                     new StringMessageElement(ProxyService.TYPE_TAG, ProxyService.TYPE_PIPE, null));
189
190             message.addMessageElement(ProxyService.PROXYNS
191                     ,
192                     new StringMessageElement(ProxyService.NAME_TAG, pipeAdv.getName(), null));
193
194             message.addMessageElement(ProxyService.PROXYNS
195                     ,
196                     new StringMessageElement(ProxyService.ID_TAG, pipeAdv.getPipeID().toString(), null));
197
198             message.addMessageElement(ProxyService.PROXYNS
199                     ,
200                     new StringMessageElement(ProxyService.ARG_TAG, pipeAdv.getType(), null));
201
202             if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
203                 LOG.fine(
204                         "send PipeAdvertisement name=" + pipeAdv.getName() + " id=" + pipeAdv.getPipeID().toString() + " arg="
205                         + pipeAdv.getType());
206             }
207
208         } else {
209             return false;
210         }
211
212         return send(message);
213     }
214
215     public boolean notifySuccess() {
216         LOG.fine("notifySuccess");
217
218         Message message = new Message();
219
220         message.addMessageElement(ProxyService.PROXYNS
221                 ,
222                 new StringMessageElement(ProxyService.RESPONSE_TAG, ProxyService.RESPONSE_SUCCESS, null));
223
224         if (requestId != null) {
225             message.addMessageElement(ProxyService.PROXYNS, requestId);
226         }
227
228         return send(message);
229     }
230
231     public boolean notifyError(String errorString) {
232         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
233             LOG.fine("notifyError " + errorString);
234         }
235
236         Message message = new Message();
237
238         if (requestId != null) {
239             message.addMessageElement(ProxyService.PROXYNS, requestId);
240         }
241
242         if (errorString != null && errorString.length() > 0) {
243             message.addMessageElement(ProxyService.PROXYNS
244                     ,
245                     new StringMessageElement(ProxyService.ERROR_MESSAGE_TAG, errorString, null));
246         }
247
248         return send(message);
249     }
250
251     /**
252      *  {@inheritDoc}
253      */
254     @Override
255     public boolean equals(Object obj) {
256         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
257             LOG.fine(this + " equals " + obj);
258         }
259
260         if (obj instanceof Requestor) {
261             Requestor dest = (Requestor) obj;
262
263             if (address != null && dest.address != null) {
264                 if (dest.address.toString().equals(address.toString())) {
265                     return true;
266                 }
267             }
268         }
269
270         return false;
271     }
272
273     /**
274      *  {@inheritDoc}
275      */
276     @Override
277     public int hashCode() {
278         int result = 17;
279
280         return 37 * result + requestId.hashCode();
281     }
282
283     /**
284      *  {@inheritDoc}
285      */
286     @Override    
287     public String toString() {
288         return "Requestor " + address.toString();
289     }
290
291     private Requestor(PeerGroup group, EndpointAddress address, MessageElement requestId) throws IOException {
292         this.group = group;
293         this.address = address;
294         this.requestId = requestId;
295     }
296
297     public static Requestor createRequestor(PeerGroup group, Message message, EndpointAddress address, int threshold) throws IOException {
298         Requestor requestor;
299
300         if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
301             LOG.fine("create new Requestor - " + address.toString());
302         }
303
304         MessageElement elem = message.getMessageElement(ProxyService.REQUESTID_TAG);
305
306         requestor = new Requestor(group, address, elem);
307         requestor.setThreshold(threshold);
308         message.removeMessageElement(elem);
309         return requestor;
310     }
311
312     void setThreshold(int threshold) {
313         this.threshold = threshold;
314     }
315
316     int getThreshold() {
317         return threshold;
318     }
319
320     private void setString(Message message, String tag, String value) {
321         StringMessageElement sme = new StringMessageElement(tag, value, null);
322
323         message.addMessageElement(ProxyService.PROXYNS, sme);
324     }
325 }