| 1 |
package org.astrogrid.samp.test; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.net.URL; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.Map; |
| 8 |
import java.util.Random; |
| 9 |
import java.util.logging.Logger; |
| 10 |
import org.astrogrid.samp.ErrInfo; |
| 11 |
import org.astrogrid.samp.Message; |
| 12 |
import org.astrogrid.samp.Metadata; |
| 13 |
import org.astrogrid.samp.Response; |
| 14 |
import org.astrogrid.samp.SampUtils; |
| 15 |
import org.astrogrid.samp.Subscriptions; |
| 16 |
import org.astrogrid.samp.client.CallableClient; |
| 17 |
import org.astrogrid.samp.client.HubConnection; |
| 18 |
import org.astrogrid.samp.client.SampException; |
| 19 |
import org.astrogrid.samp.httpd.UtilServer; |
| 20 |
|
| 21 |
/** |
| 22 |
* Test client. Performs simple integer arithmetic. |
| 23 |
* |
| 24 |
* @author Mark Taylor |
| 25 |
* @since 21 Jul 2008 |
| 26 |
*/ |
| 27 |
public class Calculator extends Tester implements CallableClient { |
| 28 |
|
| 29 |
private final HubConnection connection_; |
| 30 |
private final Map callMap_; |
| 31 |
private final Random random_; |
| 32 |
private volatile int nCalc_; |
| 33 |
private volatile int nCall_; |
| 34 |
private static final String ADD_MTYPE = "calc.int.add"; |
| 35 |
private static final String SUB_MTYPE = "calc.int.sub"; |
| 36 |
private static final String MUL_MTYPE = "calc.int.mul"; |
| 37 |
private static final String DIV_MTYPE = "calc.int.div"; |
| 38 |
private static final Logger logger_ = |
| 39 |
Logger.getLogger( Calculator.class.getName() ); |
| 40 |
private static String iconUrl_; |
| 41 |
|
| 42 |
/** Sends messages using the Notify delivery pattern. */ |
| 43 |
public static final SendMode NOTIFY_MODE = new SendMode( "notify" ) { |
| 44 |
void send( Calculator calc, String receiverId, CalcRequest request, |
| 45 |
int iseq ) |
| 46 |
throws SampException { |
| 47 |
calc.connection_.notify( receiverId, request.getMessage() ); |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
/** Sends messages using the Synchronous Call/Response delivery pattern. */ |
| 52 |
public static final SendMode ASYNCH_MODE = new SendMode( "sync" ) { |
| 53 |
void send( Calculator calc, String receiverId, CalcRequest request, |
| 54 |
int iseq ) |
| 55 |
throws SampException { |
| 56 |
String msgTag = "tag-" + iseq; |
| 57 |
synchronized ( calc.callMap_ ) { |
| 58 |
calc.callMap_.put( msgTag, request ); |
| 59 |
} |
| 60 |
calc.connection_.call( receiverId, msgTag, request.getMessage() ); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
/** Sends messages using the Asynchronous Call/Response delivery pattern. */ |
| 65 |
public static final SendMode SYNCH_MODE = new SendMode( "async" ) { |
| 66 |
void send( Calculator calc, String receiverId, CalcRequest request, |
| 67 |
int iseq ) |
| 68 |
throws SampException { |
| 69 |
Response response = |
| 70 |
calc.connection_.callAndWait( receiverId, request.getMessage(), |
| 71 |
0 ); |
| 72 |
request.checkResponse( response ); |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
/** Sends messages using a random choice of one of the other modes. */ |
| 77 |
public static final SendMode RANDOM_MODE = new SendMode( "mixture" ) { |
| 78 |
private final SendMode[] otherModes = new SendMode[] { |
| 79 |
NOTIFY_MODE, ASYNCH_MODE, SYNCH_MODE, |
| 80 |
}; |
| 81 |
void send( Calculator calc, String receiverId, CalcRequest request, |
| 82 |
int iseq ) |
| 83 |
throws SampException { |
| 84 |
otherModes[ calc.random_.nextInt( otherModes.length ) ] |
| 85 |
.send( calc, receiverId, request, iseq ); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
/** |
| 90 |
* Constructor. |
| 91 |
* |
| 92 |
* @param connection hub connection |
| 93 |
* @param random random number generator |
| 94 |
*/ |
| 95 |
public Calculator( HubConnection connection, Random random ) |
| 96 |
throws SampException { |
| 97 |
connection_ = connection; |
| 98 |
random_ = random; |
| 99 |
callMap_ = Collections.synchronizedMap( new HashMap() ); |
| 100 |
connection_.setCallable( this ); |
| 101 |
Metadata meta = new Metadata(); |
| 102 |
meta.setName( "Calculator" ); |
| 103 |
meta.setDescriptionText( "Rudimentary integer arithmetic application" ); |
| 104 |
String iconUrl = getIconUrl(); |
| 105 |
if ( iconUrl != null ) { |
| 106 |
meta.setIconUrl( iconUrl ); |
| 107 |
} |
| 108 |
connection_.declareMetadata( meta ); |
| 109 |
Subscriptions subs = new Subscriptions(); |
| 110 |
subs.addMType( ADD_MTYPE ); |
| 111 |
subs.addMType( SUB_MTYPE ); |
| 112 |
subs.addMType( MUL_MTYPE ); |
| 113 |
subs.addMType( DIV_MTYPE ); |
| 114 |
connection_.declareSubscriptions( subs ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Sends a randomly generated message in a randomly generated way to |
| 119 |
* a given receiver. The receiver should be another calculator client, |
| 120 |
* like this one. If the message is sent according to one of the |
| 121 |
* call/response delivery patterns the response will be checked to |
| 122 |
* ensure that it has the correct value. |
| 123 |
* |
| 124 |
* @param receiverId client ID of another Calculator client. |
| 125 |
*/ |
| 126 |
public void sendMessage( String receiverId, SendMode mode ) |
| 127 |
throws SampException { |
| 128 |
mode.send( this, receiverId, createRandomRequest(), nextCall() ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns the total number of messages sent using any delivery pattern. |
| 133 |
* |
| 134 |
* @return number of sends |
| 135 |
*/ |
| 136 |
public int getSendCount() { |
| 137 |
return nCall_; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns the total number of messages received using any delivery pattern. |
| 142 |
* |
| 143 |
* @return number of receives |
| 144 |
*/ |
| 145 |
public int getReceiveCount() { |
| 146 |
return nCalc_; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Returns the hub connection used by this client. |
| 151 |
* |
| 152 |
* @return connection |
| 153 |
*/ |
| 154 |
public HubConnection getConnection() { |
| 155 |
return connection_; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Waits until all the responses this client is expecting to get |
| 160 |
* have been safely received. |
| 161 |
*/ |
| 162 |
public void flush() { |
| 163 |
try { |
| 164 |
synchronized ( callMap_ ) { |
| 165 |
while ( ! callMap_.isEmpty() ) { |
| 166 |
callMap_.wait( 100 ); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
catch ( InterruptedException e ) { |
| 171 |
throw new RuntimeException( "Interrupted" ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
public void receiveNotification( String senderId, Message msg ) { |
| 176 |
processCall( senderId, msg ); |
| 177 |
} |
| 178 |
|
| 179 |
public void receiveCall( String senderId, String msgId, Message msg ) |
| 180 |
throws SampException { |
| 181 |
Response response; |
| 182 |
try { |
| 183 |
response = Response |
| 184 |
.createSuccessResponse( processCall( senderId, msg ) ); |
| 185 |
} |
| 186 |
catch ( Throwable e ) { |
| 187 |
response = Response.createErrorResponse( new ErrInfo( e ) ); |
| 188 |
} |
| 189 |
connection_.reply( msgId, response ); |
| 190 |
} |
| 191 |
|
| 192 |
public void receiveResponse( String senderId, String msgTag, |
| 193 |
Response response ) { |
| 194 |
CalcRequest request; |
| 195 |
synchronized ( callMap_ ) { |
| 196 |
request = (CalcRequest) callMap_.remove( msgTag ); |
| 197 |
} |
| 198 |
request.checkResponse( response ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Does the work for both the receiveNotify and receiveCall methods. |
| 203 |
* |
| 204 |
* @param senderId sender public ID |
| 205 |
* @param msg message object |
| 206 |
* @return content of the successful reply's samp.result entry |
| 207 |
*/ |
| 208 |
private Map processCall( String senderId, Message msg ) { |
| 209 |
String mtype = msg.getMType(); |
| 210 |
if ( ADD_MTYPE.equals( mtype ) || |
| 211 |
SUB_MTYPE.equals( mtype ) || |
| 212 |
MUL_MTYPE.equals( mtype ) || |
| 213 |
DIV_MTYPE.equals( mtype ) ) { |
| 214 |
synchronized ( this ) { |
| 215 |
nCalc_++; |
| 216 |
} |
| 217 |
int a = SampUtils.decodeInt( (String) msg.getParam( "a" ) ); |
| 218 |
int b = SampUtils.decodeInt( (String) msg.getParam( "b" ) ); |
| 219 |
final int x; |
| 220 |
if ( ADD_MTYPE.equals( mtype ) ) { |
| 221 |
x = a + b; |
| 222 |
} |
| 223 |
else if ( SUB_MTYPE.equals( mtype ) ) { |
| 224 |
x = a - b; |
| 225 |
} |
| 226 |
else if ( MUL_MTYPE.equals( mtype ) ) { |
| 227 |
x = a * b; |
| 228 |
} |
| 229 |
else if ( DIV_MTYPE.equals( mtype ) ) { |
| 230 |
x = a / b; |
| 231 |
} |
| 232 |
else { |
| 233 |
throw new AssertionError(); |
| 234 |
} |
| 235 |
Map result = new HashMap(); |
| 236 |
result.put( "x", SampUtils.encodeInt( x ) ); |
| 237 |
return result; |
| 238 |
} |
| 239 |
else { |
| 240 |
throw new TestException(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Increments and then returns the number of calls so far made by this |
| 246 |
* object. |
| 247 |
* |
| 248 |
* @return next value of the call counter |
| 249 |
*/ |
| 250 |
private synchronized int nextCall() { |
| 251 |
return ++nCall_; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Generates a random calculation request. |
| 256 |
* |
| 257 |
* @return new random request |
| 258 |
*/ |
| 259 |
private CalcRequest createRandomRequest() { |
| 260 |
String mtype = new String[] { |
| 261 |
ADD_MTYPE, |
| 262 |
SUB_MTYPE, |
| 263 |
MUL_MTYPE, |
| 264 |
DIV_MTYPE, |
| 265 |
}[ random_.nextInt( 4 ) ]; |
| 266 |
return new CalcRequest( mtype, |
| 267 |
random_.nextInt( 1000 ), |
| 268 |
500 + random_.nextInt( 500 ) ); |
| 269 |
} |
| 270 |
|
| 271 |
private static String getIconUrl() { |
| 272 |
if ( iconUrl_ == null ) { |
| 273 |
String resource = "/org/astrogrid/samp/images/tinycalc.gif"; |
| 274 |
URL url; |
| 275 |
try { |
| 276 |
url = UtilServer.getInstance().exportResource( resource ); |
| 277 |
} |
| 278 |
catch ( IOException e ) { |
| 279 |
url = null; |
| 280 |
logger_.warning( "Can't locate icon: " + resource ); |
| 281 |
} |
| 282 |
iconUrl_ = url == null ? "" : url.toString(); |
| 283 |
} |
| 284 |
return iconUrl_.length() > 0 ? iconUrl_ : null; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Represents a delivery pattern. |
| 289 |
* Instances are provided as static members of class {@link Calculator}. |
| 290 |
*/ |
| 291 |
public static abstract class SendMode { |
| 292 |
private final String name_; |
| 293 |
|
| 294 |
/** |
| 295 |
* Constructor. |
| 296 |
* |
| 297 |
* @param name mode name |
| 298 |
*/ |
| 299 |
private SendMode( String name ) { |
| 300 |
name_ = name; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Sends a message from one calculator client to another using this |
| 305 |
* send mode. |
| 306 |
* |
| 307 |
* @param calc sending client |
| 308 |
* @param receiverId public ID of receiving client |
| 309 |
* @param request calculation request object |
| 310 |
* @param iseq unique identifier for this request by this calculator |
| 311 |
*/ |
| 312 |
abstract void send( Calculator calc, String receiverId, |
| 313 |
CalcRequest request, int iseq ) |
| 314 |
throws SampException; |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns the name of this mode. |
| 318 |
*/ |
| 319 |
public String toString() { |
| 320 |
return name_; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Represents a request which may be sent to a Calculator object. |
| 326 |
*/ |
| 327 |
private class CalcRequest { |
| 328 |
|
| 329 |
private final int a_; |
| 330 |
private final int b_; |
| 331 |
private final String mtype_; |
| 332 |
private final int x_; |
| 333 |
|
| 334 |
/** |
| 335 |
* Constructor. |
| 336 |
* |
| 337 |
* @param mtype operation type as an MType string |
| 338 |
* @param a first parameter |
| 339 |
* @param b second parameter |
| 340 |
*/ |
| 341 |
public CalcRequest( String mtype, int a, int b ) { |
| 342 |
mtype_ = mtype; |
| 343 |
a_ = a; |
| 344 |
b_ = b; |
| 345 |
if ( ADD_MTYPE.equals( mtype ) ) { |
| 346 |
x_ = a + b; |
| 347 |
} |
| 348 |
else if ( SUB_MTYPE.equals( mtype ) ) { |
| 349 |
x_ = a - b; |
| 350 |
} |
| 351 |
else if ( MUL_MTYPE.equals( mtype ) ) { |
| 352 |
x_ = a * b; |
| 353 |
} |
| 354 |
else if ( DIV_MTYPE.equals( mtype ) ) { |
| 355 |
x_ = a / b; |
| 356 |
} |
| 357 |
else { |
| 358 |
throw new IllegalArgumentException(); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Returns a Message object corresponding to this request. |
| 364 |
*/ |
| 365 |
public Message getMessage() { |
| 366 |
Message msg = new Message( mtype_ ); |
| 367 |
msg.addParam( "a", SampUtils.encodeInt( a_ ) ); |
| 368 |
msg.addParam( "b", SampUtils.encodeInt( b_ ) ); |
| 369 |
return msg; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Checks that the given response is correct for this request. |
| 374 |
* |
| 375 |
* @param response response to check |
| 376 |
*/ |
| 377 |
public void checkResponse( Response response ) { |
| 378 |
assertEquals( Response.OK_STATUS, response.getStatus() ); |
| 379 |
assertEquals( x_, |
| 380 |
SampUtils.decodeInt( (String) |
| 381 |
response.getResult() |
| 382 |
.get( "x" ) ) ); |
| 383 |
} |
| 384 |
} |
| 385 |
} |