Friday, August 17, 2012

MAC implementation in Matlab

Logic in Matlab for MAC protocol is simple. Every node maintains its array for status of every activities such as packets sent, arriaval packet, collision status etc. Every time a random number is thrown to initiate the any activity and keep status of that activity. For details, ALOHA mac protocol has been implemented here as an example which is self-explanatory.

%SIMULATION PARAMETERS
%simulation for pure Aloha protocol
%total simulation time in seconds
runtime=0.2;
%total number of stations
nstation=10;
%transmission throughput of the media in bits per second
netthrou=10e6;
%frame size in bits
fsize=8000;
%avarage frame arrival rate per second for each station
%frate=10;
for frate=1:5:150
%average frame arrival rate per simulation iteration
trh=frate/10000;
%random wait window in number of simulation iterations
wwind=100;
%EVENTS VARIABLES
%transmit active
tr=zeros(1,nstation);
%transmit queue
tq=zeros(1,nstation);
%transmit progress counter
tcnt=zeros(1,nstation);
%collision keeper
colis=zeros(1,10000*runtime);
%collision station index
colin=zeros(1,nstation);
%random wait after collision
rwait=zeros(1,nstation);
%transmit keeper
trkeep=zeros(nstation,10000*runtime);
%packet arrival keeper
pakeep=0;
for i=1:10000*runtime
for j=1:nstation
    %check if the transmitter is active
    if tr(j)==1
        trkeep(j,i)=1;
    end
    %check if the packet has been sent
     if tcnt(j)>0
        tcnt(j)=tcnt(j)-1;
        if tcnt(j)==0
            tr(j)=0;
            %check if the transmission is collision free
            if colin(j)==1
                rwait(j)=ceil(wwind*rand(1,1));
                tq(j)=tq(j)+1;
                colin(j)=0;
            end
        end
    else
        if tq(j)>0 && rwait(j)==0
            tr(j)=1;
            tcnt(j)=ceil(fsize/netthrou*10000);
            tq(j)=tq(j)-1;
        end
    end
    %check if a new packet has arrived
    pa=rand(1,1);
    if pa        pakeep=pakeep+1;
        %if the transmit is ready
        if tr(j)==0 && rwait(j)==0
           tr(j)=1;
           tcnt(j)=ceil(fsize/netthrou*10000);
       else
           tq(j)=tq(j)+1;
       end
    end
    %decreaserandom waiting count
    if rwait(j)>0
        rwait(j)=rwait(j)-1;
    end
end
%check for collision
if sum(tr)>1
        colis(i)=1;
        for k=1:nstation
            if tr(k)==1
                colin(k)=1;
            end
        end
end
end
py1(frate)=(pakeep-sum(tq));
px1(frate)=pakeep;
end
g1=[0:0.01:1.2];
s1=g1.*exp(-2*g1);
figure(1)
plot(py1*8000/runtime,px1*8000/runtime,'x',g1*1e7,s1*1e7,'-')
grid
ylabel('Throughput (bps)')
xlabel('Arrival Rate (bps)')

Friday, January 14, 2011

NS2 for Wireless

from: http://www.winlab.rutgers.edu/~zhibinwu/html/network_simulator_2.html

Introduction


Ns-2 is a widely used tool to simulate the behavior of wired and wireless networks. Useful general information can be found at


