added more icmp stuff
This commit is contained in:
		
							parent
							
								
									f988c79e2f
								
							
						
					
					
						commit
						0b965d32b6
					
				@ -25,7 +25,7 @@ class Net {
 | 
			
		||||
	public static inline final DEFAULT_TTL:Int = 10;
 | 
			
		||||
 | 
			
		||||
	public final networkID:NetworkID = OS.getComputerID();
 | 
			
		||||
	private final responseBus:Map<Int, Callback<Package>> = new Map();
 | 
			
		||||
	private final responseBus:Map<Int, Callback<Outcome<Package,Error>>> = new Map();
 | 
			
		||||
	private final protoHandlers:Map<String, Callback<Package>> = new Map();
 | 
			
		||||
	private var interfaces:Array<INetworkInterface>;
 | 
			
		||||
 | 
			
		||||
@ -42,8 +42,20 @@ class Net {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private function setupPingHandle() {
 | 
			
		||||
		this.registerProto('ping', pack -> {
 | 
			
		||||
			this.respondTo(pack, pack.data);
 | 
			
		||||
		this.registerProto("icmp", pack -> {
 | 
			
		||||
			switch pack.data.type {
 | 
			
		||||
				case "ping":
 | 
			
		||||
					this.respondTo(pack, "pong");
 | 
			
		||||
				case "died":
 | 
			
		||||
					// If we get a "died" message from a node when one of our packages ttl hits 0
 | 
			
		||||
					// the `data.msgId` prop is the message id
 | 
			
		||||
					var msgID:Int = pack.data.msgID;
 | 
			
		||||
					if (responseBus.exists(msgID)) {
 | 
			
		||||
						responseBus[msgID].invoke(Outcome.Failure(new Error("TTL reached 0")));
 | 
			
		||||
					}
 | 
			
		||||
				default:
 | 
			
		||||
					Log.silly('Unknown icmp message: ${pack.data}');
 | 
			
		||||
			}
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -65,7 +77,7 @@ class Net {
 | 
			
		||||
				case Response:
 | 
			
		||||
					// Got a response to a send message. Invoke the callback
 | 
			
		||||
					if (responseBus.exists(pack.msgID)) {
 | 
			
		||||
						responseBus[pack.msgID].invoke(pack);
 | 
			
		||||
						responseBus[pack.msgID].invoke(Outcome.Success(pack));
 | 
			
		||||
					}
 | 
			
		||||
				case RouteDiscover(_) | RouteDiscoverResponse(_) | RouteDiscoverUpdate(_):
 | 
			
		||||
					// Delegate to Routing
 | 
			
		||||
@ -99,6 +111,13 @@ class Net {
 | 
			
		||||
 | 
			
		||||
	private function forwardPackage(pack: Package) {
 | 
			
		||||
		if (pack.ttl == 0){
 | 
			
		||||
 | 
			
		||||
			if (pack.type.match(Data(_))) {
 | 
			
		||||
				// If the package is a data package and the ttl hits 0
 | 
			
		||||
				// we send a "died" message to the sender
 | 
			
		||||
				sendAndForget(pack.fromID, "icmp", {type:"died", msgID: pack.msgID});
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Drop package
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
@ -172,8 +191,16 @@ class Net {
 | 
			
		||||
 | 
			
		||||
			var timeout:Timer = null;
 | 
			
		||||
 | 
			
		||||
			responseBus[pack.msgID] = ((reponse:Package) -> {
 | 
			
		||||
				resolve(reponse);
 | 
			
		||||
			responseBus[pack.msgID] = ((reponse:Outcome<Package,Error>) -> {
 | 
			
		||||
 | 
			
		||||
				switch reponse {
 | 
			
		||||
					case Success(pack):
 | 
			
		||||
						resolve(pack);
 | 
			
		||||
					case Failure(err):
 | 
			
		||||
						reject(err);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// Always remove the timeout
 | 
			
		||||
				if (timeout != null) {
 | 
			
		||||
					timeout.cancle();
 | 
			
		||||
				}
 | 
			
		||||
@ -210,14 +237,14 @@ class Net {
 | 
			
		||||
	/**
 | 
			
		||||
		Sends a ping package to the given id. Returns true if there was a response.
 | 
			
		||||
	**/
 | 
			
		||||
	public function ping(toID: NetworkID): Promise<Bool> {
 | 
			
		||||
		return new Promise<Bool>((resolve,reject)->{
 | 
			
		||||
			this.sendAndAwait(toID,"ping",null).handle(pack -> {
 | 
			
		||||
	public function ping(toID: NetworkID): Promise<Noise> {
 | 
			
		||||
		return new Promise<Noise>((resolve,reject)->{
 | 
			
		||||
			this.sendAndAwait(toID,"icmp",{type:"ping"}).handle(pack -> {
 | 
			
		||||
				switch pack {
 | 
			
		||||
					case Success(_):
 | 
			
		||||
						resolve(true);
 | 
			
		||||
						resolve(Noise);
 | 
			
		||||
					case Failure(err):
 | 
			
		||||
						resolve(false);
 | 
			
		||||
						reject(err);
 | 
			
		||||
				}
 | 
			
		||||
			});
 | 
			
		||||
			return null;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user