KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > control > ControlSignalMessageSender


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.kernel.control;
48
49
50 import java.util.ArrayList JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Set JavaDoc;
53
54 import org.mr.MantaAgent;
55 import org.mr.MantaAgentConstants;
56 import org.mr.MantaException;
57 import org.mr.core.protocol.MantaBusMessage;
58 import org.mr.core.protocol.MantaBusMessageConsts;
59 import org.mr.core.util.SystemTime;
60 import org.mr.kernel.services.MantaService;
61 import org.mr.kernel.services.ServiceActor;
62 import org.mr.kernel.services.ServiceConsumer;
63 import org.mr.kernel.world.WorldModeler;
64
65
66 /**
67  * when there is no auto discovery this object handle the advertisement of service roles
68  * @since on 17/05/2004
69  * @author Amir Shevat
70  *
71  */

72 public class ControlSignalMessageSender {
73
74     /**
75      * Notifies all the other layer that this layer is a producer or a consumer
76      * (role) of a queue or a topic (service). This method MUST be called before
77      * you perform operations on a service. example: before publishing stock
78      * messages to a stock topic you call this method with a producer role,
79      * after that you can publish as many message as you want on this topic.
80      * If you publish a message to a topic without using this method no one will
81      * consume this message and it will be lost.
82      * for a given service and a given role a one time advertise should be done,
83      * unless you recall the role using the recallService(ServiceActor
84      * serviceActor). You can recall your role to a service at any given time.
85      *
86      * @param serviceActor hold the service name and the role of the invoker
87      * of this method
88      * @see org.mr.kernel.services.ServiceProducer
89      * @see org.mr.kernel.services.ServiceConsumer
90      * @param agent the MantaAgent that advertises the service
91      * @throws MantaException if service not found
92      */

93     public void advertiseService(ServiceActor actor, MantaAgent agent) throws MantaException {
94
95         WorldModeler world = agent.getSingletonRepository().getWorldModeler();
96         MantaService service = MantaAgent.getInstance().getService(actor.getServiceName(), actor.getServiceType());
97         if (service == null) { throw new MantaException("No such Service " + actor.getServiceName(), MantaException.ID_INVALID_ARGUMENTS); }
98                
99         // insert the control payload
100
ControlSignal control = new ControlSignal(ControlSignal.OPERATION_TYPE_ADVERTISE);
101         if (!service.isServiceUpdated()) {
102             control.getParams().put(ControlSignal.SERVICE_UPDATE_NEEDED, MantaBusMessageConsts.HEADER_VALUE_TRUE);
103             service.setServiceUpdated(true);
104         }
105         
106
107
108         Set JavaDoc agents = world.getAgents(world.getDefaultDomainName());
109         int size = agents.size();
110         List JavaDoc agentList = new ArrayList JavaDoc(agents);
111         // if no one is listening to this topic we should not send it
112
if (size == 0) return;
113        
114         for (int i = 0; i < size; i++) {
115             MantaBusMessage msg = MantaBusMessage.getInstance();
116             msg.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CONTROL);
117             msg.setPayload(control);
118             String JavaDoc agentName = (String JavaDoc) agentList.get(i);
119             if (!agentName.equals(agent.getAgentName())) {
120                 ServiceConsumer adderss = new ServiceConsumer(agentName,world.getDefaultDomainName(), ControlSignalMessageConsumer.CONTROL_PSEUDO_SERVICE_NAME,(byte)0,MantaAgentConstants.AUTO_ACK);
121                 adderss.setID("ps:"+agentName);
122                 msg.setRecipient(adderss);
123
124                 agent.send(msg, actor, MantaAgentConstants.NON_PERSISTENT, MantaAgentConstants.HIGH, MantaAgentConstants.CONTROL_MESSAGES_TTL+SystemTime.gmtCurrentTimeMillis());
125
126             }
127         }
128         
129
130     }//advertiseService
131

