-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeMultiplexer.c
202 lines (165 loc) · 5.37 KB
/
deMultiplexer.c
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
/*
Function: deMultiplex packets from the main queue to their output Queues.
Definition of layer 3 structure:
NOTE: Layer 3's packet is the payload of layer 2. It is stored in an array of defineable length
using the extern variable L2MAXLOAD
Structure of L2PAYLOAD[L2MAXLOAD]
L2PAYLOAD[0][1]: Layer 3 source address //needs to be changed to an int
L2PAYLOAD[2][3]: Layer 3 destination address // also needs to be changed to an int
L2PAYLOAD[4]: priority
L2PAYLOAD[5] and above: Layer 3 payload
*/
#include <stdio.h>
#include "queues.h"
#include <string.h>
#include <stdlib.h>
#include <time.h>
extern int L3MAXLOAD;
int numOfChecksOf[6];
void deMultiplexer(int mainQSize){
enum inputQueues { mainQ = 5, outPtQA, outPtQB, outPtQC, outPtQD, outPtQE };
struct l2Packet *deqdPakt = NULL;
int srcAdrsL3, dstAdrsL3, l2PayLoadLen, i, l3LoadLen, rndmNum;
char l3SrcAdrs[3] = {'0','0','\0'};
char l3DstAdrs[3] = {'0','0', '\0'};
char l3Payload[L3MAXLOAD];
//return after doing nothing if main queue is empty, but increment the no. of times that main queue has been checked.
if (queueSize(mainQ) == 0){
numOfChecksOf[mainQ]++;
#ifdef DEBUGDEMUL
printf("if no of checks: %d\n", numOfChecksOf[mainQ] );
display(mainQ);
#endif
return;
}
else{
numOfChecksOf[mainQ]++;
#ifdef DEBUGDEMUL
printf("num of checks: %d\n", numOfChecksOf[mainQ] );
display(mainQ);
puts("I got here in deMultiplexer");
#endif
if(queueSize(mainQ) == mainQSize){
deqdPakt = (struct l2Packet*) dequeue(mainQ); //!!!!Remember to free deqdPakt
numOfChecksOf[mainQ] = 0;
}
else if(queueSize(mainQ) < mainQSize){
if(numOfChecksOf[mainQ] == mainQSize){
deqdPakt = (struct l2Packet*) dequeue(mainQ);
numOfChecksOf[mainQ] = 0;
}
}
#ifdef DEBUGDEMUL
puts("I also got here in deMultiplexer");
#endif
/*
EXTRACT LAYER 3 PACKET:
Extract l3 packet from l2 packet, only if the de-queued packet has information in it.
l3 source and destination address are converted from characters to integers
*/
if ( deqdPakt != NULL){
l3SrcAdrs[0] = deqdPakt->l2PayLoad[0];
l3SrcAdrs[1] = deqdPakt->l2PayLoad[1];
sscanf(l3SrcAdrs, "%d", &srcAdrsL3);
l3DstAdrs[0] = deqdPakt->l2PayLoad[2];
l3DstAdrs[1] = deqdPakt->l2PayLoad[3];
dstAdrsL3 = atoi(l3DstAdrs);
//end conversion
#ifdef DEBUGDEMUL
puts("I got here as well in deMultiplexer");
#endif
/*
EXTRACT LAYER 3 PAYLOAD:
Copy the portion of layer 2's payload, which contains layer 3's payload,
into the array which stores layer 3's payload
*/
l2PayLoadLen = strlen(deqdPakt->l2PayLoad);
#ifdef DEBUGDEMUL
puts(deqdPakt->l2PayLoad);
#endif
l3LoadLen = l2PayLoadLen - 4;
char fullL3Load[l3LoadLen];
for (i = 0; i < l3LoadLen; i++){
fullL3Load[i] = deqdPakt->l2PayLoad[i + 4];
}
strcpy(l3Payload, fullL3Load);
l3Payload[L3MAXLOAD] = '\0';
// End extraction of payload
// End extraction of packet
#ifdef DEBUGDEMUL
puts("I got here in deMultiplexer, past payload extraction");
printf("%s\n", deqdPakt->l2PayLoad);
#endif
//printf("Source address: %d | destination address: %d | %s\n", srcAdrsL3, dstAdrsL3, l3Payload);
/*
Final part of function:
Enqueue packets onto correct output queue.
Using original L2 payload in case user did not enter sufficient l3payload size
*/
srand(time(NULL));
switch(dstAdrsL3){
case 1:
enqueue('R','A', deqdPakt->l2PayLoad, outPtQA);
break;
case 2:
enqueue('R','A', deqdPakt->l2PayLoad, outPtQA);
break;
case 3:
enqueue('R','A', deqdPakt->l2PayLoad, outPtQA);
break;
case 4:
enqueue('R','B', deqdPakt->l2PayLoad, outPtQB);
break;
case 5:
enqueue('R','B', deqdPakt->l2PayLoad, outPtQB);
break;
case 6:
enqueue('R','B', deqdPakt->l2PayLoad, outPtQB);
break;
case 7:
rndmNum = 1 + (rand() % 2);
if (rndmNum == 1){
enqueue('R', 'C', deqdPakt->l2PayLoad, outPtQC);
}
else{
enqueue('R', 'D', deqdPakt->l2PayLoad, outPtQD);
}
break;
case 8:
rndmNum = 1 + (rand() % 2);
if (rndmNum == 1){
enqueue('R', 'C', deqdPakt->l2PayLoad, outPtQC);
}
else{
enqueue('R', 'D', deqdPakt->l2PayLoad, outPtQD);
}
break;
case 9:
rndmNum = 1 + (rand() % 2);
if (rndmNum == 1){
enqueue('R', 'C', deqdPakt->l2PayLoad, outPtQC);
}
else{
enqueue('R', 'D', deqdPakt->l2PayLoad, outPtQD);
}
break;
case 10:
enqueue('R','D', deqdPakt->l2PayLoad, outPtQD);
break;
case 11:
enqueue('R','D', deqdPakt->l2PayLoad, outPtQD);
break;
case 12:
enqueue('R','D', deqdPakt->l2PayLoad, outPtQD);
break;
default:
{break;}
//puts("Oh boy, what a mistake you've made!");
}
}
else{ //if the dequeued packet was empty, return without doing anything
return;
}
}
}
//end deMultiplexer