KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > adapters > MulticastRequestAdapterThread


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.adapters;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.objectweb.tribe.common.Member;
31 import org.objectweb.tribe.common.log.Trace;
32
33 /**
34  * This class defines a MulticastRequestAdapterThread
35  *
36  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
37  * @version 1.0
38  */

39 public class MulticastRequestAdapterThread extends Thread JavaDoc
40 {
41
42   private MulticastRequestListener multicastRequestListener;
43   private PullPushAdapter pullPushAdapter;
44   private MulticastRequestAdapterMessage requestReply;
45   private Object JavaDoc singleTheadedCallback;
46   private Member me;
47   private static Trace logger = Trace
48                                                     .getLogger("org.objectweb.tribe.blocks.multicastdispatcher");
49
50   /**
51    * Creates a new <code>MulticastRequestAdapterThread</code> object
52    *
53    * @param listener the multicastRequestListener to call
54    * @param requestReply the request to handle
55    * @param singleTheadedCallback result from handleMessageSingleThreaded
56    * @param pullPushAdapter pullPushAdapter for sending the reply
57    * @param senderMembership membership of the sender
58    */

59   public MulticastRequestAdapterThread(MulticastRequestListener listener,
60       MulticastRequestAdapterMessage requestReply,
61       Object JavaDoc singleTheadedCallback, PullPushAdapter pullPushAdapter,
62       Member senderMembership)
63   {
64     super("MulticastRequestAdapterThread");
65     this.multicastRequestListener = listener;
66     this.requestReply = requestReply;
67     this.singleTheadedCallback = singleTheadedCallback;
68     this.me = senderMembership;
69     this.pullPushAdapter = pullPushAdapter;
70   }
71
72   /**
73    * Delivers the message to the listener and send back the response.
74    */

75   public void run()
76   {
77     Member replyTo = requestReply.getSender();
78
79     Serializable JavaDoc reply = multicastRequestListener.handleMessageMultiThreaded(
80         requestReply.getMessage(), replyTo, singleTheadedCallback);
81     if (!requestReply.isRequestOnly())
82     {
83       // We cannot reuse the same object in case multiple receivers are in the
84
// same virtual machine and the message was not delivered yet to the
85
// other receivers (they would receive the reply instead of the request
86
// if we call requestReply.setReply()).
87
MulticastRequestAdapterMessage answer = new MulticastRequestAdapterMessage(
88           reply, me, requestReply.getUid(),
89           MulticastRequestAdapterMessage.REPLY);
90       ArrayList JavaDoc replyList = new ArrayList JavaDoc(1);
91       replyList.add(replyTo);
92       try
93       {
94         if (logger.isDebugEnabled())
95           logger.debug("Replying to " + replyTo + " for message "
96               + requestReply.getUid());
97         pullPushAdapter.send(answer, replyList);
98       }
99       catch (Exception JavaDoc e)
100       {
101         logger
102             .info("Error while replying to " + replyTo + " for message "
103                 + requestReply.getUid() + " (" + requestReply.getMessage()
104                 + ")", e);
105       }
106     }
107   }
108
109 }
Popular Tags