-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainserver.c
127 lines (96 loc) · 2.64 KB
/
mainserver.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
/*
gcc -c mainserver.c -lssl -lcrypto
gcc -c multithrserver.c -lssl -lcrypto
gcc -c tls_server.c -lssl -lcrypto
gcc -o server mainserver.o multithrserver.o -lpthread
./server
rm server
rm *.o
gcc -c tls_server.c -lssl -lcrypto
gcc -c multithrserver.c -lssl -lcrypto -lpthread
gcc -c mainserver.c -lssl -lcrypto -lpthread
gcc -o server mainserver.o tls_server.o multiserver.o -lpthread -lcrypto -lssl
./server
*/
#include "mainserver.h"
//global Variables
int tcp_q;
int threads;
char *home_dir;
int port;
//*******************************************************************************
/**
*
* @brief funtion that takes a string and returns the numeric value in it
*
* @param str the input string
* @return the numeric value in the string
*/
int extract_number(char *str){
char *p = str;
while (*p) { // While there are more characters to process...
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
long val = strtol(p, &p, 10); // Read number
return val; // and print it.
} else {
// Otherwise, move on to the next character.
p++;
}
}
return 0;
}
//----------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------
/**
*
* @brief funtion to read the configurstion file
*
*
*/
void read_conf(){
home_dir = malloc(65);
FILE* file = fopen("params.conf", "r");
if(!file){
printf("\n Unable to open : params.conf");
exit(-1);
}
char line[128];
char sub[16];
while (fgets(line, sizeof(line), file)) {
memcpy(sub,&line[0],4);
if(strcmp(sub,"THRE")==0)
threads = extract_number(line);
if(strcmp(sub,"PORT")==0)
port = extract_number(line);
if(strcmp(sub,"QUEU")==0)
tcp_q = extract_number(line);
if(strcmp(sub,"HOME")==0)
memcpy(home_dir,&line[5],64);
}
fclose(file);
}
int main(int argc, char * argv []){
read_conf();
init();
// reading the configuration file and loading the information
int idPeople[threads];
pthread_t tid[threads];
int i,err;
for (i=0;i<threads;i++){
idPeople[i] = i;
if (err = pthread_create(&tid[i],NULL,&workerFunction, idPeople[i])){
perror2("pthread_create",err);
exit(1);
}
}
mainThreadFunction(tcp_q,port,threads,home_dir);
return 0;
}
//check gia minimata elexou
/*
https://nachtimwald.com/2019/04/12/thread-pool-in-c/
https://code-vault.net/lesson/j62v2novkv:1609958966824
https://programmer.group/c-simple-thread-pool-based-on-pthread-implementation.html
https://code-vault.net/lesson/tlu0jq32v9:1609364042686
*/