-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
160 lines (128 loc) · 3.6 KB
/
Copy pathserver.c
File metadata and controls
160 lines (128 loc) · 3.6 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
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include <signal.h>
#include "networking.h"
#include "sharedfunctions.h"
#include "servercommands.h"
#include "queue.h"
#define THREAD_POOL_SIZE 10
pthread_mutex_t queue_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t new_command = PTHREAD_COND_INITIALIZER;
pthread_t thread_pool[THREAD_POOL_SIZE];
int serveractive = 1;
void* executecommandthread(int* value) {
int clisockfd = *(value);
free(value);
NetworkCommand* message = readMessage(clisockfd);
printf("Message recieved from client -> %d\n", message->type);
executecommand(message, clisockfd);
freeCMND(message);
return NULL;
}
void* threadentry(void* value) {
while (serveractive) {
pthread_mutex_lock(&queue_lock);
int* node = dequeue();
if (node == NULL) {
pthread_cond_wait(&new_command, &queue_lock);
node = dequeue();
}
pthread_mutex_unlock(&queue_lock);
if (node == NULL) continue;
executecommandthread(node);
}
return NULL;
}
void inturupthandler() {
if (serveractive == 0) exit(0);
// Signal threads to stop, then join them
serveractive = 0;
int i = 0;
for ( i = 0; i < THREAD_POOL_SIZE; i++) {
pthread_cond_signal(&new_command);
}
for ( i = 0; i < THREAD_POOL_SIZE; i++) {
pthread_join(thread_pool[i], NULL);
}
Node* head = getHead();
while(head != NULL) {
ProjectMeta* meta = ((ProjectMeta*) head->content);
i = 0;
for (i = 0; i < meta->maxusers; i++) {
if (meta->activecommits[i] == NULL) continue;
freeCommit(meta->activecommits[i]);
}
free(meta->activecommits);
free(meta->project);
head = head->next;
}
freeLL(head);
write(1, "\n", 1);
exit(0);
}
int main(int argc, char** argv) {
printf("Starting server\n");
if (argc != 2) {
printf("Error: Invalid argument count\n");
return -1;
}
int portlen = strlen(argv[1]);
if (!isNum(argv[1], portlen) || portlen > 5) {
printf("Error: Invalid port. Port musht be an integer between 0 an 65535\n");
return -1;
}
int port = atoi(argv[1]);
int serverfd = socket(AF_INET, SOCK_STREAM, 0);
if (serverfd < 0) {
printf("Error: couldn't open server socket\n");
return -1;
}
struct sockaddr_in serv_addr;
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if (bind(serverfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Error: couldn't bind to port\n");
return -1;
}
listen(serverfd, 5);
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
int i = 0;
for(i = 0; i < THREAD_POOL_SIZE; i++) {
pthread_create(&thread_pool[i], NULL, threadentry, NULL);
}
// LLhead = NULL;
projectCount = 0;
currentuid = 0;
maxusers = 0;
if(signal(SIGINT, inturupthandler) == SIG_ERR) {
printf("Error: couldn't set handler for inturupt signal,\n");
return -1;
}
int clisockfd = 0;
while (1) {
clisockfd = accept(serverfd, (struct sockaddr*)&cli_addr, &clilen);
if (clisockfd < 0) {
printf("Error: couldn't open client socket\n");
continue;
}
printf("Successful connection from: %s\n", inet_ntoa((&cli_addr)->sin_addr));
int* fd = malloc(sizeof(int));
*fd = clisockfd;
pthread_mutex_lock(&queue_lock);
pthread_cond_signal(&new_command);
enqueue(fd);
pthread_mutex_unlock(&queue_lock);
}
printf("An error occured while accepting new connections: %s\n", strerror(errno));
return 0;
}