-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_session.cpp
266 lines (232 loc) · 7.8 KB
/
server_session.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "server_session.h"
namespace fs = std::filesystem;
ServerSession::ServerSession() {
sessionLock = PTHREAD_MUTEX_INITIALIZER;
sessionWait = PTHREAD_COND_INITIALIZER;
}
ServerSession:: ~ServerSession() {
pthread_mutex_destroy(&sessionLock);
pthread_cond_destroy(&sessionWait);
users.clear();
activeUsers.clear();
filesMap.clear();
}
int ServerSession:: getsockfd() {
return sockfd;
}
int ServerSession::loadFileMetaData() {
try {
for (const auto &entry : fs::directory_iterator(FILE_DIR_PATH)) {
FileMetaData metadata;
metadata.filename = entry.path().filename();
metadata.path = entry.path();
filesMap[metadata.filename] = metadata;
}
} catch (const std::filesystem::filesystem_error &e) {
std::cerr << "Error: " << e.what() << std::endl;
return -1;
}
return 0;
}
int ServerSession::loadUserMetaData() {
ifstream file(USER_DATA_PATH);
if(!file.is_open()) {
cout << "file open error" << endl;
return -1;
}
string line;
while(getline(file, line)) {
stringstream ss(line);
User user;
ss >> user.username >> user.password;
users[user.username] = user;
}
return 0;
}
int ServerSession::startServer(int port, sockaddr_in serverAddr) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0) {
perror("socket creation failed\n");
return -1;
}
if(bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
perror("Bind failed\n");
return -1;
}
if((listen(sockfd, BACKLOG)) < 0) {
perror("listen failed\n");
return -1;
}
cout<<"Server started successfully"<<endl;
return 0;
}
const char* ServerSession::authenticateUser(User user, string clientid) {
pthread_mutex_lock(&sessionLock);
if(users.find(user.username) == users.end()) {
ofstream file;
file.open(USER_DATA_PATH, ios_base::app);
file << user.username << " " << user.password << " " << endl;
users[user.username] = user;
}
//TODO: add password validation also lol!
if(activeUsers.find(user.username) != activeUsers.end()) {
pthread_mutex_unlock(&sessionLock);
return "User with given username already logged in!";
}
if(users[user.username].password != user.password) {
pthread_mutex_unlock(&sessionLock);
return "Invalid password!";
}
activeUsers[user.username] = user;
pthread_mutex_unlock(&sessionLock);
return "OK";
}
void ServerSession:: checkout(string filename, int connFd, string username) {
pthread_mutex_lock(&sessionLock);
if(filesMap.find(filename) == filesMap.end()) {
write(connFd, "Given filename does not exists!", 29);
pthread_mutex_unlock(&sessionLock);
return;
}
FileMetaData fileMetaData = filesMap[filename];
// check if someone has already checked out file
if(fileMetaData.hasWriteLock) {
write(connFd, "Write lock already exists on the file!", 38);
pthread_mutex_unlock(&sessionLock);
return;
}
else
write(connFd, "OK\n", 3);
User user = activeUsers[username];
if(!user.checkedoutFiles.count(filename)) {
fileMetaData.currentReaders++;
user.checkedoutFiles.insert(filename);
activeUsers[username] = user;
}
filesMap[filename] = fileMetaData;
pthread_mutex_unlock(&sessionLock);
int fd = open(fileMetaData.path.c_str(), O_RDONLY);
char buff[BUF_SIZE] = {0};
while(1) {
int bytesread = read(fd, buff, BUF_SIZE-1);
if(bytesread <= 0)
break;
buff[bytesread] = '\0';
write(connFd, buff, bytesread);
}
}
void ServerSession:: commit(string filename, int connFd, string username) {
pthread_mutex_lock(&sessionLock);
if(filesMap.find(filename) == filesMap.end()) {
write(connFd, "Given filename does not exists!", 29);
pthread_mutex_unlock(&sessionLock);
return;
}
FileMetaData fileMetaData = filesMap[filename];
if(fileMetaData.hasWriteLock) {
if(!(fileMetaData.currentReaders == 1 && username == fileMetaData.whoHasWriteLock)) {
write(connFd, "Write lock already exists on the file! Try running checkout after some time.", 76);
pthread_mutex_unlock(&sessionLock);
return;
}
}
write(connFd, "OK", 2);
fileMetaData.hasWriteLock = true;
fileMetaData.whoHasWriteLock = username;
filesMap[filename] = fileMetaData;
pthread_mutex_unlock(&sessionLock);
pthread_mutex_lock(&fileMetaData.fileMutex);
string respStr = "";
char resp[BUF_SIZE] = {0};
while (true) {
int readSz = 0;
if ((readSz = read(connFd, resp, BUF_SIZE-1)) < 0) {
perror("commit: read failed");
break;
}
// resp[readSz] = '\0';
if (readSz == 0) {
break;
}
for (int i = 0; i < readSz; ++i) {
respStr += resp[i];
}
// cout << "readSz: " << readSz << ", resp: " << resp << endl;
}
ofstream file;
file.open(fileMetaData.path);
file << respStr;
pthread_mutex_unlock(&fileMetaData.fileMutex);
pthread_mutex_lock(&sessionLock);
fileMetaData.hasWriteLock = false;
fileMetaData.whoHasWriteLock = "";
filesMap[filename] = fileMetaData;
pthread_mutex_unlock(&sessionLock);
}
void ServerSession::addFile(string filename, int connFd, string username) {
pthread_mutex_lock(&sessionLock);
if(filesMap.find(filename) != filesMap.end()) {
write(connFd, "File with given name already exists!", 36);
pthread_mutex_unlock(&sessionLock);
return;
}
FileMetaData fileMetaData;
fileMetaData.filename = filename;
fileMetaData.path = FILE_DIR_PATH + filename;
fileMetaData.lastModified = time(nullptr); // unix epoch timestamp
filesMap[filename] = fileMetaData;
pthread_mutex_unlock(&sessionLock);
commit(filename, connFd, username);
}
void ServerSession::deleteFile(string filename, int connFd, string username) {
pthread_mutex_lock(&sessionLock);
if(filesMap.find(filename) == filesMap.end()) {
write(connFd, "Given filename does not exists!", 29);
pthread_mutex_unlock(&sessionLock);
return;
}
FileMetaData fileMetaData = filesMap[filename];
if(remove(fileMetaData.path.c_str()) == 0) {
write(connFd, "OK", 2);
filesMap.erase(filename);
}
else
write(connFd, "Err: Unable to delete file", 26);
pthread_mutex_unlock(&sessionLock);
}
string ServerSession:: listall() {
unordered_map<string, FileMetaData>:: iterator it;
string resp = "";
for (it = filesMap.begin(); it != filesMap.end(); ++it) {
resp += "filename: " + it->first + ", current readers: " + to_string(it->second.currentReaders) + "\n";
}
return resp;
}
void ServerSession::printUsers() {
int i = 1;
for (auto user : users) {
cout << "User " << i++ << "-> username: " << user.first
<< ", password: " << user.second.password << endl;
}
}
void ServerSession::printFileMetaData() {
for (auto file : filesMap) {
cout << "filename " << file.second.filename << endl;
}
}
void ServerSession::quit(int connFd, string username) {
pthread_mutex_lock(&sessionLock);
User user = activeUsers[username];
for(string filename : user.checkedoutFiles) {
FileMetaData metadata = filesMap[filename];
metadata.currentReaders--;
if(metadata.whoHasWriteLock == user.username) {
metadata.hasWriteLock = false;
metadata.whoHasWriteLock = "";
}
filesMap[filename] = metadata;
}
close(connFd);
activeUsers.erase(username);
pthread_mutex_unlock(&sessionLock);
}