//===================================================================== //[MethodImpl(MethodImplOptions.Synchronized)] /// <summary> /// Function for parse received packet /// </summary> /// <param name="pack">SimulatorPacket</param> public override void ParseReceivedPacket(SimulatorPacket pack) { Type pt = pack.GetType(); if (pt == typeof(Connect)) { ConnectRoutine(pack.Source); } else if (pt == typeof(Disconnect)) { DisonnectRoutine(pack.Source); } else if (pt == typeof(KeepAlive)) { var newThread = new Thread(() => UpdateSTAKeepAliveInfoOnReceive(pack.Source)); newThread.Start(); } else if (pt == typeof(Data)) { DataRoutine((Data)pack); } else if (pt == typeof(DataAck)) { DataAckRoutine((DataAck)pack); } else { OtherPacketsRoutine(pack); } }
//********************************************************************* private void ThreadableSendData(Key _Pk, object _ref) { ArrayList _temp = (ArrayList)_packets[_Pk]; SimulatorPacket p = (SimulatorPacket)_ref; int Rate = p.getTransmitRate(); int sleep = GetObjectSize(p) / Rate; //Thread.Sleep(sleep); Thread.Sleep(new TimeSpan(sleep * 500)); //AddToLog("Sleep for :" + sleep); if (_temp != null) { lock (_packets) { // if (_temp != null) // { if (_temp.Contains(_ref)) { _temp.Remove((SimulatorPacket)_ref); } if (_temp.Count > 0) { _packets[_Pk] = _temp; } else { _packets.Remove(_Pk); } // } } } }
//********************************************************************* public void SendData(SimulatorPacket pack) { //Random ran = new Random((int)DateTime.Now.Ticks); //while (RF_STATUS != "NONE") //{ //Thread.Sleep(ran.Next(3,10)); //} SpinWait.SpinUntil(RF_Ready); RF_STATUS = "TX"; while (!_MEDIUM.Registration(this.getOperateBand(), this.getOperateChannel(), this.x, this.y)) { RF_STATUS = "NONE"; //Thread.Sleep(ran.Next(1, 2)); Thread.Sleep(new TimeSpan(20)); Thread.Sleep(1); SpinWait.SpinUntil(RF_Ready); //while (RF_STATUS != "NONE") // Thread.Sleep(ran.Next(1, 3)); RF_STATUS = "TX"; } _MEDIUM.SendData(pack); //Thread.Sleep(ran.Next(1, 2)); //Thread.Sleep(1); //Thread.Sleep(new TimeSpan(100)); RF_STATUS = "NONE"; }
//********************************************************************* public void SendData(SimulatorPacket pack) { CheckScanConditionOnSend(); int Rate = pack.getTransmitRate(); int sleep = (int)(600 / Rate); lock (RfSync) { try{ RF_STATUS = RFStatus.Tx; short OperateChannel = this.getOperateChannel(); int try_counter = 0; while (!Medium.Registration(this.Freq, OperateChannel, this.x, this.y, sleep)) { Thread.Sleep(new TimeSpan(randomWait.Next(20, 50))); if (try_counter++ > 200) { return; } } this.MACLastTrnsmitRate = pack.getTransmitRate(); Medium.SendData(pack); } catch (Exception ex) { AddToLog("SendData:[" + this.GetType() + "] " + ex.Message); MessageBox.Show("SendData :" + ex.Message); } RF_STATUS = RFStatus.None; } if (pack.GetType() == typeof(Data)) { _DataSent++; } }
//********************************************************************* public static void SendData(SimulatorPacket pack) { Key _Pk = new Key(pack.PacketFrequency, pack.PacketChannel, pack.Destination); try { Type packet_type = null; if (pack != null) { packet_type = pack.GetType(); if (packet_type == typeof(Connect)) { _ConnectCounter++; if (_ConnectCounter == 36000) { _ConnectCounter = 0; } } else if (packet_type == typeof(ConnectionACK)) { _ConnectAckCounter++; if (_ConnectAckCounter == 36000) { _ConnectAckCounter = 0; } } ArrayList LocalPackets = null; if (_packets.ContainsKey(_Pk)) { LocalPackets = (ArrayList)_packets[_Pk]; if (LocalPackets == null) { LocalPackets = new ArrayList(); } LocalPackets.Add(pack); _packets[_Pk] = LocalPackets; } else { LocalPackets = new ArrayList(); LocalPackets.Add(pack); _packets.Add(_Pk, LocalPackets); } // Thread newThread = new Thread(() => ThreadableSendData(_Pk, pack)); // newThread.Name = "ThreadableSendData "; //newThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback((s) => ThreadableSendData(_Pk, pack))); } } catch (Exception ex) { if (DebugLogEnabled) { AddToLog("[SendData] Exception:" + ex.Message); } } }
//===================================================================== /// <summary> /// This function work with all others packets which don't have special Routines /// </summary> /// <param name="pack">Simulator Packet </param> private void OtherPacketsRoutine(SimulatorPacket pack) { // Generic Packet retransmitter // This code will create new packet by him type var instance = (SimulatorPacket)Activator.CreateInstance(pack.GetType(), pack); instance.X = this.x; instance.Y = this.y; instance.Destination = pack.Reciver; SendData(instance); }
//********************************************************************* // [MethodImpl(MethodImplOptions.Synchronized)] private static void ThreadableSendData(Key _Pk, object _ref) { EventArgs e = new EventArgs(); int sleep = 0; try { WeHavePacketsToSend(_ref, e); SimulatorPacket p = (SimulatorPacket)_ref; int Rate = p.getTransmitRate(); //long ps = GetObjectSize(p); sleep = (int)(600 / Rate); } catch (Exception ex) { MessageBox.Show("Medium 1 ThreadableSendData" + ex.Message); } Thread.Sleep(new TimeSpan(sleep * _MediumSendDataRatio * 100)); //AddToLog("Sleep for :" + sleep); _ev.WaitOne(); ArrayList _temp = (ArrayList)_packets[_Pk]; try { if (_temp != null) { if (_temp.Contains(_ref)) { _temp.Remove((SimulatorPacket)_ref); } //if (_temp.Count > 0) // ;// _packets[_Pk] = _temp; if (_temp.Count == 0) { _packets.Remove(_Pk); } } } catch (Exception ex) { MessageBox.Show("Medium ThreadableSendData" + ex.Message); } finally { _ev.Set(); //if(LockWasToken) Monitor.Exit(_packets); } }
//********************************************************************* public void SendData(SimulatorPacket pack) { Key _Pk = new Key(pack.PacketBand, pack.PacketChannel, pack.Destination); try { if (pack != null) { if (pack.GetType() == typeof(Connect)) { _ConnectCounter++; if (_ConnectCounter == 36000) { _ConnectCounter = 0; } } else if (pack.GetType() == typeof(ConnectionACK)) { _ConnectAckCounter++; if (_ConnectAckCounter == 36000) { _ConnectAckCounter = 0; } } ArrayList LocalPackets = null; if (_packets.ContainsKey(_Pk)) { LocalPackets = (ArrayList)_packets[_Pk]; if (LocalPackets == null) { LocalPackets = new ArrayList(); } LocalPackets.Add(pack); _packets[_Pk] = LocalPackets; } else { LocalPackets = new ArrayList(); LocalPackets.Add(pack); _packets.Add(_Pk, LocalPackets); } } Thread newThread = new Thread(() => ThreadableSendData(_Pk, pack)); newThread.Start(); } catch (Exception ex) { AddToLog("[SendData] Exception:" + ex.Message); } }
public SimulatorPacket CreatePacket() { SimulatorPacket pack = new SimulatorPacket(getOperateChannel(), getOperateBand()); pack.Source = getMACAddress(); if (this.GetType() == typeof(AP)) { AP _ap = (AP)this; pack.SSID = _ap.SSID; } pack.X = this.x; pack.Y = this.y; return(pack); }
public SimulatorPacket CreatePacket(bool inTDLS) { SimulatorPacket pack = new SimulatorPacket(this.getOperateChannel()) { SSID = this.SSID, Source = getMACAddress(), X = this.x, Y = this.y, PacketFrequency = this.Freq, PacketStandart = this.Stand80211, PacketBandWith = this.BandWidth, }; return(pack); }
//********************************************************************* /* public void SendData(SimulatorPacket PacketToSend) * { * //Random ran = new Random((int)DateTime.Now.Ticks); * SpinWait.SpinUntil(RF_Ready); * SpinWait.SpinUntil(tryToRegister); * lock (RF_STATUS) * { * * // Now scanning process running * if (_scanning) * { * SpinWait.SpinUntil(() => { return (bool)!_scanning; }); * } * RF_STATUS = "TX"; * _MEDIUM.SendData(PacketToSend); * RF_STATUS = "NONE"; * } * * if (PacketToSend.GetType() == typeof(Data)) * { * _DataSent++; * } * }*/ //********************************************************************* public void SendData(SimulatorPacket PacketToSend) { //Random ran = new Random((int)DateTime.Now.Ticks); SpinWait.SpinUntil(RF_Ready); // while(RF_STATUS != "NONE") // Thread.Sleep(ran.Next(1, 3)); RF_STATUS = "TX"; int trys = 0; while (!_MEDIUM.Registration(this.getOperateBand(), this.getOperateChannel(), this.x, this.y)) { RF_STATUS = "NONE"; Thread.Sleep(new TimeSpan(20)); //Thread.Sleep(ran.Next(1, 3)); if (trys > 10) { Thread.Sleep(2); trys = 0; } trys++; SpinWait.SpinUntil(RF_Ready); //while (RF_STATUS != "NONE") // Thread.Sleep(ran.Next(1, 3)); RF_STATUS = "TX"; } // Now scanning process running if (_scanning) { SpinWait.SpinUntil(() => { return((bool)!_scanning); }); } _MEDIUM.SendData(PacketToSend); //Thread.Sleep(1); RF_STATUS = "NONE"; Thread.Sleep(2); if (PacketToSend.GetType() == typeof(Data)) { _DataSent++; } }
//********************************************************************* public IPacket ReceiveData(RFDevice device) { Key Pk = null; String errPrefix = ""; try { if (_packets != null) { lock (_packets) { errPrefix = " Packets "; Pk = new Key(device.getOperateBand(), device.getOperateChannel(), device.getMACAddress()); if (_packets.ContainsKey(Pk)) { ArrayList LocalPackets = (ArrayList)_packets[Pk]; foreach (object pack in LocalPackets) { if (pack != null) { SimulatorPacket _LocalPack = (SimulatorPacket)pack; if (_LocalPack.Source != device.getMACAddress() && getDistance(device.x, device.y, _LocalPack.X, _LocalPack.Y) < _Radius + _Radius) { //LocalPackets.Remove(pack); return(_LocalPack); } } // loop body } } } Pk = new Key(device.getOperateBand(), device.getOperateChannel(), "FF:FF:FF:FF:FF:FF"); if (_packets.ContainsKey(Pk)) { errPrefix = " Beacons "; ArrayList LocalPackets = (ArrayList)_packets[Pk]; foreach (object pack in LocalPackets) { if (pack != null) { SimulatorPacket _LocalPack = (SimulatorPacket)pack; if (_LocalPack.Source != device.getMACAddress() && getDistance(device.x, device.y, _LocalPack.X, _LocalPack.Y) < _Radius + _Radius) { return(_LocalPack); } } // loop body } } else { //AddToLog("Packet not found"); } } } catch (Exception ex) { AddToLog("[ReceiveData][" + errPrefix + "]:" + ex.Message); } return(null); }
public void SendData(SimulatorPacket PacketToSend) { throw new NotImplementedException(); }
//********************************************************************* public static SimulatorPacket ReceiveData(RFDevice device) { Key Pk = null; SimulatorPacket retvalue = null; if (_packets == null) { return(null); } try { _ev.WaitOne(); // Private packets Pk = new Key(device.Freq, device.getOperateChannel(), device.getMACAddress()); if (_packets.ContainsKey(Pk)) { ArrayList LocalPackets = (ArrayList)_packets[Pk]; foreach (object pack in LocalPackets) { if (pack != null) { SimulatorPacket _LocalPack = (SimulatorPacket)pack; if (_LocalPack.Source != device.getMACAddress() && getDistance(device.x, device.y, _LocalPack.X, _LocalPack.Y) < ReceiveDistance) { retvalue = _LocalPack; LocalPackets.Remove(pack); break; } } } } // Broadcast packets if (device.getListenBeacon() && retvalue == null) { Pk = new Key(device.Freq, device.getOperateChannel(), _BROADCAST); if (_packets.ContainsKey(Pk)) { ArrayList LocalPackets = (ArrayList)_packets[Pk]; foreach (object pack in LocalPackets) { if (pack != null) { SimulatorPacket _LocalPack = (SimulatorPacket)pack; if (_LocalPack.Source != device.getMACAddress() && getDistance(device.x, device.y, _LocalPack.X, _LocalPack.Y) < ReceiveDistance) { retvalue = _LocalPack; break; } } } } } } catch (Exception ex) { if (DebugLogEnabled) { AddToLog("[ReceiveData] " + ex.Message); } } finally{ _ev.Set(); } return(retvalue); }
//===================================================================== /// <summary> /// Function for Listen And Receive Packets /// </summary> /// <param name="sender">Value for know info about received packets</param> /// <param name="args">Not used - needed by event</param> public void Listen(object sender, EventArgs args) { //while (_Enabled) //{ //SpinWait.SpinUntil(checkIfHaveDataReceive); //SpinWait.SpinUntil(RF_Ready); if (((SimulatorPacket)sender).PacketChannel != this.getOperateChannel()) { return; } if (((SimulatorPacket)sender).PacketFrequency != this.Freq) { return; } double dist = GetSTADist(((SimulatorPacket)sender).X, ((SimulatorPacket)sender).Y, this.x, this.y); if (dist > Medium.ReceiveDistance) { return; } string desinationOfReceivedPacket = ((SimulatorPacket)sender).Destination; if ((this.GetType() == typeof(STA) && desinationOfReceivedPacket.Equals("FF:FF:FF:FF:FF:FF")) || desinationOfReceivedPacket.Equals(this.getMACAddress())) { double t = GetNoiseRSSI(((SimulatorPacket)sender).Source); if (sender.GetType() == typeof(Data)) { if (!MissPacket(t, ((SimulatorPacket)sender).getTransmitRate())) { _badPackets++; return; } } //SpinWait.SpinUntil(ListenCondition);//,1); prev_guid = new Guid(); SimulatorPacket pack = null; lock (RfSync) { RF_STATUS = RFStatus.Rx; pack = Medium.ReceiveData(this); RF_STATUS = RFStatus.None; } if (pack == null) { } else //if (pack != null )//&& (prev_guid != pack.GuidD || pack.IsRetransmit)) { // Only if we have received packet before // but flag Rentransmit is UP if (prev_guid == pack.GuidD) { pack.IsReceivedRetransmit = true; } if (pack.GetType() != typeof(Beacon)) { prev_guid = pack.GuidD; } AllReceivedPackets += 1; //Thread newThread = new Thread(() => ParseReceivedPacket(pack),1); //newThread.Name = "ParseReceivedPacket of " + this.getMACAddress(); //newThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback((s) => ParseReceivedPacket(pack))); } } }
//===================================================================== public virtual void ParseReceivedPacket(SimulatorPacket pack) { }