-
Notifications
You must be signed in to change notification settings - Fork 2
/
scale_run_FaNet.m
242 lines (180 loc) · 8.45 KB
/
scale_run_FaNet.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
function [Nodes_list, TotPower, events_graph_height]=scale_run_FaNet(Nodes_list, Events_list, max_run_time, events_graph_height)
% Simulate SCALE network when all nodes are kept active
global sentEvents;
global forwardedEvents;
global lifeTime;
global activeTime;
global numNodes;
global powerOvertime;
global initial_power;
global timeInterval;
clock = 0;
sentEvents = 0;
forwardedEvents = 0;
scale_reset_events_arrived_at_APs();
lifeTime=0;
activeTime=zeros(numNodes,1);
powerOvertime=zeros(numNodes,1+floor(max_run_time/timeInterval));
powerOvertime(:,1)=initial_power;
countInterval=1;
while 1
clock = clock + 1;
% Check to see if the network topology is still active
network_status = scale_check_topology_connectors(Nodes_list);
if network_status == 0
lifeTime=clock;
disp(sprintf('At clock %d, all nodes that have access to APs are died', clock));
break;
end
if (clock > max_run_time)
break;
end
events = [];
events = scale_get_events(Events_list, events, clock);
%keep track of time interval
if(mod(clock,timeInterval)==0)
countInterval= countInterval+1;
end
for k=1:numel(Nodes_list)
%record power every time interval
if(mod(clock,timeInterval)==0)
powerOvertime(k,countInterval)=Nodes_list(k).power;
end
action = [];
action.type = 'active';
action.time = 1;
Nodes_list(k).power = scale_power_consumption(Nodes_list(k).power, action);
% Check to see if it has any event to send
event = [];
if ~isempty(events)
event_index = find([events(:).source] == k, 1);
if ~isempty(event_index)
event = events(event_index);
end
end
% Check to see if it has any any event from the event queue
if(~isempty(event) && event.instant == clock && event.source == k)
% record total generated events for each node
Nodes_list(k).generated_events = Nodes_list(k).generated_events + 1;
Nodes_list = disseminate_event(Nodes_list, event);
else % Check to see if the node has any event being buffered
if(~isempty(Nodes_list(k).buffer))
buffered_event = Nodes_list(k).buffer(1); % pick to the oldest event
Nodes_list(k).buffer(1) = []; % remove sent event from buffer
Nodes_list = disseminate_event(Nodes_list, buffered_event);
end
end
end
end
TotPower=scale_power_graph(Nodes_list, 'FaNet Data Dissemination Schema');
events_graph_height = scale_events_graph(Nodes_list,'FaNet Data Dissemination Events', events_graph_height);
return;
end
function [Nodes_list] = disseminate_event(Nodes_list, event)
global sentEvents;
global forwardedEvents;
if event.from_node ~= event.source
Nodes_list(event.source).received_events = Nodes_list(event.source).received_events + 1;
end
%Check to see if the node has received this event
if ~isempty(Nodes_list(event.source).recieved_events_queue)
event_queue_index = find([Nodes_list(event.source).recieved_events_queue(:).id] == event.id, 1);
if(isempty(event_queue_index))
Nodes_list(event.source).recieved_events_queue = [Nodes_list(event.source).recieved_events_queue, event];
else
Nodes_list(event.source).duplicated_events = Nodes_list(event.source).duplicated_events + 1;
return;
end
else
Nodes_list(event.source).recieved_events_queue = [Nodes_list(event.source).recieved_events_queue, event];
end
% receiving node get its own event
if event.from_node ~= event.source && event.originator == event.source
Nodes_list(event.source).duplicated_events = Nodes_list(event.source).duplicated_events + 1;
return;
end
% Stop dissemination process
if event.ttl < 1
return;
end
sent_events = 0;
% when nodes do not belong to any tree,
% check to see if it has accept to AP
if isempty(Nodes_list(event.source).parents) && ...
isempty(isempty(Nodes_list(event.source).children)) && ...
isempty(Nodes_list(event.source).AP_Connections)
Nodes_list(event.source).buffer = [Nodes_list(event.source).buffers, event];
end
% Send to the cloud when the connection is availble
if(~isempty(Nodes_list(event.source).AP_Connections))
% Only send event to the clound when
% It has not been done so
if event.destination == 99999
sent_events = sent_events + 1;
scale_send_to_AP(Nodes_list(event.source).AP_Connections.AP_issid);
action = [];
action.type = 'sending';
action.packet_size = event.size;
Nodes_list(event.source).power = scale_power_consumption(Nodes_list(event.source).power, action);
event.destination = -99999;
end
end
relayed_events = 0;
% Relay events to parent nodes
if ~isempty(Nodes_list(event.source).parents)
for k=1:numel(Nodes_list(event.source).parents)
if (k > numel(Nodes_list(event.source).parents))
continue;
end
if Nodes_list(event.source).parents(k).parent_node_id ~= event.originator && ...
Nodes_list(event.source).parents(k).parent_node_id ~= event.from_node
event.from_node = event.source;
event.source = Nodes_list(event.source).parents(k).parent_node_id;
% initial relay
if event.originator == event.source
sent_events = sent_events + 1;
else
relayed_events = relayed_events + 1;
event.ttl = event.ttl - 1;
end
Nodes_list = disseminate_event(Nodes_list, event);
action = [];
action.type = 'sending';
action.packet_size = event.size;
Nodes_list(event.source).power = scale_power_consumption(Nodes_list(event.source).power, action);
end
end
end
% Relay events to children nodes
if ~isempty(Nodes_list(event.source).children)
for k=1:numel(Nodes_list(event.source).children)
if (k > numel(Nodes_list(event.source).children))
continue;
end
if Nodes_list(event.source).children(k) ~= event.originator && ...
Nodes_list(event.source).children(k) ~= event.from_node
% recursively send the event to all children
event.from_node = event.source;
event.source = Nodes_list(event.source).children(k);
% Initial relay
if event.originator == event.source
sent_events = sent_events + 1;
else
relayed_events = relayed_events + 1;
event.ttl = event.ttl - 1;
end
Nodes_list = disseminate_event(Nodes_list, event);
action = [];
action.type = 'sending';
action.packet_size = event.size;
Nodes_list(event.source).power = scale_power_consumption(Nodes_list(event.source).power, action);
end
end
end
% Update global sent events and global relayed events
sentEvents = sentEvents + sent_events;
forwardedEvents = forwardedEvents + relayed_events;
Nodes_list(event.source).sent_events = Nodes_list(event.source).sent_events + sent_events;
Nodes_list(event.source).relayed_events = Nodes_list(event.source).relayed_events + relayed_events;
return;
end