public override bool SendPacketDirectly(float time, Packet pkg) { float recv_time = global.processDelay + global.internetDelay; pkg.Prev = Id; Console.WriteLine("{0:F4} [{1}] {2}{3} sends to {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.NextType, (pkg.Next == -1 ? "all" : pkg.Next.ToString())); if (pkg.Next == -1) //Broadcast return true;//No such a case. else { Node node = null; switch (pkg.NextType) { case NodeType.READER: node = global.readers[pkg.Next]; break; case NodeType.SERVER: node = global.server; break; default: Debug.Assert(false, "Error Next Type!"); break; } pkg.PrevType = type; pkg.Prev = Id; Event.AddEvent( new Event(time + recv_time, EventType.RECV, node, pkg)); } return true; }
public IOTPhenomemon(IOTPhenomemonType type, int nodeId, double time, Packet pkg) { this.type = type; this.nodeId = nodeId; this.start = this.end = time; this.pkg = pkg; }
public override void Recv(Packet pkg) { if (pkg.PrevType != NodeType.READER) { Console.WriteLine("{0:F4} [{1}] {2}{3} drop from {4}{5} due to wrong node type", scheduler.currentTime, pkg.Type, this.type,this.Id, pkg.PrevType, pkg.Prev); return; } Reader node = global.readers[pkg.Prev]; if (Utility.Distance(this, (MobileNode)node) > Global.getInstance().objectMaxDist) { Console.WriteLine("{0:F4} [{1}] {2}{3} Drop {4}{5} due to out of space", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); return; } switch (pkg.Type) { case PacketType.LOGICAL_PATH_REQUEST: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); RecvQueryRequest(pkg); break; case PacketType.DATA_AVAIL: //Override the link path update Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); UpdateLogicalPath(pkg); break; default: base.Recv(pkg); return; } return; }
public void GetQueryResponseFromServer(Packet pkg) { if (pkg.ObjectLogicalPathQueryServerReply == null) { Console.WriteLine("{0:F4} [{1}] {2}{3} Querying result: Object not found.", scheduler.currentTime, pkg.Type, this.type, this.Id); return; } else Console.WriteLine("{0:F4} [{1}] {2}{3} Querying result: Object {4}.", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.ObjectLogicalPathQueryServerReply.ToString()); ObjectLogicalPathQueryServerReplyField f = pkg.ObjectLogicalPathQueryServerReply; if (scheduler.currentTime - f.time > global.maxLocationUpdateInterval * 2) { Console.WriteLine("{0:F4} [{1}] {2}{3} Query abort: Object {4} move time out.", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.ObjectLogicalPathQueryServerReply.obj); return; } Packet pkg1 = new Packet(this, global.readers[f.reader], PacketType.LOGICAL_PATH_REQUEST); pkg1.Next = f.gateway; pkg1.NextType = NodeType.READER; pkg1.ObjectLogicalPathQueryReqeust = new ObjectLogicalPathQueryRequestField(); pkg1.ObjectLogicalPathQueryReqeust.gateway = f.gateway; pkg1.ObjectLogicalPathQueryReqeust.reader = f.reader; pkg1.ObjectLogicalPathQueryReqeust.obj = f.obj; SendPacketDirectly(scheduler.currentTime, pkg1); }
public override void Recv(Packet pkg) { if (pkg.PrevType != NodeType.READER) { Console.WriteLine("{0:F4} [{1}] {2}{3} drop from {4}{5} due to wrong node type", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); return; } Reader node = global.readers[pkg.Prev]; if (Utility.Distance(this, (MobileNode)node) > Global.getInstance().objectMaxDist) { Console.WriteLine("{0:F4} [{1}] {2}{3} Drop {4}{5} due to out of space", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); return; } switch (pkg.Type) { case PacketType.TAG_HEADER: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); RecvTagHeaderRequest(pkg); break; case PacketType.DATA_AVAIL: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); RecvAuthorization(pkg); break; default: base.Recv(pkg); return; } return; }
public void RecvAuthorization(Packet pkg) { int key = pkg.Authorization.keys[0]; if (key != Id) { Console.WriteLine("Object{0} key not match: {1}.", Id, key); return; } this.lastNearReader = this.global.readers[pkg.Src]; this.lastNearReaderTime = scheduler.currentTime; List<Packet> removeList = new List<Packet>(); List<Packet> addList = new List<Packet>(); foreach (Packet p in this.cachePackets) { if (TrySendData(p) && this.cachePackets.Contains(pkg)) removeList.Add(p); else if (!this.cachePackets.Contains(pkg)) addList.Add(pkg); } //我们就不测重试失败的例子了 /* foreach (Packet p in addList) this.cachePackets.Add(p); **/ foreach (Packet p in removeList) this.cachePackets.Remove(p); }
public void GetQueryResponseFromReader(Packet pkg) { if (pkg.ObjectLogicalPathQueryReply == null) Console.WriteLine("{0:F4} [{1}] {2}{3} Querying result: Object not found.", scheduler.currentTime, pkg.Type, this.type, this.Id); else Console.WriteLine("{0:F4} [{1}] {2}{3} Querying result: Object {4}, total time:{5}.", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.ObjectLogicalPathQueryReply, scheduler.currentTime - this.queryingTime); this.queryingTime = -1; }
public void GetQueryResponse(Packet pkg) { if (pkg.ObjectLocationReply == null) Console.WriteLine("{0:F4} [LOCATION_QUERY] {1}{2} Querying result: Object {3} not found.", scheduler.currentTime, this.type, this.Id, pkg.ObjectLocationReply.obj); else Console.WriteLine("{0:F4} [LOCATION_QUERY] {1}{2} Querying result: Object {3}.", scheduler.currentTime, this.type, this.Id, pkg.ObjectLocationReply.ToString()); this.queryingTime = -1; }
public IOTPhenomemon(IOTPhenomemonType type, int nodeId, double start, double end, Packet pkg) { this.type = type; this.nodeId = nodeId; this.start = start; this.end = end; this.pkg = pkg; }
public void SendObjectLogicalPath(Packet pkg) { Node querier = global.queriers[pkg.Src]; Packet pkg1 = new Packet(this, querier, PacketType.LOCATION_QUERY); int obj = pkg.ObjectLocationRequest.obj; if (this.objectLogicalPaths.ContainsKey(obj)) pkg1.ObjectLogicalPathQueryServerReply = new ObjectLogicalPathQueryServerReplyField(obj, this.objectLogicalPaths[obj].G, this.objectLogicalPaths[obj].R, this.objectLogicalPaths[obj].T); else pkg1.ObjectLogicalPathQueryServerReply = null; SendPacketDirectly(scheduler.currentTime, pkg1); }
public void CheckReversePathCache(Packet pkg) { int r = pkg.ObjectLogicalPathUpdate.r; int o = pkg.ObjectLogicalPathUpdate.obj; string key = r + "_" + o; int prev = pkg.Prev; if (this.reversePathCache.ContainsKey(key)) { this.reversePathCache[key].time = scheduler.currentTime; } else { this.reversePathCache.Add(key, new ReversePath(r, prev, scheduler.currentTime)); } }
public override void Recv(Packet pkg) { switch (pkg.Type) { //Some codes are hided in the base class. case PacketType.LOGICAL_PATH_UPDATE: UpdateLogicalPath(pkg); break; case PacketType.LOCATION_QUERY: SendObjectLogicalPath(pkg); break; default: base.Recv(pkg); return; } }
public void UpdateLogicalPath(Packet pkg) { int id = pkg.ObjectLogicalPathUpdate.obj; ObjectLogicalPath path = null; if (this.objectLogicalPaths.ContainsKey(id)) path = this.objectLogicalPaths[id]; else { path = new ObjectLogicalPath(); this.objectLogicalPaths.Add(id, path); } path.O = pkg.ObjectLogicalPathUpdate.obj; path.R = pkg.ObjectLogicalPathUpdate.r; path.G = pkg.ObjectLogicalPathUpdate.g; path.T = pkg.ObjectLogicalPathUpdate.t; }
public override void Recv(Packet pkg) { if (pkg.PrevType != NodeType.SERVER ) { System.Console.WriteLine("Server receive error type!"); return; } else if (pkg.PrevType == NodeType.SERVER) { switch (pkg.Type) { case PacketType.LOCATION_QUERY: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}, total time:{6}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev, scheduler.currentTime-this.queryingTime); GetQueryResponse(pkg); break; } } }
public void RecvQueryRequest(Packet pkg) { ObjectLogicalPathQueryRequestField f = pkg.ObjectLogicalPathQueryReqeust; Reader gw = global.readers[f.gateway]; Packet reply = new Packet(this, gw, PacketType.LOGICAL_PATH_REPLY); reply.NextType = NodeType.READER; reply.Next = pkg.Prev; reply.ObjectLogicalPathQueryReply = new ObjectLogicalPathQueryReplyField(); reply.ObjectLogicalPathQueryReply.gateway = f.gateway; reply.ObjectLogicalPathQueryReply.obj = Id; reply.ObjectLogicalPathQueryReply.querier = pkg.Src; //TODO, x and y should be acquired from landmark reader... reply.ObjectLogicalPathQueryReply.shape = new PointShape(this.x, this.y); //TODO fill the shape. //do some stuf here SendPacketDirectly(scheduler.currentTime, reply); }
public override void Recv(Packet pkg) { if (pkg.PrevType != NodeType.READER) { Debug.Assert(false, "Wrong packet src"); } Node node = global.readers[pkg.Prev]; if (Utility.Distance(this, (MobileNode)node) > Global.getInstance().nodeMaxDist) { Console.WriteLine("{0:F4} [{1}] {2}{3} Drop from {4}{5} out of space", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); return; } switch (pkg.Type) { case PacketType.LANDMARK: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); SetCurrentLandmarkLocation(pkg); break; case PacketType.DATA_AVAIL: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); UpdateLocation(pkg); List<Packet> removeList = new List<Packet>(); foreach (Packet p in this.cachePackets) { if (this.cachePackets.Contains(pkg) && TrySendData(p)) { this.lastNearReader = this.global.readers[pkg.Src]; this.lastNearReaderTime = scheduler.currentTime; removeList.Add(p); } else if (!this.cachePackets.Contains(pkg)) this.cachePackets.Add(pkg); } foreach (Packet p in removeList) this.cachePackets.Remove(p); break; default: break; } return; }
public void UpdateLogicalPath(Packet pkg) { float interval = scheduler.currentTime - this.lastLocationUpdate; if (interval < global.minLocationUpdateInterval) return; Reader reader = this.global.readers[pkg.Src]; //if (reader == lastNearReader && interval < global.maxLocationUpdateInterval) if (interval < global.maxLocationUpdateInterval) return; Packet pkg1 = new Packet(this, global.server, PacketType.LOGICAL_PATH_UPDATE); pkg1.ObjectLogicalPathUpdate = new ObjectLogicalPathUpdateField(Id); pkg1.ObjectLogicalPathUpdate.t = scheduler.currentTime; pkg1.ObjectLogicalPathUpdate.s = global.server.Id; pkg1.Next = reader.Id; pkg1.NextType = NodeType.READER; SendPacketDirectly(scheduler.currentTime, pkg1); this.lastLocationUpdate = scheduler.currentTime; this.lastNearReader = reader; //Console.WriteLine("{0:F4} [{1}] {2}{3} starts to update logical path", scheduler.CurrentTime,pkg1.Type, this.type, this.id); }
public override void Recv(Packet pkg) { Node node = Node.getNode(pkg.Prev, pkg.PrevType); if (node.type == NodeType.SERVER) { switch (pkg.Type) { case PacketType.LOCATION_QUERY: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); GetQueryResponseFromServer(pkg); break; } } else if (node.type == NodeType.READER) { switch (pkg.Type) { case PacketType.LOGICAL_PATH_REPLY: GetQueryResponseFromReader(pkg); break; } } }
public override void Recv(Packet pkg) { if (pkg.PrevType != NodeType.QUERIER && pkg.PrevType != NodeType.READER) { Debug.Assert(false, scheduler.currentTime + " Server receive error type!"); } if (pkg.PrevType == NodeType.READER) { switch (pkg.Type) { case PacketType.LOCATION_UPDATE: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); Console.WriteLine("{0:F4} [{1}] {2}{3} update location for {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, NodeType.OBJECT, pkg.ObjectUpdateLocation.obj); updateLocation(pkg.ObjectUpdateLocation.obj, pkg.ObjectUpdateLocation.location.X, pkg.ObjectUpdateLocation.location.Y, pkg.ObjectUpdateLocation.updateTime); break; case PacketType.DATA: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); Console.WriteLine("{0:F4} [{1}] {2}{3} recv data from {4}{5}. total time: {6}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.SrcType, pkg.Src, scheduler.currentTime-pkg.beginSentTime); break; } } else if (pkg.PrevType == NodeType.QUERIER) { switch (pkg.Type) { case PacketType.LOCATION_QUERY: Console.WriteLine("{0:F4} [{1}] {2}{3} recv from {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.PrevType, pkg.Prev); SendObjectLocation(pkg); break; } } }
public void RecvTagHeaderResponse(Packet pkg) { //终端节点收到标签的请求 if (pkg.PrevType == NodeType.OBJECT) { int tagId = pkg.ObjectTagHeader.tagId; int orgId = pkg.ObjectTagHeader.orgId; if (!this.NearbyObjectCache.ContainsKey(tagId)) { throw new Exception("no such a tag before, design eror?"); } if (this.NearbyObjectCache[tagId].orgId < 0) { //首先添加缓存 Console.WriteLine("{0:F4} [NEW_TAG_FOUND] {1}{2} of ORG{3} found by READER{4}", scheduler.currentTime, pkg.PrevType, tagId, orgId, Id); //this.NearbyObjectCache.Add(tagId, new ObjectEntity(tagId, orgId, scheduler.CurrentTime)); this.NearbyObjectCache[tagId].key = -1; this.NearbyObjectCache[tagId].orgId = orgId; this.NearbyObjectCache[tagId].time = scheduler.currentTime; } if (this.NearbyObjectCache[tagId].key >= 0) { Packet pkg1 = new Packet(this, global.objects[pkg.Src], PacketType.DATA_AVAIL); pkg1.Authorization = new AuthorizationField(new int[1] { tagId }, new int[1] { this.NearbyObjectCache[tagId].key }); SendPacketDirectly(scheduler.currentTime, pkg1); return; } //如果缓存中没有该节点,或尚未授权,则发请求到相应机构 //TODO make sure the request does not send too soon //send request to the org Packet pkg2 = new Packet(this, global.orgs[orgId], PacketType.TAG_HEADER); pkg2.ObjectTagHeader = new ObjectTagHeaderField( pkg.ObjectTagHeader.tagId, pkg.ObjectTagHeader.orgId); pkg2.ObjectTagHeader.networkId = networkId; RoutePacket(pkg2); return; } if (this.IsGateway == true) { pkg.Next = pkg.Dst; pkg.NextType = pkg.DstType; SendPacketDirectly(scheduler.currentTime, pkg); return; } else { RoutePacket(pkg); return; } }
public override void SendBeacon(float time) { Packet pkg = new Packet(); pkg.Type = PacketType.BEACON; pkg.Src = pkg.Prev = Id; pkg.Dst = pkg.Next = -1;//Broadcast pkg.TTL = 1; pkg.Beacon = new BeaconField(); if (this.gatewayEntities.Count > 0) { pkg.Beacon.gatewayEntities = new GatewayEntity[this.gatewayEntities.Count]; int i = 0; foreach (int g in this.gatewayEntities.Keys) { pkg.Beacon.gatewayEntities[i++] = new GatewayEntity(g, this.Id, this.gatewayEntities[g].hops); } } SendPacketDirectly(time, pkg); float nextBeacon = 0; if (scheduler.currentTime < global.beaconWarming) nextBeacon = (float)(Utility.P_Rand(10 * (global.beaconWarmingInterval + 0.4)) / 10);//0.5是为了设定最小值 else nextBeacon = (float)(Utility.P_Rand(10 * global.beaconInterval) / 10); Event.AddEvent(new Event(scheduler.currentTime + nextBeacon, EventType.SND_BCN, this, null)); }
public void RecvMonitorResponse(Packet pkg) { GetMonitorResponseField f = pkg.GetMonitorResponse; if (this.IsGateway) { if (!this.cachedMonitorNodes.ContainsKey(f.monitorOrg)) this.cachedMonitorNodes.Add(f.monitorOrg, new HashSet<int>()); if (this.cachedMonitorNodes[f.monitorOrg].Contains(f.monitorNode)) this.cachedMonitorNodes[f.monitorNode].Add(f.monitorNode); } RoutePacket(pkg); }
public void RecvSetMonitor(Packet pkg) { int monitorNode = pkg.SetMonitorResponse.monitorNode; int monitorOrg = pkg.SetMonitorResponse.monitorOrg; int monitorNetwork = pkg.SetMonitorResponse.monitorNetwork; //机构发向某节点的setmonitor if (pkg.Dst != BroadcastNode.Node.Id) { if (pkg.Dst == Id) // i am dst { if (this.assignedMonitorOrgs == null) // the first time { Console.WriteLine("{0:F4} [{1}] {2}{3} is selected as a monitor of {4}{5}", scheduler.currentTime, pkg.Type, this.type, this.Id, pkg.SrcType, pkg.Src); this.assignedMonitorOrgs = new HashSet<int>(); this.cachedEventTrustResult = new List<IOTEventTrustResult>(); CheckEvents(); } if (!this.assignedMonitorOrgs.Contains(pkg.Src)) this.assignedMonitorOrgs.Add(pkg.Src); else return; Packet pkg1 = new Packet(this, BroadcastNode.Node, PacketType.SET_MONITOR); pkg1.SetMonitorResponse = new SetMonitorResponseField(monitorNode, monitorOrg, monitorNetwork); SendPacketDirectly(scheduler.currentTime, pkg1); return; } else //not dst, forward RoutePacket(pkg); } else { if (this.orgMonitorMapping.ContainsKey(monitorOrg) && this.orgMonitorMapping[monitorOrg].Contains(monitorNode)) return; else { if (!this.orgMonitorMapping.ContainsKey(monitorOrg)) this.orgMonitorMapping.Add(monitorOrg, new List<int>()); this.orgMonitorMapping[monitorOrg].Add(monitorNode); Packet pkg1 = new Packet(this, BroadcastNode.Node, PacketType.SET_MONITOR); pkg1.SetMonitorResponse = new SetMonitorResponseField(monitorNode, monitorOrg, monitorNetwork); SendPacketDirectly(scheduler.currentTime, pkg1); return; } } }
public void RecvEventReport(Packet pkg) { if (pkg.Dst != Id) { RoutePacket(pkg); return; } if (this.assignedMonitorOrgs == null || this.assignedMonitorOrgs.Count == 0) { Console.WriteLine("Reader{0} is not a monitor node.", Id); return; } MemoryStream ms = new MemoryStream(pkg.TrustReport.result); BinaryFormatter formatter = new BinaryFormatter(); List<IOTEventTrustResult> results = (List<IOTEventTrustResult>)formatter.Deserialize(ms); if(global.debug) Console.WriteLine("READER{0} recv {1} reports.", Id, results.Count); foreach (IOTEventTrustResult r in results) this.cachedEventTrustResult.Add(r); }
public void RecvMonitorRequest(Packet pkg) { int networkId = pkg.GetMonitorRequest.network; if (this.networkId != networkId) { Console.WriteLine("Fatal: wrong network: {0} - {1}", this.networkId, networkId); return; } GetMonitorRequestField f = pkg.GetMonitorRequest; int[] orgs = f.orgs; int requestOrg = f.requestOrg; if (this.IsGateway) { //如果自己是网关,则查找 foreach (int o in orgs) { if (this.cachedMonitorNodes.ContainsKey(o)) { Packet pkg1 = new Packet(this, global.orgs[requestOrg], PacketType.GET_MONITOR_RESPONSE); pkg1.GetMonitorResponse = new GetMonitorResponseField(o, this.cachedMonitorNodes[o].First(), networkId); SendPacketDirectly(scheduler.currentTime, pkg1); return; } } } //如果自己网关,但没有缓存信息,或者自己是普通节点,检查自己的机构 for (int i = 0; i < orgs.Length; i++) { if (orgs[i] == this.OrgId) { Packet pkg1 = new Packet(this, global.orgs[requestOrg], PacketType.GET_MONITOR_RESPONSE); pkg1.GetMonitorResponse = new GetMonitorResponseField(this.Id, this.OrgId, networkId); SendPacketDirectly(scheduler.currentTime, pkg1); return; } } //如果自己的机构不在列表中,则广播其他邻居 Packet pkg2 = new Packet(); pkg.Src = pkg.Prev = Id; pkg.Dst = pkg.Next = -1;//Broadcast pkg.GetMonitorRequest = new GetMonitorRequestField(f.orgs, f.network, f.requestOrg); SendPacketDirectly(scheduler.currentTime, pkg2); }
public void RecvCommand(Packet pkg) { if (pkg.Dst == Id) { CommandField cmd = pkg.Command; CommandField cmd1 = new CommandField(cmd.tag, cmd.operation); if (this.readerType == ReaderType.DROP_COMMAND) return; if (this.readerType == ReaderType.MODIFY_COMMAND) cmd1.operation = -1; // -1表示被修改了 Packet pkg1 = new Packet(this, global.objects[cmd.tag], PacketType.COMMAND); SendPacketDirectly(scheduler.currentTime, pkg1); } else RoutePacket(pkg); }
public override void RecvBeacon(Packet pkg) { Scheduler scheduler = Scheduler.getInstance(); Reader node = global.readers[pkg.Src]; if (pkg.Prev == Id && pkg.PrevType == type) return; Neighbor nb = null; if (Neighbors.ContainsKey(node.Id)) { nb = (Neighbor)Neighbors[node.Id]; } if (nb != null) { nb.lastBeacon = scheduler.currentTime; } else { //Add as a neighbor AddNeighbor(node); } if (pkg.Beacon != null) { if (pkg.Beacon.gatewayEntities != null) { for (int i = 0; i < pkg.Beacon.gatewayEntities.Length; i++) { GatewayEntity g = pkg.Beacon.gatewayEntities[i]; if (!this.gatewayEntities.ContainsKey(g.gateway)) { this.gatewayEntities.Add(g.gateway, new GatewayEntity(g.gateway, g.next, g.hops + 1)); Console.WriteLine("{0:F4} [{1}] {2}{3} add a gateway of {4} hops {5}.", scheduler.currentTime, pkg.Type, this.type, this.Id, g.gateway, g.hops); } else if (this.gatewayEntities[g.gateway].hops > g.hops + 1) { this.gatewayEntities[g.gateway].hops = g.hops + 1; this.gatewayEntities[g.gateway].next = g.next; Console.WriteLine("{0:F4} [{1}] {2}{3} update a gateway of {4} hops {5}.", scheduler.currentTime, pkg.Type, this.type, this.Id, g.gateway, g.hops); } else if (this.gatewayEntities[g.gateway].next == g.next)//update in case of the next hop moves this.gatewayEntities[g.gateway].hops = g.hops + 1; } } } }
public void RecvAuthorization(Packet pkg) { if (pkg.Dst == Id) // i am dst { AuthorizationField f = pkg.Authorization; for (int i = 0; i < f.tags.Length; i++) { int tagId = f.tags[i]; int key = f.keys[i]; if (!this.NearbyObjectCache.ContainsKey(tagId)) { Console.WriteLine("OBJECT{0} is out of READER{1}", tagId, Id); continue; } this.NearbyObjectCache[tagId].key = key; Packet pkg1 = new Packet(this, global.objects[tagId], PacketType.DATA_AVAIL); int[] tags = new int[1] { tagId }; int[] keys = new int[1] { key }; pkg1.Authorization = new AuthorizationField(tags, keys); SendPacketDirectly(scheduler.currentTime, pkg1); } return; } //forward RoutePacket(pkg); }
//Get all near reader rather than objects. public override void NotifyObjects() { List<ObjectNode> list = GetAllNearObjects(this, global.objectMaxDist); foreach (ObjectNode obj in list) { if (!this.NearbyObjectCache.ContainsKey(obj.Id)) { this.NearbyObjectCache.Add(obj.Id, new ObjectEntity(obj.Id, -1, scheduler.currentTime)); } else { if (scheduler.currentTime - this.NearbyObjectCache[obj.Id].time < 1)//TODO 1 is fixed... continue; this.NearbyObjectCache[obj.Id].time = scheduler.currentTime; } Packet pkg = new Packet(this, obj, PacketType.TAG_HEADER); //Request the object's tag header. SendPacketDirectly(scheduler.currentTime, pkg); } }
//普通节点检查现象 public void CheckRoutine() { if(global.debug) Console.WriteLine("{0}{1} check routing.", type, Id); CheckNetworkBandwidth(); CheckNodeSpeeds(); //Console.WriteLine("Reader{0} ClearOutdatedPhenomemons.", id); ClearOutdatedPhenomemons(); //Console.WriteLine("Reader{0} CheckTimeoutPhenomemons.", id); CheckTimeoutPhenomemons(); if (this.observedPhenomemons.Count > 0) { List<IOTEventTrustResult> list = IOTEventTrust.DeduceAllEventTrusts(this.Id, this.observedPhenomemons, scheduler.currentTime); Dictionary<int, List<IOTEventTrustResult>> l = new Dictionary<int, List<IOTEventTrustResult>>(); for (int i = 0; i < list.Count; i++) { IOTEventTrustResult r = list[i]; if (!l.ContainsKey(r.app)) l.Add(r.app, new List<IOTEventTrustResult>()); l[r.app].Add(r); } foreach (KeyValuePair<int, List<IOTEventTrustResult>> entity in l) { int org = entity.Key;//机构和应用是一一对应的,所以不作区分 List<IOTEventTrustResult> results = entity.Value; foreach(IOTEventTrustResult e in results) { Console.WriteLine("reader{0} report a event of {1} of reader{2}", Id, e.eventIdent, e.node); } if (this.orgMonitorMapping.ContainsKey(org) && this.orgMonitorMapping[org].Count > 0) { byte[] buf = new byte[global.BufSize * results.Count]; MemoryStream ms = new MemoryStream(buf); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, results); byte[] tmp = new byte[ms.Position]; Array.Copy(buf, tmp, ms.Position); if (global.debug) Console.WriteLine("READER{0} sends {1} reports.", Id, results.Count); //发往每一个monitor节点 for (int j = 0; j < this.orgMonitorMapping[org].Count; j++) { int dst = this.orgMonitorMapping[org][j]; Packet pkg = new Packet(this, global.readers[dst], PacketType.EVENT_REPORT); //pkg.TrustReport = new TrustReportField(org, data, bw.BaseStream.Position); pkg.TrustReport = new TrustReportField(org, tmp, tmp.Length); RoutePacket(pkg); //SendPacketDirectly(scheduler.CurrentTime, pkg); } } else { Console.WriteLine("Unable to find a monitor for org {0}, abort.", org); } } } float time = scheduler.currentTime + global.checkPhenomemonTimeout; Event.AddEvent(new Event(time, EventType.CHK_RT_TIMEOUT, this, null)); //Console.WriteLine("Reader{0} check routing done.", id); }