-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
162 lines (132 loc) · 4.04 KB
/
server.cpp
File metadata and controls
162 lines (132 loc) · 4.04 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
#include <atomic>
#include <iostream>
#include <string>
#include <sys/epoll.h>
#include <unistd.h>
#include <thread>
#include "message.pb.h"
#include "utils.h"
std::atomic<int64_t> number{0};
using namespace std;
// #define MAX_EVENTS 5
// struct epoll_event event, events[MAX_EVENTS];
// int epoll_fd = epoll_create1(0), event_count;
int handle_connection(int sockfd);
// Class for using threads, commeted all threads out
class thread_obj
{
public:
void operator()(int sockfd)
{
// printf("Thread for socket %d is handling connection\n\n", sockfd);
handle_connection(sockfd);
}
};
int main(int args, char *argv[])
{
if (args < 3)
{
std::cerr << "usage: ./server <numThreads> <port>\n";
exit(1);
}
int numThreads = std::atoi(argv[1]);
int port = std::atoi(argv[2]);
// printf("---------Input--------\n numThreads: %d\n port: %d\n-------Input End------\n\n",
// numThreads, port);
number.store(0);
int server_socket = 0;
/* if (epoll_fd == -1)
{
printf("Failed to create epoll file descriptor\n");
return 1;
} */
// Set server socket listening
if ((server_socket = listening_socket(port)) < 0)
{
printf("listen() failed!\n");
return 0;
}
else
{
// printf("Socket %d is listening on %d\n\n", server_socket, port);
}
thread threads[numThreads]; // spawn numThreads
// event.events = EPOLLIN;
// event.data.fd = 0;
int sockfd;
// int threadCounter = 0;
// "Long lasting" loop for accepting and handling connections
while (true) // for (int i = 0; i < 1; i++)
{
// printf("Waiting for another connection...\n");
if ((sockfd = accept_connection(server_socket)) < 0)
{
// printf("accept_connect() failed!\n");
}
else
{
// printf("Socket %d is connected!\n\n", sockfd);
}
// if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sockfd, &event))
// printf("Failed to add Sockfd to epoll!\n");
// else
// printf("Sockfd is added to epoll! \n");
handle_connection(sockfd);
/*
printf("Thread %d is starting:\n", threadCounter + 1);
threads[threadCounter] = thread(thread_obj(), sockfd);
[threadCounter].join();
sockfd = server_socket;
threadCounter = (threadCounter + 1) % numThreads;
*/
}
// if (close(epoll_fd))
// printf("Failed to close epoll file descriptor\n");
close_socket(server_socket);
return 0;
}
// helper method for handling a socket connection
int handle_connection(int sockfd)
{
int32_t operation_type = -1;
int64_t argument = -1;
// printf("\nPolling for input...\n");
// event_count = epoll_wait(epoll_fd, events, MAX_EVENTS, 60000);
// printf("%d ready events\n\n", event_count);
int last_operation = -1;
// Loop untill a termination is received
while (last_operation != 3) // for (int i = 0; i < 3; i++)
{
if (recv_msg(sockfd, &operation_type, &argument) == 0)
{
// printf("Server received: %d, %lld\n\n",
// operation_type, (long long)argument);
// For exiting the loop
last_operation = operation_type;
switch (last_operation)
{
case 1:
number.fetch_add(argument);
break;
case 2:
number.fetch_sub(argument);
break;
case 3:
argument = number.load();
send_msg(sockfd, 4, argument);
// printf("Global Counter is: %lld and %lld should have been added\n\n",
// (long long)number.load(), (long long)argument);
printf("%lld\n", (long long)argument);
fflush(NULL);
break;
}
}
else
{
// printf("receive message failed in handle_connection!\n");
return 1;
}
}
close_socket(sockfd);
return 0;
}