-
Notifications
You must be signed in to change notification settings - Fork 0
/
demux.c
134 lines (111 loc) · 2.97 KB
/
demux.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include "vector.h"
#include "vec.c"
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 12345
void demux(char *line, Vector *reg0, Vector *reg1);
int main()
{
Vector reg0;
Vector reg1;
FILE *fPtrO0 = NULL;
FILE *fPtrO1 = NULL;
const char filePathO0[] = "out0.txt";
const char filepathO1[] = "out1.txt";
vector_setup(®0, 64, sizeof(char));
vector_setup(®1, 64, sizeof(char));
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1)
{
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(SERVER_PORT);
inet_pton(AF_INET, SERVER_IP, &(serverAddress.sin_addr));
if (connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == -1)
{
perror("Connection failed");
exit(EXIT_FAILURE);
}
fPtrO0 = fopen(filePathO0, "w");
fPtrO1 = fopen(filepathO1, "w");
/* fopen() return NULL if unable to open file in given mode. */
if (fPtrO0 == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open '%s' file.\n", filePathO0);
printf("Please check whether file exists and you have write privilege.\n");
exit(EXIT_FAILURE);
}
if (fPtrO1 == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open '%s' file.\n", filepathO1);
printf("Please check whether file exists and you have write privilege.\n");
exit(EXIT_FAILURE);
}
// Loop for fetching data
while (1)
{
const char *fetchCommand = "fetch";
if (send(clientSocket, fetchCommand, strlen(fetchCommand), 0) == -1)
{
perror("Send failed");
exit(EXIT_FAILURE);
}
char lineBuffer[64];
ssize_t bytesRead = recv(clientSocket, lineBuffer, 64, 0);
if (bytesRead == -1)
{
perror("Receive failed");
exit(EXIT_FAILURE);
}
else if (bytesRead == 0)
{
printf("Done");
break;
}
lineBuffer[bytesRead] = '\0';
demux(lineBuffer, ®0, ®1);
VECTOR_FOR_EACH(®0, i)
{
char s0 = ITERATOR_GET_AS(char, &i);
printf("0%c", s0);
fputc(s0, fPtrO0);
}
VECTOR_FOR_EACH(®1, d)
{
char s1 = ITERATOR_GET_AS(char, &d);
printf("1%c", s1);
fputc(s1, fPtrO1);
}
vector_clear(®0);
vector_clear(®1);
}
fclose(fPtrO0);
fclose(fPtrO1);
close(clientSocket);
return 0;
}
// packet: 64 bytes of muxed data
void demux(char *packet, Vector *reg0, Vector *reg1) {
int i = 0;
// For packets total length 64 bytes
// loop and assign the data byte to the correct registry
while(i <= 62) {
// the header byte is the first byte every two bytes
// [data][0|1]
if (i % 2 == 0) {
Vector *vp = *(packet[i] == '0' ? ®0 : ®1);
vector_push_back(vp, &packet[i + 1]);
}
i+=1;
}
}