2014-02-21 06:09:47 +00:00
|
|
|
package org.servalproject.servaldna;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.channels.SelectableChannel;
|
|
|
|
import java.nio.channels.SelectionKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jeremy on 21/02/14.
|
|
|
|
*/
|
|
|
|
public class MdpDnaLookup extends ChannelSelector.Handler{
|
|
|
|
private final ChannelSelector selector;
|
|
|
|
private final MdpSocket socket;
|
|
|
|
private final AsyncResult<ServalDCommand.LookupResult> results;
|
|
|
|
|
|
|
|
public MdpDnaLookup(ChannelSelector selector, AsyncResult<ServalDCommand.LookupResult> results) throws IOException {
|
|
|
|
socket = new MdpSocket();
|
2014-02-24 06:20:44 +00:00
|
|
|
socket.bind();
|
2014-02-21 06:09:47 +00:00
|
|
|
this.selector = selector;
|
|
|
|
this.results = results;
|
|
|
|
selector.register(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendRequest(SubscriberId destination, String did) throws IOException {
|
|
|
|
MdpPacket request = new MdpPacket();
|
|
|
|
if (destination.isBroadcast())
|
|
|
|
request.setFlags(MdpPacket.MDP_FLAG_NO_CRYPT);
|
|
|
|
request.setRemoteSid(destination);
|
|
|
|
request.setRemotePort(MdpPacket.MDP_PORT_DNALOOKUP);
|
|
|
|
request.payload.put(did.getBytes());
|
|
|
|
request.payload.put((byte)0);
|
|
|
|
request.payload.flip();
|
|
|
|
socket.send(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void read() {
|
|
|
|
try {
|
|
|
|
MdpPacket response = new MdpPacket();
|
|
|
|
socket.receive(response);
|
|
|
|
byte bytes[] = new byte[response.payload.remaining()];
|
|
|
|
response.payload.get(bytes);
|
|
|
|
String resultString = new String(bytes);
|
|
|
|
String fields[] = resultString.split("\\|");
|
2014-02-25 04:56:13 +00:00
|
|
|
if (fields.length < 2)
|
|
|
|
throw new IOException("Expected at least 2 result fields, got \""+resultString+"\"");
|
2014-02-21 06:09:47 +00:00
|
|
|
ServalDCommand.LookupResult result = new ServalDCommand.LookupResult();
|
|
|
|
result.subscriberId = new SubscriberId(fields[0]);
|
2014-02-25 02:12:36 +00:00
|
|
|
result.uri = fields[1];
|
2014-02-25 04:56:13 +00:00
|
|
|
result.did = (fields.length>2)?fields[2]:"";
|
|
|
|
result.name = (fields.length>3)?fields[3]:"";
|
2014-02-21 06:09:47 +00:00
|
|
|
results.result(result);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (AbstractId.InvalidHexException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close(){
|
|
|
|
try {
|
|
|
|
selector.unregister(this);
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-24 06:20:44 +00:00
|
|
|
public SelectableChannel getChannel() throws IOException {
|
2014-02-21 06:09:47 +00:00
|
|
|
return socket.getChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getInterest() {
|
|
|
|
return SelectionKey.OP_READ;
|
|
|
|
}
|
|
|
|
}
|