132
133      /**
134      * Recalls a previously advertised role (consumer or producer) in service
135      * (queue or a topic) which is not longer accessible from this layer. If at
136      * runtime you want to stop being a consumer or a producer (i.e., before you go
137      * offline) you SHOULD use this method to recall the role in the service
138      *
139      * @see org.mr.kernel.services.ServiceProducer
140      * @see org.mr.kernel.services.ServiceConsumer
141      * @throws MantaException
142      * if service not found
143      */

144     public void recallService(ServiceActor serviceActor, MantaAgent agent) throws MantaException {
145
146         WorldModeler world = agent.getSingletonRepository().getWorldModeler();
147         MantaService service =agent.getService(serviceActor.getServiceName(), serviceActor.getServiceType());
148         if (service == null) { throw new MantaException("No such Service " + serviceActor.getServiceName(), MantaException.ID_INVALID_ARGUMENTS); }
149         //checks if i have the security to do this
150

151         // insert the control payload
152
ControlSignal control = new ControlSignal(ControlSignal.OPERATION_TYPE_RECALL);
153         
154
155
156         Set JavaDoc agents = world.getAgents(world.getDefaultDomainName());
157         int size = agents.size();
158         List JavaDoc agentList = new ArrayList JavaDoc(agents);
159         // if no one is listening to this topic we should not send it
160
if (size == 0) return;
161         
162         for (int i = 0; i < size; i++) {
163             MantaBusMessage cbm = MantaBusMessage.getInstance();
164             cbm.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CONTROL);
165             cbm.setPayload(control);
166             String JavaDoc agentName = (String JavaDoc) agentList.get(i);
167             ServiceConsumer adderss = new ServiceConsumer(agentName,agent.getDomainName(), ControlSignalMessageConsumer.CONTROL_PSEUDO_SERVICE_NAME,(byte)0,MantaAgentConstants.AUTO_ACK);
168             adderss.setID("ps:"+agentName);
169             cbm.setRecipient(adderss);
170             agent.send(cbm, serviceActor, MantaAgentConstants.NON_PERSISTENT, MantaAgentConstants.HIGH, MantaAgentConstants.CONTROL_MESSAGES_TTL+SystemTime.gmtCurrentTimeMillis());
171
172         }
173         
174
175        
176     }//recallService
177

178
179     public void recallDurableSubscription(ServiceActor serviceActor, MantaAgent agent) throws MantaException {
180
181         WorldModeler world = agent.getSingletonRepository().getWorldModeler();
182         MantaService service = agent.getService(serviceActor.getServiceName(), serviceActor.getServiceType());
183         if (service == null) { throw new MantaException("No such Service " + serviceActor.getServiceName(), MantaException.ID_INVALID_ARGUMENTS); }
184                
185         // insert the control payload
186
ControlSignal control = new ControlSignal(ControlSignal.OPERATION_TYPE_UNSUBSCRIBE_DURABLE);
187         
188
189
190         Set JavaDoc agents = world.getAgents(world.getDefaultDomainName());
191         int size = agents.size();
192         List JavaDoc agentList = new ArrayList JavaDoc(agents);
193         // if no one is listening to this topic we should not send it
194
if (size == 0) return;
195         
196         for (int i = 0; i < size; i++) {
197             MantaBusMessage cbm = MantaBusMessage.getInstance();
198             cbm.setMessageType(MantaBusMessageConsts.MESSAGE_TYPE_CONTROL);
199             cbm.setPayload(control);
200             String JavaDoc agentName = (String JavaDoc) agentList.get(i);
201             ServiceConsumer adderss = new ServiceConsumer(agentName,agent.getDomainName(), ControlSignalMessageConsumer.CONTROL_PSEUDO_SERVICE_NAME,(byte)0,MantaAgentConstants.AUTO_ACK);
202             adderss.setID("ps:"+agentName);
203             cbm.setRecipient(adderss);
204             agent.send(cbm, serviceActor, MantaAgentConstants.NON_PERSISTENT, MantaAgentConstants.HIGH, MantaAgentConstants.CONTROL_MESSAGES_TTL+SystemTime.gmtCurrentTimeMillis());
205
206         }
207         
208         
209     }
210 }
Popular Tags