-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol_Prophet.m
334 lines (215 loc) · 12 KB
/
protocol_Prophet.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
%% PRoPHET Routing Protocol
%% Get messages and buffered messages destination address
messages_x = [];
messages_y = [];
buffer_x = [];
buffer_y = [];
in_refresh_buffers;
%% Get messages and buffered messages IDs
ID_messages_x = [];
ID_messages_y = [];
ID_buffer_x = [];
ID_buffer_y = [];
prophet_threshold = 0.985;
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
%% Check if either nodes are the destination of any message and buffered messages for both nodes
% Any of messages in first node destined to second node?
if sum (messages_x == MN_INDEX_2 ) >= 1
% Forward message to its destination
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).RECEIVED_MESSAGE( end+1: end+ sum(messages_x == MN_INDEX_2 ) ) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).MESSAGE( messages_x == MN_INDEX_2 );
for adding_time_index = 0:sum( messages_x == MN_INDEX_2 )-1
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).RECEIVED_MESSAGE( end - adding_time_index ).RECEIPTION_TIME = time;
end
% Remove message from source queue
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).MESSAGE( messages_x == MN_INDEX_2 ) = [];
% Increment message counter
MN_DATA_ROUTING_temp.RECEIVED_COUNT = MN_DATA_ROUTING_temp.RECEIVED_COUNT + sum(messages_x == MN_INDEX_2 );
MN_DATA_ROUTING_temp.RECEIVED_DIRECTLY = MN_DATA_ROUTING_temp.RECEIVED_DIRECTLY + sum(messages_x == MN_INDEX_2 );
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end
%%2.节点2是节点1缓存中传信目标
% Any of buffered messages in first node destined to second node?
if sum(buffer_x == MN_INDEX_2 ) >= 1
% Forward message to its destination
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).RECEIVED_MESSAGE( end+1 : end+ sum( buffer_x == MN_INDEX_2 ) ) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( buffer_x == MN_INDEX_2 );
for adding_time_index = 0:sum( buffer_x == MN_INDEX_2 )-1
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).RECEIVED_MESSAGE( end - adding_time_index ).RECEIPTION_TIME = time;
end
% Remove message from source queue
MN_DATA_ROUTING_temp.VS_NODE( MN_INDEX_1 ).BUFFER( buffer_x == MN_INDEX_2 ) = [];
% Increment message counter
MN_DATA_ROUTING_temp.RECEIVED_COUNT = MN_DATA_ROUTING_temp.RECEIVED_COUNT + sum( buffer_x == MN_INDEX_2 );
MN_DATA_ROUTING_temp.RECEIVED_FROM_BUFFERED = MN_DATA_ROUTING_temp.RECEIVED_FROM_BUFFERED + sum( buffer_x == MN_INDEX_2 );
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end
%%3.节点1是节点2的传信目标 不存入buffer
% Any of messages in second node destined to first node?
if sum(messages_y == MN_INDEX_1 ) >= 1
% Forward message to its destination
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).RECEIVED_MESSAGE ( end+1 : end+ sum( messages_y == MN_INDEX_1 ) ) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).MESSAGE( messages_y == MN_INDEX_1 );
% Add time index to each received message
for adding_time_index = 0:sum( messages_y == MN_INDEX_1 )-1
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).RECEIVED_MESSAGE(end - adding_time_index).RECEIPTION_TIME = time;
end
% Remove message from source queue
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).MESSAGE( messages_y == MN_INDEX_1 ) = [];
% Increment message counter
MN_DATA_ROUTING_temp.RECEIVED_COUNT = MN_DATA_ROUTING_temp.RECEIVED_COUNT + sum( messages_y == MN_INDEX_1 );
MN_DATA_ROUTING_temp.RECEIVED_DIRECTLY = MN_DATA_ROUTING_temp.RECEIVED_DIRECTLY + sum( messages_y == MN_INDEX_1 );
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end
%%4. 节点1是节点2缓存中的传信目标
% Any of buffered messages in second node destined to first node?
if sum(buffer_y == MN_INDEX_1 ) >= 1
% Forward message to its destination
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).RECEIVED_MESSAGE(end+1: end+ sum( buffer_y == MN_INDEX_1 )) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( buffer_y == MN_INDEX_1 );
% Add time index to each received message
for adding_time_index = 0:sum( buffer_y == MN_INDEX_1 )-1
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).RECEIVED_MESSAGE(end - adding_time_index).RECEIPTION_TIME = time;
end
% Remove message from source queue
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( buffer_y == MN_INDEX_1 ) = [];
% Increment message counter
MN_DATA_ROUTING_temp.RECEIVED_COUNT = MN_DATA_ROUTING_temp.RECEIVED_COUNT + sum( buffer_y == MN_INDEX_1 );
MN_DATA_ROUTING_temp.RECEIVED_FROM_BUFFERED = MN_DATA_ROUTING_temp.RECEIVED_FROM_BUFFERED + sum( buffer_y == MN_INDEX_1 );
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%依靠中间节点传递信息
%%节点1信息传至节点2缓存
%节点1信息非空且节点1信息并节点2并非节点1目标
%%1.MSG_X -> Buffer_Y
if ( ~isempty( messages_x ) ) && (sum (messages_x == MN_INDEX_2 ) == 0)
delete_messages = [];
% for forward_node = unique(messages_x)
for forward_node = ID_messages_x
if (~isempty (messages_x))
to_node = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).MESSAGE(ID_messages_x == forward_node).TO;
node1_to_nodex = routing_table.VS_NODE(MN_INDEX_1).PROPHET(to_node);
node2_to_nodex = routing_table.VS_NODE(MN_INDEX_2).PROPHET(to_node);
else
node2_to_nodex = 0;
end
if (sum (ID_buffer_y == forward_node) == 0) && ( node2_to_nodex > prophet_threshold )
% Copy message of node one to the buffer of the next node
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end+1 ) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).MESSAGE( forward_node == ID_messages_x );
% Add forwarded node index
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end ).NUMBER_OF_FORWARDS(end + 1) = MN_INDEX_1;
temp_TTL = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end ).TTL;
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end ).TTL = temp_TTL + 1;
% Increment buffer counter
MN_DATA_ROUTING_temp.BUFFERED_COUNT = MN_DATA_ROUTING_temp.BUFFERED_COUNT + 1;
delete_messages(end +1) = find(forward_node == ID_messages_x);
end
end % End for message_index_temp
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end
%%节点2信息传至节点1缓存
%%2.MSG_Y -> Buffer_X
if ( ~isempty( messages_y ) ) && (sum (messages_y == MN_INDEX_1 ) == 0)
delete_messages = [];
% for forward_node = unique(messages_y)
for forward_node = ID_messages_y
if (~isempty (messages_y))
to_node = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).MESSAGE(ID_messages_y == forward_node).TO;
node1_to_nodex = routing_table.VS_NODE(MN_INDEX_1).PROPHET(to_node);
node2_to_nodex = routing_table.VS_NODE(MN_INDEX_2).PROPHET(to_node);
else
node1_to_nodex = 0;
end
if (sum(ID_buffer_x == forward_node) == 0) && ( node1_to_nodex > prophet_threshold )
% Copy message of node one to the buffer of the next node
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end+1 ) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).MESSAGE( forward_node == ID_messages_y );
% Add forwarded node index
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end ).NUMBER_OF_FORWARDS(end + 1) = MN_INDEX_2;
temp_TTL = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end ).TTL;
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end ).TTL = temp_TTL + 1;
% Increment buffer counter
MN_DATA_ROUTING_temp.BUFFERED_COUNT = MN_DATA_ROUTING_temp.BUFFERED_COUNT + 1;
delete_messages(end +1) = find(forward_node == ID_messages_y);
end
end % End for message_index_temp
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end
%%节点1缓存传至节点2缓存
%%3.BufferX -> BufferY
if ( ~isempty( buffer_x ) ) && (sum (buffer_x == MN_INDEX_2 ) == 0)
delete_messages = [];
% for forward_node = unique(buffer_x)
for forward_node = ID_buffer_x
if (~isempty (buffer_x))
to_node = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER(ID_buffer_x == forward_node).TO;
node1_to_nodex = routing_table.VS_NODE(MN_INDEX_1).PROPHET(to_node);
node2_to_nodex = routing_table.VS_NODE(MN_INDEX_2).PROPHET(to_node);
else
node2_to_nodex = 0;
end
if ( sum(ID_buffer_y == forward_node) == 0 ) && ( node2_to_nodex > prophet_threshold )
% Copy message of node one to the buffer of the next node
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end+1 ) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( forward_node == ID_buffer_x );
% Add forwarded node index
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end ).NUMBER_OF_FORWARDS(end + 1) = MN_INDEX_1;
temp_TTL = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end ).TTL;
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( end ).TTL = temp_TTL + 1;
% Increment buffer counter
MN_DATA_ROUTING_temp.BUFFERED_COUNT = MN_DATA_ROUTING_temp.BUFFERED_COUNT + 1;
delete_messages(end +1) = find(forward_node == ID_buffer_x);
end
end % End for message_index_temp
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end
%%节点2缓存传至节点1缓存
%%4.BufferY -> BufferX
if ( ~isempty( buffer_y ) ) && (sum (buffer_y == MN_INDEX_1 ) == 0)
delete_messages = [];
% for forward_node = unique(buffer_y)
for forward_node = ID_buffer_y
if (~isempty (buffer_y))
to_node = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER(ID_buffer_y == forward_node).TO;
node1_to_nodex = routing_table.VS_NODE(MN_INDEX_1).PROPHET(to_node);
node2_to_nodex = routing_table.VS_NODE(MN_INDEX_2).PROPHET(to_node);
else
node1_to_nodex = 0;
end
if ( sum (ID_buffer_x == forward_node) == 0 ) && ( node1_to_nodex > prophet_threshold )
% Copy message of node one to the buffer of the next node
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end+1 ) = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_2).BUFFER( forward_node == ID_buffer_y );
% Add forwarded node index
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end ).NUMBER_OF_FORWARDS(end + 1) = MN_INDEX_2;
temp_TTL = MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end ).TTL;
MN_DATA_ROUTING_temp.VS_NODE(MN_INDEX_1).BUFFER( end ).TTL = temp_TTL + 1;
% Increment buffer counter
MN_DATA_ROUTING_temp.BUFFERED_COUNT = MN_DATA_ROUTING_temp.BUFFERED_COUNT + 1;
delete_messages(end +1) = find(forward_node == ID_buffer_y);
end
end % End for message_index_temp
% Then refresh
in_Refresh_ID;
in_refresh_buffers;
in_already_received_cleared;
end