-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_or.c
136 lines (111 loc) · 3.39 KB
/
server_or.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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<errno.h>
#include<string.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdbool.h>
int main() {
int sock, length, n;
struct sockaddr_in serverAddress, clientAddress;
char buf[40];
bzero(buf, 40);
bool printCompFlag = true;
int numOfOrOperations = 0;
bool isFirstTransmision = true;
char sizeOfOrOperationsBuffer[10];
int numOfOrOperationsFromBuffer = 0;
bzero(sizeOfOrOperationsBuffer, 10);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock < 0) {
perror("Socket");
exit(-1);
}
length = sizeof(serverAddress);
bzero(&serverAddress, length);
// init adddress
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(21355);
inet_pton(AF_INET, "127.0.0.1", &serverAddress.sin_addr);
bzero(&serverAddress.sin_zero, 8);
if(bind(sock, (struct sockaddr *)&serverAddress, length) < 0) {
perror("Binding");
exit(-1);
}
printf("The Server OR is up and running using UDP on port 21355\n");
unsigned int fromLength = sizeof(clientAddress);
while(true) {
bzero(buf, 40);
n = recvfrom(sock, buf, 40, 0, (struct sockaddr *)&clientAddress, &fromLength);
if(n < 0) {
//perror("Received from");
} else {
// splitting string based on , here
if (printCompFlag) {
printf("The server OR started receiving lines from the edge server for OR computation. The computation results are:\n");
printCompFlag = false;
}
int j=0;
char buffer[40];
bzero(buffer, 40);
strncpy(buffer, buf, strlen(buf));
buffer[strlen(buf)] = '\0';
// checking if it's the first reception, so as to get the count
if (isFirstTransmision) {
strncpy(sizeOfOrOperationsBuffer, buffer, strlen(buffer));
numOfOrOperationsFromBuffer = atoi(sizeOfOrOperationsBuffer);
isFirstTransmision = false;
} else {
numOfOrOperations += 1;
char *token;
char *rest = buffer;
char *result;
int count = 0;
char *numOne;
char *numTwo;
int indexN;
// re-used this chunk of code from stack-overflow
while ((token = strtok_r(rest, ",", &rest))) {
if (count == 1) {
char *numOneTemp = token;
numOne = numOneTemp;
printf("%s or ",token);
} else if (count == 2) {
char *numTwoTemp = token;
numTwo = numTwoTemp;
printf("%s = ", token);
} else if (count == 3){
indexN = atoi(token);
}
count++;
}
char tempBuffer[41];
bzero(tempBuffer, 41);
int i=0;
while(i < 10) {
tempBuffer[i] = (((*numOne++)-'0') | ((*numTwo++)-'0')) + '0';
i++;
}
int firstDigit = indexN / 10;
int lastDigit = indexN % 10;
char c1 = '0' + firstDigit;
char c2 = '0' + lastDigit;
puts(tempBuffer);
tempBuffer[10] = ',';
tempBuffer[11] = c1;
tempBuffer[12] = c2;
tempBuffer[13] = '\0';
//puts(tempBuffer);
sendto(sock, tempBuffer, strlen(tempBuffer), 0, (struct sockaddr *)&clientAddress, fromLength);
// print all sent if the numOfOrOperations = sizeOfOrOperations
if (numOfOrOperations == numOfOrOperationsFromBuffer) {
printf("The Server OR has successfully received %d lines from the edge server and finished all OR computations.\n",numOfOrOperationsFromBuffer);
printf("The Server OR has successfully finished sending all computation results to the edge server.\n");
}
}
}
}
}