1 |
package appl.parallel.client; |
2 |
|
3 |
import java.rmi.RemoteException; |
4 |
import java.rmi.server.UnicastRemoteObject; |
5 |
import java.util.Vector; |
6 |
|
7 |
import org.apache.log4j.LogManager; |
8 |
import org.apache.log4j.Logger; |
9 |
|
10 |
import appl.ext.XuluConfig; |
11 |
import appl.parallel.event.CommEventSink; |
12 |
import appl.parallel.event.RemoteEvent; |
13 |
import appl.parallel.event.TimeEvent; |
14 |
import appl.parallel.event.TimeMonitor; |
15 |
import appl.parallel.event.TransferEvent; |
16 |
import appl.parallel.event.TransferMonitor; |
17 |
import appl.parallel.services.Service; |
18 |
import appl.parallel.util.Helper; |
19 |
|
20 |
/** |
21 |
* Starts a Service for receiving local and remote events. Monitors can register |
22 |
* to this service. All events are forwarded the registered listeners. The |
23 |
* service tries to get info for registry port from property |
24 |
* <code>XuluClient.registryport</code> in {@link XuluConfig}. |
25 |
* |
26 |
* @author Dominik Appl |
27 |
*/ |
28 |
public class RemoteEventHandler extends UnicastRemoteObject implements Service, |
29 |
CommEventSink { |
30 |
|
31 |
private final Logger LOG = LogManager.getLogger(this.getClass().getName()); |
32 |
|
33 |
// defaults |
34 |
private String bindingName = "RemoteEventReceiver"; |
35 |
|
36 |
private int registryPort = 1099; |
37 |
|
38 |
private Vector<TimeMonitor> timeMonitors = new Vector<TimeMonitor>(); |
39 |
|
40 |
private Vector<TransferMonitor> transferMonitors = new Vector<TransferMonitor>(); |
41 |
|
42 |
private boolean timeMonitoring = false; |
43 |
|
44 |
private boolean transferMonitoring = false; |
45 |
|
46 |
private boolean isRunning; |
47 |
|
48 |
/** |
49 |
* @throws RemoteException |
50 |
* @throws RemoteException |
51 |
* if binding fails |
52 |
*/ |
53 |
public RemoteEventHandler() throws RemoteException { |
54 |
super(); |
55 |
isRunning = false; |
56 |
} |
57 |
|
58 |
/** |
59 |
* tries to bind to a running registry or creates one. |
60 |
* |
61 |
* @throws RemoteException |
62 |
* if binding fails |
63 |
*/ |
64 |
private void bind() throws RemoteException { |
65 |
// try to get port from registry |
66 |
int port = XuluConfig.getXuluConfig().getIntProperty( |
67 |
"XuluClient.registryport"); |
68 |
if (port != 0) |
69 |
registryPort = port; |
70 |
// bind it |
71 |
CommEventSink sink = this; |
72 |
Helper.bind(bindingName, sink, registryPort); |
73 |
} |
74 |
|
75 |
/* |
76 |
* (non-Javadoc) |
77 |
* |
78 |
* @see appl.parallel.services.Service#isRunning() |
79 |
*/ |
80 |
public boolean isRunning() { |
81 |
return isRunning; |
82 |
} |
83 |
|
84 |
/* |
85 |
* (non-Javadoc) |
86 |
* |
87 |
* @see appl.parallel.services.Service#startService() |
88 |
*/ |
89 |
public void startService() { |
90 |
try { |
91 |
bind(); |
92 |
} catch (RemoteException e) { |
93 |
LOG.error("Could not bind " + bindingName |
94 |
+ " to the registry! Eventsystem will fail."); |
95 |
} |
96 |
isRunning = true; |
97 |
} |
98 |
|
99 |
/* |
100 |
* (non-Javadoc) |
101 |
* |
102 |
* @see appl.parallel.services.Service#stopService() |
103 |
*/ |
104 |
public void stopService() { |
105 |
unbind(); |
106 |
isRunning = false; |
107 |
} |
108 |
|
109 |
/** |
110 |
* unbinds the Service |
111 |
*/ |
112 |
private void unbind() { |
113 |
Helper.unbind(bindingName); |
114 |
} |
115 |
|
116 |
public void fireRemoteEvent(TimeEvent t) throws RemoteException { |
117 |
for (TimeMonitor timeMonitor : timeMonitors) { |
118 |
timeMonitor.receiveTimeEvent(t); |
119 |
} |
120 |
} |
121 |
|
122 |
|
123 |
public void fireRemoteEvent(TransferEvent t) throws RemoteException { |
124 |
for (TransferMonitor transferMonitor : transferMonitors) { |
125 |
transferMonitor.receiveTransferEvent(t); |
126 |
} |
127 |
} |
128 |
|
129 |
|
130 |
|
131 |
/** |
132 |
* Adds a new time monitor |
133 |
* |
134 |
* @param monitor |
135 |
* a time monitor |
136 |
*/ |
137 |
public void addTimeEventListener(TimeMonitor monitor) { |
138 |
if (monitor != null) { |
139 |
timeMonitors.add(monitor); |
140 |
timeMonitoring = true; |
141 |
} |
142 |
} |
143 |
|
144 |
/** |
145 |
* Adds a new {@link TransferMonitor} |
146 |
* |
147 |
* @param monitor |
148 |
* a time monitor |
149 |
*/ |
150 |
public void addTransferEventListener(TransferMonitor monitor) { |
151 |
if (monitor != null) { |
152 |
transferMonitors.add(monitor); |
153 |
transferMonitoring = true; |
154 |
} |
155 |
} |
156 |
|
157 |
/** |
158 |
* removes the given monitor |
159 |
* |
160 |
* @param monitor |
161 |
*/ |
162 |
public void removeTimeMonitor(TimeMonitor monitor) { |
163 |
timeMonitors.remove(monitor); |
164 |
if (timeMonitors.size() == 0) |
165 |
timeMonitoring = false; |
166 |
} |
167 |
|
168 |
/** |
169 |
* adds the given Monitor |
170 |
* |
171 |
* @param monitor |
172 |
*/ |
173 |
public void removeTransferMonitor(TransferMonitor monitor) { |
174 |
transferMonitors.remove(monitor); |
175 |
if (transferMonitors.size() == 0) |
176 |
transferMonitoring = false; |
177 |
|
178 |
} |
179 |
|
180 |
/** |
181 |
* @return true if the service is running and time monitoring is enabled |
182 |
*/ |
183 |
public boolean isTimeMonitoringEnabled() throws RemoteException { |
184 |
return timeMonitoring; |
185 |
} |
186 |
|
187 |
/** |
188 |
* @return true if the service is running and transfer monitoring is enabled |
189 |
*/ |
190 |
public boolean isTransferMonitoringEnabled() throws RemoteException { |
191 |
return transferMonitoring; |
192 |
} |
193 |
|
194 |
/* |
195 |
* (non-Javadoc) |
196 |
* |
197 |
* @see appl.parallel.event.RemoteEventSink#fireRemoteEvent(appl.parallel.event.RemoteEvent) |
198 |
*/ |
199 |
public void fireRemoteEvent(RemoteEvent e) throws RemoteException { |
200 |
if (e instanceof TimeEvent) |
201 |
fireRemoteEvent((TimeEvent) e); |
202 |
else if (e instanceof TransferEvent) |
203 |
fireRemoteEvent((TransferEvent) e); |
204 |
else |
205 |
// All events should be handled properly |
206 |
System.err.println("Received unknown event of class " |
207 |
+ e.getClass().getName()); |
208 |
|
209 |
} |
210 |
|
211 |
/* |
212 |
* (non-Javadoc) |
213 |
* |
214 |
* @see appl.parallel.event.RemoteEventSink#fireRemoteEvents(appl.parallel.event.RemoteEvent[]) |
215 |
*/ |
216 |
public void fireRemoteEvents(RemoteEvent[] e) throws RemoteException { |
217 |
for (RemoteEvent event : e) { |
218 |
fireRemoteEvent(event); |
219 |
} |
220 |
} |
221 |
|
222 |
} |