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.
setup()
method. 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 |
---|
static final int EndOfStream
Method Detail |
---|
void setup(ITestExecutionServices tesRef, ISckReceiveAction receiveActionRef)
tesRef
- the reference to the ITestExecutionServices, that allows to log custom events in
the Test Log amongst other thingsreceiveActionRef
- the reference to the customized receive actionboolean onRead(int readByte)
receiveSuccess()
method of the
ISckReceiveAction reference must be done and true
must be returned.false
.handleException()
method of the
ISckReceiveAction reference and return true
.
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.