...
A Protocol Handler is responsible for the conversion between an arbitrary data model and the Odysseus data model. Multiple Protocol Handlers already exists in Odysseus and are documented here. They also serve as a good starting point for own Protocol Handler.
To implement a Protocol handler an AbstractProtocolHandler has to be extended. A few method has to be implemented here. First of all the method getName has to be implemented that returns the unique name of the protocol handler. After that, the method createInstance need to be implemented to create a new instance of the protocol handler with the given options. By the fact that a protocol handler can be used in a pull and a push fashion, the methods hasNext and getNext for pull access and process for push access have both to be implemented to read data and the method write has to be implemented to write data. Further, the methods open and close to open and close the transport handler and initialize or shutdown the the source/sink should be implemented. Other methods like the onConnect and onDisconnectcan be implemented to treat events in the transport channel adequate. Depending on the applications ones wants to override the methods getExchangePattern to define the supported exchange pattern for the transport direction and access.
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
public class ExampleProtocolHandler<T> extends AbstractProtocolHandler<T> {
public ExampleProtocolHandler() {
super();
}
public ExampleProtocolHandler(ITransportDirection direction,
IAccessPattern access) {
super(direction, access);
}
@Override
public void open() throws UnknownHostException, IOException {
getTransportHandler().open();
}
@Override
public void close() throws IOException {
getTransportHandler().close();
}
@Override
public boolean hasNext() throws IOException {
return true;
}
@Override
public T getNext() throws IOException {
return getDataHandler().readData("data");
}
@Override
public void write(T object) throws IOException {
}
@Override
public IProtocolHandler<T> createInstance(ITransportDirection direction,
IAccessPattern access, Map<String, String> options,
IDataHandler<T> dataHandler, ITransferHandler<T> transfer) {
ExampleProtocolHandler<T> instance = new ExampleProtocolHandler<T>(direction,
access);
instance.setDataHandler(dataHandler);
instance.setTransfer(transfer);
return instance;
}
@Override
public String getName() {
return "Example";
}
@Override
public ITransportExchangePattern getExchangePattern() {
if (this.getDirection().equals(ITransportDirection.IN)) {
return ITransportExchangePattern.InOnly;
} else {
return ITransportExchangePattern.OutOnly;
}
}
@Override
public void onConnect(ITransportHandler caller) {
// TODO Auto-generated method stub
}
@Override
public void onDisonnect(ITransportHandler caller) {
// TODO Auto-generated method stub
}
@Override
public void process(ByteBuffer message) {
getTransfer().transfer(getDataHandler().readData(message));
}
} |
...