1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package snifos.communication.test;
21
22 import org.apache.log4j.Logger;
23
24 import org.custommonkey.xmlunit.Diff;
25
26 import org.dom4j.Document;
27 import org.dom4j.DocumentException;
28 import org.dom4j.DocumentHelper;
29
30 import org.xml.sax.SAXException;
31
32 import snifos.common.UserId;
33
34 import snifos.common.test.DummyMessages;
35
36 import snifos.communication.CommModule;
37
38 import snifos.server.Server;
39 import snifos.server.SnifosServer;
40
41 import java.io.IOException;
42
43 import java.util.Properties;
44 import java.util.Random;
45
46 import javax.xml.parsers.ParserConfigurationException;
47
48
49 /***
50 * Dummy implementation of communication module. It just simulates connections
51 * from few users, sends dummy messages from them and checks if responses are
52 * the responses which were expected.
53 * @version $Id: DummyCommModule.java,v 1.3 2004/05/08 21:55:31 mwerla Exp $
54 */
55 public class DummyCommModule implements CommModule {
56 private static Logger logger = Logger.getLogger(DummyCommModule.class);
57 private String name = "UNSET";
58 private Server server;
59 private int id;
60 int responseCounter = 0;
61
62 /***
63 *
64 */
65 public DummyCommModule() {
66 logger.info("Comm module created.");
67 }
68
69 /***
70 * @see CommModule#configure(Properties, int)
71 */
72 public void configure(Properties configuration, int commModuleId) {
73 this.name = configuration.getProperty("name");
74 this.id = commModuleId;
75 logger.info("Comm module nr " + id + " configured");
76 }
77
78 /***
79 * @see CommModule#setServer(Server)
80 */
81 public void setServer(Server server) {
82 this.server = server;
83 logger.info("Server set in comm module");
84 }
85
86 /***
87 * @see CommModule#sendMessage(UserId, Document)
88 */
89 public void sendMessage(UserId userId, Document message) {
90 logger.info("Sending message \"" + message.asXML() + "\" to user " +
91 userId + " from comm module nr " + id);
92
93 try {
94
95 new Diff(message.asXML(),
96 DocumentHelper.parseText(
97 DummyMessages.responses[responseCounter++]).asXML());
98
99
100 } catch (SAXException e) {
101 e.printStackTrace();
102 } catch (IOException e) {
103 e.printStackTrace();
104 } catch (ParserConfigurationException e) {
105 e.printStackTrace();
106 } catch (DocumentException e) {
107 e.printStackTrace();
108 }
109 }
110
111 /***
112 * @see CommModule#disconnectUser(UserId)
113 */
114 public void disconnectUser(UserId userId) {
115 logger.info("User " + userId + " disconnected from comm module nr " +
116 id);
117 }
118
119 /***
120 * @see CommModule#shutdown()
121 */
122 public void shutdown() {
123
124 }
125
126 /***
127 * @see java.lang.Runnable#run()
128 */
129 public void run() {
130 logger.info("Hello from comm module nr " + id + " with name " + name);
131
132 Random r = new Random();
133
134 for (int j = 1; j < 7; j++) {
135 responseCounter = 0;
136
137 UserId userId = new UserId(id, j);
138
139 try {
140 for (int i = 0; i < DummyMessages.requests.length; i++) {
141 Thread.sleep(r.nextInt(3000) + 3000);
142 server.receiveMessage(userId,
143 DocumentHelper.parseText(DummyMessages.requests[i]));
144 }
145
146 Thread.sleep(100);
147 } catch (Exception e) {
148 throw new RuntimeException(e);
149 }
150
151 server.unregisterUser(userId);
152
153 if (responseCounter != 2) {
154 throw new RuntimeException("Wrong number of responses! : " +
155 responseCounter + " (" + userId + ")");
156 }
157 }
158
159 if (((SnifosServer) server).isWait()) {
160 synchronized (server) {
161 server.notify();
162 }
163 }
164 }
165 }