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