-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
354 lines (284 loc) · 9.84 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include "server.h"
static struct sockaddr_in serv_addr; //server address struct
static struct sockaddr_in cli_addr; //client address struct
static serverData net_info; //server informations
struct mysql_info my_info; //mysql server connection information
static int kill_server = 0; /*if none zero, kill server*/
void set_net_data(int port, const char *ip)
{
net_info.socket = -1;
net_info.port = port;
strcpy(net_info.ip_addr, ip);
}
void set_mysql_data(const char *serv, const char *user, const char *pwd, const char *dbname)
{
strcpy(my_info.server_name, serv);
strcpy(my_info.user_name, user);
strcpy(my_info.user_password, pwd);
strcpy(my_info.database_name, dbname);
}
static void sig_handler(int sig) {
kill_server = 23;
}
void make_server()
{
struct thread_block *thr_node = NULL; //Thread info nodes
Generic_list *thread_list = NULL; //list of thread ids
MYSQL *mysql_con = NULL; //main mysql server connection handler
MYSQL_RES *mysql_res = NULL;
char db_query[MYSQL_QUERY_BUFF];
int connection_socket, ret, size;
//int thread_count = 0; //thread count
struct list_node *thread_id_node = NULL;
pthread_attr_t pattr; //pthread attributes
struct sigaction sa;
thread_list = create_generic_list(ENABLE_LOCK);
if (thread_list == NULL) {
log_errors( NULL,
STD_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
"Cannot initialize generic list for thread IDs",
NULL,
error_ignore
);
} else
SERVER_MSG("Initialized generic list for thread ID");
mysql_con = mysql_init(NULL);
if (mysql_con == NULL) {
log_errors( NULL,
MYSQL_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
MYSQL_INIT_FAILED,
mysql_con,
(void (*)(void*))mysql_close);
}
SERVER_MSG("Connecting to database server on host [%s]...", my_info.server_name);
if (! mysql_real_connect( mysql_con,
my_info.server_name,
my_info.user_name,
my_info.user_password,
NULL,
0,
NULL,
0
)
) {
log_errors( NULL,
MYSQL_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
MYSQL_CONNECTION_FAILED,
mysql_con,
(void (*)(void*))mysql_close);
} else
SERVER_MSG("Connected to database server on host [%s]", my_info.server_name);
//check whether database exist
sprintf( db_query,
"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '%s'",
my_info.database_name
);
if ( mysql_query(mysql_con, db_query) != 0 ) {
log_errors( NULL,
MYSQL_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
mysql_con,
(void (*)(void*))mysql_close);
} else
SERVER_MSG("Checked whether database [%s] exist on host [%s].", my_info.database_name, my_info.server_name);
//check result of the query executed
mysql_res = mysql_store_result(mysql_con);
int test = mysql_num_rows(mysql_res);
if (test == 0) {
//then create the database and tables again
memset(db_query, '\0', sizeof(db_query));
sprintf( db_query, "CREATE DATABASE %s", my_info.database_name);
mysql_query(mysql_con, db_query);
memset(db_query, '\0', sizeof(db_query));
sprintf( db_query,
"CREATE TABLE %s.users ( \
userid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, \
username VARCHAR(%d) NOT NULL, \
userpasswd VARCHAR(%d) NOT NULL \
)",
my_info.database_name, NAME_BUF_SIZE, PASSWD_BUF_SIZE );
if ( mysql_query(mysql_con, db_query) !=0) {
log_errors( NULL,
MYSQL_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
mysql_con,
(void (*)(void*))mysql_close);
} else
SERVER_MSG("Created database [%s] on host [%s]", my_info.database_name, my_info.server_name);
memset(db_query, '\0', sizeof(db_query));
sprintf( db_query,
"CREATE TABLE %s.messages ( \
sender_id INTEGER NOT NULL, \
receiver_id INTEGER NOT NULL, \
time_sent DECIMAL(20, 0), \
time_received DECIMAL(20, 0), \
msg_type INTEGER NOT NULL, \
message VARCHAR(%d) NOT NULL \
)",
my_info.database_name, MAX_DATA_SIZE ); /*time_sent and time_received is measured the unix epoch*/
if ( mysql_query(mysql_con, db_query) !=0 ) {
log_errors( NULL,
MYSQL_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
MYSQL_QUERY_FAILED,
mysql_con,
(void (*)(void*))mysql_close);
}
server_debug("Created database %s.", my_info.database_name);
mysql_free_result(mysql_res);
}
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = sig_handler;
if ( sigaction(SIGINT, &sa, NULL) == -1 ) {
log_errors( NULL,
STD_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
SIGNAL_FAILED,
NULL,
error_ignore);
}
if ( pthread_attr_init(&pattr) == 0 ) {
pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setschedpolicy(&pattr, SCHED_OTHER);
pthread_attr_setscope(&pattr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setinheritsched(&pattr, PTHREAD_EXPLICIT_SCHED);
}
if ( (net_info.socket = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
log_errors( NULL,
STD_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
SOCKET_NOT_CREATED,
&(net_info.socket),
error_close_fd);
} else
SERVER_MSG("Creating a TCP socket...");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(net_info.port);
serv_addr.sin_addr.s_addr = inet_addr(net_info.ip_addr);
if ( bind(net_info.socket, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0 ) {
log_errors( NULL,
STD_ERRORS,
DO_EXIT,
WRITE_STDDER,
LOGS_FATAL_ERRORS,
SOCKET_BINDING_FAILED,
&(net_info.socket),
error_close_fd);
} else
SERVER_MSG("Binding socket to IP:[%s] and port number:[%d]...", net_info.ip_addr, net_info.port);
SERVER_MSG("Listening on port:[%d] for connection...", net_info.port);
listen(net_info.socket, MAX_CONN);
while(1) {
if ( kill_server != 0 ) break;
size = sizeof(cli_addr);
connection_socket = accept(net_info.socket, (struct sockaddr*)&cli_addr, (socklen_t*)&size);
if ( connection_socket < 0 ) {
if (errno != EINTR) {
log_errors( NULL,
STD_ERRORS,
DONT_EXIT,
WRITE_STDDER,
LOGS_WARNING,
SOCKET_NOT_CREATED,
NULL,
error_ignore );
continue;
} else
break;
} else
SERVER_MSG("User connecting to the server. %d users have connected.", thread_list->list_len);
thread_id_node = create_list_node(sizeof(pthread_t));
if (thread_id_node == NULL) {
shutdown(connection_socket, SHUT_RDWR);
errno = ENOMEM;
log_errors( NULL,
STD_ERRORS,
DONT_EXIT,
WRITE_STDDER,
LOGS_INFO,
"Cannot create list nodes",
&connection_socket,
error_close_fd );
continue;
}
thr_node = create_thread_node(NULL, connection_socket, thread_list);
if ( thr_node != NULL ) {
ret = pthread_create((pthread_t*)thread_id_node->data, &pattr, connection_handler, (void*)thr_node);
if ( ret != 0 ) {
log_errors( NULL,
STD_ERRORS,
DONT_EXIT,
WRITE_STDDER,
LOGS_WARNING,
THREAD_NOT_START,
NULL,
error_ignore );
destroy_thread_node(thr_node);
} else
set_thread_node_tid(thr_node, thread_id_node);
} else {
shutdown(connection_socket, SHUT_RDWR);
errno = ENOMEM;
log_errors( NULL,
STD_ERRORS,
DONT_EXIT,
WRITE_STDDER,
LOGS_INFO,
"Cannot create thread information block(TIB)",
&connection_socket,
error_close_fd );
free(thread_id_node->data);
free(thread_id_node);
}
}
SERVER_MSG("Server shutting down...");
while (thread_list->list_len > 0)
{
pthread_t *data = (pthread_t*)list_popfront(thread_list);
if (data != NULL) {
pthread_cancel(*data);
pthread_join(*data, NULL);
}
}
list_lock_acquire( thread_list );
destroy_generic_list( thread_list, free);
pthread_attr_destroy(&pattr);
mysql_close(mysql_con);
destroy_logs_object();
close(net_info.socket);
SERVER_MSG("Server Shutdown!");
}
#ifdef _TRY_
int main(int argc, char **argv) {
set_net_data(4040, "127.0.0.1");
set_mysql_data("localhost", "root", "database_password", "concurrent_chat");
make_server();
return 0;
}
#endif