-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_or.cpp
More file actions
66 lines (54 loc) · 2.1 KB
/
server_or.cpp
File metadata and controls
66 lines (54 loc) · 2.1 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
#include "server_or.h"
//Our friend the trim fn from both client and edge
string trim0s(string in){
int i = in.find_first_of("1");
if(i != string::npos) return in.substr(i);
else return "0";
}
int main(){
//make our buffer,
char buf[1000];
//make sure hints is clear, and then set the family as "I don't care" and the
//socket as a datagram socket.
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
//Get the info!
status = getaddrinfo(myHost, myPort, &hints, &info);
cout << "The OR server is up and has booted with status: " << status << endl;
cout << "Running on port: " << myPort << endl;
//Use the info! (Bind the listening socket.)
sock = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
bind(sock, info->ai_addr, info->ai_addrlen);
freeaddrinfo(info);
while(1){
//their (the edge's) address.
socklen_t addr_len = sizeof their_addr;
cout << "Wating for next job." << endl;
//if the recieve is valid put it in the buffer...
if(recvfrom(sock, buf, sizeof buf, 0, (struct sockaddr *) &their_addr, &addr_len) == -1){
cout << "Recvfrom error" << endl;
exit(1);
}
//... save it in a string. Then,
cout << "Recieved job! " << endl;
string resString(buf);
//if the string isn't the exit string (Not actually implemented...)
if(strcmp(resString.c_str(), "EXIT") == 0){
close(sock);
exit(0);
}
//do the maths
string a = resString.substr(0, 10);
string b = resString.substr(10);
bitset<10> result = bitset<10>(resString.substr(0, 10)) | bitset<10>(resString.substr(10));
//spit out the result for the user, and handle the data shuffling
cout << "Result: " << trim0s(a) << " or " << trim0s(b) << " = " << trim0s(result.to_string()) << endl;
memset(&buf, 0, sizeof buf);
strcpy(buf, result.to_string().c_str());
//then send it back! ☜(⌒▽⌒)☞
sendto(sock, buf, sizeof buf, 0, (struct sockaddr *)&their_addr, sizeof(their_addr));
cout << "Successfully sent result back to sender." << endl;
}
return 0;
}