title: Simulation of Pure Aloha Protocol
body: <p>I am trying to simulate the Pure Aloha protocol. After some research I understood how this protocol works. In a nutshell, assuming we have 5 nodes and frame size is fixed. All nodes  share a single channel. When a node has data to send it sends the data over the channel. If two ore more nodes try to send the frames at the same time (or within the same time frame) the packets collide and each node has to resend the packet again. Collided frames will have to be re-transmitted after a random time which will decrease the probability of colliding again.</p>
  
  <p>This is what I have so far. I have five nodes [A-E]. These are the five stations. Each node has its own list of integers. These are the time when to send the frame. I generate five random frames and I have this result:- E.g. A 5, A 7, B 7, C 8, E 9. These are generated randomly and mean that node a have two frames to send at time = 5 and time = 7. Frame B has one frame to send at time = 7. Since we have two frames that try to send a frames at the same time we have a collision.</p>
  
  <p>Using this code I have the below result...</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Timers;
  
  namespace Test_v1
  {
      public class Simulation
      {
          Utility utils = new Utility();
          static Random random = new Random();
          private Timer t1;
          int currentTime;
          int frameSize;               //frame size in bits.
          int channelCapacity;         //channel capacity in bits.
          int frameTime;               //time to transmit one frame on the channel in milliseconds.
          private List&lt;Node&gt; nodeList;
          Node A;
          Node B;
          Node C;
          Node D;
          Node E;
          int collisions = 0;
  
          public Simulation()
          {
              frameSize = 10;
              channelCapacity = 100;
              frameTime = (frameSize / channelCapacity) * 100;
              nodeList = new List&lt;Node&gt;();
              A = new Node(); A.stationName = "Station A";
              B = new Node(); B.stationName = "Station B";
              C = new Node(); C.stationName = "Station C";
              D = new Node(); D.stationName = "Station D";
              E = new Node(); E.stationName = "Station E";
              nodeList.Add(A);
              nodeList.Add(B);
              nodeList.Add(C);
              nodeList.Add(D);
              nodeList.Add(E);
  
              generateFrames(5);
  
              t1 = new Timer(100);
              t1.Elapsed += new ElapsedEventHandler(IntervalTimerElapsed);
              t1.Start();
  
  
          }
  
  
          protected void IntervalTimerElapsed(object sender, EventArgs e)
          {
              int framesOnChannel = 0;
  
              foreach (Node n in nodeList)
              {
                  for (int i = 0; i &lt; n.queue.Count; i++)
                  {
                      if (currentTime == n.queue[i])
                      {
                          Console.WriteLine(currentTime + " " + n.stationName + " " + n.queue[i]);                    
                          framesOnChannel = framesOnChannel + 1;
                          collisions = collisions + 1;
                          if(framesOnChannel &gt; 1)
                          n.queue[i] = BackOff(n) + currentTime;
                      }
                      else
                      {
                          Console.WriteLine(currentTime);
                      }
  
                  }
              }
              currentTime++;
              if (framesOnChannel &gt; 1)
              {
                  Console.WriteLine("Frames on Channel:" + framesOnChannel + " Collision!");
              }
              else
              {
                  Console.WriteLine("Frames on Channel:" + framesOnChannel);
              }
  
              framesOnChannel = 0;
  
              if (currentTime &gt; 10)
                  t1.Stop();
          }
  
          private void generateFrames(int load_G)
          {
              for (int i = 0; i &lt; load_G; i++)
              {
                  Node randomStation = getRandomNode();                
                  randomStation.queue.Add(utils.RandomNumber(0, 10));
              }
  
              foreach (Node n in nodeList)
              {
                 n.queue.Sort();       
              }
          }
  
          private int BackOff(Node node)
          {
              int k = node.attempts;
  
              if (k &gt; 10)
                  k = 10;
  
              int R = (int)Math.Round(Math.Pow(2, k) - 1);
              int backOffTime = random.Next(0, R) * frameTime;
  
              return backOffTime + (frameTime * 2);
          }
  
          private Node getRandomNode(){
          switch (random.Next(1, 6))
              {
                  case 1:
                      return A;
  
                  case 2:
                      return B;
  
                  case 3:
                      return C;
  
                  case 4:
                      return D;
  
                  default:
                      return E;
              }
          } 
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/8vteE.png" alt="Output so far">
  I have so far managed to detect when there is a collisions. 
  a)I need somehow to apply the Backoff method to those Nodes that collide and send them in a future time. I applied the backoff when a collision is detected but nothing happens. In this case all the frames should collide since for a frame to be succesfully trasnmitted there must be only one frame in time of two ticks.</p>
  
  <p>b) I need to count the succefull transmissions. A transmission is sucessfull if no other frame is send within 2 ticks. Example if a frame is send at time = 1. There must be no other frame within time = 1 to time = 3 to be sucesfull.</p>
  
  <p>Thanks in advance for the help. If something is not clear please tell me.</p>