Distributed Cache
server_thread_pool.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <arpa/inet.h>
4 #include <fcntl.h>
5 #include <netinet/in.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 
13 #include <mutex>
14 #include <queue>
15 #include <string>
16 #include <thread>
17 #include <vector>
18 
19 #include "server_interface.h"
20 
21 struct ThreadObject;
22 void* threadHandler(void* _pool);
23 
25  std::queue<int> connectionQueue;
26  std::mutex queueLock;
27  std::condition_variable c_cons;
28  std::condition_variable c_prod;
29  int port;
30  std::string ip;
33  std::vector<std::thread> threads;
36 
37  public:
39  int port, int threadCount = 30, int queueCapacity = 20)
40  : ip(ip),
41  port(port),
43  server(server),
45 
46  void start() {
47  socklen_t clientLen;
48 
49  struct sockaddr_in server, client;
50 
51  socketConnection = socket(AF_INET, SOCK_STREAM, 0);
52 
53  server.sin_family = AF_INET;
54  server.sin_addr.s_addr = inet_addr(ip.c_str());
55  server.sin_port = htons(port);
56 
57  {
58  int optionValue = 1;
59  setsockopt(socketConnection, SOL_SOCKET, SO_REUSEADDR, &optionValue,
60  sizeof(int));
61  }
62 
63  if (bind(socketConnection, (struct sockaddr*)&server, sizeof(server)) <
64  0) {
65  perror("bind failed");
66  throw std::runtime_error("bind failed");
67  }
68 
69  for (int i = 0; i < threadCount; i++)
70  threads.push_back(
71  std::thread(&ServerThreadPool::threadHandler, this));
72 
73  listen(socketConnection, 30);
74 
75  while (1) {
76  int nsd =
77  accept(socketConnection, (struct sockaddr*)&client, &clientLen);
78 
79  std::unique_lock lock(queueLock);
80 
81  while (connectionQueue.size() >= queueCapacity) {
82  std::cout << "Queue Full";
83  c_prod.wait(lock);
84  }
85 
86  if (connectionQueue.size() >= 5) {
87  std::cout << "Queue Size: " << connectionQueue.size()
88  << std::endl;
89  }
90 
91  connectionQueue.push(nsd);
92  lock.unlock();
93  c_cons.notify_one();
94  }
95  }
96 
97  void stop() {
98  for (int i = 0; i < threadCount; i++) {
99  // pthread_cancel(threads[i]);
100  }
101  }
102 
103  void threadHandler() {
104  while (1) {
105  std::unique_lock lock(queueLock);
106 
107  while (connectionQueue.empty()) c_cons.wait(lock);
108 
109  int connection = connectionQueue.front();
110  connectionQueue.pop();
111 
112  lock.unlock();
113  c_prod.notify_one();
114 
115  server->controller(connection);
116  close(connection);
117  }
118  }
119 };
ServerThreadPool
Definition: server_thread_pool.h:24
ServerThreadPool::threads
std::vector< std::thread > threads
Definition: server_thread_pool.h:33
ServerThreadPool::queueCapacity
int queueCapacity
Definition: server_thread_pool.h:35
MultiThreadedServerInterface::controller
virtual void controller(int nsd)=0
ServerThreadPool::threadCount
int threadCount
Definition: server_thread_pool.h:31
ServerThreadPool::socketConnection
int socketConnection
Definition: server_thread_pool.h:34
ServerThreadPool::queueLock
std::mutex queueLock
Definition: server_thread_pool.h:26
ServerThreadPool::connectionQueue
std::queue< int > connectionQueue
Definition: server_thread_pool.h:25
ServerThreadPool::ServerThreadPool
ServerThreadPool(MultiThreadedServerInterface *server, std::string ip, int port, int threadCount=30, int queueCapacity=20)
Definition: server_thread_pool.h:38
ServerThreadPool::ip
std::string ip
Definition: server_thread_pool.h:30
server_interface.h
ServerThreadPool::stop
void stop()
Definition: server_thread_pool.h:97
ServerThreadPool::c_prod
std::condition_variable c_prod
Definition: server_thread_pool.h:28
ServerThreadPool::start
void start()
Definition: server_thread_pool.h:46
threadHandler
void * threadHandler(void *_pool)
MultiThreadedServerInterface
Definition: server_interface.h:3
ServerThreadPool::threadHandler
void threadHandler()
Definition: server_thread_pool.h:103
ServerThreadPool::port
int port
Definition: server_thread_pool.h:29
ServerThreadPool::c_cons
std::condition_variable c_cons
Definition: server_thread_pool.h:27
ServerThreadPool::server
MultiThreadedServerInterface * server
Definition: server_thread_pool.h:32