KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > protocol > utobcast > DeliveredMessageRouterImpl


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

24
25 package org.objectweb.dream.protocol.utobcast;
26
27 import java.util.Map JavaDoc;
28
29 import org.objectweb.dream.AbstractComponent;
30 import org.objectweb.dream.InterruptedPushException;
31 import org.objectweb.dream.Push;
32 import org.objectweb.dream.PushException;
33 import org.objectweb.dream.message.Message;
34 import org.objectweb.dream.protocol.utobcast.message.UTOBcastChunk;
35 import org.objectweb.fractal.api.NoSuchInterfaceException;
36 import org.objectweb.fractal.api.control.IllegalBindingException;
37 import org.objectweb.fractal.api.control.IllegalLifeCycleException;
38 import org.objectweb.util.monolog.api.BasicLevel;
39
40 /**
41  * Implementation of the DeliveredMessageRouter component. This component routes
42  * delivered messages on one of its outputs. It has one output for messages that
43  * were broadcast by the node itself, and one output for messages broadcast by
44  * other nodes.
45  */

46 public class DeliveredMessageRouterImpl extends AbstractComponent
47     implements
48       Push
49 {
50
51   // ---------------------------------------------------------------------------
52
// Fields of this class
53
// ---------------------------------------------------------------------------
54

55   /** The client interfaces. */
56   Push localMessageItf;
57   Push remoteMessageItf;
58   ProcessMembership processMembershipItf;
59
60   short myProcessId;
61   boolean configured = false;
62
63   static final String JavaDoc LOCAL_MESSAGE_ITF_NAME = "local-message";
64   static final String JavaDoc REMOTE_MESSAGE_ITF_NAME = "remote-message";
65
66   // ---------------------------------------------------------------------------
67
// Constructor
68
// ---------------------------------------------------------------------------
69

70   /**
71    * Constructor.
72    */

73   public DeliveredMessageRouterImpl()
74   {
75   }
76
77   // ---------------------------------------------------------------------------
78
// Implementation of the Push interface
79
// ---------------------------------------------------------------------------
80

81   /**
82    * @see org.objectweb.dream.Push#push(org.objectweb.dream.message.Message,
83    * java.util.Map)
84    */

85   public void push(Message message, Map JavaDoc context) throws PushException
86   {
87     if (!configured)
88     {
89       try
90       {
91         configure();
92       }
93       catch (InterruptedException JavaDoc e)
94       {
95         throw new InterruptedPushException(e);
96       }
97     }
98     UTOBcastChunk chunk = (UTOBcastChunk) message
99         .getChunk(UTOBcastChunk.DEFAULT_NAME);
100
101     if (chunk.getProcessFrom().getId() == myProcessId)
102     {
103       if (logger.isLoggable(BasicLevel.DEBUG))
104       {
105         logger.log(BasicLevel.DEBUG, "Deliver a local message " + message);
106       }
107       localMessageItf.push(message, context);
108     }
109     else
110     {
111       if (logger.isLoggable(BasicLevel.DEBUG))
112       {
113         logger.log(BasicLevel.DEBUG, "Deliver a remote message " + message);
114       }
115       remoteMessageItf.push(message, context);
116     }
117   }
118
119   // ---------------------------------------------------------------------------
120
// Utility methods.
121
// ---------------------------------------------------------------------------
122

123   void configure() throws InterruptedException JavaDoc
124   {
125     myProcessId = processMembershipItf.getMyself().getId();
126     configured = true;
127   }
128
129   // ---------------------------------------------------------------------------
130
// Implementation of the BindingController interface
131
// ---------------------------------------------------------------------------
132

133   /**
134    * @see org.objectweb.fractal.api.control.BindingController#bindFc(java.lang.String,
135    * java.lang.Object)
136    */

137   public void bindFc(String JavaDoc clientItfName, Object JavaDoc serverItf)
138       throws NoSuchInterfaceException, IllegalBindingException,
139       IllegalLifeCycleException
140   {
141     super.bindFc(clientItfName, serverItf);
142     if (clientItfName.equals(LOCAL_MESSAGE_ITF_NAME))
143     {
144       localMessageItf = (Push) serverItf;
145     }
146     else if (clientItfName.equals(REMOTE_MESSAGE_ITF_NAME))
147     {
148       remoteMessageItf = (Push) serverItf;
149     }
150     else if (clientItfName.equals(ProcessMembership.ITF_NAME))
151     {
152       processMembershipItf = (ProcessMembership) serverItf;
153     }
154
155   }
156
157   /**
158    * @see org.objectweb.fractal.api.control.BindingController#listFc()
159    */

160   public String JavaDoc[] listFc()
161   {
162     return new String JavaDoc[]{LOCAL_MESSAGE_ITF_NAME, REMOTE_MESSAGE_ITF_NAME,
163         ProcessMembership.ITF_NAME};
164   }
165
166 }
Popular Tags