1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.gscg.common.interactionlayer.messaging;
16
17 import java.io.Serializable;
18 import java.rmi.RemoteException;
19 import java.util.ArrayList;
20
21 import nl.tudelft.simulation.event.Event;
22 import nl.tudelft.simulation.event.EventInterface;
23 import nl.tudelft.simulation.event.EventListenerInterface;
24 import nl.tudelft.simulation.event.EventProducer;
25 import nl.tudelft.simulation.event.EventType;
26 import nl.tudelft.simulation.logger.Logger;
27
28 import org.gscg.common.gui.exceptions.ReceivedUnknownEventException;
29 import org.gscg.common.interactionlayer.AnnounceInterface;
30 import org.gscg.common.interactionlayer.GlobalInteractionLayerInterface;
31 import org.gscg.common.interactionlayer.dataobjects.SocialMessage;
32 import org.gscg.common.interactionlayer.location.YellowPage;
33 import org.gscg.common.interactionlayer.timecontrol.GlobalProgressDateAndTime;
34 import org.gscg.singleuser.interactionlayer.SingleUserInteractionLayerInterface;
35
36 /***
37 * <p>
38 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
39 * Delft, the Netherlands. All rights reserved.
40 *
41 * See for project information <a href="http://www.simulation.tudelft.nl/">
42 * www.simulation.tudelft.nl </a>.
43 *
44 * The source code and binary code of this software is proprietary information
45 * of Delft University of Technology.
46 *
47 * @author <a
48 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
49 * van Houten </a>
50 * @version $Revision: 1.2 $ $Date: 2005/08/09 15:43:42 $
51 * @since 1.0.0
52 *
53 */
54 public class SocialMessaging extends EventProducer implements
55 AnnounceInterface, EventListenerInterface, Serializable
56 {
57 /*** the serial version uid */
58 private static final long serialVersionUID = 11L;
59
60 /*** event for social messages sent by a human controlled actor */
61 public static final EventType SOCIAL_MESSAGE_SENT = new EventType(
62 "SOCIAL_MESSAGE_SENT");
63
64 /*** event for social messages received by a human controlled actor */
65 public static final EventType RECEIVE_SOCIAL_MESSAGE = new EventType(
66 "RECEIVE_SOCIAL_MESSAGE");
67
68 /*** event fired when a user interface needs to be updated */
69 public static final EventType UPDATE_RECEIVED_SOCIAL_MESSAGES = new EventType(
70 "UPDATE_RECEIVED_SOCIAL_MESSAGES");
71
72 /*** event fired when a user interface needs to be updated */
73 public static final EventType UPDATE_SENT_SOCIAL_MESSAGES = new EventType(
74 "UPDATE_SENT_SOCIAL_MESSAGES");
75
76 /*** event fired when a user interface needs to be updated */
77 public static final EventType UPDATE_RECEIVED_SOCIAL_MESSAGE = new EventType(
78 "UPDATE_RECEIVED_SOCIAL_MESSAGE");
79
80 /*** event fired when a user interface needs to be updated */
81 public static final EventType UPDATE_SENT_SOCIAL_MESSAGE = new EventType(
82 "UPDATE_SENT_SOCIAL_MESSAGE");
83
84 /*** the owner */
85 private GlobalInteractionLayerInterface owner = null;
86
87 /*** the yellow page */
88 private YellowPage yellowPage = null;
89
90 /*** the events to subscribe the owner to */
91 private EventType[] eventsToSubscribeOwnerTo = {
92 SocialMessaging.SOCIAL_MESSAGE_SENT,
93 SocialMessaging.RECEIVE_SOCIAL_MESSAGE};
94
95 /*** the list with received social messages */
96 private ArrayList receivedSocialMessages = new ArrayList();
97
98 /*** the list with sent social messages */
99 private ArrayList sentSocialMessages = new ArrayList();
100
101 /***
102 * constructs a new SocialMessaging
103 *
104 * @param owner the single user interacion layer
105 * @param yellowPage the yellow page
106 */
107 public SocialMessaging(final GlobalInteractionLayerInterface owner,
108 final YellowPage yellowPage)
109 {
110 super();
111 this.owner = owner;
112 this.yellowPage = yellowPage;
113
114 try
115 {
116
117
118 this.owner
119 .addEventTypeSentByClient(SocialMessaging.SOCIAL_MESSAGE_SENT);
120
121
122 for (int i = 0; i < this.eventsToSubscribeOwnerTo.length; i++)
123 {
124 this.addListener(this.owner, this.eventsToSubscribeOwnerTo[i]);
125 if (SingleUserInteractionLayerInterface.class
126 .isAssignableFrom(owner.getClass()))
127 {
128 ((SingleUserInteractionLayerInterface) this.owner)
129 .addEventType(this.eventsToSubscribeOwnerTo[i]);
130 }
131 this.owner.addListener(this, this.eventsToSubscribeOwnerTo[i]);
132 }
133
134 this.addListener(this.owner,
135 SocialMessaging.UPDATE_RECEIVED_SOCIAL_MESSAGE);
136 if (SingleUserInteractionLayerInterface.class
137 .isAssignableFrom(owner.getClass()))
138 {
139 ((SingleUserInteractionLayerInterface) this.owner)
140 .addEventType(SocialMessaging.UPDATE_RECEIVED_SOCIAL_MESSAGE);
141 ((SingleUserInteractionLayerInterface) this.owner)
142 .addEventType(SocialMessaging.UPDATE_SENT_SOCIAL_MESSAGE);
143
144 }
145 this.addListener(this.owner,
146 SocialMessaging.UPDATE_SENT_SOCIAL_MESSAGE);
147
148
149 this.owner.addEventTypeToAnnounceList(
150 SocialMessaging.UPDATE_RECEIVED_SOCIAL_MESSAGES, this);
151 this.owner.addEventTypeToAnnounceList(
152 SocialMessaging.UPDATE_SENT_SOCIAL_MESSAGES, this);
153 } catch (RemoteException remoteException)
154 {
155 Logger.severe(this, "<init>", remoteException);
156 }
157
158 }
159
160 /***
161 * @see org.gscg.common.interactionlayer.AnnounceInterface#announce(nl.tudelft.simulation.event.EventType,
162 * boolean)
163 */
164 public void announce(final EventType eventType, final boolean announce)
165 {
166 if (eventType.equals(SocialMessaging.UPDATE_RECEIVED_SOCIAL_MESSAGES))
167 {
168 for (int i = 0; i < this.receivedSocialMessages.size(); i++)
169 {
170 this.fireEvent(new Event(
171 SocialMessaging.UPDATE_RECEIVED_SOCIAL_MESSAGE, this,
172 this.receivedSocialMessages.get(i)));
173 }
174 return;
175 }
176 if (eventType.equals(SocialMessaging.UPDATE_SENT_SOCIAL_MESSAGES))
177 {
178 for (int i = 0; i < this.sentSocialMessages.size(); i++)
179 {
180 this.fireEvent(new Event(
181 SocialMessaging.UPDATE_SENT_SOCIAL_MESSAGE, this,
182 this.sentSocialMessages.get(i)));
183 }
184 return;
185 }
186 new ReceivedUnknownEventException(this, "announce", eventType);
187 }
188
189 /***
190 * @return Returns the received social messages
191 */
192 public ArrayList getReceivedSocialMessages()
193 {
194 return this.receivedSocialMessages;
195 }
196
197 /***
198 * @return Returns the sent social messages
199 */
200 public ArrayList getSentSocialMessages()
201 {
202 return this.sentSocialMessages;
203 }
204
205 /***
206 * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
207 */
208 public void notify(final EventInterface event) throws RemoteException
209 {
210
211 if (event.getType().equals(SocialMessaging.SOCIAL_MESSAGE_SENT))
212 {
213 SocialMessage message = (SocialMessage) event.getContent();
214
215
216 message.setDate(GlobalProgressDateAndTime.getDate());
217
218 EventListenerInterface receiver = this.yellowPage.findActor(message
219 .getReceiverName());
220 receiver.notify(new Event(SocialMessaging.RECEIVE_SOCIAL_MESSAGE,
221 this, message));
222 this.sentSocialMessages.add(message);
223
224
225 this.fireEvent(new Event(
226 SocialMessaging.UPDATE_SENT_SOCIAL_MESSAGE, this, message));
227 return;
228 }
229 if (event.getType().equals(SocialMessaging.RECEIVE_SOCIAL_MESSAGE))
230 {
231 SocialMessage message = (SocialMessage) event.getContent();
232 this.receivedSocialMessages.add(message);
233 return;
234 }
235 new ReceivedUnknownEventException(this, "notify", event.getType());
236 }
237 }