-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_links.cpp
More file actions
97 lines (72 loc) · 2.95 KB
/
data_links.cpp
File metadata and controls
97 lines (72 loc) · 2.95 KB
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
#include <mutex>
#include <iostream>
#include "data_links.h"
#include "pdu.h"
#include <condition_variable> // condition_variable
#include "wqueue.h"
#include <stdlib.h>
#include "misc.h"
#include "addressing.h"
#include "sim_tcpdump.h"
#include <chrono>
using namespace std;
EthernetWire::EthernetWire(chrono::time_point<chrono::high_resolution_clock> start_time, bool monitor, string name,
int* fc_ptr) {
//initialize a queue as the interface and set the maximum size of the queue to however
// many frames this interface can hold (in this case 1)
interface = new wqueue<MPDU*>;
interface->set_max_size(1);
link_start_time = start_time;
print_metadata = monitor;
frame_count_ptr = fc_ptr;
if_name = name;
}
EthernetWire::~EthernetWire() {
delete interface;
}
void EthernetWire::transmit(MPDU* tx_frame) {
// the method for transmitting information across the link as well as showing tcpdump/wireshark-esque info
if (print_metadata) {
chrono::duration<double> diff = chrono::high_resolution_clock::now() - link_start_time;
(*frame_count_ptr)++;
string * frame_info = get_frame_details(tx_frame);
// cout << *frame_info << endl;
string statement = "[" + if_name + "] " + to_string((*frame_count_ptr)) + " " + to_string(diff.count())
+ " " + *frame_info;
cout << statement << endl;
// for (vector<string>::iterator it = frame_info->info.begin(); it != frame_info->info.end(); ++it) {
// cout << statement << endl;
// statement += " " + *it;
// }
delete frame_info;
}
interface->add(tx_frame);
// print the information about the
// in the case a frame being dropped, just get rid of the frame
/*if (rand() % 1000000 <= FAILURES_PER_1000000) {
delete tx_frame;
} else if (rand() % 1000000 <= CORRUPTIONS_PER_1000000) {
// produce a corruption then transmit
corrupt_frame(tx_frame);
interface->add(tx_frame);
} else {
// transmit the frame just fine
}*/
}
MPDU* EthernetWire::receive() {
// wait for a signal to be received and then process the frames
return interface->remove();
}
// the EthernetLink is used to set up the wires and provide them to the device to which they are being connected
EthernetLink::EthernetLink(bool monitor_interface, string name) {
// monitor interface is used to determine if this is a link that should print out metadata about frames
// that traverse it, somewhat like tcpdump and wireshark
chrono::time_point<chrono::high_resolution_clock> link_start_time = chrono::high_resolution_clock::now();
frame_count = 0;
FD_wire_1 = new EthernetWire(link_start_time, monitor_interface, name, &frame_count);
FD_wire_2 = new EthernetWire(link_start_time, monitor_interface, name, &frame_count);
}
EthernetLink::~EthernetLink() {
delete FD_wire_1;
delete FD_wire_2;
}