2014-05-09 00:32:44 +00:00
|
|
|
package org.servalproject.servaldna;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.channels.SelectableChannel;
|
|
|
|
import java.nio.channels.SelectionKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jeremy on 8/05/14.
|
|
|
|
*/
|
|
|
|
public abstract class AbstractMdpProtocol<T> extends ChannelSelector.Handler {
|
|
|
|
private final ChannelSelector selector;
|
|
|
|
protected final MdpSocket socket;
|
|
|
|
protected final AsyncResult<T> results;
|
|
|
|
|
2014-06-20 01:25:09 +00:00
|
|
|
public AbstractMdpProtocol(ChannelSelector selector, int loopbackMdpPort, AsyncResult<T> results) throws IOException {
|
2014-09-15 05:09:26 +00:00
|
|
|
this(selector, loopbackMdpPort, results, 0);
|
|
|
|
}
|
|
|
|
public AbstractMdpProtocol(ChannelSelector selector, int loopbackMdpPort, AsyncResult<T> results, int port) throws IOException {
|
2014-06-20 01:25:09 +00:00
|
|
|
this.socket = new MdpSocket(loopbackMdpPort);
|
2014-09-15 05:09:26 +00:00
|
|
|
socket.bind(port);
|
2014-05-09 00:32:44 +00:00
|
|
|
this.selector = selector;
|
|
|
|
this.results = results;
|
|
|
|
selector.register(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close(){
|
|
|
|
try {
|
|
|
|
selector.unregister(this);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void parse(MdpPacket response);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void read() {
|
|
|
|
try {
|
|
|
|
MdpPacket response = new MdpPacket();
|
|
|
|
socket.receive(response);
|
|
|
|
parse(response);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SelectableChannel getChannel() throws IOException {
|
|
|
|
return socket.getChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInterest() {
|
|
|
|
return SelectionKey.OP_READ;
|
|
|
|
}
|
|
|
|
}
|