-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhw2+3.cpp
1509 lines (1285 loc) · 62.3 KB
/
hw2+3.cpp
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <climits>
#include <concepts>
#include <cstddef>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
// The double parentheses in decltype are significant.
#define SET(var_name) \
template <typename T> \
void set_##var_name(T &&_##var_name) { \
static_assert(std::is_assignable_v<decltype((var_name)), T>); \
(var_name) = std::forward<T>(_##var_name); \
}
#define SET_WITH_NAME(setter_name, var_name) \
template <typename T> \
void setter_name(T &&_##var_name) { \
static_assert(std::is_assignable_v<decltype((var_name)), T>); \
(var_name) = std::forward<T>(_##var_name); \
}
#define GET(var_name) \
const auto &get_##var_name() const & { return var_name; } \
auto get_##var_name() && { return std::move(var_name); }
#define GET_WITH_NAME(getter_name, var_name) \
const auto &getter_name() const & { return var_name; } \
auto getter_name() && { return std::move(var_name); }
/*
This is unreliable. The static constructor seems to only be called in
non-template classes, including non-template subclasses of template classes.
Even then, the standard doesn't even guarantee that it will be called in a
non-template classes.
*/
#define STATIC_CONSTRUCTOR(body) STATIC_CONSTRUCTOR_IMPL1(body, __COUNTER__)
#define STATIC_CONSTRUCTOR_IMPL1(body, counter) STATIC_CONSTRUCTOR_IMPL2(body, counter) // Expands __COUNTER__
#define STATIC_CONSTRUCTOR_IMPL2(body, counter) \
class static_constructor##counter { \
public: \
static_constructor##counter() { \
body \
} \
}; \
static inline static_constructor##counter static_constructor;
#define DEFAULTED_SPECIAL_MEMBERS(class_name) \
virtual ~class_name() = default; \
DEFAULTED_SPECIAL_MEMBERS_WITHOUT_DESTRUCTOR(class_name)
#define DEFAULTED_SPECIAL_MEMBERS_WITHOUT_DESTRUCTOR(class_name) \
class_name() = default; \
class_name(const class_name &other) = default; \
class_name(class_name &&other) = default; \
class_name &operator=(const class_name &other) = default; \
class_name &operator=(class_name &&other) = default;
template<typename... Ts>
struct overloaded : Ts... { using Ts::operator()...; };
class header;
class payload;
template <std::derived_from<header> HeaderType, std::derived_from<payload> PayloadType, typename Derived>
class packet;
class node;
class event;
class link; // new
// for simplicity, we use a const int to simulate the delay
// if you want to simulate the more details, you should revise it to be a class
const unsigned int ONE_HOP_DELAY = 10;
const unsigned int BROADCAST_ID = UINT_MAX;
// BROADCAST_ID means that all neighbors are receivers; UINT_MAX is the maximum value of unsigned int
class header {
public:
virtual ~header() = default;
SET(src_ID)
SET(dst_ID)
SET(pre_ID)
SET(nex_ID)
GET(src_ID)
GET(dst_ID)
GET(pre_ID)
GET(nex_ID)
virtual std::string type() = 0;
static void print () {
std::cout << "registered header types:\n";
for (const auto &name: derived_class_names) {
std::cout << name << '\n';
}
}
protected:
DEFAULTED_SPECIAL_MEMBERS_WITHOUT_DESTRUCTOR(header)
static inline std::vector<std::string> derived_class_names;
private:
unsigned int src_ID = BROADCAST_ID;
unsigned int dst_ID = BROADCAST_ID;
unsigned int pre_ID = BROADCAST_ID;
unsigned int nex_ID = BROADCAST_ID;
};
class IoT_data_header : public header{
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_data_header");
)
public:
std::string type() override { return "IoT_data_header"; }
};
class IoT_ctrl_header : public header{
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_ctrl_header");
)
public:
std::string type() override { return "IoT_ctrl_header"; }
};
class AGG_ctrl_header : public header{
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("AGG_ctrl_header");
)
public:
std::string type() override { return "AGG_ctrl_header"; }
};
class DIS_ctrl_header : public header{
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("DIS_ctrl_header");
)
public:
std::string type() override { return "DIS_ctrl_header"; }
};
class payload {
std::string msg;
protected:
DEFAULTED_SPECIAL_MEMBERS_WITHOUT_DESTRUCTOR(payload)
static inline std::vector<std::string> derived_class_names;
public:
virtual ~payload() = default;
virtual std::string type() = 0;
SET(msg)
GET(msg)
static void print () {
std::cout << "registered payload types:\n";
for (const auto &name: derived_class_names) {
std::cout << name << '\n';
}
}
};
class IoT_data_payload : public payload {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_data_payload");
)
public:
std::string type() override { return "IoT_data_payload"; }
};
class IoT_ctrl_payload : public payload {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_ctrl_payload");
)
unsigned int counter = 0;
public:
void increase() { counter ++; } // used to increase the counter
GET(counter) // used to get the value of counter
std::string type() override { return "IoT_ctrl_payload"; }
};
class AGG_ctrl_payload : public payload {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("AGG_ctrl_payload");
)
// unsigned int counter ;
public:
// void increase() { counter ++; } // used to increase the counter
// GET(getCounter,unsigned int,counter); // used to get the value of counter
std::string type() override { return "AGG_ctrl_payload"; }
};
class DIS_ctrl_payload : public payload {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("DIS_ctrl_payload");
)
// unsigned int counter ;
unsigned int parent = 0;
public:
// void increase() { counter ++; } // used to increase the counter
SET(parent)
GET(parent) // used to get the value of counter
explicit DIS_ctrl_payload(unsigned int _parent = 0): parent (_parent) {}
std::string type() override { return "DIS_ctrl_payload"; }
};
class packet_derived_classes_common_fields_holder {
protected:
static inline std::vector<std::string> derived_class_names;
static inline unsigned int last_packet_id;
static inline unsigned int live_packet_num;
};
template <std::derived_from<header> HeaderType, std::derived_from<payload> PayloadType, typename Derived>
class packet : protected packet_derived_classes_common_fields_holder {
// a packet usually contains a header and a payload
HeaderType hdr;
PayloadType pld;
unsigned int p_id;
using packet_derived_classes_common_fields_holder::last_packet_id;
using packet_derived_classes_common_fields_holder::live_packet_num;
protected:
PayloadType &get_payload_non_const() {
return pld;
}
packet(): p_id(last_packet_id++) {
live_packet_num++;
}
packet(const packet &other) : hdr(other.hdr), pld(other.pld), p_id(other.p_id) {
live_packet_num++;
}
/*
The move operations does the same thing as the copy operations.
The reason why the move operations also increment live_packet_num,
unlike shared_ptr, is because shared_ptr is thread-safe, so incrementing
and decrementing the reference count is time-consuming, but here,
live_packet_num isn't thread-safe in the first place, so it becomes
pointless to do so.
*/
packet(packet &&other) noexcept : hdr(std::move(other.hdr)), pld(std::move(other.pld)), p_id(std::move(other.p_id)) {
live_packet_num++;
}
packet &operator=(const packet &other) {
Derived temp(*static_cast<const Derived *>(&other)); // Derived must be a subclass so static_cast will do.
swap(*this, temp);
return *this;
}
packet &operator=(packet &&other) noexcept {
(void)std::move(other); // Silences "not moved from" warning
swap(*this, other);
return *this;
}
template <typename DeducedHeaderType, typename DeducedPayloadType>
requires std::same_as<std::remove_cvref_t<DeducedHeaderType>, HeaderType> &&
std::same_as<std::remove_cvref_t<DeducedPayloadType>, PayloadType>
packet(DeducedHeaderType &&_hdr, DeducedPayloadType &&_pld, bool rep = false, unsigned int rep_id = 0) : hdr(std::forward<DeducedHeaderType>(_hdr)), pld(std::forward<DeducedPayloadType>(_pld)) {
if (! rep ) { // a duplicated packet does not have a new packet id
p_id = last_packet_id ++;
}
else {
p_id = rep_id;
}
live_packet_num ++;
}
public:
virtual ~packet(){
// cout << "packet destructor begin" << '\n';
live_packet_num --;
// cout << "packet destructor end" << '\n';
}
// This is friend so that ADL can find it.
friend void swap(packet &first, packet &second) noexcept {
using std::swap;
swap(first.hdr, second.hdr);
swap(first.pld, second.pld);
swap(first.p_id, second.p_id);
}
SET_WITH_NAME(set_header, hdr)
GET_WITH_NAME(get_header, hdr)
SET_WITH_NAME(set_payload, pld)
GET_WITH_NAME(get_payload, pld)
GET_WITH_NAME(get_packet_ID, p_id)
virtual std::string type () const = 0;
// you can define your own packet's addition_information
// to print more information for recv_event and send_event
virtual std::string addition_information () const { return ""; }
static unsigned int get_live_packet_num () { return live_packet_num; }
static void print () {
std::cout << "registered packet types:\n";
for (const auto &name: derived_class_names) {
std::cout << name << '\n';
}
}
void set_src_ID(unsigned int id) {
hdr.set_src_ID(id);
}
void set_dst_ID(unsigned int id) {
hdr.set_dst_ID(id);
}
void set_pre_ID(unsigned int id) {
hdr.set_pre_ID(id);
}
void set_nex_ID(unsigned int id) {
hdr.set_nex_ID(id);
}
void set_msg(const std::string &msg) {
pld.set_msg(msg);
}
};
// this packet is used to transmit the data
class IoT_data_packet: public packet<IoT_data_header, IoT_data_payload, IoT_data_packet> {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_data_packet");
)
public:
IoT_data_packet() = default;
template <typename DeducedPacketType>
requires std::same_as<std::remove_cvref_t<DeducedPacketType>, packet>
explicit IoT_data_packet(DeducedPacketType &&other) : packet(std::forward<DeducedPacketType>(other).get_header(), std::forward<DeducedPacketType>(other).get_payload(), true, other.get_packet_ID()) {}
template <typename DeducedHeaderType, typename DeducedPayloadType>
requires std::same_as<std::remove_cvref_t<DeducedHeaderType>, IoT_data_header> &&
std::same_as<std::remove_cvref_t<DeducedPayloadType>, IoT_data_payload>
IoT_data_packet(DeducedHeaderType &&header, DeducedPayloadType &&payload) : packet(std::forward<DeducedHeaderType>(header), std::forward<DeducedPayloadType>(payload)) {}
std::string type() const override { return "IoT_data_packet"; }
};
// this packet type is used to conduct distributed BFS
class IoT_ctrl_packet: public packet<IoT_ctrl_header, IoT_ctrl_payload, IoT_ctrl_packet> {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_ctrl_packet");
)
public:
IoT_ctrl_packet() = default;
template <typename DeducedPacketType>
requires std::same_as<std::remove_cvref_t<DeducedPacketType>, packet>
explicit IoT_ctrl_packet(DeducedPacketType &&other) : packet(std::forward<DeducedPacketType>(other).get_header(), std::forward<DeducedPacketType>(other).get_payload(), true, other.get_packet_ID()) {}
template <typename DeducedHeaderType, typename DeducedPayloadType>
requires std::same_as<std::remove_cvref_t<DeducedHeaderType>, IoT_ctrl_header> &&
std::same_as<std::remove_cvref_t<DeducedPayloadType>, IoT_ctrl_payload>
IoT_ctrl_packet(DeducedHeaderType &&header, DeducedPayloadType &&payload) : packet(std::forward<DeducedHeaderType>(header), std::forward<DeducedPayloadType>(payload)) {}
std::string type() const override { return "IoT_ctrl_packet"; }
std::string addition_information() const override {
unsigned int counter = get_payload().get_counter();
// cout << counter << '\n';
return " counter " + std::to_string(counter);
}
void increase_payload_counter() {
get_payload_non_const().increase();
}
};
// this packet type is used to transmit each device's nblist to the sink
class AGG_ctrl_packet: public packet<AGG_ctrl_header, AGG_ctrl_payload, AGG_ctrl_packet> {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("AGG_ctrl_packet");
)
public:
AGG_ctrl_packet() = default;
template <typename DeducedPacketType>
requires std::same_as<std::remove_cvref_t<DeducedPacketType>, packet>
explicit AGG_ctrl_packet(DeducedPacketType &&other) : packet(std::forward<DeducedPacketType>(other).get_header(), std::forward<DeducedPacketType>(other).get_payload(), true, other.get_packet_ID()) {}
template <typename DeducedHeaderType, typename DeducedPayloadType>
requires std::same_as<std::remove_cvref_t<DeducedHeaderType>, AGG_ctrl_header> &&
std::same_as<std::remove_cvref_t<DeducedPayloadType>, AGG_ctrl_payload>
AGG_ctrl_packet(DeducedHeaderType &&header, DeducedPayloadType &&payload) : packet(std::forward<DeducedHeaderType>(header), std::forward<DeducedPayloadType>(payload)) {}
std::string type() const override { return "AGG_ctrl_packet"; }
// virtual string addition_information() {
// string msg = (dynamic_cast<AGG_ctrl_payload*>(this->get_payload()))->getMsg();
// return " msg " + msg;
// }
};
// this packet type is used to transmit the new parent to each device
class DIS_ctrl_packet: public packet<DIS_ctrl_header, DIS_ctrl_payload, DIS_ctrl_packet> {
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("DIS_ctrl_packet");
)
public:
DIS_ctrl_packet() = default;
template <typename DeducedPacketType>
requires std::same_as<std::remove_cvref_t<DeducedPacketType>, packet>
explicit DIS_ctrl_packet(DeducedPacketType &&other) : packet(std::forward<DeducedPacketType>(other).get_header(), std::forward<DeducedPacketType>(other).get_payload(), true, other.get_packet_ID()) {}
template <typename DeducedHeaderType, typename DeducedPayloadType>
requires std::same_as<std::remove_cvref_t<DeducedHeaderType>, DIS_ctrl_header> &&
std::same_as<std::remove_cvref_t<DeducedPayloadType>, DIS_ctrl_payload>
DIS_ctrl_packet(DeducedHeaderType &&header, DeducedPayloadType &&payload) : packet(std::forward<DeducedHeaderType>(header), std::forward<DeducedPayloadType>(payload)) {}
std::string type() const override { return "DIS_ctrl_packet"; }
std::string addition_information() const override {
unsigned int parent = get_payload().get_parent();
// cout << counter << '\n';
return " parent " + std::to_string(parent);
}
void set_parent(unsigned int parent) {
get_payload_non_const().set_parent(parent);
}
};
class node {
// all nodes created in the program
static inline std::map<unsigned int, std::shared_ptr<node>> id_node_table;
unsigned int id;
std::set<unsigned int> phy_neighbors;
protected:
static inline std::vector<std::string> derived_class_names;
explicit node(unsigned int _id): id(_id) {
if(id_node_table.find(_id) != id_node_table.end()){
throw std::invalid_argument("Duplicate node id");
}
if ( BROADCAST_ID == _id ) {
throw std::invalid_argument("BROADCAST_ID cannot be used");
}
}
static void register_node(const std::shared_ptr<node> &node) {
id_node_table[node->id] = node;
}
public:
/*
When a node destructs, it removes itself from id_node_table, so it will
be troublesome if a node is copyable because it will then try to remove
itself twice.
Although technically move operations are possible to implement, it
wasn't used in the original program, so they are also deleted for
simplicity.
*/
node() = delete;
node(const node &other) = delete;
node(node &&other) = delete;
node &operator=(const node &other) = delete;
node &operator=(node &&other) = delete;
virtual ~node() = default; // erase the node
virtual std::string type() = 0; // please define it in your derived node class
void add_phy_neighbor (unsigned int _id); // we only add a directed link from id to _id
void del_phy_neighbor (unsigned int _id) { // we only delete a directed link from id to _id
phy_neighbors.erase(_id);
}
// you can use the function to get the node's neighbors at this time
// but in the project 3, you are not allowed to use this function
const std::set<unsigned int> &get_phy_neighbors() { return phy_neighbors; }
using PacketTypes = std::variant<std::monostate, IoT_ctrl_packet, IoT_data_packet, AGG_ctrl_packet, DIS_ctrl_packet>;
void recv (PacketTypes &p) {
recv_handler(p);
} // the packet will be directly deleted after the handler
void send (const PacketTypes &p);
// receive the packet and do something; this is a pure virtual function
virtual void recv_handler(PacketTypes &p) = 0;
static void send_handler(const PacketTypes &p);
static std::shared_ptr<node> id_to_node (unsigned int _id) {
const auto it = id_node_table.find(_id);
return it != id_node_table.cend() ? it->second : nullptr;
}
GET_WITH_NAME(get_node_ID, id)
static void del_node (unsigned int _id) {
const auto it = id_node_table.find(_id);
if (it != id_node_table.cend()) {
id_node_table.erase(it);
}
}
static auto get_node_num () { return id_node_table.size(); }
static void print () {
std::cout << "registered node types:\n";
for (const auto &name: derived_class_names) {
std::cout << name << '\n';
}
}
};
class has_parent {
public:
virtual unsigned int get_parent_id() const = 0;
virtual ~has_parent() = default;
protected:
DEFAULTED_SPECIAL_MEMBERS_WITHOUT_DESTRUCTOR(has_parent)
};
class IoT_device: public node, public has_parent {
// map<unsigned int,bool> one_hop_neighbors; // you can use this variable to record the node's 1-hop neighbors
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_device");
)
bool hi = false; // this is used for example; you can remove it when doing hw2
unsigned int parent_id = 0;
explicit IoT_device(unsigned int _id): node(_id) {}
public:
unsigned int get_parent_id() const override {
return parent_id;
}
static std::shared_ptr<IoT_device> generate(unsigned int _id) {
std::shared_ptr<IoT_device> device(new IoT_device(_id));
register_node(device);
return device;
}
std::string type() override { return "IoT_device"; }
// please define recv_handler function to deal with the incoming packet
// you have to write the code in recv_handler of IoT_device
void recv_handler (PacketTypes &p) override {
// in this function, you are "not" allowed to use node::id_to_node(id) !!!!!!!!
// this is a simple example
// node 0 broadcasts its message to every node and every node relays the packet "only once" and increases its counter
// the variable hi is used to examine whether the packet has been received by this node before
// you can remove the variable hi and create your own routing table in class IoT_device
std::visit(
overloaded {
[&](IoT_ctrl_packet &packet) { // the device receives a packet from the sink
if (hi) {
return;
}
packet.set_pre_ID(get_node_ID());
packet.set_nex_ID(BROADCAST_ID);
packet.set_dst_ID(BROADCAST_ID);
packet.increase_payload_counter();
hi = true;
send_handler(p);
// unsigned mat = l3->getMatID();
// unsigned act = l3->getActID();
// string msg = l3->getMsg(); // get the msg
},
[&](IoT_data_packet &packet) { // the device receives a packet
(void)packet;
// cout << "node " << getNodeID() << " send the packet" << '\n';
},
[&](AGG_ctrl_packet &packet) {
(void)packet;
// cout << "node id = " << getNodeID() << ", msg = " << l3->getMsg() << '\n';
},
[&](DIS_ctrl_packet &packet) {
(void)packet;
// cout << "node id = " << getNodeID() << ", parent = " << l3->get_parent() << '\n';
},
[](std::monostate) {}
},
p
);
// you should implement the OSPF algorithm in recv_handler
// getNodeID() returns the id of the current node
// The current node's neighbors are already stored in the following variable
// map<unsigned int,bool> node::phy_neighbors
// however, this variable is private in the class node
// You have to use node::getPhyNeighbors to get the variable
// for example, if you want to print all the neighbors of this node
// const map<unsigned int,bool> &nblist = getPhyNeighbors();
// cout << "node " << getNodeID() << "'s nblist: ";
// for (map<unsigned int,bool>::const_iterator it = nblist.begin(); it != nblist.end(); it ++) {
// cout << it->first << ", " ;
// }
// cout << '\n';
// you can use p->get_header()->set_src_ID() or get_src_ID()
// p->get_header()->set_dst_ID() or get_dst_ID()
// p->get_header()->set_pre_ID() or get_pre_ID()
// p->get_header()->set_nex_ID() or get_nex_ID() to change or read the packet header
// In addition, you can get the packet, header, and payload with the correct type
// in fact, this is downcasting
// IoT_data_packet * pkt = dynamic_cast<IoT_data_packet*> (p);
// IoT_data_header * hdr = dynamic_cast<IoT_data_header*> (p->get_header());
// IoT_data_payload * pld = dynamic_cast<IoT_data_payload*> (p->get_payload());
// you can also change the IoT_data_payload setting
// pld->set_msg(string): to set the message transmitted to the destination
// Besides, you can use packet::generator::generate() to generate a new packet; note that you should fill the header and payload in the packet
// moreover, you can use "packet *p2 = packet::generator::replicate(p)" to make a clone p2 of packet p
// note that if the packet is generated or replicated manually, you must delete it by packet::discard() manually before recv_handler finishes
// "IMPORTANT":
// You have to "carefully" fill the correct information (e.g., srcID, dstID, ...) in the packet before you send it
// Note that if you want to transmit a packet to only one next node (i.e., unicast), then you fill the ID of a specific node to "nexID" in the header
// Otherwise, i.e., you want to broadcasts, then you fill "BROADCAST_ID" to "nexID" in the header
// after that, you can use send() to transmit the packet
// usage: send_handler (p);
// note that packet p will be discarded (deleted) after recv_handler(); you don't need to manually delete it
}
// void add_one_hop_neighbor (unsigned int n_id) { one_hop_neighbors[n_id] = true; }
// unsigned int get_one_hop_neighbor_num () { return one_hop_neighbors.size(); }
// IoT_device::generator is derived from node::generator to generate a node
};
class mycomp {
bool reverse;
public:
explicit mycomp(bool revparam = false) : reverse(revparam) {}
bool operator() (const std::unique_ptr<event> &lhs, const std::unique_ptr<event> &rhs) const;
};
class event {
static std::priority_queue<std::unique_ptr<event>, std::vector<std::unique_ptr<event>>, mycomp> events;
static inline unsigned int cur_time; // timer
static inline unsigned int end_time;
// get the next event
static std::unique_ptr<event> get_next_event() {
if(events.empty()) {
return nullptr;
}
// This is safe and the only way to move a unique_ptr out of a priority_queue. The elements are never actually const.
std::unique_ptr<event> e = std::move(const_cast<std::unique_ptr<event> &>(events.top())); // NOLINT(cppcoreguidelines-pro-type-const-cast)
events.pop();
// cout << events.size() << " events remains" << '\n';
return e;
}
static inline std::hash<std::string> event_seq;
unsigned int trigger_time = 0;
protected:
SET(trigger_time)
static inline std::vector<std::string> derived_class_names;
explicit event(unsigned int _trigger_time): trigger_time(_trigger_time) {}
event() = default;
event(const event &other) = default;
event(event &&other) = default;
event &operator=(const event &other) = default;
event &operator=(event &&other) = default;
static void add_event (std::unique_ptr<event> &&e) { events.push(std::move(e)); }
public:
virtual void trigger()=0;
virtual ~event() = default;
virtual unsigned int event_priority() const = 0;
static unsigned int get_hash_value(const std::string &string_for_hash) {
size_t priority = event_seq(string_for_hash);
return static_cast<unsigned int>(priority);
}
static void flush_events () { // only for debug
std::cout << "**flush begin" << '\n';
while ( ! events.empty() ) {
std::cout << std::setw(11) << events.top()->trigger_time << ": " << std::setw(11) << events.top()->event_priority() << '\n';
events.pop();
}
std::cout << "**flush end" << '\n';
}
GET(trigger_time)
static void start_simulate( unsigned int _end_time ) { // the function is used to start the simulation
end_time = _end_time;
std::unique_ptr<event> e = get_next_event();
while (e && e->trigger_time <= end_time ) {
if ( cur_time > e->trigger_time ) {
std::cerr << "cur_time = " << cur_time << ", event trigger_time = " << e->trigger_time << '\n';
break;
}
cur_time = e->trigger_time;
// cout << "event trigger_time = " << e->trigger_time << '\n';
e->print(); // for log
// cout << " event begin" << '\n';
e->trigger();
// cout << " event end" << '\n';
e = event::get_next_event ();
}
// cout << "no more event" << '\n';
}
static unsigned int get_cur_time() { return cur_time; }
static void get_cur_time(unsigned int _cur_time) { cur_time = _cur_time; }
// static unsigned int getEndTime() { return end_time ; }
// static void getEndTime(unsigned int _end_time) { end_time = _end_time; }
virtual void print() const = 0; // the function is used to print the event information
static void print_registered_event_types () {
std::cout << "registered event types:\n";
for (const auto &name: derived_class_names) {
std::cout << name << '\n';
}
}
};
std::priority_queue<std::unique_ptr<event>, std::vector<std::unique_ptr<event>>, mycomp> event::events;
bool mycomp::operator() (const std::unique_ptr<event> &lhs, const std::unique_ptr<event> &rhs) const {
// cout << lhs->get_trigger_time() << ", " << rhs->get_trigger_time() << '\n';
// cout << lhs->type() << ", " << rhs->type() << '\n';
size_t lhs_pri = lhs->event_priority();
size_t rhs_pri = rhs->event_priority();
// cout << "lhs hash = " << lhs_pri << '\n';
// cout << "rhs hash = " << rhs_pri << '\n';
bool result = lhs->get_trigger_time() == rhs->get_trigger_time() ? lhs_pri > rhs_pri : lhs->get_trigger_time() > rhs->get_trigger_time();
return result ^ reverse;
}
class recv_event : public event {
public:
class recv_data; // forward declaration
private:
unsigned int sender_id; // the sender
unsigned int receiver_id; // the receiver; the packet will be given to the receiver
node::PacketTypes pkt; // the packet
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("recv_event");
)
// this constructor cannot be directly called by users; only by generator
// the packet will be given to the receiver
recv_event(unsigned int _trigger_time, const recv_data &data) : event(_trigger_time), sender_id(data.s_id), receiver_id(data.r_id), pkt(data._pkt) {}
public:
// recv_event will trigger the recv function
void trigger() override {
if (!node::id_to_node(receiver_id)){
std::cerr << "recv_event error: no node " << receiver_id << "!" << '\n';
return ;
}
node::id_to_node(receiver_id)->recv(pkt);
}
unsigned int event_priority() const override {
std::string string_for_hash;
string_for_hash = std::to_string(get_trigger_time()) +
std::to_string(sender_id) +
std::to_string (receiver_id) +
std::to_string (std::visit(overloaded {
[](auto &&packet) { return packet.get_packet_ID(); },
[](std::monostate) -> unsigned { throw std::domain_error("The packet has not been assigned any specific packet type"); }
}, pkt));
return get_hash_value(string_for_hash);
}
static void generate(unsigned int _trigger_time, const recv_data &data) {
add_event(std::unique_ptr<recv_event>(new recv_event(_trigger_time, data)));
}
// this class is used to initialize the recv_event
class recv_data{
public:
unsigned int s_id = 0;
unsigned int r_id = 0;
node::PacketTypes _pkt;
};
// the recv_event::print() function is used for log file
void print () const override {
std::visit(overloaded {
[&](auto &&packet) {
std::cout << "time " << std::setw(11) << event::get_cur_time()
<< " recID" << std::setw(11) << receiver_id
<< " pktID" << std::setw(11) << packet.get_packet_ID()
<< " srcID" << std::setw(11) << packet.get_header().get_src_ID()
<< " dstID" << std::setw(11) << packet.get_header().get_dst_ID()
<< " preID" << std::setw(11) << packet.get_header().get_pre_ID()
<< " nexID" << std::setw(11) << packet.get_header().get_nex_ID()
<< " " << packet.type()
<< packet.addition_information();
},
[](std::monostate) {}
}, pkt);
// if ( pkt->type() == "IoT_ctrl_packet" ) cout << " " << ((IoT_ctrl_payload*)pkt->get_payload())->getCounter();
std::cout << '\n';
// cout << pkt->type()
// << " time " << setw(11) << event::getCurTime()
// << " recID " << setw(11) << receiver_id
// << " pktID" << setw(11) << pkt->get_packet_ID()
// << " srcID " << setw(11) << pkt->get_header()->get_src_ID()
// << " dstID" << setw(11) << pkt->get_header()->get_dst_ID()
// << " preID" << setw(11) << pkt->get_header()->get_pre_ID()
// << " nexID" << setw(11) << pkt->get_header()->get_nex_ID()
// << '\n';
}
};
class send_event : public event {
public:
class send_data; // forward declaration
private:
// this constructor cannot be directly called by users; only by generator
unsigned int sender_id; // the sender
unsigned int receiver_id; // the receiver
node::PacketTypes pkt; // the packet
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("send_event");
)
send_event(unsigned int _trigger_time, const send_data &data) : event(_trigger_time), sender_id(data.s_id), receiver_id(data.r_id), pkt(data._pkt) {}
public:
// send_event will trigger the send function
void trigger() override {
if (!node::id_to_node(sender_id)){
std::cerr << "send_event error: no node " << sender_id << "!" << '\n';
return ;
}
node::id_to_node(sender_id)->send(pkt);
}
unsigned int event_priority() const override {
std::string string_for_hash;
string_for_hash = std::to_string(get_trigger_time()) +
std::to_string(sender_id) +
std::to_string (receiver_id) +
std::to_string (std::visit(overloaded {
[](auto &&packet) { return packet.get_packet_ID(); },
[](std::monostate) -> unsigned { throw std::domain_error("The packet has not been assigned any specific packet type"); }
}, pkt));
return get_hash_value(string_for_hash);
}
static void generate(unsigned int _trigger_time, const send_data &data) {
add_event(std::unique_ptr<send_event>(new send_event(_trigger_time, data)));
}
// this class is used to initialize the send_event
class send_data{
public:
unsigned int s_id = 0;
unsigned int r_id = 0;
node::PacketTypes _pkt;
unsigned int t = 0;
};
void print () const override { // the send_event::print() function is used for log file
std::visit(overloaded {
[&](auto &&packet) {
std::cout << "time " << std::setw(11) << event::get_cur_time()
<< " senID" << std::setw(11) << sender_id
<< " pktID" << std::setw(11) << packet.get_packet_ID()
<< " srcID" << std::setw(11) << packet.get_header().get_src_ID()
<< " dstID" << std::setw(11) << packet.get_header().get_dst_ID()
<< " preID" << std::setw(11) << packet.get_header().get_pre_ID()
<< " nexID" << std::setw(11) << packet.get_header().get_nex_ID()
<< " " << packet.type()
<< packet.addition_information()
// << " msg" << setw(11) << dynamic_cast<IoT_data_payload*>(pkt->get_payload())->getMsg()
<< '\n';
},
[](std::monostate) {}
}, pkt);
// cout << pkt->type()
// << " time " << setw(11) << event::getCurTime()
// << " senID " << setw(11) << sender_id
// << " pktID" << setw(11) << pkt->get_packet_ID()
// << " srcID " << setw(11) << pkt->get_header()->get_src_ID()
// << " dstID" << setw(11) << pkt->get_header()->get_dst_ID()
// << " preID" << setw(11) << pkt->get_header()->get_pre_ID()
// << " nexID" << setw(11) << pkt->get_header()->get_nex_ID()
// << '\n';
}
};
////////////////////////////////////////////////////////////////////////////////
class IoT_data_pkt_gen_event : public event {
public:
class gen_data; // forward declaration
// this class is used to initialize the IoT_data_pkt_gen_event
class pkt_gen_data {
public:
unsigned int src_id = 0;
unsigned int dst_id = 0;
std::string msg;
// packet *_pkt;
};
private:
// this constructor cannot be directly called by users; only by generator
unsigned int src; // the src
unsigned int dst; // the dst
// packet *pkt; // the packet
std::string msg;
STATIC_CONSTRUCTOR (
derived_class_names.emplace_back("IoT_data_pkt_gen_event");
)
IoT_data_pkt_gen_event(unsigned int _trigger_time, const pkt_gen_data &data) : event(_trigger_time), src(data.src_id), dst(data.dst_id), msg(data.msg) {}
public:
static void generate(unsigned int _trigger_time, const pkt_gen_data &data) {
add_event(std::unique_ptr<IoT_data_pkt_gen_event>(new IoT_data_pkt_gen_event(_trigger_time, data)));
}
// IoT_data_pkt_gen_event will trigger the packet gen function
void trigger() override {
if (!node::id_to_node(src)){
std::cerr << "IoT_data_pkt_gen_event error: no node " << src << "!" << '\n';
return ;
}
if ( dst != BROADCAST_ID && !node::id_to_node(dst)) {
std::cerr << "IoT_data_pkt_gen_event error: no node " << dst << "!" << '\n';
return;
}
IoT_data_packet pkt;
pkt.set_src_ID(src);
pkt.set_dst_ID(dst);
pkt.set_pre_ID(src); // this column is not important when the packet is first received by the src (i.e., just generated)
pkt.set_nex_ID(src); // this column is not important when the packet is first received by the src (i.e., just generated)
pkt.set_msg(msg);
recv_event::recv_data e_data;
e_data.s_id = src;
e_data.r_id = src; // to make the packet start from the src
e_data._pkt = pkt;
recv_event::generate(get_trigger_time(), e_data);
}
unsigned int event_priority() const override {
std::string string_for_hash;
string_for_hash = std::to_string(get_trigger_time()) + std::to_string(src) + std::to_string (dst) ; //to_string (pkt->get_packet_ID());
return get_hash_value(string_for_hash);
}
// the IoT_data_pkt_gen_event::print() function is used for log file
void print () const override {
std::cout << "time " << std::setw(11) << event::get_cur_time()
<< " " << std::setw(11) << " "
<< " " << std::setw(11) << " "
<< " srcID" << std::setw(11) << src
<< " dstID" << std::setw(11) << dst
<< " " << std::setw(11) << " "
<< " " << std::setw(11) << " "
<< " IoT_data_packet generating"
<< '\n';
}
};
class IoT_ctrl_pkt_gen_event : public event {
public:
class gen_data; // forward declaration