Add java classes for receiving routing table updates

This commit is contained in:
Jeremy Lakeman 2016-05-11 14:18:31 +09:30
parent 2b991a0917
commit 6f388cec63
4 changed files with 136 additions and 2 deletions

View File

@ -0,0 +1,37 @@
package org.servalproject.servaldna;
import java.io.IOException;
import java.nio.BufferUnderflowException;
/**
* Created by jeremy on 10/05/16.
*/
public class MdpRoutingChanges extends AbstractMdpProtocol<RouteLink>{
private static final int MDP_ROUTE_TABLE = 5;
public MdpRoutingChanges(ChannelSelector selector, int loopbackMdpPort, AsyncResult<RouteLink> results) throws IOException {
super(selector, loopbackMdpPort, results, SubscriberId.Internal, MDP_ROUTE_TABLE);
refresh();
}
public void refresh() throws IOException {
MdpPacket request = new MdpPacket();
request.setRemoteSid(SubscriberId.ANY);
request.setRemotePort(MDP_ROUTE_TABLE);
request.payload.flip();
socket.send(request);
}
@Override
protected void parse(MdpPacket response) {
try {
while(response.payload.hasRemaining())
results.result(new RouteLink(response.payload));
} catch (AbstractId.InvalidBinaryException e) {
e.printStackTrace();
} catch (BufferUnderflowException e){
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,83 @@
package org.servalproject.servaldna;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
/**
* Created by jeremy on 10/05/16.
*/
public class RouteLink {
private static final int REACHABLE_SELF = (1<<0);
private static final int REACHABLE_BROADCAST = (1<<1);
private static final int REACHABLE_UNICAST = (1<<2);
//private static final int REACHABLE_INDIRECT = (1<<3);
public final SubscriberId sid;
public final SubscriberId next_hop;
public final SubscriberId prior_hop;
public final int hop_count;
public final int interface_id;
public final String interface_name;
private final int reachable;
public boolean isReachable(){
return reachable!=0;
}
public boolean isNeighbour(){
return (reachable&(REACHABLE_BROADCAST|REACHABLE_UNICAST))!=0;
}
public boolean isSelf() {
return reachable == REACHABLE_SELF;
}
RouteLink(ByteBuffer buff) throws AbstractId.InvalidBinaryException, BufferUnderflowException {
sid = new SubscriberId(buff);
reachable = 0xFF & (int)buff.get();
int hop_count=-1;
SubscriberId next_hop = null;
SubscriberId prior_hop = null;
int interface_id=-1;
String interface_name = null;
if (reachable != 0 && reachable!= REACHABLE_SELF) {
hop_count = 0xFF & (int)buff.get();
if (hop_count>1) {
next_hop = new SubscriberId(buff);
if (hop_count>2)
prior_hop = new SubscriberId(buff);
}else{
interface_id = 0xFF & (int)buff.get();
StringBuilder builder = new StringBuilder();
while(true){
byte b = buff.get();
if (b==0)
break;
builder.append((char)b);
}
interface_name = builder.toString();
}
}
this.next_hop = next_hop;
this.prior_hop = prior_hop;
this.hop_count = hop_count;
this.interface_id = interface_id;
this.interface_name = interface_name;
}
@Override
public String toString() {
return "RouteLink{" +
"sid=" + sid +
", next_hop=" + next_hop +
", prior_hop=" + prior_hop +
", hop_count=" + hop_count +
", interface_id=" + interface_id +
", interface_name='" + interface_name + '\'' +
", reachable=" + reachable +
'}';
}
}

View File

@ -542,6 +542,12 @@ public class ServalDCommand
return results;
}
public static ConfigItems getConfig() throws ServalDFailureException {
ConfigItems results = new ConfigItems();
results.setResult(command(results, "config", "get"));
return results;
}
public static String getConfigItem(String name) throws ServalDFailureException{
Object result = getConfig(name).values.get(name);
if (result == null)

View File

@ -60,6 +60,7 @@ public class SubscriberId extends AbstractId {
public static SubscriberId broadcastSid;
public static SubscriberId ANY;
public static SubscriberId Internal;
static {
byte buff[] = new byte[BINARY_SIZE];
for (int i = 0; i < BINARY_SIZE; i++)
@ -67,7 +68,6 @@ public class SubscriberId extends AbstractId {
try {
broadcastSid = new SubscriberId(buff);
} catch (InvalidBinaryException e) {
// TODO log error?
}
buff = new byte[BINARY_SIZE];
@ -76,7 +76,15 @@ public class SubscriberId extends AbstractId {
try {
ANY = new SubscriberId(buff);
} catch (InvalidBinaryException e) {
// TODO log error?
}
buff = new byte[BINARY_SIZE];
for (int i = 0; i < BINARY_SIZE - 1; i++)
buff[i] = (byte) 0x00;
buff[BINARY_SIZE - 1] = 1;
try {
Internal = new SubscriberId(buff);
} catch (InvalidBinaryException e) {
}
}
}