View Javadoc

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   * Change with a complete rehaul of communication.
14   * each subssytem will have its Jgroup address
15   * and so messages cna be adressed to specific subsystems
16   * view can also tell if a subssysem is down!
17   *
18   * @author bamade
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      // due to ThreadLocal variables in appLayer each should be in a different Thread!
27      private ParallelCommandDispatcher<CommandListener> dispatchCommands;
28      //@TODO: is it necessary? (added later due to bug in dispatching)
29      //private ParallelCommandDispatcher<CommandListener> dispatchReplies;
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              //@TODO: parameterize protocol stack for groups
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);  //CHANGE and get Logger
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              //dispatchReplies = new ParallelCommandDispatcher<CommandListener>();
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                              //System.out.println("####receiving command: " + command);
68                              //String name = Subsystem.getCurrentSubsystemName();
69                              //log.info("command SUBSYSTEM NAME :"+name);
70                              CommandFor<CommandListener> toBeDone = new CommandFor<CommandListener>() {
71                                  public void invokeOn(CommandListener listener) {
72                                      // this is executed in a separate thread so calling applayer only here
73                                      Command transformed = appLayer.receivingCommand(command);
74                                      //if (null != transformed) listener.onCommand(transformed);
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                              //System.out.println("####receiving reply: " +reply.hashCode()+"  "+ reply);
85                              //String name = Subsystem.getCurrentSubsystemName();
86                              //log.info("reply SUBSYSTEM NAME :"+name);
87                              CommandFor<CommandListener> toBeDone = new CommandFor<CommandListener>() {
88                                  public void invokeOn(CommandListener listener) {
89                                      // this is executed in a separate thread so calling applayer only here
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                             //dispatchReplies.dispatchCommand(toBeDone);
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         //To change body of implemented methods use File | Settings | File Templates.
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                             //if (null != status) proxy.onStatus(status);
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         //To change body of implemented methods use File | Settings | File Templates.
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         //To change body of implemented methods use File | Settings | File Templates.
208     }
209 
210     // @TODO: implement, those filters????
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         //System.out.println("#### sending command ");
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);  //CHANGE and get Logger
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);  //CHANGE and get Logger
252         }
253     }
254 
255     @Override
256     public void sendLogEvent(LogEvent evt) {
257         //System.out.println("JGROUPS: sending log");
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);  //CHANGE and get Logger
264         }
265     }
266 
267     @Override
268     public String getToken() {
269         return appLayer.getToken();
270     }
271 
272 
273     /* @TODO : has to be called on the same thread as the command check implementation*/
274     @Override
275     public void reply(CommandAckOrReply cmd) {
276         //System.out.println("#### sending reply ");
277         CommandAckOrReply todo = appLayer.replyForSending(cmd);
278         try {
279             commandTopic.sendMessage(todo);
280         } catch (Exception e) {
281             log.error("sending reply message", e);  //CHANGE and get Logger
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         //TODO: stop listening? to proxies?
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 }