|
|
|
|
@@ -20,23 +20,22 @@ class Routing {
|
|
|
|
|
/**
|
|
|
|
|
Depends on: Peripheral
|
|
|
|
|
**/
|
|
|
|
|
public static var instance:Routing;
|
|
|
|
|
|
|
|
|
|
public static inline final UPDATE_WAIT_TIME:Float = 1;
|
|
|
|
|
|
|
|
|
|
public final onNewNeigbor:Signal<Int>;
|
|
|
|
|
public static var onNewNeigbor(default, null):Signal<Int>;
|
|
|
|
|
|
|
|
|
|
private final onNewNeigborTrigger:SignalTrigger<NetworkID> = Signal.trigger();
|
|
|
|
|
private final routingTable:Map<NetworkID, Route> = new Map();
|
|
|
|
|
private var routeUpdateInQueue:Bool = false;
|
|
|
|
|
private static final onNewNeigborTrigger:SignalTrigger<NetworkID> = Signal.trigger();
|
|
|
|
|
private static final routingTable:Map<NetworkID, Route> = new Map();
|
|
|
|
|
private static var routeUpdateInQueue:Bool = false;
|
|
|
|
|
|
|
|
|
|
@:allow(kernel.Init)
|
|
|
|
|
private function new() {
|
|
|
|
|
this.onNewNeigbor = this.onNewNeigborTrigger.asSignal();
|
|
|
|
|
private static function init() {
|
|
|
|
|
onNewNeigbor = onNewNeigborTrigger.asSignal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@:allow(kernel.Init)
|
|
|
|
|
private function init() {
|
|
|
|
|
@:allow(kernel.net.Net)
|
|
|
|
|
private static function setup() {
|
|
|
|
|
routingTable.set(Net.instance.networkID, {interf: Loopback.instance, cost: 0, rep: Net.instance.networkID});
|
|
|
|
|
brodcastRoutingTable();
|
|
|
|
|
}
|
|
|
|
|
@@ -45,23 +44,23 @@ class Routing {
|
|
|
|
|
Prepares an brodcast of the current routing table. If a brodcast is already in queue it will be ignored.
|
|
|
|
|
After UPDATE_WAIT_TIME seconds the routing table will be brodcasted. This is done to prevent spamming the network.
|
|
|
|
|
**/
|
|
|
|
|
private function prepareRouteUpdate() {
|
|
|
|
|
if (this.routeUpdateInQueue) {
|
|
|
|
|
private static function prepareRouteUpdate() {
|
|
|
|
|
if (routeUpdateInQueue) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.routeUpdateInQueue = true;
|
|
|
|
|
routeUpdateInQueue = true;
|
|
|
|
|
|
|
|
|
|
new Timer(UPDATE_WAIT_TIME, () -> {
|
|
|
|
|
brodcastRoutingTable();
|
|
|
|
|
this.routeUpdateInQueue = false;
|
|
|
|
|
routeUpdateInQueue = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
Brodcast the current routing table to all peers.
|
|
|
|
|
**/
|
|
|
|
|
private function brodcastRoutingTable() {
|
|
|
|
|
private static function brodcastRoutingTable() {
|
|
|
|
|
var pack = newRoutDiscoverPackage();
|
|
|
|
|
|
|
|
|
|
for (modem in Peripheral.getAllModems()) {
|
|
|
|
|
@@ -73,7 +72,7 @@ class Routing {
|
|
|
|
|
Handle incomming packages that involve route discovery.
|
|
|
|
|
**/
|
|
|
|
|
@:allow(kernel.net.Net)
|
|
|
|
|
private function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void {
|
|
|
|
|
private static function handleRoutePackage(pack:Package<Noise>, interf:INetworkInterface):Void {
|
|
|
|
|
addPossibleRoute(pack.fromID, interf, 0, pack.fromID);
|
|
|
|
|
|
|
|
|
|
var shouldRespond:Bool = switch pack.type {
|
|
|
|
|
@@ -117,9 +116,9 @@ class Routing {
|
|
|
|
|
/**
|
|
|
|
|
Generate a list of routes to send to a peer based on the current routing table.
|
|
|
|
|
**/
|
|
|
|
|
private function genRouteList():Array<{id:NetworkID, cost:Int}> {
|
|
|
|
|
private static function genRouteList():Array<{id:NetworkID, cost:Int}> {
|
|
|
|
|
var routes:Array<{id:NetworkID, cost:Int}> = [];
|
|
|
|
|
for (k => v in this.routingTable) {
|
|
|
|
|
for (k => v in routingTable) {
|
|
|
|
|
routes.push({
|
|
|
|
|
id: k,
|
|
|
|
|
cost: v.cost
|
|
|
|
|
@@ -128,7 +127,7 @@ class Routing {
|
|
|
|
|
return routes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function newRoutDiscoverPackage():Package<Noise> {
|
|
|
|
|
private static function newRoutDiscoverPackage():Package<Noise> {
|
|
|
|
|
var pack:Package<Noise> = {
|
|
|
|
|
type: RouteDiscover(genRouteList()),
|
|
|
|
|
toID: Net.BRODCAST_PORT,
|
|
|
|
|
@@ -145,31 +144,31 @@ class Routing {
|
|
|
|
|
Called when a route to a client has been disoverd.
|
|
|
|
|
Its possible to be called multiple times with the same id but different addr.
|
|
|
|
|
**/
|
|
|
|
|
private function addPossibleRoute(toID:Int, interf:INetworkInterface, cost:Int, rep:NetworkID) {
|
|
|
|
|
private static function addPossibleRoute(toID:Int, interf:INetworkInterface, cost:Int, rep:NetworkID) {
|
|
|
|
|
if (toID == Net.instance.networkID) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fullCost = cost + interf.getBaseRoutingCost();
|
|
|
|
|
|
|
|
|
|
if (this.routingTable.exists(toID)) {
|
|
|
|
|
if (this.routingTable[toID].cost > fullCost) {
|
|
|
|
|
this.routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep};
|
|
|
|
|
this.prepareRouteUpdate();
|
|
|
|
|
if (routingTable.exists(toID)) {
|
|
|
|
|
if (routingTable[toID].cost > fullCost) {
|
|
|
|
|
routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep};
|
|
|
|
|
prepareRouteUpdate();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep};
|
|
|
|
|
this.onNewNeigborTrigger.trigger(toID);
|
|
|
|
|
this.prepareRouteUpdate();
|
|
|
|
|
routingTable[toID] = {interf: interf, cost: cost + interf.getBaseRoutingCost(), rep: rep};
|
|
|
|
|
onNewNeigborTrigger.trigger(toID);
|
|
|
|
|
prepareRouteUpdate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getRouteToID(networkID:NetworkID):{interf:INetworkInterface, rep:NetworkID} {
|
|
|
|
|
public static function getRouteToID(networkID:NetworkID):{interf:INetworkInterface, rep:NetworkID} {
|
|
|
|
|
if (networkID == Net.instance.networkID) {
|
|
|
|
|
return {interf: Loopback.instance, rep: Net.instance.networkID};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var route:Null<Route> = this.routingTable[networkID];
|
|
|
|
|
var route:Null<Route> = routingTable[networkID];
|
|
|
|
|
|
|
|
|
|
if (route == null) {
|
|
|
|
|
return null;
|
|
|
|
|
@@ -178,7 +177,7 @@ class Routing {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getRouteTable():Map<NetworkID, Route> {
|
|
|
|
|
return this.routingTable;
|
|
|
|
|
public static function getRouteTable():Map<NetworkID, Route> {
|
|
|
|
|
return routingTable;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|