-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
171 lines (157 loc) · 4.94 KB
/
server.c
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
/**
* Server Implementation of the 3331 assignemnt
* author: Jabez Wilson(z5027406)
*
*
*/
#include <stdio.h>
#include "server_tcp.h"
#include "shared.h"
#include "server_methods.h"
#include "server_data.h"
#include <pthread.h>
#include <semaphore.h>
void init() {
FILE* fd = fopen("credentials.txt","r");
char user[12], pass[12];
int isUser = 1;
int uI=0,pI=0;
int i;
char c;
if(fd) {
c = getc(fd);
while(c != EOF ) {
char ch = (char)c;
c = getc(fd);
if(ch == ' ') {
isUser = 0;
user[uI] = '\0';
continue;
}
if(ch == '\n') {
isUser = 1;
pass[pI] = '\0';
printf("<%s> <%s>\n",user,pass);
add_user(user,pass);
pI = 0;
uI = 0;
continue;
}
if(isUser) {
user[uI++] = ch;
} else {
pass[pI++] = ch;
}
}
if(uI != 0 && pI != 0) {
pass[pI] = '\0';
printf("<%s> <%s>\n",user,pass);
add_user(user,pass);
}
fclose(fd);
}
}
int notLoggedIn[12];
int numInList = 0;
// mutex to make sure the notLoggedIn list is not corrupted
// only this resource is used between threads hence only one mutex is required
pthread_mutex_t not_logged_mutex;
/**
* this thread continually listens for new connections and adds them to the notLoggedIn array
*
* It is the job of the main thread to listen for login requests from the ports in the notLoggedIn List
*/
void* thread_worker(void* arg) {
while(1) {
int newSocket = waitForConnection();
pthread_mutex_lock(¬_logged_mutex);
notLoggedIn[numInList++] = newSocket;
/*printf("adding new socket to the list, current size: %d\n",numInList);*/
pthread_mutex_unlock(¬_logged_mutex);
}
}
/**
* main thread
*/
int main(int argc, char* argv[]) {
if(argc < 4) {
printf("ERROR: Usage: ./server <server_port> <block_duration> <timeout>\n");
exit(1);
}
pthread_mutex_init(¬_logged_mutex,NULL);
pthread_t pth;
int timeoutDuration = atoi(argv[3]);
long blockDuration = atol(argv[2]);
int port = atoi(argv[1]);
if(port <= 0 || blockDuration <= 0 || timeoutDuration <= 0) {
printf("given values must be numbers and larger than 0\n");
exit(1);
}
init();
setBlockDuration(blockDuration);
initialize_tcp(port);
pthread_create(&pth,NULL,thread_worker,NULL);
while(1) {
//checking the not logged in list for messages
int toBeRemoved[12] = {-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1};
int numInRm = 0;
pthread_mutex_lock(¬_logged_mutex);
//parsing the notLoggedInList, for connections that might have send a login request
for(int i=0;i<numInList;i++) {
char buffer[1024];
int retVal;
retVal = isSocketReady(notLoggedIn[i],500000);
if( retVal > 0) {
if(tryLogin(notLoggedIn[i])) {
//it worked
toBeRemoved[numInRm++] = i;
}
} else if (retVal < 0) {
perror("INTERNAL_SERVER_ERROR\n");
}
}
pthread_mutex_unlock(¬_logged_mutex);
//removing the index from notLoggedIn
if(numInRm > 0) {
//unlocking and locking again to allow the other thread to continue
pthread_mutex_lock(¬_logged_mutex);
//first set value to 0;
for(int i=0;i<numInRm;i++)
notLoggedIn[toBeRemoved[i]] = 0;
//then fill in the blanks
for(int i=0;i<numInList;i++) {
if(notLoggedIn[i] == 0) {
for(int j=i+1; j<numInList; j++) {
notLoggedIn[j-1] = notLoggedIn[j];
}
}
}
numInList -= numInRm;
pthread_mutex_unlock(¬_logged_mutex);
}
//this block will check for a request from any og the users that are online
for(int i=0;i<getNumUsers();i++) {
if(!isUserOnline(i))
continue;
int sk = getUserSocket(i);
int retVal = isSocketReady(sk,500000);
if( retVal > 0) {
//TODO: there is no way to handle SIGPIPE right now
resetLastCmd(i);
connectionHandler(sk,i);
} else if (retVal < 0) {
perror("INTERNAL_SERVER_ERROR\n");
} else {
time_t crnt;
time(&crnt);
if((crnt - getLastCmd(i)) > timeoutDuration) {
sendMsgToUser(i,"User logged out due to lack of activity");
logout(i);
close(sk);
}
}
}
usleep(10000);
}
return 0;
}