-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
402 lines (382 loc) · 12.8 KB
/
main.cpp
File metadata and controls
402 lines (382 loc) · 12.8 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
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
#include<bits/stdc++.h>
#include "device.h"
#include "hub.h"
using namespace std;
//A cyclic redundancy check (CRC) Class
class CRC {
string input, divisor, divident, result;
int len_divident, len_gen, len_inp;
//A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data.
public:
//Function for doing XOR
string fun_xor(string a, string b) {
string result = "";
if(a[0] == '0') {
return a.substr(1);
} else {
for(int i=0;i<len_gen;i++) {
result = result + (a[i] == b[i] ? '0' : '1');
}
return result.substr(1);
}
}
//Function to get Modulo
void modulo_div() {
string temp_div = divisor;
string temp_divident = divident.substr(0, len_gen);
int j = len_gen;
while(j < len_divident) {
temp_divident = fun_xor(temp_divident, temp_div);
temp_divident = temp_divident + divident[j];
j++;
}
result = input + fun_xor(temp_divident, temp_div);
}
//Function to GetData From the User
void getdata(string sourceData) {
input = sourceData;
cout<<"Enter Coefficients of generator polynomial: "<<endl;
cin>>divisor;
len_gen = divisor.length();
len_inp = input.length();
divident = input;
int r = len_gen-1;
for(int i=0;i<r;i++) {
divident = divident + '0';
}
len_divident = divident.length();
modulo_div();
}
//function showing SenderSide-Info
void sender_side() {
cout<<"Input: "<<input<<endl;
cout<<"Polynomial: "<<divisor<<endl;
cout<<"Divident: "<<divident<<endl;
cout<<"Data to send: "<<result<<endl;
}
//function showing receiverSide-Info
void receiver_side() {
string data_rec;
cout<<"Enter Data Received: "<<endl;
cin>>data_rec;
string temp_div = divisor;
string temp_divident = data_rec.substr(0, len_gen);
int j = len_gen;
while(j < data_rec.length()) {
temp_divident = fun_xor(temp_divident, temp_div);
temp_divident = temp_divident + data_rec[j];
j++;
}
string error = fun_xor(temp_divident, temp_div);
cout<<"Remainder is: "<<error<<endl;
bool flag = 0;
for(int i=0;i<len_gen-1;i++) {
if(error[i] == '1') {
flag = 1;
break;
}
}
if(flag == 0) {
cout<<"Correct Data Received Without Any Error" <<endl;
} else {
cout<<"Data Received Contain Some Error"<<endl;
}
}
};
//Naming Devices and Hub
Device dev1,dev2,dev3,dev4,dev5;
Hub hb1;
void deviceInfo() {
//here, I am initializing Devices.
dev1.setData("0");
dev2.setData("0");
dev3.setData("0");
dev4.setData("0");
dev5.setData("0");
dev1.setMACaddress(00-15-9-2-99-3);
dev2.setMACaddress(00-15-8-3-99-6);
dev3.setMACaddress(00-15-7-4-99-9);
dev4.setMACaddress(00-15-6-5-99-2);
dev5.setMACaddress(00-15-5-6-99-4);
}
//HUB2DEVICE-Connection
void Hub::hub2dev(string data2,string sourceDevice, int macAdd)
{
if(dev1.MACaddress==macAdd && dev1.data==data2 && sourceDevice!="dev1") {
cout<<" dev1 Accepted the Data and the Received data is: "<<data2<<endl;
}
else {
if(sourceDevice!="dev1") {
cout<<" Data doesn't belong to dev1"<<endl;
}
}
if(dev2.MACaddress==macAdd && dev2.data==data2 && sourceDevice!="dev2") {
cout<<" dev2 Accepted the Data and the Received data is: "<<data2<<endl;
}
else {
if(sourceDevice!="dev2") {
cout<<" Data doesn't belong to dev2"<<endl;
}
}
if(dev3.MACaddress==macAdd && dev3.data==data2 && sourceDevice!="dev3") {
cout<<" dev3 Accepted the Data and the Received data is: "<<data2<<endl;
}
else {
if(sourceDevice!="dev3") {
cout<<" Data doesn't belong to dev3"<<endl;
}
}
if(dev4.MACaddress==macAdd && dev4.data==data2 && sourceDevice!="dev4") {
cout<<" dev4 Accepted the Data and the Received data is: "<<data2<<endl;
}
else {
if(sourceDevice!="dev4") {
cout<<" Data doesn't belong to dev4"<<endl;
}
}
if(dev5.MACaddress==macAdd && dev5.data==data2 && sourceDevice!="dev5") {
cout<<" dev5 Accepted the Data and the Received data is: "<<data2<<endl;
}
else {
if(sourceDevice!="dev5") {
cout<<" Data doesn't belong to dev5"<<endl;
}
}
cout<<endl;
}
//MainFunction
int main() {
int c1, c2;
string SD_Data;
string sourceDevice, destinationDevice;
deviceInfo();
cout<<"Devices we have: dev1, dev2, dev3, dev4, dev5"<<endl;
cout<<"Choose SourceDevice: "<<endl;
cin>>sourceDevice;//Taking sourceDevice as Input
//sourceDevice-Validation
if(sourceDevice!="dev1" && sourceDevice!="dev2" && sourceDevice!="dev3" && sourceDevice!="dev4" && sourceDevice!="dev5") {
cout<<"Invalid SourceDevice "<<endl;
}
cout<<"Choose DestinationDevice: "<<endl;
cin>>destinationDevice;
//Taking DestinationDevice as Input
//destinationDevice-Validation
if(destinationDevice!="dev1" && destinationDevice!="dev2" && destinationDevice!="dev3" && destinationDevice!="dev4" && destinationDevice!="dev5") {
cout<<"Invalid destinationDevice "<<endl;
}
cout<<"SourceDevice Data Input for transmission: "<<endl;
cin>>SD_Data;
//Taking sourceDeviceData as Input
//sourceDeviceData Validation
for(int i=0;i<SD_Data.size();i++) {
if(SD_Data[i]!='0' && SD_Data[i]!='1') {
cout<<"Invalid sourceDeviceData "<<endl;
}
}
//assigning sourceDeviceData to sourceDevice
if(sourceDevice=="dev1") {
dev1.setData(SD_Data);
}
if(sourceDevice=="dev2") {
dev2.setData(SD_Data);
}
if(sourceDevice=="dev3") {
dev3.setData(SD_Data);
}
if(sourceDevice=="dev4") {
dev4.setData(SD_Data);
}
if(sourceDevice=="dev5") {
dev5.setData(SD_Data);
}
//DevicesData in the initialState
cout<<"DevicesData before Transmission: "<<endl;
cout<<"dev1 data before Transmission: "<<dev1.data<<endl;
cout<<"dev2 data before Transmission: "<<dev2.data<<endl;
cout<<"dev3 data before Transmission: "<<dev3.data<<endl;
cout<<"dev4 data before Transmission: "<<dev4.data<<endl;
cout<<"dev5 data before Transmission: "<<dev5.data<<endl;
cout<<"What's your Choice? :"<<endl;
cout<<"1: DedicatedNetworkConnection 2: StarTopologyConnection 3: CRC 4: BridgeDeviceConfiguration " <<endl;
cin>>c1;
switch(c1) {
case 1: {
//Implementation of PhysicalLayer
//DEDICATED-NETWORK-CONNECTION
if(destinationDevice=="dev1"||destinationDevice=="dev2"||destinationDevice=="dev3"||destinationDevice=="dev4"||destinationDevice=="dev5")
{
//sourceDevice-transfer
if(sourceDevice=="dev1")
{
dev1.setData("0");
}
if(sourceDevice=="dev2")
{
dev2.setData("0");
}
if(sourceDevice=="dev3")
{
dev3.setData("0");
}
if(sourceDevice=="dev4")
{
dev4.setData("0");
}
if(sourceDevice=="dev5")
{
dev5.setData("0");
}
//DEDICATED-CONNECTION
if(destinationDevice=="dev1")
{
dev1.setData(SD_Data);
}
if(destinationDevice=="dev2")
{
dev2.setData(SD_Data);
}
if(destinationDevice=="dev3")
{
dev3.setData(SD_Data);
}
if(destinationDevice=="dev4")
{
dev4.setData(SD_Data);
}
if(destinationDevice=="dev5")
{
dev5.setData(SD_Data);
}
cout<<"DevicesData after Transmission: "<<endl;
cout<<"dev1 data after Transmission: "<<dev1.data<<endl;
cout<<"dev2 data after Transmission: "<<dev2.data<<endl;
cout<<"dev3 data after Transmission: "<<dev3.data<<endl;
cout<<"dev4 data after Transmission: "<<dev4.data<<endl;
cout<<"dev5 data after Transmission: "<<dev5.data<<endl;
}
break;
}
case 2: {
//STAR-TOPOLOGY-CONNECTION
if(destinationDevice=="dev1")
{
dev1.setData(SD_Data);
hb1.store(SD_Data, sourceDevice, dev1.MACaddress);
}
if(destinationDevice=="dev2")
{
dev2.setData(SD_Data);
hb1.store(SD_Data, sourceDevice, dev2.MACaddress);
}
if(destinationDevice=="dev3")
{
dev3.setData(SD_Data);
hb1.store(SD_Data, sourceDevice, dev3.MACaddress);
}
if(destinationDevice=="dev4")
{
dev4.setData(SD_Data);
hb1.store(SD_Data, sourceDevice, dev4.MACaddress);
}
if(destinationDevice=="dev5")
{
dev5.setData(SD_Data);
hb1.store(SD_Data, sourceDevice, dev5.MACaddress);
}
break;
}
case 3: {
//A cyclic redundancy check
CRC crc;
crc.getdata(SD_Data);
crc.sender_side();
crc.receiver_side();
break;
}
case 4: {
//Implementation of SwitchDevice-Configuration
int SourceDevice2,destinationDevice2,extra,TotDevices,count=1,bridge_count=0;
string data3;
cout<<"Switch is Created "<<endl;
cout<<"Give total number of end devices: "<<endl;
//Total number of end devices
cin>>TotDevices;
while(count) {
cout<<"Choose source from 1,2,3: "<<endl;
cin>>SourceDevice2;
cout<<"Choose Destination from 1,2,3 (shouldn't be same as Source): "<<endl;
cin>>destinationDevice2;
cout<<"Enter Data to do transmission: "<<endl;
cin>>data3;
bridge_count++;
//DataLink Part
//SourceDevice and destinationDevice are from 1,2,3
Device d[TotDevices];
//first time forwards ARP request to all ports to get mac of all
if(bridge_count == 1) {
for(int i=0;i<TotDevices;i++) {
d[i].setMACaddress(i+500);
}
clock_t start;
double time_taken;
start = clock();
for(int j=0; j<TotDevices; j++){
if (j==SourceDevice2-1) {
continue;
}
for(int i=0;i<data3.length();i++) {
d[destinationDevice2-1].data+=data3[i];
if(j==destinationDevice2-1) {
cout<<"Received frame number: "<<(i+1)<<" at The Destination "<<j+1<<endl;
cout<<"Received frame number: "<<(i+1)<<endl;
cout<<"The source "<<SourceDevice2<<" received the ACK"<<endl;
}
else {
break;
}
}
}
time_taken = ( clock() - start) / (double) CLOCKS_PER_SEC;
cout<<"Total Time Taken for Transmission: "<< time_taken <<endl;
cout<<"Broadcast domain = 1 and Collision domain = "<<TotDevices;
}
else {
//now source know which port connected to which so direct transmission
//look up in table and forward frame to destination
clock_t start;
double time_taken;
start = clock();
for(int j=0; j<TotDevices; j++) {
if(destinationDevice2 == j+1) {
for(int i=0;i<data3.length();i++) {
d[destinationDevice2-1].data+= data3[i];
if(j==destinationDevice2-1) {
cout<<"Received frame number: "<<(i+1)<<" at The Destination "<<(j+1)<<endl;
cout<<"The source "<<SourceDevice2<<" received the ACK "<<endl;
} else {
break;
}
}
}
}
time_taken = (clock() - start ) / (double) CLOCKS_PER_SEC;
cout<<"Total Time Taken for Transmission: "<< time_taken <<endl;
cout<<"Broadcast domain = 1 and Collision domain = "<<TotDevices<<endl;
}
cout<<endl;
cout<<"Enter any key greater than 0 if you want to do more transmission: "<<endl;
cin>>extra;
if(extra<=0) {
count = 0;
}
}
//SwitchDeviceConfigurationCompleted
break;
}
default: {
cout<<"Invalid Choice :( "<<endl;
break;
}
}
return 0;
}