Source code: org/miamm/soapmmil/agents/ResponseReceivingService.java
1 /* -*- Mode: java; indent-tabs-mode:nil; c-basic-offset: 2 -*-
2 * ex: set sw=2 expandtab:
3 * $Id: ResponseReceivingService.java,v 1.1 2003/05/04 20:45:50 kowey Exp $
4 *
5 * This software is released under a BSD-style license.
6 * Please see the LICENSE file in this distribution.
7 */
8
9 package org.miamm.soapmmil.agents;
10
11 import java.io.InputStream;
12 import java.io.Reader;
13 import java.io.StringReader;
14
15 import java.net.MalformedURLException;
16 import java.net.URL;
17
18 import org.apache.log4j.Logger;
19
20 import org.miamm.castor.headerblock.MiammSoapHeaderBlock;
21 import org.miamm.castor.headerblock.types.MsgContentType;
22 import org.miamm.castor.headerblock.types.MsgRoleType;
23 import org.miamm.soapmmil.MIAMMException;
24 import org.miamm.soapmmil.MIAMMService;
25 import org.miamm.soapmmil.MIAMMServiceEngine;
26 import org.miamm.soapmmil.MIAMMServiceEngineTest;
27 import org.miamm.soapmmil.SoapmmilMessage;
28
29 /**
30 * For testing the request-response functionality in MIAMM.
31 *
32 * It recognises MsgContentType.WORD_GRAPH
33 * as a regular event, and MsgContentType.SEMANTIC_REPRESENTATION
34 * as a response, and MsgContentType.GOAL_REPRESENTATION
35 *
36 *
37 * @version
38 * $Revision: 1.1 $<br>
39 * $Date: 2003/05/04 20:45:50 $<br>
40 * @author Eric Kow (kow at loria point fr)
41 */
42 public class ResponseReceivingService extends MIAMMService {
43 static Logger _logger =
44 Logger.getLogger(ResponseReceivingService.class);
45
46 // ------------------------------------------------------------------
47 // constructors
48 // ------------------------------------------------------------------
49
50 public ResponseReceivingService() throws MIAMMException {
51 super();
52
53 _logger.debug("constructing ResponseReceivingService");
54 // note: this is normally not the right place to do this
55 // but since we're just doing unit testing with it,
56 // i'll let it slide, eh?
57 soapmmil().allowEventType(NORMAL_EVENT_TYPE);
58 soapmmil().allowEventType(TRIGGER_AWAIT_1_TYPE);
59 soapmmil().allowEventType(TRIGGER_AWAIT_2_TYPE);
60 }
61
62 // ------------------------------------------------------------------
63 // testing
64 // ------------------------------------------------------------------
65
66 public void recogniseEvent(SoapmmilMessage message)
67 throws MIAMMException
68 {
69 MiammSoapHeaderBlock misoh = message.getMisoh();
70 final MsgContentType type = misoh.getMsgContent();
71
72 // if it is a regular message we increment the counter
73 if (type == NORMAL_EVENT_TYPE) {
74 incrementMsgCount();
75 String info = getDebuggingId() +
76 "recognised event with misoh: " + misoh +
77 " event number: " + _msgCount;
78 _logger.info(info);
79 MIAMMServiceEngineTest.wakeUp("received normal event");
80 }
81
82 // if it is a message designed to make us wait for a
83 // response, we dutifully perform the await
84 else if (type == TRIGGER_AWAIT_1_TYPE ||
85 type == TRIGGER_AWAIT_2_TYPE)
86 {
87 // we await in a seperate thread so we can test reception of
88 // multiple messages at a time
89 Thread t = new Thread() {
90 public void run() { doAwait(type); }
91 };
92 t.start();
93 }
94
95 else throw new MIAMMException("got unexpected event type: " +
96 type);
97 }
98
99 private void doAwait(MsgContentType type) {
100 SoapmmilMessage requestMessage =
101 new SoapmmilMessage(MsgContentType.NOTHING,
102 new StringReader("<foo/>"));
103 MiammSoapHeaderBlock requestMisoh = requestMessage.getMisoh();
104
105 // we can await responses to either the default request
106 // or the alternate request
107 String msgId = (type == TRIGGER_AWAIT_2_TYPE)
108 ? getRequest2MsgId () : getRequest1MsgId();
109 requestMisoh.setMsgId(msgId);
110 long timeout = _timeout;
111
112 // this might seem magical to you: how do we await a response to a
113 // message we haven't even sent? This is because of a short-cut
114 // we use in unit-testing; the other end knows what the message id
115 // of our request is, so it will simply recreate the message on
116 // its end
117
118 // handle the awaiting
119 _logger.debug("using timeout of " + timeout);
120 MIAMMServiceEngineTest.wakeUp("started awaiting");
121 if (timeout < 0) {
122 // if the timeout was unset, we use the default
123 _lastResponseReceived = soapmmil().awaitResponse(requestMessage);
124 } else {
125 _lastResponseReceived = soapmmil().awaitResponse(requestMessage,
126 timeout);
127 }
128 _logger.debug("response or timeout received");
129 incrementResponseCount();
130 MIAMMServiceEngineTest.wakeUp("received response");
131 }
132
133 /**
134 * This service assumes that it only sent two messages. We return
135 * the msgId for the default ficticious message.
136 */
137 public static String getRequest1MsgId() {
138 return "some_request";
139 }
140
141 /**
142 * This service assumes that it only sent two messages. We return
143 * the msgId for the alternate message.
144 */
145 public static String getRequest2MsgId() {
146 return "some_other_request";
147 }
148
149 public static synchronized void setTimeout(long timeout) {
150 _timeout = timeout;
151 }
152
153 // ------------------------------------------------------------------
154 // testing basics
155 // ------------------------------------------------------------------
156
157 /**
158 * Exposes the soapmmil engine connected to this service.
159 * This is normally a no-no, but we need it for unit testing
160 */
161 public MIAMMServiceEngine soapmmil() {
162 return super.soapmmil();
163 }
164
165 private static synchronized void incrementMsgCount() {
166 _msgCount++;
167 }
168
169 private static synchronized void incrementResponseCount() {
170 _responseCount++;
171 }
172
173 public static void reset() {
174 _logger.debug("Enter: reset");
175 _msgCount = 0;
176 _responseCount = 0;
177 // clear out any awaits we had
178 }
179
180 public static int getMsgCount() {
181 return _msgCount;
182 }
183
184 public static int getResponseCount() {
185 return _responseCount;
186 }
187
188 public static SoapmmilMessage getLastResponse() {
189 return _lastResponseReceived;
190 }
191
192 // ------------------------------------------------------------------
193 // boring MIAMMService stuff
194 // ------------------------------------------------------------------
195
196 public void start() {
197 _logger.debug(getDebuggingId() + "started");
198 }
199
200 public void restart() {
201 _logger.debug(getDebuggingId() + "restarted");
202 }
203
204 public void stop() {
205 _logger.debug(getDebuggingId() + "stopped");
206 }
207
208 // ------------------------------------------------------------------
209 // class members
210 // ------------------------------------------------------------------
211
212 // These are static because we want to unit test them in a server,
213 // which means we won't have control over the class instance.
214 protected static int _msgCount;
215 protected static int _responseCount;
216 protected static long _timeout = 0;
217
218
219
220 protected static SoapmmilMessage _lastResponseReceived;
221
222 public static MsgContentType NORMAL_EVENT_TYPE =
223 MsgContentType.WORD_GRAPH;
224 public static MsgContentType TRIGGER_AWAIT_1_TYPE=
225 MsgContentType.SEMANTIC_REPRESENTATION;
226 public static MsgContentType TRIGGER_AWAIT_2_TYPE =
227 MsgContentType.GOAL_REPRESENTATION;
228 } // end ResponseReceivingService
229
230