المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Wireless Sensor Node Simulation Time in C#



C# Programming
03-08-2013, 10:42 AM
I am trying to develop a very simple wireless sensor node simulator using C#. I have figured out how to "divide" the simulation time into different "states" of the sensor node but I am currently having some problems on how to properly implement a global simulation time for all the sensor nodes.

Here's what I did so far but I am sure there is a much better way to do this.

I made a NODE class which contains a Run() method that has a for loop executed to simulate time. But the obvious problem is, if I duplicate this NODE class to make other nodes (5-10 other nodes), there's a tendency that they will not be synchronized (especially when they communicate with each other).

public void Run() { //simulation loop for (int i = 0; i < 1000; i++ ) //this acts as the basis for the simulation time { if (sleep) { if (csleep < timeToSleep) { Sleep(i); csleep++; } else { sleep = false; csleep = 0; wakeup = true; } } if (wakeup) { if (cwakeup < timeToWakeUp) { WakeUp(i); cwakeup++; } else { wakeup = false; cwakeup = 0; listen = true; } } if (listen) { ...same as above code... } if (send) { ...same as above code... } if (receive) { ...same as above code... } }//end main simulation loop Console.WriteLine("END SIMULATION"); }
So, when I execute the nodes in the main simulation class, it looks like this:

class Program { static void Main(string[] args) { Node node1 = new Node(); Node node2 = new Node(); node1.Run(); node2.Run(); } }
The above codes are obviously incorrect since the simulation time is placed inside each node object. What I am trying to do is this:

I want to treat all nodes as if they are threads communicating with each other or could sometimes just independently "exist" in the simulation. But how do I make it that all of these nodes could have one single basis for the simulation time?

Simulation loop { Node 1 executed; //nodes execute sending, receiving, sleeping, etc Node 2 executed; //nodes execute sending, receiving, sleeping, etc }