-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
206 lines (179 loc) · 5.55 KB
/
server.c
File metadata and controls
206 lines (179 loc) · 5.55 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
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
#include "helper.h"
#define PORT 8001
UserInfo *users[MAX_CONNECTIONS] = {NULL};
int currentConnections = 0;
pthread_mutex_t globalMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t connectionCond = PTHREAD_COND_INITIALIZER;
// Helper function to get or initialize UserInfo
UserInfo *getUserInfo(const char *userName)
{
pthread_mutex_lock(&globalMutex);
for (int i = 0; i < MAX_CONNECTIONS; i++)
{
if (users[i] && strcmp(users[i]->userName, userName) == 0)
{
pthread_mutex_unlock(&globalMutex);
return users[i];
}
}
for (int i = 0; i < MAX_CONNECTIONS; i++)
{
if (users[i] == NULL)
{
users[i] = (UserInfo *)my_malloc(sizeof(UserInfo));
/*
if (!users[i]) {
perror("malloc failed");
pthread_mutex_unlock(&globalMutex);
return NULL;
} */
strncpy(users[i]->userName, userName, sizeof(users[i]->userName));
users[i]->readCount = 0;
users[i]->isWriting = 0;
users[i]->queueFront = users[i]->queueBack = 0;
pthread_mutex_init(&users[i]->mutex, NULL);
pthread_cond_init(&users[i]->queueCond, NULL);
pthread_mutex_unlock(&globalMutex);
return users[i];
}
}
pthread_mutex_unlock(&globalMutex);
return NULL;
}
void enqueueRequest(UserInfo *user, Request *request)
{
user->requestQueue[user->queueBack] = request;
user->queueBack = (user->queueBack + 1) % MAX_CONNECTIONS;
}
Request *dequeueRequest(UserInfo *user)
{
if (user->queueFront == user->queueBack)
return NULL;
Request *request = user->requestQueue[user->queueFront];
user->queueFront = (user->queueFront + 1) % MAX_CONNECTIONS;
return request;
}
void processQueue(UserInfo *user)
{
if (user->queueFront == user->queueBack)
return;
Request *nextRequest = user->requestQueue[user->queueFront];
if (nextRequest->type == READ && !user->isWriting)
{
pthread_cond_broadcast(&nextRequest->cond);
}
else if (nextRequest->type == WRITE && user->readCount == 0 && !user->isWriting)
{
pthread_cond_signal(&nextRequest->cond);
}
}
void *handleClient(void *clientSocketPtr)
{
int clientSocket = *((int *)clientSocketPtr);
free_memory(clientSocketPtr);
pthread_mutex_lock(&globalMutex);
if (currentConnections >= MAX_CONNECTIONS)
{
const char *messageBusy = "Server is busy. Please try again later\0";
send(clientSocket, messageBusy, strlen(messageBusy) + 1, 0);
close(clientSocket);
pthread_mutex_unlock(&globalMutex);
return NULL;
}
const char *messageReady = "Server is ready.\0";
send(clientSocket, messageReady, strlen(messageReady) + 1, 0);
currentConnections++;
pthread_mutex_unlock(&globalMutex);
// =====================================================================
int option;
ssize_t bytesReceived;
bytesReceived = recv(clientSocket, &option, sizeof(option), 0);
if (bytesReceived <= 0)
{
perror("Error receiving option");
close(clientSocket);
return NULL;
}
if (option == 1)
{
createUser(clientSocket);
}
else if (option == 2)
{
authenticateUser(clientSocket);
}
else
{
send(clientSocket, "Invalid option", strlen("Invalid option"), 0);
}
// ==============================================================
close(clientSocket);
// Lock mutex to safely update the connection counter and signal waiting threads
pthread_mutex_lock(&globalMutex);
currentConnections--;
// Signal one of the waiting threads that a connection slot is available
pthread_cond_signal(&connectionCond);
pthread_mutex_unlock(&globalMutex);
return NULL;
}
int main()
{
int serverSocket;
struct sockaddr_in serverAddr, clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
perror("Error binding socket");
close(serverSocket);
exit(EXIT_FAILURE);
}
if (listen(serverSocket, 5) < 0)
{
perror("Error listening");
close(serverSocket);
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", PORT);
if (initialize_arena() == -1)
{
printf("Failed to initialize memory arena.\n");
return 1;
}
while (1)
{
int *clientSocket = my_malloc(sizeof(int));
if (!clientSocket)
{
perror("Error allocating memory");
continue;
}
*clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddr, &clientAddrLen);
if (*clientSocket < 0)
{
perror("Error accepting connection");
free(clientSocket);
continue;
}
printf("Client connected\n");
pthread_t clientThread;
if (pthread_create(&clientThread, NULL, handleClient, clientSocket) != 0)
{
perror("Error creating thread");
close(*clientSocket);
free(clientSocket);
continue;
}
pthread_detach(clientThread);
}
close(serverSocket);
return 0;
}