1 package org.lsst.ccs.bus.jgroups;
2
3 import org.jgroups.Message;
4 import org.jgroups.ReceiverAdapter;
5 import org.lsst.ccs.bus.*;
6 import org.lsst.ccs.utilities.dispatch.CommandFor;
7 import org.lsst.ccs.utilities.dispatch.ParallelCommandDispatcher;
8 import org.lsst.ccs.utilities.dispatch.ParallelDispatchProxy;
9 import org.lsst.ccs.utilities.logging.Logger;
10
11
12
13
14
15
16
17
18
19
20 @Deprecated
21 public class JGroupsMessagingFactory extends MessagingFactory {
22 protected final static Logger log = Logger.getLogger("org.lsst.ccs.bus.jgroups");
23
24 private MessagingApplicationLayer appLayer = new MessagingApplicationLayer();
25
26
27 private ParallelCommandDispatcher<CommandListener> dispatchCommands;
28
29
30
31 private ParallelDispatchProxy<StatusListens> proxyStatus;
32 private ParallelDispatchProxy<LogListener> proxyLog ;
33
34 private ReceiverAdapter receiverCommands;
35 private ReceiverAdapter receiverStatus;
36 private ReceiverAdapter receiverLog;
37
38 private GroupTopic commandTopic;
39 private GroupTopic statusTopic;
40 private GroupTopic logTopic;
41
42 public JGroupsMessagingFactory() {
43 log.info("JGROUPS: creating factory");
44 try {
45
46 commandTopic = GroupTopic.create("command", "UDP(mcast_port=26969)");
47 statusTopic = GroupTopic.create("status", "UDP(mcast_port=36969)");
48 logTopic = GroupTopic.create("log", "UDP(mcast_port=46969)");
49 } catch (Exception e) {
50 log.error("group creation", e);
51 throw new Error("group communication cannot start: " + e);
52 }
53 }
54
55 synchronized void initListenToCommand() {
56 if (null == commandTopic.getAdapter()) {
57 log.info("JGROUPS:initializing command listen");
58 dispatchCommands = new ParallelCommandDispatcher<CommandListener>();
59
60 receiverCommands = new ReceiverAdapter() {
61 public void receive(Message msg) {
62 try {
63 Object payload = msg.getObject();
64 if (payload instanceof Command) {
65 final Command command = (Command) payload;
66 log.info("####receiving command: " + command);
67
68
69
70 CommandFor<CommandListener> toBeDone = new CommandFor<CommandListener>() {
71 public void invokeOn(CommandListener listener) {
72
73 Command transformed = appLayer.receivingCommand(command);
74
75 }
76 public String toString() {
77 return "CommandFor #" + command.hashCode() ;
78 }
79 };
80 dispatchCommands.dispatchCommand(toBeDone);
81 } else if (payload instanceof CommandAckOrReply) {
82 final CommandAckOrReply reply = (CommandAckOrReply) payload;
83 log.info("####receiving reply: "+reply.hashCode()+ " " + reply);
84
85
86
87 CommandFor<CommandListener> toBeDone = new CommandFor<CommandListener>() {
88 public void invokeOn(CommandListener listener) {
89
90 CommandAckOrReply transformed = appLayer.receivingReply(reply);
91 if (null != transformed){
92 if(transformed instanceof CommandReply) {
93 listener.onReply((CommandReply) transformed);
94 } else {
95 listener.onAck((CommandAck)transformed);
96 }
97 }
98 }
99 public String toString() {
100 return "ReplyFor #" + reply.hashCode() ;
101 }
102 };
103 dispatchCommands.dispatchCommand(toBeDone);
104
105 } else {
106 log.warning("Message payload type "
107 + payload.getClass().getName()
108 + " not handled " + payload);
109 }
110 } catch (Exception exc) {
111 log.error("object not deserialized: ", exc);
112 }
113 }
114 };
115 commandTopic.setAdapter(receiverCommands);
116
117 }
118
119 }
120
121 @Override
122 public void addCommandListener(CommandListener l) {
123 initListenToCommand();
124 dispatchCommands.addExecutant(l);
125 }
126
127 @Override
128 public void removeCommandListener(CommandListener l) {
129
130 }
131
132 synchronized void initListenToStatus() {
133 if (null == statusTopic.getAdapter()) {
134 log.info("JGROUPS: initializing status listen");
135 proxyStatus = new ParallelDispatchProxy<StatusListens>(StatusListens.class);
136 receiverStatus = new ReceiverAdapter() {
137 StatusListens proxy = proxyStatus.getProxy();
138
139 public void receive(Message msg) {
140 try {
141 Object payload = msg.getObject();
142 if (payload instanceof Status) {
143 Status status = appLayer.receivingStatus((Status) payload);
144
145 } else {
146 log.warning("Message payload type "
147 + payload.getClass().getName()
148 + " not handled " + payload);
149 }
150 } catch (Exception exc) {
151 log.error("object not deserialized: ", exc);
152 }
153 }
154 };
155 statusTopic.setAdapter(receiverStatus);
156 }
157
158 }
159
160 @Override
161 public void addStatusListener(StatusListens l) {
162 initListenToStatus();
163 proxyStatus.addExecutant(l);
164 }
165
166 @Override
167 public void removeStatusListener(StatusListens l) {
168
169 }
170
171 synchronized void initListenToLog() {
172 if (null == logTopic.getAdapter()) {
173 log.info("JGROUPS: initializing log listen");
174 proxyLog = new ParallelDispatchProxy<LogListener>(LogListener.class);
175 receiverLog = new ReceiverAdapter() {
176 LogListener proxy = proxyLog.getProxy();
177
178 public void receive(Message msg) {
179 try {
180 Object payload = msg.getObject();
181 if (payload instanceof LogEvent) {
182 LogEvent event = appLayer.receivingLog((LogEvent) payload);
183 if (event != null) proxy.onLog(event);
184 } else {
185 log.warning("Message payload type "
186 + payload.getClass().getName()
187 + " not handled " + payload);
188 }
189 } catch (Exception exc) {
190 log.error("object not deserialized: ", exc);
191 }
192 }
193 };
194 logTopic.setAdapter(receiverLog);
195 }
196
197 }
198
199 @Override
200 public void addLogListener(LogListener l) {
201 initListenToLog();
202 proxyLog.addExecutant(l);
203 }
204
205 @Override
206 public void removeLogListener(LogListener l) {
207
208 }
209
210
211 @Override
212 public void addCommandListener(CommandListener l, String selector) {
213 initListenToCommand();
214 dispatchCommands.addExecutant(l);
215 }
216
217 @Override
218 public void addStatusListener(StatusListens l, String selector) {
219 initListenToStatus();
220 proxyStatus.addExecutant(l);
221 }
222
223 @Override
224 public void addLogListener(LogListener l, String selector) {
225 initListenToLog();
226 proxyLog.addExecutant(l);
227 }
228
229 @Override
230 public void sendCommand(Command cmd) {
231
232 Command todo = appLayer.commandForSending(cmd);
233 if (null == todo) return;
234 try {
235 log.info("JGROUPS: sending command from" + MessagingFactory.getInstance().getSubsystemName() +
236 "(" + cmd.getOrigin() +" ?) to " + cmd.getDestination());
237 commandTopic.sendMessage(todo);
238 } catch (Exception e) {
239 log.error("sending command message", e);
240 }
241 }
242
243 @Override
244 public void sendStatus(Status status) {
245 log.info("JGROUPS: sending status");
246 Status todo = appLayer.statusForSending(status);
247 if (null == todo) return;
248 try {
249 statusTopic.sendMessage(todo);
250 } catch (Exception e) {
251 log.error("sending status message", e);
252 }
253 }
254
255 @Override
256 public void sendLogEvent(LogEvent evt) {
257
258 LogEvent todo = appLayer.logForSending(evt);
259 if (null == todo) return;
260 try {
261 logTopic.sendMessage(todo);
262 } catch (Exception e) {
263 log.error("sending log message", e);
264 }
265 }
266
267 @Override
268 public String getToken() {
269 return appLayer.getToken();
270 }
271
272
273
274 @Override
275 public void reply(CommandAckOrReply cmd) {
276
277 CommandAckOrReply todo = appLayer.replyForSending(cmd);
278 try {
279 commandTopic.sendMessage(todo);
280 } catch (Exception e) {
281 log.error("sending reply message", e);
282 }
283 }
284
285 public boolean isReplyRequested() {
286 return appLayer.isReplyRequested();
287 }
288 @Override
289 public void noAutoReply() {
290 appLayer.noAutoReply();
291 }
292
293 public void shutdownBusAccess() {
294 GroupTopic.closeAll();
295
296 }
297
298 @Override
299 public void addMembershipListener(BusMembershipListener l) {
300 throw new UnsupportedOperationException("Not implementing new method in deprecated class.");
301 }
302
303 @Override
304 public void removeMembershipListener(BusMembershipListener l) {
305 throw new UnsupportedOperationException("Not implementing new method in deprecated class.");
306 }
307 }