-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikipedia_server.cpp
More file actions
216 lines (184 loc) · 5.42 KB
/
wikipedia_server.cpp
File metadata and controls
216 lines (184 loc) · 5.42 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
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
207
208
209
210
211
212
213
214
215
216
#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <optional>
#include <cctype>
#include "max_block_decomposition.cpp"
using namespace std;
string t;
string lower(string s) {
string ans = "";
for (char c : s) {
ans += tolower(c);
}
return ans;
}
//error values
//0 = closed
//-1 = socket error
//1 = ok
string buffer;
string readUntilDelimiter(int sockfd, char delimiter, int *error) {
char temp[1024];
while (true) {
// Check for delimiter in buffer
size_t pos = buffer.find(delimiter);
if (pos != string::npos) {
string result = buffer.substr(0, pos);
buffer = buffer.substr(pos + 1); // Remove processed part
*error = 1;
return result;
}
// Read from socket
ssize_t bytesRead = recv(sockfd, temp, sizeof(temp), 0);
if (bytesRead < 0) {
// Error
*error = bytesRead;
return "";
} else if (bytesRead == 0) {
// Connection closed
if (!buffer.empty()) {
std::string result = buffer;
buffer.clear();
return result;
}
*error = 0;
return "";
}
buffer.append(temp, bytesRead);
}
}
struct SearchResult {
string title;
Slice match;
bool operator<(SearchResult other) {
return title.size() < other.title.size();
}
};
SearchResult get_result(Slice t_slice) {
long title_begin = t_slice.start;
while (title_begin > 0 && t[title_begin-1] != '\n') {
title_begin--;
}
long title_end = t_slice.end;
while (title_end < t.size()-1 && t[title_end+1] != '\n') {
title_end++;
}
return {Slice{title_begin, title_end}.apply(t), {t_slice.start - title_begin, t_slice.end - title_begin}};
}
string answer_query(string diff, MaxBlockDecomposition &mbd) {
diff = lower(diff);
auto iter = diff.begin();
auto next = [&]() {
return *(iter++);
};
while (iter != diff.end()) {
char op = next();
if (op == 'r') {
string index = "";
while (*iter >= '0' && *iter <= '9') {
index += next();
}
char replacement = next();
mbd.replace(atol(index.c_str()), replacement);
} else if (op == '+') {
mbd.append(next());
} else if (op == '-') {
mbd.unappend();
} else {
cout << "BAD OP: " + op << endl;
return "";
}
}
Slice lcs = mbd.get_lcs();
vector<Slice> t_matches = mbd.get_t_matches(lcs, 20);
vector<SearchResult> results(t_matches.size());
for (int i = 0; i < t_matches.size(); i++) {
results[i] = get_result(t_matches[i]);
}
sort(results.begin(), results.end());
string reply = "";
reply += to_string(results.size()) + '\n';
for (auto result : results) {
reply += result.title + '\n' + to_string(result.match.start) + '\n' + to_string(result.match.end) + '\n';
}
return reply + '\n';
}
int main() {
ifstream infile("titles.txt"); // Open the file for reading
if (!infile) {
cerr << "Error: Could not open file 'titles.txt'" << std::endl;
return 1;
}
string line;
int limit = -1;
while (getline(infile, line)) {
for (char &c : line) {
if (c == '_') {
c = ' ';
}
}
t += line + '\n';
if (!limit--) {
break;
}
}
cout << "construction started" << '\n';
MaxBlockDecomposition mbd("", lower(t), true);
cout << "constructed!" << '\n';
const int PORT = 19921;
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("socket");
return 1;
}
sockaddr_in server_addr{};
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); // 127.0.0.1
if (bind(server_fd, (sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
return 1;
}
if (listen(server_fd, 1) == -1) {
perror("listen");
return 1;
}
std::cout << "Server listening on port " << PORT << std::endl;
while (true) {
int client_fd = accept(server_fd, nullptr, nullptr);
if (client_fd == -1) {
perror("accept");
continue;
}
cout << "new connection, clearing search\n";
while (mbd.s.size() > 0) {
mbd.unappend();
}
while (true) {
int error;
string result = readUntilDelimiter(client_fd, '\n', &error);
if (error != 1) {
cout << "closing on read... with error " << error << '\n';
close(client_fd);
buffer = "";
} else {
cout << "got message: " << result << endl;
}
string answer = answer_query(result, mbd);
cout << "s: " << mbd.s << '\n';
cout << "LCS len: " << mbd.get_lcs().size() << '\n';
cout << "LCS: " << mbd.get_lcs().apply(mbd.s) << '\n';
int sent = send(client_fd, answer.c_str(), answer.size(), 0);
if (sent == -1) {
close(client_fd);
break;
}
}
}
close(server_fd);
return 0;
}