-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
165 lines (147 loc) · 5.11 KB
/
server.cpp
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
163
164
165
#include<iostream>
#include<sys/socket.h>
#include<fcntl.h>
#include<arpa/inet.h>
#include<string.h>
#include<unistd.h>
#include<errno.h>
#include "server_session.h"
using namespace std;
// #define BACKLOG 4096
// #define BUF_SIZE 1024
// #define USER_DATA_PATH "./usermetadata.txt"
// #define FILES_METADATA_PATH "./filemetadata.txt"
// #define FILE_DIR_PATH "./files/"
queue<pair<int,string>> connQueue;
pthread_mutex_t queueLock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t queueWait = PTHREAD_COND_INITIALIZER;
void* processClient(void*);
int initThreadPool(vector<pthread_t>&, int, ServerSession*);
int main(int argc, char** argv) {
if(argc != 3) {
cout<<"Usage: ./server <port_no> <thread_pool_size>"<<endl;
return 1;
}
int port = atoi(argv[1]);
int threadPoolSize = atoi(argv[2]);
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = INADDR_ANY;
ServerSession* session = new ServerSession();
if (session->loadUserMetaData() < 0) {
cout << "ERROR: server->loadUserMetaData()" << endl;
}
// session->printUsers();
if (session->loadFileMetaData() < 0) {
return 1;
}
vector<pthread_t>workerThreads(threadPoolSize);
for(int i = 0; i < threadPoolSize; ++i) {
if(pthread_create(&workerThreads[i], NULL, &processClient, session) != 0) {
cerr << "Failed to create thread";
return 1;
}
}
session->startServer(port, serverAddr);
// session->startListening();
sockaddr_in clientAddr;
socklen_t clientLen = sizeof(clientAddr);
while(1) {
int connFd;
connFd = accept(session->getsockfd(), (struct sockaddr*)&clientAddr, &clientLen);
if(connFd < 0) {
if(errno == EMFILE) {
continue;
}
perror("accept connection error\n");
return 1;
}
int port = ntohs(clientAddr.sin_port);
string clientid = inet_ntoa(clientAddr.sin_addr);
clientid += ":" + to_string(port);
pthread_mutex_lock(&queueLock);
connQueue.push({connFd, clientid});
// count++;
// cout<<"Queue size: " << connQueue.size() << " req:" << count << endl;
pthread_cond_signal(&queueWait);
pthread_mutex_unlock(&queueLock);
}
return 0;
}
void* processClient(void* arg) {
ServerSession* session = (ServerSession*)arg;
while(1) {
pthread_mutex_lock(&queueLock);
while(connQueue.empty()) {
pthread_cond_wait(&queueWait, &queueLock); // wait till queue is empty
}
int connFd = connQueue.front().first;
string clientid = connQueue.front().second;
connQueue.pop();
pthread_mutex_unlock(&queueLock);
// read input commands here
char buff[BUF_SIZE] = {0};
if(read(connFd, buff, sizeof(buff)) < 0) {
cerr << "commmand read: read failed";
close(connFd);
break;
}
if(strlen(buff) == 0) {
close(connFd);
break;
}
stringstream cmdSS(buff);
vector<string> command;
string word;
while (cmdSS >> word) {
command.push_back(word);
}
if(command[0] == "login") {
User user(command[1], command[2]);
const char* resp = session->authenticateUser(user, clientid);
write(connFd, resp, strlen(resp));
} else if(command[0] == "add") {
if(command.size() != 3) {
write(connFd, "Usage: add <filename.txt>", 25);
close(connFd);
continue;
}
session->addFile(command[1], connFd, command[2]);
} else if(command[0] == "listall") {
string resp = session->listall();
write(connFd, resp.c_str(), resp.length());
} else if(command[0] == "quit") {
if(command.size() == 2)
session->quit(connFd, command[1]);
continue;
} else if(command[0] == "checkout") {
if(command.size() != 3) {
write(connFd, "Usage: checkout <filename.txt>", 30);
close(connFd);
continue;
}
session->checkout(command[1], connFd, command[2]);
} else if(command[0] == "commit") {
if(command.size() != 3) {
write(connFd, "Usage: commit <filename.txt>", 28);
close(connFd);
continue;
}
session->commit(command[1], connFd, command[2]);
} else if(command[0] == "delete") {
if(command.size() != 3) {
write(connFd, "Usage: delete <filename.txt>", 28);
close(connFd);
continue;
}
session->deleteFile(command[1], connFd, command[2]);
} else {
cout << "Enter a valid command!" << endl;
}
close(connFd);
}
cout<<"Connection with client terminated successfully"<<endl;
return NULL;
}