1 |
ajm |
1.1 |
package uk.org.iscream.mnote; |
2 |
|
|
|
3 |
|
|
import java.util.Vector; |
4 |
|
|
|
5 |
|
|
class MNoteProtocolHandler { |
6 |
|
|
|
7 |
|
|
public void processMessage(String message) { |
8 |
|
|
if (message.startsWith("REGISTER")) { |
9 |
|
|
fireRegisterEvent(new RegisterEvent(message.substring(8))); |
10 |
|
|
} else if (message.startsWith("NOTIFY")) { |
11 |
|
|
fireNotificationEvent(new NotificationEvent(message.substring(6))); |
12 |
|
|
} |
13 |
|
|
} |
14 |
|
|
|
15 |
|
|
public void sendRegister(String name) { |
16 |
|
|
fireSendEvent(new SendEvent("REGISTER" + name)); |
17 |
|
|
} |
18 |
|
|
|
19 |
|
|
public void sendNotification(String message) { |
20 |
|
|
fireSendEvent(new SendEvent("NOTIFY" + message)); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
public void addSendListener(SendListener listener) { |
24 |
|
|
synchronized (_sendListeners) { |
25 |
|
|
_sendListeners.add(listener); |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
public void addRegisterListener(RegisterListener listener) { |
30 |
|
|
synchronized (_registerListeners) { |
31 |
|
|
_registerListeners.add(listener); |
32 |
|
|
} |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
public void addNotificationListener(NotificationListener listener) { |
36 |
|
|
synchronized (_notificationListeners) { |
37 |
|
|
_notificationListeners.add(listener); |
38 |
|
|
} |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
private void fireSendEvent(SendEvent se) { |
42 |
|
|
if (_sendListeners.isEmpty()) { |
43 |
|
|
return; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
Vector v; |
47 |
|
|
synchronized (_sendListeners) { |
48 |
|
|
v = (Vector) _sendListeners.clone(); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
for ( int i = 0; i < v.size(); i++) { |
52 |
|
|
SendListener sl = (SendListener) v.elementAt(i); |
53 |
|
|
sl.acceptSendEvent(se); |
54 |
|
|
} |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
private void fireRegisterEvent(RegisterEvent re) { |
58 |
|
|
if (_registerListeners.isEmpty()) { |
59 |
|
|
return; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
Vector v; |
63 |
|
|
synchronized (_registerListeners) { |
64 |
|
|
v = (Vector) _registerListeners.clone(); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
for ( int i = 0; i < v.size(); i++) { |
68 |
|
|
RegisterListener rl = (RegisterListener) v.elementAt(i); |
69 |
|
|
rl.acceptRegisterEvent(re); |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
private void fireNotificationEvent(NotificationEvent ne) { |
74 |
|
|
if (_notificationListeners.isEmpty()) { |
75 |
|
|
return; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
Vector v; |
79 |
|
|
synchronized (_notificationListeners) { |
80 |
|
|
v = (Vector) _notificationListeners.clone(); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
for ( int i = 0; i < v.size(); i++) { |
84 |
|
|
NotificationListener nl = (NotificationListener) v.elementAt(i); |
85 |
|
|
nl.acceptNotificationEvent(ne); |
86 |
|
|
} |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
private Vector _sendListeners = new Vector(); |
90 |
|
|
private Vector _registerListeners = new Vector(); |
91 |
|
|
private Vector _notificationListeners = new Vector(); |
92 |
|
|
} |