Official ns-2 website (http://www.isi.edu/nsnam/ns/ )
An ns-2 Documentation generated by Doxygen is in http://www.grid.unina.it/~vollero/resources/doc_ns2.27/html/index.html
Two good tutorials about ns-2.
Wireless tutorial by Marc Greis
ns by example
My presentation: ns2 tutorial for wireless simulation WINLAB, June 2003
Another good tutorial of ns-2


Installation & Debug


Install ns-2.27 in Red Hat Linux
Install ns 2.28 in Debian with gcc 4.0 or above
Debug ns-2 with gdb


Simulation Configuration



  1. Basic simulation scripts of wireless scenarios.
  2. Traffic, Topology and Mobility generation in ns-2
  3. Analyze CMU traces (for wireless simulation) and awk code.
  4. Header and Address Handling in ns-2
  5. Understanding OTcl syntax in ns-2

Physical Layer


  1. Demystify TwoRayGround Propagation Model
  2. Introduce Ricean Fading to produce probabilistic Link Error (weiwei: Good!!!)
  3. Why list-based improvements in channel.cc of ns-2.27 cause problems?

MAC layer



  1. Interfacing MAC
  2. Understand IEEE 802.11 MAC Standard
  3. Analysis of the 802.11 MAC code
  4. Simulation example: Measuring the effects of RTS/CTS on 802.11 link

  5. Practice: Implement Multi-Rate MAC

  6. Discussion of bugs and issues of IEEE802.11 MAC implementation

Routing



  1. Analysis of the DSR sourcecode in ns-2.
  2. Analysis of the DSDV in network simulator-2.
  3. How to produce a new Routing Agent with Fixed Routing Table.
  4. Calculate Routing Performance Metric from trace file

Thursday, August 19, 2010

Ns2 Building Experiences

NS-2 is useful for a variety of researches related to networking. However, there might be difficult to install all the necessary packages of ns-2 in any platform. it may be troublesome, especially with specific combination of the ns2 versions and OS versions. the more problems lie on the issues of gcc and NS2 versions. Here, I share my personal experiences of building NS2 over Linux systems, namely Fedora, Ubuntu.

For the NS2 case, the most relevant is the Gcc compiler. Installing the latest version
of ns-2 (ns-2.33) with gcc 4.1.2 is not difficult at all because the source code has mapped to the current gcc configuration. Even though there is a small bug in ./configure of tk, tcl, otcl, less attempt has been done compare to the older version. You can't compile ns-2.27 (and 2.26 too) with gcc3.4.2, even with new versions of linux distributions. There is Error of "tclcl make failed".
The old version such as ns-2.26 will not work with current gcc 4.1.2. So we’ve to install the gcc-2.95.

HOW-TO:

1. Download the all-in-one package of ns-2 and unzip it:
OR by command line, do following
cd to home directory and type:
wget http://www.isi.edu/nsnam/dist/ns-allinone-2.26.tar.gz;
tar -xzvf ns-allinone-2.26.tar.gz;
2. install the following Debian packages:
  • apt-get install gcc-2.95
  • apt-get install g++-2.95
  • apt-get install libstdc++2.10-glibc2.2
  • apt-get install libx11-dev
  • apt-get install libxt-dev
3. In my shell (tcsh), there were several changes needed in the configure files to compensate for stricter parsing. All lines of the form:
system=MP-RAS-`awk ‘{print }’ /etc/.relid’`

need changed to:

system=MP-RAS-`awk ‘{print }’ /etc/.relid`

That is, the second to last character (') needs to be deleted. In ns-allinone-2.26, this occurs in four places (files and line numbers):

./tcl8.3.2/unix/configure:4701
./tcl8.3.2/unix/configure:5705
./tk8.3.2/unix/configure:1520
./otcl-1.0a8/configure:4167

4. Set your compiler environment variables in the terminal in which you will run the install script such that they point to the 2.95 version of the tools instead of the default ones. In the C shell:
setenv CC /usr/bin/gcc-2.95;
setenv CPP /usr/bin/cpp-2.95;
setenv CXX /usr/bin/g++-2.95;

Use the paths appropriate for your system. If you use the bash shell, the general form for setting an environment variable is:

export CC=/usr/bin/gcc-2.95;

Now, the configure scripts will use these versions of the compiler tools.

5. When compiling the TCL/C++ interface (e.g., tclcl-1.0b13), I received a warning that stopped the compilation that line 193 of /usr/include/sys/types.h does not declare anything. Though this is not generally recommended, I used the -fpermissive option for the compiler to continue compilation.

In the file /usr/local/dload/ns-allinone-2.26/tclcl-1.0b13/Makefile.in, I changed the CCOPT parameter on line 56 to be:

CCOPT = -fpermissive @V_CCOPT@

6. At this point, I just had to run the install script an everything compiled (with lots of warnings) except for nam which I didn’t need since I don’t use the visualization tools in ns-2.
/usr/local/dload/ns-allinone-2.26/install

(Make sure you run this in the terminal in which the CC, CPP, and CXX variables are set appropriately)

7. Now, to get your existing ns-2 code to compile (i.e., the code in /usr/local/ns-2.26), you need to reconfigure your Makefile by specifying the new paths:

cd /usr/local/ns-2.26; ./configure –with-tcl=/usr/local/dload/ns-allinone-2.26/tcl8.3.2 –with-tk=/usr/local/dload/ns-allinone-2.26/tk8.3.2 –with-otcl=/usr/local/dload/ns-allinone-2.26/otcl-1.0a8 –with-tclcl=/usr/local/dload/ns-allinone-2.26/tclcl-1.0b13

Note that this will overwrite your existing Makefile, so if there was anything specific that you needed in it, you should move those commands to the Makefile.in file in /usr/local/ns-2.26.

....Now successful to install old version...


Thursday, November 27, 2008

Wireless Sensor Network

Interests:
  1. Self-organization schemes to incorporate the flaxibility of wireless sensor network's architecture.