void SendActivationPacket(double preDelay = 0) { ProtocolPacket pkt = new ProtocolPacket(); pkt.SourceDeviceID = ActivatingId; pkt.TimeToActivation = ActivateAt - CurrentTime - preDelay; if (pkt.TimeToActivation > 0 && (PacketCount < PacketThreshold || MasterMode)) { RadioTransmitPacket(pkt, preDelay); } PacketCount = 0; MyPacketSpacing += PacketSpacingIncrease; // Decide whether to send more packets. double timetoEnd = ActivateAt - CurrentTime; double thisPacketSpacing = MyPacketSpacing + ParentSimulation.NextRandom() * PacketRandomness; if (timetoEnd < thisPacketSpacing) { SetTimerCallback(timetoEnd, () => Activate()); } else { SetTimerCallback(thisPacketSpacing, () => SendActivationPacket()); } }
/// <summary> /// Called when a packet is received by this device. /// </summary> public void ReceivePacket(object packet) { ProtocolPacket pkt = (ProtocolPacket)packet; if (WaitingForActivation) { // Do nothing- already aware of the impending activation // Future: Deal with the possibility of two nodes being activated independently PacketCount++; } else { // Imperfect computation - try to improve. ActivateAt = CurrentTime + pkt.TimeToActivation; ActivatingId = pkt.SourceDeviceID; WaitingForActivation = true; double delay = ParentSimulation.NextRandom() * PacketSpacing; SetTimerCallback(delay, () => SendActivationPacket()); SetLedColor(Colors.Red); PacketCount = 0; MasterMode = false; MyPacketSpacing = PacketSpacing; RadioSetModeReceive(); } }