how can I dynamically simulate an ad hoc network?

I have to simulate a network with n nodes, each of which periodically wakes up and sends a HELLO message in which it indicates whether packets to be sent to other nodes in the network. For example, node 2 wakes up at t = 1 and has a packet for node 6. In the positive case, the node remains active for another s slot (time intervals), otherwise goes off. In the example, node 2 remains active until t = 5. When the node is active waits for the recipient to send a HELLO message: in the positive case sends a DONT SLEEP packet to the recipient in the next slot and then a DATA packet, otherwise turns off. Considering the example then, node 2 remains active waiting for node 6 is awake (it is expected that node 6 sends a HELLO). When t = 5 node 6 sends a HELLO and node 2 is still active: node 2 should send to node 6 a DONT SLEEP packet and then a DATA packet. Here’s the code (the first part is missing).
for t=0:1:tmax
hold off;
figure(1);
%recipient
dst = floor(n*rand(1));
for i=1:1:n
plot(NODE(i).xd,NODE(i).yd, 'o');
hold on;
%node wakes up periodically
if(mod(t,T) == i-1 && T >= n)
%struct NODE updates the status vs. time
NODE(i).state(1,t+1) = 'T';
%sender sends a HELLO
PKT(t+1).type = 'H';
plot(NODE(i).xd,NODE(i).yd, 'k*');
PKT(t+1).src = i;
PKT(t+1).dst = 0; %0 broadcast message
%checks whether there are packets to queue
if(dst ~= 0 && dst ~= i)
%opt field indicates the recipient of HELLO
PKT(t+1).opt = dst;
%update the queue of packets to send
%NODE(i).send is a column vector; when the node i
%has a packet to send increments by 1
%the cell relative to the recipient
NODE(i).send(dst,1) = NODE(i).send(dst,1) + 1;
else
PKT(t+1).opt = 0;
end
% checks whether there are packets to be sent
for j=1:1:n
if(NODE(i).send(j,1) ~= 0)
for k=1:1:s
% book s successive slots and remains active
%for the duration
NODE(i).state(1,t+1+k) = 'A';
end
end
end
else
%node is not activate periodically; check the status
if(NODE(i).state(1,t+1) == 'A')
for j=1:1:n
% the active node receives a HELLO from one
%of the nodes to which it must send data
if(PKT(t+1).type == 'H' && PKT(t+1).src == NODE(i).send(j,1))
%transmit status
NODE(i).state(1,t+2) = 'T';
%receive status
NODE(j).state(1,t+2) = 'R';
%DON'T SLEEP
PKT(t+2).type = 'S';
plot(NODE(i).xd,NODE(i).yd, 'r*');
PKT(t+2).src = i;
PKT(t+2).dst = j;
%opt field in DONT SLEEP packet indicates
%the number of packets destined for that node
PKT(t+2).opt = NODE(i).send(j,1);
end
end
elseif(NODE(i).state(1,t+1) == 'T')
for j=1:1:n
%the active node receives a DONT SLEEP from
%one of the nodes to which it must send data
if(PKT(t+1).type == 'S' && PKT(t+1).src == NODE(i).send(j,1))
NODE(i).state(1,t+2) = 'T';
NODE(j).state(1,t+2) = 'R';
%DATA packet
PKT(t+2).type = 'D';
pkt(t+2).src = i;
PKT(t+2).dst = j;
% decrease the packet queue
NODE(i).send(j,1) = NODE(i).send(j,1) - 1;
end
end
end
end
end
end
it doen't work and I think that the problem is this line of code "if(PKT(t+1).type == 'H' && PKT(t+1).src == NODE(i).send(j,1))". Because PKT(6) does not exist until i = 6. Can anyone help me?

5 Comments

can any one use this code and find error or whats is inputs?mean t,T ,Node,PKT???
t is current simulation time
T appears to be the interval to periodically wake up
Node is the data structure representing the state of individual network nodes.
PKT is a data structure representing packets of data being transmitted and received.
Thanks dear, do you have complete code with same inputs ,can you send me complete code with dummy inputs.
I do not have that code.

Sign in to comment.

Answers (1)

please can you help me. how to broadcast a packet (code source) with matlab.

Products

Asked:

on 16 Jan 2013

Commented:

on 22 Nov 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!