📄 action.m
字号:
if cdebug, disp(['node ' num2str(j) ' will send an RREP with route ' num2str(event.net.route) ' at time ' num2str(t)]); end
end
return;
end
% I am not the destination of this RREQ: just re-broadcast it
% maybe one of my previous RREPs contains the route to the
% requesting source node, but do not worry for now
if event.pkt.ttl < 0
% already checked above, no use
% cannot go further: drop it
else
if event.net.id > bcast_table(j, event.net.src)
% forward this RREQ only if I have not forwarded the
% same broadcast RREQ from the same source before
rreq_forward(j) = rreq_forward(j) + 1;
if strcmp(event.app.type, 'crosslayer_searching')
rreq_forward_crosslayer(j) = rreq_forward_crosslayer(j) + 1;
end
bcast_table(j, event.net.src) = event.net.id;
newevent = event;
newevent.instant = t + rand*slot_time; % question: random delay before rebroadcasting
newevent.type = 'send_mac';
newevent.node = j;
newevent.pkt.tx=j;
newevent.pkt.rv=0;
NewEvents = [NewEvents; newevent]; clear newevent;
end
end
case 'rrep'
% if cdebug, disp(['time ' num2str(t) ' node ' num2str(j) ' receives a RREP with route: ' num2str(event.net.route)]); end
rrep_in(j) = rrep_in(j) + 1;
if strcmp(event.app.type, 'crosslayer_searching')
rrep_in_crosslayer(j) = rrep_in_crosslayer(j) + 1;
end
if isempty(event.net.route)
warning(['recv_net: node ' num2str(j) ' is receiving a RREP without any route entry']);
return;
end
temp = find(event.net.route == j);
if length(temp) > 1
warning(['recv_net: node ' num2str(j) ' appears more than once in a RREP']);
return;
end
if length(temp) <= 0
warning(['recv_net: node ' num2str(j) ' does not appear in a RREP it receives']);
return;
end
if temp == 1
% I am the requesting node so this RREP is what I am waiting for
if cdebug, disp(['time ' num2str(t) ' node ' num2str(j) ' receives a RREP with route: ' num2str(event.net.route)]); end
temp2 = find(net_pending(j).id == event.net.id);
if isempty(temp2)
% no RREQ waiting for this RREP;
% probably this is an RREP for an earlier timeout RREQ, but I have already received an RREP for the latest RREQ.
if ddebug, disp(['recv_net: node ' num2str(j) ' receives an RREP without a corresponding pending RREQ']); end
return;
end
if length(temp2) > 1
error(['recv_net: node ' num2str(j) ' receives an RREP with more than one pending RREQ']);
end
% Removes the pending RREQ
net_pending(j).id(temp2) = [];
net_pending(j).retransmit(temp2) = [];
if strcmp(event.app.type, 'crosslayer_searching')
% cross-layer searching application, no data to transmit
% send the packet up to the application layer
rrep_destination_crosslayer(j) = rrep_destination_crosslayer(j) + 1;
newevent = event;
newevent.instant = t;
newevent.node = j;
newevent.type = 'recv_app';
NewEvents = [NewEvents; newevent]; clear newevent;
else % a regular RREP at network layer received
% send the following data packet by this route
newevent = event;
newevent.instant = t;
newevent.type = 'send_mac';
newevent.node = j;
newevent.net.type = 'data';
newevent.net.id = new_id(j);
newevent.net.src = j;
newevent.net.dst = i;
% keep net.size, net.route
newevent.pkt.tx = j;
newevent.pkt.rv = newevent.net.route(2); % next hop
newevent.pkt.type='data';
newevent.pkt.size=newevent.net.size;
newevent.pkt.rate=default_rate;
newevent.pkt.ttl = length(newevent.net.route) + 1;
newevent.pkt.power=default_power;
newevent.pkt.id=0; % will be updated in 'send_phy'
newevent.pkt.nav=0; % will be updated in lower layer
NewEvents = [NewEvents; newevent];
clear newevent;
end
% no ACK at network layer
% the net_queue is always empty, so no next network layer packet to send
else
% I need to forward this RREP back to the next hop towards the source
rrep_forward(j) = rrep_forward(j) + 1;
if strcmp(event.app.type, 'crosslayer_searching')
rrep_forward_crosslayer(j) = rrep_forward_crosslayer(j) + 1;
end
newevent = event;
newevent.instant = t;
newevent.type = 'send_mac';
newevent.node = j;
newevent.net.type = 'rrep';
newevent.pkt.tx = j;
newevent.pkt.rv = newevent.net.route(temp - 1); % next hop
% if cdebug, disp(['time ' num2str(t) ' node ' num2str(j) ' will forward RREP to node ' num2str(newevent.pkt.rv)]); end
NewEvents = [NewEvents; newevent]; clear newevent;
end
case 'data'
if event.net.dst == 0
% a network layer broadcast packet
if event.pkt.rv ~= 0
warning(['recv_net: node ' num2str(j) ' receives a broadcast at NET, but not at MAC']);
end
if event.net.id > bcast_table(j, event.net.src)
bcast_table(j, event.net.src) = event.net.id;
newevent = event;
newevent.instant = t + rand*slot_time;
newevent.type = 'send_mac';
newevent.pkt.tx = j;
NewEvents = [NewEvents; newevent]; clear newevent;
end
return;
end
% receives a unicast data packet at network layer
if isempty(event.net.route)
warning(['recv_net: node ' num2str(j) ' is receiving a Net_DATA without any route entry']);
return;
end
temp = find(event.net.route == j);
if length(temp) > 1
warning(['recv_net: node ' num2str(j) ' appears more than once in a route for data packet']);
return;
end
if length(temp) <= 0
warning(['recv_net: node ' num2str(j) ' does not appear in a data packet it receives']);
return;
end
if j == event.net.dst % or temp == length(event.net.route)
% I am the destination
newevent = event;
newevent.instant = t;
newevent.type = 'recv_app';
newevent.node = j;
NewEvents = [NewEvents; newevent]; clear newevent;
else
% I should forward this data packet to the next hop towards the destination
newevent = event;
newevent.instant = t;
newevent.type = 'send_mac';
newevent.pkt.tx = j;
newevent.pkt.rv = newevent.net.route(temp + 1); % next hop
NewEvents = [NewEvents; newevent]; clear newevent;
end
otherwise
disp(['recv_net: Undefined network layer packet type: ' event.net.type]);
end
case 'send_app'
t = event.instant;
i = event.node;
switch event.app.type
case 'crosslayer_searching'
if ddebug, disp(['send_app: time ' num2str(t) ' node ' num2str(i) ' sends a crosslayer searching request for key(node) ' num2str(event.app.key)]); end
fid = fopen(log_file, 'a');
if fid == -1, error(['Cannot open file', log_file]); end
% Record traffic_id, topo_id, start_time, start_hop_count(=0), requesting node, requesting key
fprintf(fid, '%d %d %g %d %d %d \n', [event.app.id1; event.app.id2; t; 0; i; event.app.key]);
fclose(fid);
newevent = event;
newevent.type = 'send_net';
newevent.net.src = i;
newevent.net.dst = newevent.app.key;
newevent.net.size = 100*8;
NewEvents = [NewEvents; newevent]; clear newevent;
case 'dht_searching'
newevent = event;
newevent.type = 'send_net';
newevent.net.src = i;
newevent.net.size = 100*8;
if isempty(newevent.app.route)
% Initiate the overlay searching
newevent.app.route = [i];
tempn = floor(rand*log2(n));
if tempn > 0
for tempi = 1:tempn
while 1
tempx = ceil(rand*n); % 1 to n
if isempty(find([newevent.app.route newevent.app.key]==tempx)), break; end
end
newevent.app.route = [newevent.app.route tempx];
end
end
newevent.app.route = [newevent.app.route newevent.app.key];
fid = fopen(log_file, 'a');
if fid == -1, error(['Cannot open file', log_file]); end
% Record traffic_id, topo_id, start_time, start_hop_count(=0), requesting node, requesting key,overlay route length
fprintf(fid, '%d %d %g %d %d %d %d \n', [newevent.app.id1; newevent.app.id2; t; 0; i; newevent.app.key; tempn + 1]);
fclose(fid);
newevent.net.dst = newevent.app.route(2);
if ddebug, disp(['send_app: at time ' num2str(t) ' node ' num2str(i) ' sends a DHT searching request for key(node) ' num2str(newevent.app.key) ' through overlay route: ' num2str(newevent.app.route)]); end
else
disp(['send_app: at time ' num2str(t) ' node ' num2str(i) ' should not have a non-empty DHT overlay route ' num2str(event.app.route)]);
% forward the request to the next hop in the overlay
% tempi = find(newevent.app.route==i);
% if isempty(tempi) | length(tempi)>1 | tempi >= length(newevent.app.route)
% error(['send_app: dht_searching: node ' num2str(i) ' is not supposed to send such an overlay searching request']);
% end
% newevent.net.dst = newevent.app.route(tempi+1);
end
NewEvents = [NewEvents; newevent]; clear newevent;
otherwise
disp(['send_app: Undefined application layer type: ' event.app.type]);
end
case 'recv_app'
t = event.instant;
j = event.node;
if bdebug, disp(['recv_app @ node ' num2str(j)]); end
switch event.app.type
case 'crosslayer_searching'
if ddebug, disp(['recv_app: time ' num2str(t) ' node ' num2str(j) ' receives the reply of crosslayer searching with route ' num2str(event.net.route)]); end
fid = fopen(log_file, 'a');
if fid == -1, error(['Cannot open file', log_file]); end
% Record traffic_id, topo_id, end_time, hop_count, requesting node, requesting key
fprintf(fid, '%d %d %g %d %d %d \n', [event.app.id1; event.app.id2; t; length(event.net.route)-1; j; event.app.key]);
fclose(fid);
case 'dht_searching'
tempi = find(event.app.route==j);
if isempty(tempi) | length(tempi)>1
if ddebug, disp(['recv_app: dht_searching: node ' num2str(j) ' receives a wrong DHT searching request with route: ' num2str(event.app.route)]); end
return;
end
if tempi == length(event.app.route)
% I am the destination of this overlay request: send the answer back to the requestor
% if ddebug, disp(['recv_app: at time ' num2str(t) ' destination node ' num2str(j) ' receives the DHT request from node ' num2str(event.app.route(1))]); end
newevent = event;
newevent.type = 'send_net';
% Make sure the previous ACK at MAC layer is finished
newevent.instant = t; % already taken care of in MAC layer. + SIFS + ack_tx_time + 2*eps;
newevent.app.hopcount = newevent.app.hopcount + length(newevent.net.route) - 1;
newevent.net.src = j;
newevent.net.dst = newevent.app.route(1);
newevent.net.size = 100*8;
NewEvents = [NewEvents; newevent]; clear newevent;
elseif tempi == 1
% I am the requester: just received the answer from the destination
if ddebug, disp(['recv_app: at time ' num2str(t) ' node ' num2str(j) ' receives the DHT reply']); end
fid = fopen(log_file, 'a');
if fid == -1, error(['Cannot open file', log_file]); end
% Record traffic_id, topo_id, start_time, start_hop_count(=0), requesting node, requesting key, overlay route length
fprintf(fid, '%d %d %g %d %d %d %d \n', [event.app.id1; event.app.id2; t; event.app.hopcount; j; event.app.key; length(event.app.route)-1]);
fclose(fid);
else
% if ddebug, disp(['recv_app: at time ' num2str(t) ' overlay node ' num2str(j) ' will forward the DHT request to next overlay node ' num2str(event.app.route(tempi + 1))]); end
% I should send request to the next hop in overlay
newevent = event;
newevent.type = 'send_net';
% Make sure the previous ACK at MAC layer is finished
newevent.instant = t; % alread taken care of in MAC layer. + SIFS + ack_tx_time + 2*eps;
newevent.app.hopcount = event.app.hopcount + length(event.net.route) - 1;
newevent.net.src = j;
newevent.net.dst = event.app.route(tempi + 1);
newevent.net.size = 100*8;
NewEvents = [NewEvents; newevent]; clear newevent;
end
otherwise
disp(['recv_app: Undefined application layer type: ' event.app.type]);
end
otherwise
disp(['action: Undefined event type: ' event.type]);
end;
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -