2014-02-21 06:09:47 +00:00
|
|
|
package org.servalproject.servaldna;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by jeremy on 21/02/14.
|
|
|
|
*/
|
2014-05-09 00:32:44 +00:00
|
|
|
public class MdpDnaLookup extends AbstractMdpProtocol<ServalDCommand.LookupResult> {
|
2014-02-21 06:09:47 +00:00
|
|
|
|
2014-06-20 01:25:09 +00:00
|
|
|
public MdpDnaLookup(ChannelSelector selector, int loopbackMdpPort, AsyncResult<ServalDCommand.LookupResult> results) throws IOException {
|
|
|
|
super(selector, loopbackMdpPort, results);
|
2014-02-21 06:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2014-05-09 00:32:44 +00:00
|
|
|
protected void parse(MdpPacket response) {
|
2014-02-21 06:09:47 +00:00
|
|
|
try {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|