-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
89 lines (77 loc) · 3.46 KB
/
main.cpp
File metadata and controls
89 lines (77 loc) · 3.46 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
#include <iostream>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "CommandProcessor.h"
#include "keystore.h"
#include "walogger.h"
// TODO explore not hardcoding this
constexpr int PORT = 9001;
constexpr int MAX_CONNECTIONS = 5;
std::string_view trim(std::string_view input_string_view) {
const auto first = input_string_view.find_first_not_of(" \t\n\r");
if (first == std::string_view::npos) return "";
const auto last = input_string_view.find_last_not_of(" \t\n\r");
return input_string_view.substr(first, (last - first + 1));
}
int main() {
std::cout << "Starting momoDB" << std::endl;
WaLogger logger = WaLogger();
logger.loadLogFile("./momodb.log");
keystore keystore(logger);
keystore.rebuildFromLog();
momodb::CommandProcessor command_processor(logger, keystore);
const int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
std::cerr << "Could not create socket" << std::endl;
return 1;
}
std::cout << "Socket created successfully: " << server_fd << std::endl;
sockaddr_in address{.sin_family = AF_INET, .sin_port = htons(PORT)}; // host to network short
address.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(server_fd, reinterpret_cast<sockaddr *>(&address), sizeof(address)) == -1) {
std::cerr << "Could not bind address " << address.sin_addr.s_addr << ":" << ntohs(address.sin_port) <<
std::endl;
}
std::cout << "Binding successfull to port " << ntohs(address.sin_port) << std::endl;
if (listen(server_fd, MAX_CONNECTIONS) == -1) {
std::cerr << "Could not listen on socket" << std::endl;
}
std::cout << "Listening on port " << ntohs(address.sin_port) << std::endl;
try {
while (true) {
int address_len = sizeof(address);
const int client_socket = accept(server_fd, reinterpret_cast<sockaddr *>(&address),
reinterpret_cast<socklen_t *>(&address_len));
if (client_socket == -1) {
std::cerr << "Could not accept connection" << std::endl;
continue;
}
char buffer[1024] = {};
const size_t chars_read = read(client_socket, buffer, 1024);
buffer[chars_read] = '\0'; // adding null just to be sure we have a delimiter
if (chars_read > 0) {
std::string_view untrimmed_view{buffer};
auto view = trim(untrimmed_view);
// TODO make this appear only in debug mode
std::cout << "Received message: <" << view << "> of length " << view.length() << std::endl;
// command processing
// TODO check if exit should possibly check for other open sockets as well
auto execution_success = command_processor.parse_and_execute_command(view);
send(client_socket, execution_success.data(), execution_success.length(), 0);
if (execution_success == "EXIT") {
std::cout << "Received exit command; shutting down." << std::endl;
close(client_socket);
break;
}
} else {
std::cerr << "Connection closed" << std::endl;
}
close(client_socket);
}
} catch (std::exception &e) {
std::cerr << "Exception occurred!" << std::endl;
std::cerr << e.what() << std::endl;
}
return 0;
}