Skip to content

Commit f1e85ee

Browse files
author
lh5053
committed
First working version with Router/Manager/Worker split.
1 parent 551830a commit f1e85ee

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/Logger.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Logger::Logger(std::string path, std::string name) : LoggerBase(path) {
4040
logFileDescriptor = open(log_path.c_str(), O_WRONLY | O_APPEND);
4141

4242
if(logFileDescriptor < 0){
43-
loggerStatus = "[" + this->class_name + "]" + "Error opening logfile, disabling logging to file\nError: " + strerror(errno);
43+
loggerStatus = "[" + this->class_name + "]" + " Error opening logfile, disabling logging to file\nError: " + strerror(errno) + "\n";
4444
cLoggerStatus = loggerStatus.c_str();
4545
write(2, cLoggerStatus, strlen(cLoggerStatus));
4646
isLoggingToFileEnabled = false;
@@ -72,8 +72,8 @@ void Logger::_log(std::string msg, int level) {
7272

7373
write(1, cFullMessage, strlen(cFullMessage));
7474
if (isLoggingToFileEnabled) {
75-
write(logFileDescriptor, cFullMessage, strlen(cFullMessage));
76-
}
75+
write(logFileDescriptor, cFullMessage, strlen(cFullMessage));
76+
}
7777
mutex.unlock();
7878
}
7979
}

src/Router.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#include <sys/types.h>
22
#include <sys/socket.h>
3+
#include <errno.h>
34
#include <netdb.h>
45
#include <unistd.h>
56
#include <thread>
67
#include <chrono>
8+
#include <cstring>
9+
#include <netinet/in.h>
10+
#include <arpa/inet.h>
711

812
#include "Router.h"
913
#include "Manager.h"
@@ -19,7 +23,7 @@ Router::Router(int qsize, std::string port) {
1923
this->logger = Logger("Router");
2024
this->port = port;
2125
// initialize the socket
22-
this->init_socket();
26+
this->listening_socket_fd = this->init_socket();
2327
}
2428

2529
Router::~Router() {
@@ -29,9 +33,17 @@ Router::~Router() {
2933
freeaddrinfo(addr);
3034
}
3135

36+
void *get_in_addr(struct sockaddr *sa) {
37+
if (sa->sa_family == AF_INET) {
38+
return &(((struct sockaddr_in*)sa)->sin_addr);
39+
}
40+
41+
return &(((struct sockaddr_in6*)sa)->sin6_addr);
42+
}
43+
3244
void Router::watch() {
3345
socklen_t addr_size;
34-
struct sockaddr_storage incoming_connection_info;
46+
struct sockaddr_storage client;
3547
int handling_socket;
3648

3749
this->logger.debug("watching port: " + this->port);
@@ -44,15 +56,20 @@ void Router::watch() {
4456
// listen for new connections
4557
listen(this->listening_socket_fd, this->queue_size);
4658

47-
addr_size = sizeof incoming_connection_info;
48-
handling_socket = accept(listening_socket_fd, (struct sockaddr *) &incoming_connection_info, &addr_size);
49-
this->logger.info("Handling incoming connection from: ");
59+
addr_size = sizeof client;
60+
handling_socket = accept(this->listening_socket_fd, (struct sockaddr *) &client, &addr_size);
61+
if (handling_socket < 0) {
62+
char * err = std::strerror(errno);
63+
throw RouterException("Error when accepting connection: " + std::string(err ? err : "unknown error"));
64+
}
65+
66+
struct sockaddr_in *sin = (struct sockaddr_in *)&client;
67+
char client_addr[INET6_ADDRSTRLEN];
68+
inet_ntop(AF_INET, &sin->sin_addr, client_addr, sizeof client_addr);
69+
this->logger.info("Handling incoming connection from: " + std::string(client_addr));
5070
// this will create a new worker to work with
51-
// TODO: handle too many workers exception
71+
// TODO: add try/catch and handle too many workers exception
5272
manager.handle_request(handling_socket);
53-
54-
// sleep a little
55-
std::this_thread::sleep_for (std::chrono::seconds(1));
5673
}
5774
}
5875

@@ -70,9 +87,18 @@ int Router::init_socket() {
7087

7188
// TODO error handling (what if we cannot open the socket)
7289
int listening_socket = socket(this->addr->ai_family, this->addr->ai_socktype, this->addr->ai_protocol);
90+
if (listening_socket < 0) {
91+
char * err = std::strerror(errno);
92+
throw RouterException("Error opening socket: " + std::string(err ? err : "unknown error"));
93+
}
7394

7495
// TODO error handling (what if we can't bind)
75-
bind(listening_socket, this->addr->ai_addr, this->addr->ai_addrlen);
96+
int check = bind(listening_socket, this->addr->ai_addr, this->addr->ai_addrlen);
97+
if (check < 0) {
98+
char * err = std::strerror(errno);
99+
throw RouterException("Error binding socket: " + std::string(err ? err : "unknown error"));
100+
}
76101

102+
this->logger.debug("Got socket: " + std::to_string(listening_socket));
77103
return listening_socket;
78104
}

src/Router.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,19 @@ class Router {
2020
void watch();
2121
};
2222

23+
using namespace std;
24+
// Possibly we could also add a stack trace here:
25+
// http://stackoverflow.com/questions/353180/how-do-i-find-the-name-of-the-calling-function
26+
class RouterException: public exception {
27+
std::string reason;
28+
29+
public:
30+
RouterException(std::string msg = "Unknown exception") {
31+
reason = msg;
32+
}
33+
virtual const char* what() const throw() {
34+
return reason.c_str();
35+
}
36+
};
37+
2338
#endif /* SRC_ROUTER_H_ */

0 commit comments

Comments
 (0)