Package org.astrogrid.samp.test
Class Calculator
- java.lang.Object
-
- org.astrogrid.samp.test.Tester
-
- org.astrogrid.samp.test.Calculator
-
- All Implemented Interfaces:
CallableClient
public class Calculator extends Tester implements CallableClient
Test client. Performs simple integer arithmetic.- Since:
- 21 Jul 2008
- Author:
- Mark Taylor
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classCalculator.CalcRequestRepresents a request which may be sent to a Calculator object.static classCalculator.SendModeRepresents a delivery pattern.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringADD_MTYPEstatic Calculator.SendModeASYNCH_MODESends messages using the Synchronous Call/Response delivery pattern.private java.util.MapcallMap_private HubConnectionconnection_private static java.lang.StringDIV_MTYPEprivate static java.lang.StringiconUrl_private static java.util.logging.Loggerlogger_private static java.lang.StringMUL_MTYPEprivate intnCalc_private intnCall_static Calculator.SendModeNOTIFY_MODESends messages using the Notify delivery pattern.private java.util.Randomrandom_static Calculator.SendModeRANDOM_MODESends messages using a random choice of one of the other modes.private static java.lang.StringSUB_MTYPEstatic Calculator.SendModeSYNCH_MODESends messages using the Asynchronous Call/Response delivery pattern.
-
Constructor Summary
Constructors Constructor Description Calculator(HubConnection connection, java.util.Random random)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private Calculator.CalcRequestcreateRandomRequest()Generates a random calculation request.voidflush()Waits until all the responses this client is expecting to get have been safely received.HubConnectiongetConnection()Returns the hub connection used by this client.private static java.lang.StringgetIconUrl()intgetReceiveCount()Returns the total number of messages received using any delivery pattern.intgetSendCount()Returns the total number of messages sent using any delivery pattern.private intnextCall()Increments and then returns the number of calls so far made by this object.private java.util.MapprocessCall(java.lang.String senderId, Message msg)Does the work for both the receiveNotify and receiveCall methods.voidreceiveCall(java.lang.String senderId, java.lang.String msgId, Message msg)Receives a message for which a response is required.voidreceiveNotification(java.lang.String senderId, Message msg)Receives a message for which no response is required.voidreceiveResponse(java.lang.String senderId, java.lang.String msgTag, Response response)Receives a response to a message previously sent by this client.voidsendMessage(java.lang.String receiverId, Calculator.SendMode mode)Sends a randomly generated message in a randomly generated way to a given receiver.-
Methods inherited from class org.astrogrid.samp.test.Tester
assertEquals, assertEquals, assertTrue, fail
-
-
-
-
Field Detail
-
connection_
private final HubConnection connection_
-
callMap_
private final java.util.Map callMap_
-
random_
private final java.util.Random random_
-
nCalc_
private volatile int nCalc_
-
nCall_
private volatile int nCall_
-
ADD_MTYPE
private static final java.lang.String ADD_MTYPE
- See Also:
- Constant Field Values
-
SUB_MTYPE
private static final java.lang.String SUB_MTYPE
- See Also:
- Constant Field Values
-
MUL_MTYPE
private static final java.lang.String MUL_MTYPE
- See Also:
- Constant Field Values
-
DIV_MTYPE
private static final java.lang.String DIV_MTYPE
- See Also:
- Constant Field Values
-
logger_
private static final java.util.logging.Logger logger_
-
iconUrl_
private static java.lang.String iconUrl_
-
NOTIFY_MODE
public static final Calculator.SendMode NOTIFY_MODE
Sends messages using the Notify delivery pattern.
-
ASYNCH_MODE
public static final Calculator.SendMode ASYNCH_MODE
Sends messages using the Synchronous Call/Response delivery pattern.
-
SYNCH_MODE
public static final Calculator.SendMode SYNCH_MODE
Sends messages using the Asynchronous Call/Response delivery pattern.
-
RANDOM_MODE
public static final Calculator.SendMode RANDOM_MODE
Sends messages using a random choice of one of the other modes.
-
-
Constructor Detail
-
Calculator
public Calculator(HubConnection connection, java.util.Random random) throws SampException
Constructor.- Parameters:
connection- hub connectionrandom- random number generator- Throws:
SampException
-
-
Method Detail
-
sendMessage
public void sendMessage(java.lang.String receiverId, Calculator.SendMode mode) throws SampExceptionSends a randomly generated message in a randomly generated way to a given receiver. The receiver should be another calculator client, like this one. If the message is sent according to one of the call/response delivery patterns the response will be checked to ensure that it has the correct value.- Parameters:
receiverId- client ID of another Calculator client.- Throws:
SampException
-
getSendCount
public int getSendCount()
Returns the total number of messages sent using any delivery pattern.- Returns:
- number of sends
-
getReceiveCount
public int getReceiveCount()
Returns the total number of messages received using any delivery pattern.- Returns:
- number of receives
-
getConnection
public HubConnection getConnection()
Returns the hub connection used by this client.- Returns:
- connection
-
flush
public void flush()
Waits until all the responses this client is expecting to get have been safely received.
-
receiveNotification
public void receiveNotification(java.lang.String senderId, Message msg)Description copied from interface:CallableClientReceives a message for which no response is required.- Specified by:
receiveNotificationin interfaceCallableClient- Parameters:
senderId- public ID of sending clientmsg- message
-
receiveCall
public void receiveCall(java.lang.String senderId, java.lang.String msgId, Message msg) throws SampExceptionDescription copied from interface:CallableClientReceives a message for which a response is required. The implementation must take care to call the hub'sreplymethod at some future point.- Specified by:
receiveCallin interfaceCallableClient- Parameters:
senderId- public ID of sending clientmsgId- message identifier for later use with replymsg- message- Throws:
SampException
-
receiveResponse
public void receiveResponse(java.lang.String senderId, java.lang.String msgTag, Response response)Description copied from interface:CallableClientReceives a response to a message previously sent by this client.- Specified by:
receiveResponsein interfaceCallableClient- Parameters:
senderId- public ID of responding clientmsgTag- client-defined tag labelling previously-sent messageresponse- returned response object
-
processCall
private java.util.Map processCall(java.lang.String senderId, Message msg)Does the work for both the receiveNotify and receiveCall methods.- Parameters:
senderId- sender public IDmsg- message object- Returns:
- content of the successful reply's samp.result entry
-
nextCall
private int nextCall()
Increments and then returns the number of calls so far made by this object.- Returns:
- next value of the call counter
-
createRandomRequest
private Calculator.CalcRequest createRandomRequest()
Generates a random calculation request.- Returns:
- new random request
-
getIconUrl
private static java.lang.String getIconUrl()
-
-