-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.cpp
More file actions
149 lines (123 loc) · 3.95 KB
/
Copy pathsocket.cpp
File metadata and controls
149 lines (123 loc) · 3.95 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
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
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <vector>
#include <sstream>
struct Demand {
std::string customer_id;
int quantity;
int post_day;
int start_delivery_day;
int end_delivery_day;
};
std::vector<Demand> demands;
std::vector<Demand> parseDemands(const std::string& encoded) {
std::vector<Demand> demands;
std::istringstream iss(encoded);
int num_demands;
std::string demand_data;
if (!(iss >> num_demands)) {
std::cerr << "Error reading number of demands." << std::endl;
return demands;
}
iss.ignore(1);
std::getline(iss, demand_data);
// Split the demand data using '@' as the delimiter
std::istringstream demand_stream(demand_data);
std::string demand_entry;
while (std::getline(demand_stream, demand_entry, '@')) {
// Trim leading and trailing spaces from demand_entry
size_t start = demand_entry.find_first_not_of(" ");
size_t end = demand_entry.find_last_not_of(" ");
if (start == std::string::npos || end == std::string::npos) continue; // Skip empty entries
demand_entry = demand_entry.substr(start, end - start + 1);
// Split by ',' to get the individual fields
std::istringstream field_stream(demand_entry);
std::string field;
Demand demand;
int field_index = 0;
while (std::getline(field_stream, field, ',')) {
switch (field_index) {
case 0:
demand.customer_id = field; // CUST_ID
break;
case 1:
demand.quantity = std::stoi(field); // AMOUNT
break;
case 2:
demand.post_day = std::stoi(field); // POST_DAY
break;
case 3:
demand.start_delivery_day = std::stoi(field); // START_DAY
break;
case 4:
demand.end_delivery_day = std::stoi(field); // END_DAY
break;
default:
break;
}
++field_index;
}
demands.push_back(demand);
}
return demands;
}
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
const int PORT = 12345;
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
std::cout << "Server is listening on port " << PORT << std::endl;
while (true) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
std::cout << "New connection accepted." << std::endl;
char buffer[4096];
memset(buffer, 0, sizeof(buffer));
ssize_t bytes = recv(new_socket, buffer, 4096, 0);
if (bytes == -1) {
std::cerr << "Error in recv(). Quitting" << std::endl;
close(new_socket);
break;
}
buffer[bytes] = '\0';
if(strncmp(buffer, "move", 4) == 0)
{
//Run algo get moves
}
else if(strncmp(buffer, "quit", 4) == 0)
{
close(new_socket);
break;
}
else
{
demands = parseDemands(buffer);
}
}
return 0;
}