com.ibm.rational.test.lt.execution.socket.custom
Interface ISckCustomReceivePolicy


public interface ISckCustomReceivePolicy

ISckCustomReceivePolicy is the interface that Custom Receive Policy writers must implement.
The customized receive action, whom a reference is passed through receiveActionRef parameter of the setup() method, is responsible for actually reading the received bytes on the wire and also for the response and end timeouts.


 The custom receive policy code is called by the mean of a hook each time a new byte is received
 on the wire.
References to the ITestExecutionServices and ISckReceiveAction are passed to the custom code through a call to the setup() method.

Here is an implementation example:
 package test;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
 import org.eclipse.hyades.test.common.event.MessageEvent;
 
 import com.ibm.rational.test.lt.execution.socket.custom.ISckCustomReceivePolicy;
 import com.ibm.rational.test.lt.execution.socket.custom.ISckReceiveAction;
 import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
 
 /**
  *  Custom receive policy CustomReceivePolicy_Msg
* For javadoc of ITestExecutionServices, ISckCustomReceivePolicy, ISckReceiveAction, select 'Help Contents' in the * Help menu and select 'Extending Rational Performance Tester functionality' -> 'Extending test execution with custom code' */ @SuppressWarnings("nls") public class CustomReceivePolicy_Msg implements ISckCustomReceivePolicy { // static { // static blocks are called once on each run and allow for example to bind // to an external dynamic library // } private ISckReceiveAction receiveAction; private ITestExecutionServices tes; private int count; private int version; private int length; private ByteArrayOutputStream responseContent; public CustomReceivePolicy_Msg() { // The constructor is called during the test creation, not at the time of the execution of // the customized receive action } public void setup(ITestExecutionServices tesRef, ISckReceiveAction receiveActionRef) { receiveAction = receiveActionRef; tes = tesRef; /* Do here any custom setup */ count = 0; version = 0; length = 0; responseContent = new ByteArrayOutputStream(); } // In this example, we are reading 4 bytes that compose an int to carry the version number of a // proprietary protocol. // Then 4 bytes that compose an int indicating the length (in bytes) of the message content. // Then <length> bytes for the message content. public boolean onRead(int readByte) { count++; if (count <= 4) { version <<= 8; version |= readByte; if (count == 4 && version != 101) { receiveAction.handleException(new Exception("Bad version of the protocol: got " + version + " expecting 101")); return true; } return false; } else if (count <= 8) { length <<= 8; length |= readByte; if (count < 8) { return false; } } else if (length >= count - 8){ responseContent.write(readByte); } if (length <= count - 8) { // We're done long timeStamp = System.currentTimeMillis(); int fullResponseTime = (int)(timeStamp - receiveAction.getConnectionHolder().getLastConnectOrSendTimeStamp()); MessageEvent messageEvent = new MessageEvent(); messageEvent.setName(getClass().getSimpleName()); messageEvent.setParentId(receiveAction.getParent().getStartHistoryId()); messageEvent.setText("From Custom Receive Policy of action " + receiveAction.getName() + ", received " + new String(responseContent.toByteArray()) + ", fullResponseTime=" + fullResponseTime); tes.getTestLogManager().reportEvent(messageEvent); receiveAction.receiveSuccess(); try { responseContent.close(); } catch (IOException e) { // Silently ignored } return true; // true -> receive action is done / false otherwise } return false; // Let's continue with further bytes } }


Field Summary
static int EndOfStream
           
 
Method Summary
 boolean onRead(int readByte)
          Called like an hook each time a new byte is received on the wire during the execution of the customized receive action.
 void setup(ITestExecutionServices tesRef, ISckReceiveAction receiveActionRef)
          Setup is called the first time the customized receive action is executed, before any bytes are received from the remote peer.
 

Field Detail

EndOfStream

static final int EndOfStream
See Also:
Constant Field Values
Method Detail

setup

void setup(ITestExecutionServices tesRef,
           ISckReceiveAction receiveActionRef)
Setup is called the first time the customized receive action is executed, before any bytes are received from the remote peer.

Parameters:
tesRef - the reference to the ITestExecutionServices, that allows to log custom events in the Test Log amongst other things
receiveActionRef - the reference to the customized receive action

onRead

boolean onRead(int readByte)
Called like an hook each time a new byte is received on the wire during the execution of the customized receive action.

 This is where the custom policy makes the decision about continuing the read operations or
 going on with the next action in the Test.
When the policy is finishing and the Test can go on, a call to the receiveSuccess() method of the ISckReceiveAction reference must be done and true must be returned.
For further attempts to synchronize the virtual user in this customized receive action with further bytes, just return false.
To handle a situation where an exception occurs, call the handleException() method of the ISckReceiveAction reference and return true.
The response timeout while waiting for the first byte of the response is handled by the receive action reference, not by this custom code. So does the end timeout.
Both response and end timeouts are set up from the Test Editor.

Parameters:
readByte - the last read byte, as an integer between -128 and 127, or EndOfStream constant when the end of stream is reached. If the end of stream is reached, it's up to the custom code to decide what to do, i.e. a success or a failure. By default, when returning false, it's a failure.
Returns:
true if the policy is finishing, or false otherwise.