SearchSECODatabaseAPI
ConnectionHandler.h
1/*
2This program has been developed by students from the bachelor Computer Science at
3Utrecht University within the Software Project course.
4© Copyright Utrecht University (Department of Information and Computing Sciences)
5*/
6
7#pragma once
8#include "RequestHandler.h"
9#include "RAFTConsensus.h"
10#include "Statistics.h"
11
12#include <boost/shared_ptr.hpp>
13#include <boost/enable_shared_from_this.hpp>
14#include <boost/asio.hpp>
15
16#define PORT 8003
17#define CONNECTION_TIMEOUT 10000000 // Timeout in microseconds.
18
19using boost::asio::ip::tcp;
20
21class TcpServer;
22
27{
28public:
41 void startListen(DatabaseHandler* databaseHandler,
42 DatabaseConnection* databaseConnection,
43 RAFTConsensus *raft,
44 Statistics *stats,
45 int port = PORT,
46 RequestHandler *handler = nullptr);
47
48 RequestHandler* getRequestHandler() { return handler; };
49private:
50 RequestHandler* handler;
51 TcpServer* server;
52};
53
55 : public boost::enable_shared_from_this<TcpConnection>
56{
57public:
58 typedef boost::shared_ptr<TcpConnection> pointer;
59
63 static pointer create(boost::asio::io_context& ioContext);
64
65 tcp::socket& socket()
66 {
67 return socket_;
68 }
69
73 virtual void sendData(const std::string &data, boost::system::error_code &error);
74
78 virtual void start(RequestHandler *handler, pointer thisPointer, Statistics *stats);
79
80protected:
85 TcpConnection(boost::asio::io_context& ioContext)
86 : socket_(ioContext)
87 {
88 }
89
90private:
94 void readExpectedData(int &size, std::vector<char> &data, std::string &totalData,
95 boost::system::error_code &error);
96
97
98 tcp::socket socket_;
99 std::string message_;
100};
101
103{
104public:
105 TcpServer(boost::asio::io_context &ioContext, DatabaseHandler *databaseHandler,
106 DatabaseConnection *databaseConnection, RAFTConsensus *raft, RequestHandler *handler, int port,
107 Statistics *stats);
108
112 void stop();
113
114private:
118 void startAccept();
119
124 void handleAccept(TcpConnection::pointer newConnection, const boost::system::error_code& error);
125
126 boost::asio::io_context& ioContext_;
127 tcp::acceptor acceptor_;
128 RequestHandler* handler;
129 Statistics *stats;
130
131 bool stopped = false;
132};
Handles connections with database.
Definition: ConnectionHandler.h:27
void startListen(DatabaseHandler *databaseHandler, DatabaseConnection *databaseConnection, RAFTConsensus *raft, Statistics *stats, int port=PORT, RequestHandler *handler=nullptr)
Starts listening for requests. Takes in a pointer to the database handler.
Definition: ConnectionHandler.cpp:23
Handles interaction with database when dealing with job requests.
Definition: DatabaseConnection.h:25
Handles interaction with database.
Definition: DatabaseHandler.h:23
Definition: RAFTConsensus.h:41
Definition: RequestHandler.h:43
Stores the Prometheus statistics variables.
Definition: Statistics.h:35
Definition: ConnectionHandler.h:56
static pointer create(boost::asio::io_context &ioContext)
Creates the pointer to the tcp connection. Takes in the io context.
Definition: ConnectionHandler.cpp:51
TcpConnection(boost::asio::io_context &ioContext)
Constructor. Not public because you need to use the create method. Not private because we need this c...
Definition: ConnectionHandler.h:85
virtual void start(RequestHandler *handler, pointer thisPointer, Statistics *stats)
Starts the handeling of a request. Takes in the request handler to call.
Definition: ConnectionHandler.cpp:61
virtual void sendData(const std::string &data, boost::system::error_code &error)
Sends the given data to the other side of the connection.
Definition: ConnectionHandler.cpp:56
Definition: ConnectionHandler.h:103
void stop()
Stops the server.
Definition: ConnectionHandler.cpp:167