-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.c
206 lines (168 loc) · 4.78 KB
/
mux.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
203
204
205
206
// multiplexer.c
// Simple multiplexer 2 to 1
// - Control bit gets controlled by weights that are allocated on sources
// - Implementation uses static sources, static packages
// - You can test the implementation by doing a telnet 127.0.0.1 12345
// -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <time.h>
#include <stdbool.h>
#include "vec.c"
#include "vector.h"
#include "data.h"
#define PORT 12345
#define MAX_BUFFER 64
#define BIG_PACKET_SIZE 640
#define SOURCE_0_PACKAGES 4
#define SOURCE_1_PACKAGES 2
typedef struct
{
char identifier;
char *data;
int consumed_packets;
int remaning_packets;
double ratio;
} source;
double randomDouble() {
return (double)rand() / RAND_MAX;
}
source* bitToggle(source *s1, source *s2) {
double ratio1 = s1->ratio;
double ratio2 = s2->ratio;
int bit = 0;
if (s1->remaning_packets > 0 && s2->remaning_packets > 0) {
double randomValue = randomDouble();
if (randomValue > ratio1 / (ratio1 + ratio2)) {
bit = 1;
}
} else {
if (s2->remaning_packets > 0) {
bit = 1;
}
}
return bit == 0 ? s1 : s2;
}
void initDataPackages(source *s, int big_packets_numbers) {
s->data = malloc(big_packets_numbers * BIG_PACKET_SIZE);
s->remaning_packets = BIG_PACKET_SIZE * big_packets_numbers;
for (int i = 0; i < big_packets_numbers; i++) {
memcpy(s->data + (i * BIG_PACKET_SIZE), byteArray, BIG_PACKET_SIZE);
}
}
// This is the multiplexer
void multiplexer(source *s1, source *s2, Vector *line, int space) {
int s1_count = s1->remaning_packets;
int s2_count = s2->remaning_packets;
bool packets = (s1_count > 0 || s2_count > 0);
if (s1_count == 0 && s2_count == 0) {
return;
}
int i=0;
while (i < space && packets)
{
source *s = bitToggle(s1, s2);
vector_push_back(line, &s->identifier);
vector_push_back(line, &s->data[s->consumed_packets]);
s->consumed_packets += 1;
s->remaning_packets -= 1;
i++;
}
}
void trim(char *str) {
int start = 0, end = strlen(str) - 1;
while (isspace((unsigned char)str[start])) {
start++;
}
while ((end > start) && isspace((unsigned char)str[end])) {
end--;
}
str[end + 1] = '\0';
}
int main()
{
srand(time(NULL));
Vector line;
vector_setup(&line, 64, sizeof(char));
source source_0 = {
.identifier = '0',
.consumed_packets = 0,
.ratio = 1.0};
source source_1 = {
.identifier = '1',
.consumed_packets = 0,
.ratio = 1.0};
initDataPackages(&source_0, SOURCE_0_PACKAGES);
initDataPackages(&source_1, SOURCE_1_PACKAGES);
char outputBuffer[MAX_BUFFER];
// unix sockets
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
int enable = 1;
if (setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
perror("setsockopt(SO_REUSEADDR) failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
if (listen(serverSocket, 3) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Waiting for connections...\n");
int i=0;
while (1)
{
int clientSocket = accept(serverSocket, NULL, NULL);
if (clientSocket < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
char command[MAX_BUFFER];
ssize_t bytesRead;
while ((bytesRead = recv(clientSocket, command, sizeof(command), 0)) > 0) {
command[bytesRead] = '\0';
trim(command);
if (strcasecmp(command, "fetch") == 0) {
if (source_0.remaning_packets == 0 &&
source_1.remaning_packets == 0) {
send(clientSocket, NULL, 0, 0);
close(clientSocket);
} else {
// multiplex data of 32 bytes + 32 identifier bytes
multiplexer(&source_0, &source_1, &line, 32);
// send the packet
}
send(clientSocket, line.data, strlen(line.data), 0);
vector_clear(&line);
} else if (strcasecmp(command, "quit") == 0) {
send(clientSocket, "Bye\n", strlen("Bye\n"), 0);
close(clientSocket);
} else if (strcasecmp(command, "help") == 0) {
strcpy(outputBuffer, "Commands: fetch | quit | help\n");
send(clientSocket, outputBuffer, strlen(outputBuffer), 0);
} else {
send(clientSocket, "Invalid command\n", strlen("Invalid command\n"), 0);
}
}
break;
}
printf("%d", i);
free(source_0.data);
free(source_1.data);
close(serverSocket);
return 0;
}