protected ObjectNode(int id) : base(id) { this.type = NodeType.OBJECT; this.currentLandmarkLocation = new Location(); this.lastLocationUpdate = 0; this.lastNearReader = null; this.lastNearReaderTime = -1; this.cachePackets = new List<Packet>(); this.global = Global.getInstance(); }
public virtual void SendAODVReply(int dst, Reader node, int hops, double lastTime) { Packet pkg = new Packet(this, node, PacketType.AODV_REPLY); pkg.TTL = 1; pkg.Data = new AODVReply(dst, hops, lastTime); SendPacketDirectly(scheduler.currentTime, pkg); }
public virtual Neighbor AddNeighbor(Reader nb) { if (!this.Neighbors.ContainsKey(nb.Id)) this.Neighbors.Add(nb.Id, new Neighbor(nb)); if (!this.routeTable.ContainsKey(nb.Id)) this.routeTable.Add(nb.Id, new RouteEntity(nb.Id, nb.Id, 1, scheduler.currentTime, scheduler.currentTime)); return this.Neighbors[nb.Id]; }
public Neighbor(Reader node) { this.node = node; lastBeacon = -1; firstBeacon = -1; }
public Dictionary<Node, MODEventTrustResult> AdjustNodeTrust(Dictionary<Node, double> localNodeTrustWeights, Dictionary<Node, double> localNodeTrustWeightsLastUpdate, Dictionary<Node, List<double>> localNodeSuspectCount, List<DirectTrustEntity>[] localOrgDirectTrustWeights, Dictionary<Node, List<double>> localNodeHistoryMyVariance, Dictionary<Node, List<double>> localNodeHistoryTotalVariance, Dictionary<Node, MODEventTrustResult> cachedresults, string pkgIdent, Reader deducingNode, int suspectNodeId) { Node[] reportNodes = cachedresults.Keys.ToArray(); MODEventTrustResult myReport = cachedresults[deducingNode]; Dictionary<int, HashSet<int>> orgMapping = new Dictionary<int, HashSet<int>>(); Dictionary<Node, MODEventTrustResult> suspectedReportOrgs = new Dictionary<Node, MODEventTrustResult>(); Dictionary<Node, MODEventTrustResult> finalSuspectedReportNodes = new Dictionary<Node, MODEventTrustResult>(); bool isNB = deducingNode.Neighbors.ContainsKey(suspectNodeId); foreach (Node reportNode in reportNodes) { Reader reportReader = ((Reader)reportNode); if (!orgMapping.ContainsKey(reportReader.OrgId)) { orgMapping.Add(reportReader.OrgId, new HashSet<int>()); } if (!orgMapping[reportReader.OrgId].Contains(reportReader.Id)) orgMapping[reportReader.OrgId].Add(reportReader.Id); } Dictionary<Node, MODEventTrustResult> orgReports = new Dictionary<Node, MODEventTrustResult>(); //计算每个机构的一致性 foreach (KeyValuePair<int, HashSet<int>> k in orgMapping) { int orgId = k.Key; Organization org = global.orgs[orgId]; HashSet<int> nodeIds = k.Value; List<MODEventTrustResult> nodeResults = new List<MODEventTrustResult>(); foreach (int nodeId in nodeIds) { nodeResults.Add(cachedresults[global.readers[nodeId]]); } MODEventTrustResult orgReport = MODEventTrust.MergeMaliciousEventTrustResult(suspectNodeId, org, nodeResults, pkgIdent, MODEventCategoryType.DropPacket); MODEventTrust.CalculateRelativeMaliciousEventTrustResult(orgReport, myReport); orgReports.Add(org, orgReport); } MODEventTrustResult totalOrgReport = MODEventTrust.MergeMaliciousEventTrustResult(suspectNodeId, MODOrganization.totalOrg, orgReports.Values.ToList(), pkgIdent, MODEventCategoryType.DropPacket); //初始化机构信任值 for (int i = 0; i < global.orgNum; i++) { Organization org = global.orgs[i]; List<DirectTrustEntity> temp = new List<DirectTrustEntity>(); double total = 0; foreach (DirectTrustEntity e in localOrgDirectTrustWeights[org.Id]) { if (e.time < scheduler.currentTime - 0.1f) { temp.Add(e); total += e.value; } } if (!localNodeTrustWeights.ContainsKey(org)) { if (localOrgDirectTrustWeights[org.Id].Count>0) localNodeTrustWeights.Add(org, total / localOrgDirectTrustWeights[org.Id].Count); else localNodeTrustWeights.Add(org, 1f); localNodeTrustWeightsLastUpdate.Add(org, scheduler.currentTime); } else { //如果是机构的话,则将其信誉提高一些,作为遗忘因子 //7s恢复0.1 localNodeTrustWeights[org] = Math.Min(1.0f, localNodeTrustWeights[org] + 0.1 * (scheduler.currentTime - localNodeTrustWeightsLastUpdate[org]) / 6); if (localOrgDirectTrustWeights[org.Id].Count > 0) { localNodeTrustWeights[org] = 0.3 * localNodeTrustWeights[org] + 0.7 * total / localOrgDirectTrustWeights[org.Id].Count; } localNodeTrustWeightsLastUpdate[org] = scheduler.currentTime; } foreach (DirectTrustEntity e in temp) { localOrgDirectTrustWeights[org.Id].Remove(e); } } double variance = MODEventTrust.CalculateRelativeMaliciousEventTrustResult(orgReports.Values.ToList()); //if (totalOrgReport.variance > global.MaxTotalOrgVariance) if (variance > global.MaxTotalOrgVariance) { //找到所有与自己差别较大的机构 foreach (KeyValuePair<int, HashSet<int>> k in orgMapping) { int orgId = k.Key; Organization org = global.orgs[orgId]; //是否存在同机构的报告节点 bool hasPeer = orgReports.ContainsKey(global.orgs[deducingNode.OrgId]); MODEventTrustResult refReport = null; if (isNB || hasPeer) { if (isNB) refReport = myReport; else if (hasPeer) refReport = orgReports[global.orgs[deducingNode.OrgId]]; double supportMe = 0, nonsupportMe = 0; foreach (int nodeId in orgMapping[org.Id]) { Node node = global.readers[nodeId]; double myVarianceDist = MODEventTrust.CalculateRelativeMaliciousEventTrustResult(cachedresults[node], refReport); if (myVarianceDist > global.MaxReportDistance) nonsupportMe++; else supportMe++; } if ((supportMe / (supportMe + nonsupportMe) < 0.5f && nonsupportMe>1) || nonsupportMe > 3) { suspectedReportOrgs.Add(org, orgReports[org]); finalSuspectedReportNodes.Add(org, orgReports[org]); } //double dist = MODEventTrust.Distance(myReport, orgReports[org]); } //非邻居 else { //orgReports[org].myVarianceDist = MODEventTrust.CalculateRelativeMaliciousEventTrustResult(orgReports[org], myReport); orgReports[org].totalVarianceDist = MODEventTrust.CalculateRelativeMaliciousEventTrustResult(orgReports[org], totalOrgReport); if (orgReports[org].totalVarianceDist > global.MaxReportDistance) { suspectedReportOrgs.Add(org, orgReports[org]); finalSuspectedReportNodes.Add(org, orgReports[org]); } } } //找到所有可疑的报告节点 foreach (KeyValuePair<int, HashSet<int>> k in orgMapping) { int orgId = k.Key; Organization org = global.orgs[orgId]; if (suspectedReportOrgs.ContainsKey(org)) continue; foreach (int nodeId in orgMapping[orgId]) { Node node = global.readers[nodeId]; cachedresults[node].myVarianceDist = MODEventTrust.CalculateRelativeMaliciousEventTrustResult(cachedresults[node], myReport); cachedresults[node].totalVarianceDist = MODEventTrust.CalculateRelativeMaliciousEventTrustResult(cachedresults[node], totalOrgReport); if (isNB && cachedresults[node].myVarianceDist > global.MaxReportDistance) { //if (MODEventTrust.Distance(myReport, cachedresults[node]) > global.MaxReportDistance) finalSuspectedReportNodes.Add(node, cachedresults[node]); continue; } else if (cachedresults[node].totalVarianceDist > global.MaxReportDistance) { finalSuspectedReportNodes.Add(node, cachedresults[node]); } } } //所有节点添加历史相关度 foreach (KeyValuePair<Node, MODEventTrustResult> k in cachedresults) { Node node = k.Key; if (!localNodeHistoryMyVariance.ContainsKey(node)) localNodeHistoryMyVariance.Add(node, new List<double>()); if(isNB) localNodeHistoryMyVariance[node].Add(k.Value.myVarianceDist); if (!localNodeHistoryTotalVariance.ContainsKey(node)) localNodeHistoryTotalVariance.Add(node, new List<double>()); localNodeHistoryMyVariance[node].Add(k.Value.totalVarianceDist); } foreach (KeyValuePair<Node, MODEventTrustResult> k in suspectedReportOrgs) { Node node = k.Key; if (!localNodeHistoryMyVariance.ContainsKey(node)) localNodeHistoryMyVariance.Add(node, new List<double>()); if (isNB) localNodeHistoryMyVariance[node].Add(k.Value.myVarianceDist); if (!localNodeHistoryTotalVariance.ContainsKey(node)) localNodeHistoryTotalVariance.Add(node, new List<double>()); localNodeHistoryMyVariance[node].Add(k.Value.totalVarianceDist); } //计算将可疑机构或节点的信任值 foreach (KeyValuePair<Node, MODEventTrustResult> k in finalSuspectedReportNodes) { Node node = k.Key; double myVarianceDist = k.Value.myVarianceDist; double freq = 1; if (!localNodeSuspectCount.ContainsKey(node)) localNodeSuspectCount.Add(node, new List<double>()); //初始化节点信任值,机构信任值已经初始化完毕 if (!localNodeTrustWeights.ContainsKey(node) && node.type == NodeType.READER) { localNodeTrustWeights.Add(node, 1f); localNodeTrustWeightsLastUpdate.Add(node, scheduler.currentTime); } if (node.type == NodeType.READER)//考察节点短期的行为,则置1 localNodeTrustWeights[node] = 1f; localNodeSuspectCount[node].Add(scheduler.currentTime); while (localNodeSuspectCount[node].Count>0)//删除首部过时的恶意报告 { if (localNodeSuspectCount[node][0] < scheduler.currentTime - global.maxCountPeriod) localNodeSuspectCount[node].RemoveAt(0); else break; } freq = localNodeSuspectCount[node].Count; //这里惩罚节点有三个条件,一个是与自己的相关度(惩罚小的),另一个是被怀疑的频率(惩罚大的),还有一个是历史的相关度变化(惩罚变化频繁的) //考察历史相关度偏移程度 double historyVarianceStandardDeviation = 1; if (isNB && localNodeHistoryMyVariance[node].Count > 2)//曲线至少有三个点 { //使历史记录数量维持在MaxHistoryCount之内 if (localNodeHistoryMyVariance[node].Count > global.MaxHistoryCount) localNodeHistoryMyVariance[node].RemoveRange(0, localNodeHistoryMyVariance[node].Count - global.MaxHistoryCount); double[] arcs = new double[localNodeHistoryMyVariance[node].Count]; for (int i = 0; i < localNodeHistoryMyVariance[node].Count; i++) arcs[i] = 0.1f * i; LinearRegression reg = new LinearRegression(); reg.BuildLSMCurve(arcs, localNodeHistoryMyVariance[node], 1, false); //考察reg.CoefficientsStandardError historyVarianceStandardDeviation = reg.StandardDeviation; } else if (localNodeHistoryTotalVariance[node].Count > 2)//曲线至少有三个点 { //使历史记录数量维持在MaxHistoryCount之内 if (localNodeHistoryTotalVariance[node].Count > global.MaxHistoryCount) localNodeHistoryTotalVariance[node].RemoveRange(0, localNodeHistoryTotalVariance[node].Count - global.MaxHistoryCount); double[] arcs = new double[localNodeHistoryTotalVariance[node].Count]; for (int i = 0; i < localNodeHistoryTotalVariance[node].Count; i++) arcs[i] = 0.1f * i; LinearRegression reg = new LinearRegression(); reg.BuildLSMCurve(arcs, localNodeHistoryTotalVariance[node], 1, false); //考察reg.CoefficientsStandardError historyVarianceStandardDeviation = reg.StandardDeviation; } double vv = isNB? Math.Pow(global.VarianceBase, myVarianceDist):1; localNodeTrustWeights[node] *= global.AdjustFactor / ((Math.Log(freq + 1, global.SuspectedCountBase) + 1) * vv * (Math.Log(historyVarianceStandardDeviation, global.HistoryVSDBase) + 1)); if (localNodeTrustWeights[node] > 1) localNodeTrustWeights[node] = 0.99f; else if(localNodeTrustWeights[node] <=0) localNodeTrustWeights[node] = 0.01f; } } return finalSuspectedReportNodes; }
public Dictionary<int, SubTreeEntry> subtree; //子树 #endregion Fields #region Constructors public AnonyTreeEntry(int rootId, Reader father, int m) { this.rootId = rootId; this.parent = father; //this.parenthops = 1; //this.parentconfirmed = Scheduler.getInstance().CurrentTime; //this.regionReqConfirmed = regionReqConfirmed; this.m = m; this.cn = 0; this.subtree = new Dictionary<int, SubTreeEntry>(); this.anonyRequests = new Dictionary<string, AnonyRequest>(); this.pingRecords = new Dictionary<string, float>(); this.pendingNewGroupNodes = new Dictionary<string, Dictionary<int, HashSet<int>>>(); this.pendingCandidates = new Dictionary<string, HashSet<int>>(); }
public void ProcessNewGroupRequest(int rootId, int k, int origId, double L, double l, double preAngle, int hops, Reader snode) { string key = rootId + ""; AnonyTreeEntry subTreeInfo = this.CachedTreeEntries[key]; string newgroupident = rootId + "-" + origId + "-" + k; Console.WriteLine("READER{0} is process new group request...", Id); Dictionary<int, SubTreeEntry> subtree = this.CachedTreeEntries[key].subtree; //原来的思路是寻找包含所有候选节点的父节点 //现在通过框添加节点 PrivacyReader.AnonFrames.DumpFrames(this.Id); //如果有的节点被交换,则需要调整其原有的你们匿名组,在我们的方案中,只有两类交换 //第一类是锚点交换,只在本框内,不会涉及跨组,而第二类在节点不够的条件下,需要跨组交换,这个需要调整 //由于只与自由框交换,所以只需要调整origId节点即可 //swapList是指示是否需要交换节点的列表 List<int> swapList = null; HashSet<int> result = PrivacyReader.AnonFrames.AddNewGroup(origId, this.Id, k, this.anonGroups, subtree, ref swapList); //建完组,处理后面的事情 if (result != null) { PrintGroupNodes(result); PrivacyReader.AnonFrames.DumpFrames(this.Id); //如果已经在该组内,且匿名组没有变化,则返回 if (result.Contains(this.Id) && this.anonGroups.groups.ContainsKey(k) && Utility.IsSameHashSet(this.anonGroups.groups[k], result)) return; if (swapList != null && swapList.Count > 0) AdjustPreviousGroupMembers(origId, swapList); //向子节点发送通知 //PrintGroupNodes(result); if (result.Contains(this.Id)) { HashSet<int> tmp = new HashSet<int>(); Utility.CopyHashSet(tmp, result); this.anonGroups.groups.Add(k, tmp); } foreach (KeyValuePair<int, SubTreeEntry> pair in subTreeInfo.subtree) { int c = pair.Key; HashSet<int> list = pair.Value.subnodes; HashSet<int> newsubnode = new HashSet<int>(); foreach (int x in result) { if (c == x || list.Contains(x)) newsubnode.Add(x); } if (newsubnode.Count > 0) SendSetGroup(rootId, origId, k, c, result, newsubnode); } //向父亲报告通知 if (subTreeInfo.parent != null) SendSetGroup(rootId, origId, k, subTreeInfo.parent.Id, result, null); } else if (this.Id != rootId) //如果建组失败,则向父节点发送 { SendNewGroupRequest(rootId, k, origId, subTreeInfo.parent.Id, L, l, preAngle, hops); //继续发送 return; } else //如果建组失败,且自己就是根节点,则放弃建组 { //不重试了,直接返回失败 Console.WriteLine("{0:F4} [NEW_GROUP_FAIL] {1}{2} {3}", scheduler.currentTime, "READER", this.Id, "new group failed, not enough nodes."); } }
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 override bool SendData(Packet pkg) { if (TrySendData(pkg)==false) { //这里如果发送失败就不重新试了 //if (!this.cachePackets.Contains(pkg)) // this.cachePackets.Add(pkg); return false; } this.lastNearReader = this.global.readers[pkg.Src]; this.lastNearReaderTime = scheduler.currentTime; return true; }
public SWNeighbor(Reader node) : base(node) { this.ClaimedForwardStrategy = null; }
public PointShape NextReaderPosition(Reader reader, float time) { double x = reader.X, y =reader.Y; if (reader.DstX == null || reader.Speed[0] < global.delta)//Not moving return new PointShape(reader.X, reader.Y); double t1 = 0.0f, t2 = 0.0f; for(int i=0;i<reader.DstX.Count;i++) { if (reader.DstX.Count == 0)//Arrived { break; } double dX = reader.DstX[i]; double dY = reader.DstY[i]; double speed = reader.Speed[i]; t2 = Utility.Distance(reader.X, reader.Y, dX, dY) / speed; if (t1 + t2 < time)//Still have time { x = dX; y = dY; t1 += t2; } else { x += (time - t1) * (dX - reader.X) / t2; y += (time - t1) * (dY - reader.Y) / t2; break; } } return new PointShape(x, y); }
public PrivacyNeighbor(Reader node) : base(node) { }