public void CheckRoutine() { IOTGlobal g = (IOTGlobal)global; double forgetFactor = g.ForgetFactor; for (int i = 0; i < g.orgNum; i++) { double v = this.orgReputations[i].trustValue; v = Math.Min(v + forgetFactor, g.InitTrust); this.orgReputations[i].trustValue = v; } if (this.cachedNodeTrustTypeResult.Count > 0) { //先将节点报告根据节点分类 Dictionary<int, List<IOTNodeTrustTypeResult>> hashedNodeTrustTypes = new Dictionary<int, List<IOTNodeTrustTypeResult>>(); foreach (IOTNodeTrustTypeResult nodeTrustType in this.cachedNodeTrustTypeResult) { if (!hashedNodeTrustTypes.ContainsKey(nodeTrustType.nodeId)) hashedNodeTrustTypes.Add(nodeTrustType.nodeId, new List<IOTNodeTrustTypeResult>()); hashedNodeTrustTypes[nodeTrustType.nodeId].Add(nodeTrustType); } //对每一个节点分析其最可能的类型 List<IOTNodeTrustTypeResult> combinedNodeTrustNodeTypes = new List<IOTNodeTrustTypeResult>(); foreach (KeyValuePair<int, List<IOTNodeTrustTypeResult>> k in hashedNodeTrustTypes) { int[] t = new int[(int)IOTNodeType.COUNT]; int node = k.Key; List<IOTNodeTrustTypeResult> hashedNodeTrustType = k.Value; int org = global.readers[node].OrgId; foreach (IOTNodeTrustTypeResult r in hashedNodeTrustType) { switch (r.type) { case IOTNodeType.NORMAL: t[(int)IOTNodeType.NORMAL]++; break; case IOTNodeType.NODE_FAULTY: t[(int)IOTNodeType.NODE_FAULTY]++; break; case IOTNodeType.ENV_FAULTY: t[(int)IOTNodeType.ENV_FAULTY]++; break; case IOTNodeType.MALICIOUS: t[(int)IOTNodeType.MALICIOUS]++; break; default: break; } } int max = 0; for (int i = 0; i < (int)IOTNodeType.COUNT; i++) { if (t[i] > t[max]) max = i; } if ((IOTNodeType)max == IOTNodeType.NORMAL) continue; //只讲异常的节点放入待处理列表中 IOTNodeTrustTypeResult finalNodeTrustType = new IOTNodeTrustTypeResult(node, org, (IOTNodeType)max); combinedNodeTrustNodeTypes.Add(finalNodeTrustType); } //按照机构划分节点报告 Dictionary<int, List<IOTNodeTrustTypeResult>> hashedCombinedNodeTrustNodeTypes = new Dictionary<int, List<IOTNodeTrustTypeResult>>(); foreach (IOTNodeTrustTypeResult r in combinedNodeTrustNodeTypes) { if (!hashedCombinedNodeTrustNodeTypes.ContainsKey(r.orgId)) hashedCombinedNodeTrustNodeTypes.Add(r.orgId, new List<IOTNodeTrustTypeResult>()); hashedCombinedNodeTrustNodeTypes[r.orgId].Add(r); } //对同一机构的节点归纳,计算该机构的信誉 CalculateOrganizationReputation(hashedCombinedNodeTrustNodeTypes); this.cachedNodeTrustTypeResult.Clear(); } OutputReputations(); float time = scheduler.currentTime + global.checkNodeTypeTimeout; Event.AddEvent(new Event(time, EventType.CHK_RT_TIMEOUT, this, null)); }
//每隔一段时间检查一下节点的信任值 public void CheckRoutine() { Dictionary<int, EventCount> checkedNodes = new Dictionary<int, EventCount>(); foreach (KeyValuePair<int, ObjectDataRecordList> k in this.cachedObjectDataRecordList) { int obj = k.Key; ObjectDataRecordList l = k.Value; for (int seq = l.minSeq + 1; seq <= l.maxSeq; seq++) { if (!l.list.ContainsKey(seq)) continue; int lastNode = l.list[seq].lastReader; if (lastNode < 0) continue; if (!checkedNodes.ContainsKey(lastNode)) checkedNodes.Add(lastNode, new EventCount()); if (!l.list.ContainsKey(seq - 1)) { //不正常的 checkedNodes[lastNode].badCount++; } checkedNodes[lastNode].totalCount++; } } List<IOTNodeTrustTypeResult> nodeTrustResults = new List<IOTNodeTrustTypeResult>(); foreach (KeyValuePair<int, EventCount> k in checkedNodes) { int node = k.Key; int badCount = k.Value.badCount; int totalCount = k.Value.totalCount; float badCountRate = ((float)badCount)/ totalCount; if (badCount > global.ConfirmedThrehold && badCountRate>global.CofirmedRateThrehold) { IOTNodeTrustTypeResult nodeTrustType = new IOTNodeTrustTypeResult(node, global.readers[node].OrgId, IOTNodeType.MALICIOUS); Console.WriteLine("{0}{1} deduces Reader{2} not work well", type, Id, nodeTrustType.nodeId); nodeTrustResults.Add(nodeTrustType); if (!this.cacheHistoricaldNodeTrustResult.ContainsKey(node)) this.cacheHistoricaldNodeTrustResult.Add(node, nodeTrustType); else this.cacheHistoricaldNodeTrustResult[node] = nodeTrustType; } } //将最终的数据发送给信任管理机构 if (nodeTrustResults.Count > 0) { byte[] buf = new byte[global.BufSize * nodeTrustResults.Count]; MemoryStream ms = new MemoryStream(buf); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, nodeTrustResults); byte[] tmp = new byte[ms.Position]; Array.Copy(buf, tmp, ms.Position); Packet pkg = new Packet(this, global.trustManager, PacketType.NODE_TYPE_REPORT); pkg.TrustReport = new TrustReportField(0, tmp, tmp.Length); SendPacketDirectly(scheduler.currentTime, pkg); } float time = scheduler.currentTime + global.checkNodeTimeout; Event.AddEvent(new Event(time, EventType.CHK_RT_TIMEOUT, this, null)); }