private void RecivePacket(Sensor Reciver, Packet packt) { // string cluster = packt.Branch.Cluster.MembersString; packt.Path += ">" + Reciver.ID; // if Reciver is an agent, then reciev the packet and remove the Reciver from the cluster // reFindBranches if the cluster still have more members. if (new LoopMechanizimAvoidance(packt).isLoop) { // drop the packet: packt.IsLooped = true; counter.DropPacket(packt, Reciver, PacketDropedReasons.Loop); } else { // not loop: packt.ReTransmissionTry = 0; if (Operations.FindInAlist(Reciver, packt.Branch.Cluster.Members) != null) { // Reciver is an agent: counter.ComputeOverhead(packt, EnergyConsumption.Recive, null, Reciver); counter.DisplayRefreshAtReceivingPacket(Reciver); // when the packet arrived to Reciver and holds more clustreds. this case reclustring process should be performed. if (packt.Branch.Cluster.Members.Count > 0) { packt.Branch.Cluster.Members.Remove(Reciver); List <Branch> Branches = FindBranches.GetBranches(packt.Branch.Cluster.Members, Reciver.CenterLocation, PublicParamerters.MainWindow.Canvas_SensingFeild, Settings.Default.MaxClusteringThreshold); if (Branches.Count > 0) { MergedPathsMessages merged = new MergedPathsMessages(Reciver, Branches, packt); } } HandOffToTheSinkOrRecovry(Reciver, packt); } else { double dis1 = Operations.DistanceBetweenTwoPoints(Reciver.CenterLocation, packt.Branch.EndPoint); double dis2 = Operations.DistanceBetweenTwoPoints(Reciver.CenterLocation, packt.Branch.Cluster.Centeriod); double disThre = PublicParamerters.CommunicationRangeRadius; if ((dis1 < disThre) && (packt.Branch.Cluster.Members.Count > 1)) { double clusteringThreshould = 0; if (dis2 <= disThre) { clusteringThreshould = Settings.Default.MinClusteringThreshold; } else { clusteringThreshould = Settings.Default.MaxClusteringThreshold; } counter.ComputeOverhead(packt, EnergyConsumption.Recive, null, Reciver); List <Branch> Branches = FindBranches.GetBranches(packt.Branch.Cluster.Members, Reciver.CenterLocation, PublicParamerters.MainWindow.Canvas_SensingFeild, clusteringThreshould); if (Branches.Count > 1) { MergedPathsMessages merged = new MergedPathsMessages(Reciver, Branches, packt); } else { counter.ComputeOverhead(packt, EnergyConsumption.Recive, null, Reciver); packt.Branch = Branches[0]; // the branch is not changed. only the end coordinat of the branch is changed. SendPacket(Reciver, packt); } } else { counter.ComputeOverhead(packt, EnergyConsumption.Recive, null, Reciver); if (packt.Hops <= packt.TimeToLive) { SendPacket(Reciver, packt); } else { counter.DropPacket(packt, Reciver, PacketDropedReasons.TimeToLive); // since we did not count PID during the generation, thuse we should add here: PublicParamerters.NumberofGeneratedPackets += 1; packt.PID = PublicParamerters.NumberofGeneratedPackets; counter.IncreasePacketsCounter(Reciver, PacketType.Data); // } } } } }
/// <summary> /// if source is an agent, we just send the packet directly to the sink and no need for clustring. /// </summary> /// <param name="source"></param> /// <param name="packt"></param> private void PreparDataTransmission(Sensor source, Packet packt) { // when the old agent is hightier node... if (packt.isRecovery) { // durig recovery, the agent should not be a source. // source should add a record. List <Sensor> NewAents = new List <Sensor>(); // new agent for the recovery. foreach (SinksAgentsRow inpacket in packt.SinksAgentsList) { if (inpacket.AgentNode.ID != source.ID) { bool isFound = Operations.FindInAlistbool(inpacket.AgentNode, NewAents); if (!isFound) { NewAents.Add(inpacket.AgentNode); } } } if (source.RecoveryRow == null) { Console.WriteLine("ResonseSinkPositionMessage. New Recovery Record is created at prevAgent ID" + source.ID + " Packet PID " + packt.PID + " Path: " + packt.Path); // keep recored for the recovery of upcoming packets. no need for request and respanse a gain. source.RecoveryRow = new RecoveryRow() { ObtiantedTime = PublicParamerters.SimulationTime, PrevAgent = source, RecoveryAgentList = packt.SinksAgentsList, ObtainPacketsRetry = 0 }; } else { Console.WriteLine("ResonseSinkPositionMessage: PrevAgent ID " + source.ID + " has recovery record Packet PID " + packt.PID + " Path:" + packt.Path); source.RecoveryRow.ObtiantedTime = PublicParamerters.SimulationTime; source.RecoveryRow.PrevAgent = source; source.RecoveryRow.RecoveryAgentList = packt.SinksAgentsList; source.RecoveryRow.ObtainPacketsRetry += 1; } // the new agent is found: if (NewAents.Count > 0) { List <Branch> Branches = FindBranches.GetBranches(NewAents, source.CenterLocation, PublicParamerters.MainWindow.Canvas_SensingFeild, Settings.Default.MaxClusteringThreshold); new MergedPathsMessages(source, Branches, packt); } else { if (packt.SinkIDsNeedsRecovery != null) { new RecoveryMessage(source, packt); // obtain } else { Console.WriteLine("ResonseSinkPositionMessage: ******************************* PrevAgent ID " + source.ID + " has recovery record Packet PID " + packt.PID + " Path:" + packt.Path); MessageBox.Show("xxx&&&&&&&&&&&ResonseSinkPositionMessage"); } } } else { // call the merged path: List <Sensor> NotMySinks = new List <Sensor>(); // source node (Reciver) is not an agent for these sinks. List <Sensor> MySinks = new List <Sensor>(); // the source node (Reciver) is an agent for these sinks foreach (SinksAgentsRow row in packt.SinksAgentsList) { if (row.AgentNode.ID == source.ID) // if the agent itslef has data. that is to say no need for clusetrign. { // no need to join merged path and clustring process. MySinks.Add(row.AgentNode); } else { // the sinks are not mine. SOURCE NODE is not an agent for these sinks. bool isFound = Operations.FindInAlistbool(row.AgentNode, NotMySinks); if (!isFound) { NotMySinks.Add(row.AgentNode); } } } // not my sinks: if (NotMySinks.Count > 0) { List <Branch> Branches = FindBranches.GetBranches(NotMySinks, source.CenterLocation, PublicParamerters.MainWindow.Canvas_SensingFeild, Settings.Default.MaxClusteringThreshold); new MergedPathsMessages(source, Branches, packt); } // my sinks: if (MySinks.Count > 0) { // the source is an agent in this case. new MergedPathsMessages(source, packt); } } // in recovery probelm }
/// <summary> /// preAgent send an obtian packet to ask for the new agent of the sink. /// </summary> /// <param name="preAgent"></param> /// <param name="SinksIDsRequiredRecovery"></param> public RecoveryMessage(Sensor preAgent, Packet packet) { // preAgent: if it has no record, then we obtian the sink position first. // if the preAgent did not sent an obtian message to the hightier node if (preAgent.RecoveryRow == null) { // obtian the recovery. obtian the sink location. Console.WriteLine("RecoveryMessage. >>>>Trying to recover the packet. Source ID=" + packet.Source.ID + " PID=" + packet.PID + " Path " + packet.Path); new ObtainSinkFreshPositionMessage(preAgent, packet.SinkIDsNeedsRecovery); } else { // it has record. no need to resend an obtian: but you should wait. if (!preAgent.RecoveryRow.IsExpired) { // not expired List <Sensor> NewAents = new List <Sensor>(); // new agent for the recovery. foreach (SinksAgentsRow newAgentRow in preAgent.RecoveryRow.RecoveryAgentList) { if (newAgentRow.AgentNode.ID != preAgent.ID) { bool isFound = Operations.FindInAlistbool(newAgentRow.AgentNode, NewAents); if (!isFound) { packet.Destination = newAgentRow.AgentNode; NewAents.Add(newAgentRow.AgentNode); } } } // if we found the new agent. if (NewAents.Count > 0) { Console.WriteLine("RecoveryMessage. Source ID=" + packet.Source.ID + " PID=" + packet.PID + " Path " + packet.Path); List <Branch> Branches = FindBranches.GetBranches(NewAents, preAgent.CenterLocation, PublicParamerters.MainWindow.Canvas_SensingFeild, Settings.Default.MaxClusteringThreshold); packet.SinksAgentsList = preAgent.RecoveryRow.RecoveryAgentList; new MergedPathsMessages(preAgent, Branches, packet); } else { if (preAgent.RecoveryRow.ObtainPacketsRetry >= 3) { // in case no new agent is found. Console.WriteLine("RecoveryMessage. No agent is found during the recovery.Source ID = " + packet.Source.ID + " PID = " + packet.PID + " Path " + packet.Path); NetworkOverheadCounter counter = new NetworkOverheadCounter(); // counter.DropPacket(packet, preAgent, PacketDropedReasons.RecoveryNoNewAgentFound); counter.SaveToQueue(preAgent, packet); } else { Console.WriteLine("RecoveryMessage. Recovery period is expired. Old Agent is sending new obtain packet!" + " Path " + packet.Path); new ObtainSinkFreshPositionMessage(preAgent, packet.SinkIDsNeedsRecovery); // obtain } } } else { // resent the obtian packet. 2 times. if (preAgent.RecoveryRow.ObtainPacketsRetry <= 3) { Console.WriteLine("RecoveryMessage. Recovery period is expired. Old Agent is sending new obtain packet!" + " Path " + packet.Path); new ObtainSinkFreshPositionMessage(preAgent, packet.SinkIDsNeedsRecovery); // obtain } else { Console.WriteLine("RecoveryMessage. Recovery period is expired. we tryied to re-sent the obtian packet for three times and faild. The packet will be droped." + " Path " + packet.Path); // drop the packet: NetworkOverheadCounter counter = new NetworkOverheadCounter(); counter.DropPacket(packet, preAgent, PacketDropedReasons.RecoveryPeriodExpired); preAgent.RecoveryRow = null; } } } }