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.proxy;
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;
70 import java.io.IOException;
73 public class Requestor {
74 private final static Logger LOG = Logger.getLogger(Requestor.class.getName());
76 private PeerGroup group;
77 private EndpointAddress address;
78 private MessageElement requestId;
79 private Messenger messenger;
80 private int threshold = 1;
82 public boolean send(Message message) {
85 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
86 LOG.fine("send to " + address.toString());
91 if ((null == messenger) || messenger.isClosed()) {
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
98 while (count < 2 && messenger == null) {
99 messenger = group.getEndpointService().getMessengerImmediate(address, null);
100 if (messenger != null) {
105 } catch (InterruptedException e) {
106 Thread.interrupted();
107 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
108 LOG.fine("Retry getting a messenger" + e);
114 if (null == messenger) {
115 LOG.warning("Could not get messenger for " + address);
120 messenger.sendMessage(message);
121 } catch (IOException e) {
122 LOG.log(Level.WARNING, "Could not send message to requestor for " + address, e);
126 ProxyService.logMessage(message, LOG);
131 public boolean send(Advertisement adv, String resultType) {
132 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
133 LOG.fine("send " + adv);
136 Message message = new Message();
138 if (resultType == null) {
141 setString(message, ProxyService.RESPONSE_TAG, resultType);
143 if (requestId != null) {
144 message.addMessageElement(ProxyService.PROXYNS, requestId);
147 if (adv instanceof PeerAdvertisement) {
148 PeerAdvertisement peerAdv = (PeerAdvertisement) adv;
150 message.addMessageElement(ProxyService.PROXYNS
152 new StringMessageElement(ProxyService.TYPE_TAG, ProxyService.TYPE_PEER, null));
154 message.addMessageElement(ProxyService.PROXYNS
156 new StringMessageElement(ProxyService.NAME_TAG, peerAdv.getName(), null));
158 message.addMessageElement(ProxyService.PROXYNS
160 new StringMessageElement(ProxyService.ID_TAG, peerAdv.getPeerID().toString(), null));
162 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
163 LOG.fine("send PeerAdvertisement name=" + peerAdv.getName() + " id=" + peerAdv.getPeerID().toString());
166 } else if (adv instanceof PeerGroupAdvertisement) {
167 PeerGroupAdvertisement groupAdv = (PeerGroupAdvertisement) adv;
169 message.addMessageElement(ProxyService.PROXYNS
171 new StringMessageElement(ProxyService.TYPE_TAG, ProxyService.TYPE_GROUP, null));
173 message.addMessageElement(ProxyService.PROXYNS
175 new StringMessageElement(ProxyService.NAME_TAG, groupAdv.getName(), null));
177 message.addMessageElement(ProxyService.PROXYNS
179 new StringMessageElement(ProxyService.ID_TAG, groupAdv.getPeerGroupID().toString(), null));
181 LOG.fine("send GroupAdvertisement name=" + groupAdv.getName() + " id=" + groupAdv.getPeerGroupID().toString());
183 } else if (adv instanceof PipeAdvertisement) {
184 PipeAdvertisement pipeAdv = (PipeAdvertisement) adv;
186 message.addMessageElement(ProxyService.PROXYNS
188 new StringMessageElement(ProxyService.TYPE_TAG, ProxyService.TYPE_PIPE, null));
190 message.addMessageElement(ProxyService.PROXYNS
192 new StringMessageElement(ProxyService.NAME_TAG, pipeAdv.getName(), null));
194 message.addMessageElement(ProxyService.PROXYNS
196 new StringMessageElement(ProxyService.ID_TAG, pipeAdv.getPipeID().toString(), null));
198 message.addMessageElement(ProxyService.PROXYNS
200 new StringMessageElement(ProxyService.ARG_TAG, pipeAdv.getType(), null));
202 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
204 "send PipeAdvertisement name=" + pipeAdv.getName() + " id=" + pipeAdv.getPipeID().toString() + " arg="
205 + pipeAdv.getType());
212 return send(message);
215 public boolean notifySuccess() {
216 LOG.fine("notifySuccess");
218 Message message = new Message();
220 message.addMessageElement(ProxyService.PROXYNS
222 new StringMessageElement(ProxyService.RESPONSE_TAG, ProxyService.RESPONSE_SUCCESS, null));
224 if (requestId != null) {
225 message.addMessageElement(ProxyService.PROXYNS, requestId);
228 return send(message);
231 public boolean notifyError(String errorString) {
232 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
233 LOG.fine("notifyError " + errorString);
236 Message message = new Message();
238 if (requestId != null) {
239 message.addMessageElement(ProxyService.PROXYNS, requestId);
242 if (errorString != null && errorString.length() > 0) {
243 message.addMessageElement(ProxyService.PROXYNS
245 new StringMessageElement(ProxyService.ERROR_MESSAGE_TAG, errorString, null));
248 return send(message);
255 public boolean equals(Object obj) {
256 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
257 LOG.fine(this + " equals " + obj);
260 if (obj instanceof Requestor) {
261 Requestor dest = (Requestor) obj;
263 if (address != null && dest.address != null) {
264 if (dest.address.toString().equals(address.toString())) {
277 public int hashCode() {
280 return 37 * result + requestId.hashCode();
287 public String toString() {
288 return "Requestor " + address.toString();
291 private Requestor(PeerGroup group, EndpointAddress address, MessageElement requestId) throws IOException {
293 this.address = address;
294 this.requestId = requestId;
297 public static Requestor createRequestor(PeerGroup group, Message message, EndpointAddress address, int threshold) throws IOException {
300 if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
301 LOG.fine("create new Requestor - " + address.toString());
304 MessageElement elem = message.getMessageElement(ProxyService.REQUESTID_TAG);
306 requestor = new Requestor(group, address, elem);
307 requestor.setThreshold(threshold);
308 message.removeMessageElement(elem);
312 void setThreshold(int threshold) {
313 this.threshold = threshold;
320 private void setString(Message message, String tag, String value) {
321 StringMessageElement sme = new StringMessageElement(tag, value, null);
323 message.addMessageElement(ProxyService.PROXYNS, sme);