Subversion Repositories MLServ

Rev

Rev 4 | Rev 7 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4 Rev 5
Line 1... Line 1...
1
/* $Id: server.c 4 2024-09-24 18:35:15Z nishi $ */
1
/* $Id: server.c 5 2024-09-25 00:19:35Z nishi $ */
2
 
2
 
3
#include "ms_server.h"
3
#include "ms_server.h"
4
 
4
 
5
#include <stdio.h>
5
#include <stdio.h>
-
 
6
#include <poll.h>
-
 
7
#include <sys/un.h>
-
 
8
#include <sys/socket.h>
-
 
9
#include <signal.h>
-
 
10
#include <unistd.h>
-
 
11
#include <string.h>
6
 
12
 
7
#include <cm_ipc.h>
13
#include <cm_ipc.h>
-
 
14
#include <cm_db.h>
-
 
15
 
-
 
16
#define strsend(sock, str) send(sock, str, strlen(str), 0)
-
 
17
 
-
 
18
void ms_handle_socket(int sock) {
-
 
19
	sqlite3* db = cm_db_init();
-
 
20
	if(db == NULL) {
-
 
21
		strsend(sock, "* DBERROR\n");
-
 
22
		return;
-
 
23
	}
-
 
24
	struct pollfd pollfds[1];
-
 
25
	pollfds[0].fd = sock;
-
 
26
	pollfds[0].events = POLLIN | POLLPRI;
-
 
27
	while(1) {
-
 
28
		int ret = poll(pollfds, 1, 1000);
-
 
29
		if(ret < 0) {
-
 
30
			break;
-
 
31
		} else if(ret == 0) {
-
 
32
			strsend(sock, "* TIMEOUT\n");
-
 
33
			break;
-
 
34
		} else {
-
 
35
			int i;
-
 
36
			char buffer[512];
-
 
37
			int len = recv(sock, buffer, 512, 0);
-
 
38
			for(i = 0; i < len; i++) {
-
 
39
			}
-
 
40
		}
-
 
41
	}
-
 
42
	sqlite3_close(db);
-
 
43
}
8
 
44
 
9
int ms_handle_server(int argc, char** argv, int start) {
45
int ms_handle_server(int argc, char** argv, int start) {
10
	int sock = cm_ipc_create();
46
	int sock = cm_ipc_create();
11
	if(sock == -1) {
47
	if(sock == -1) {
12
		fprintf(stderr, "Failed to create the socket\n");
48
		fprintf(stderr, "Failed to create the socket\n");
13
	}
49
	}
-
 
50
	signal(SIGPIPE, SIG_IGN);
-
 
51
	signal(SIGCHLD, SIG_IGN);
-
 
52
	printf("MLServ server ready\n");
-
 
53
	struct pollfd pollfds[1];
-
 
54
	pollfds[0].fd = sock;
-
 
55
	pollfds[0].events = POLLIN | POLLPRI;
-
 
56
	while(1) {
-
 
57
		int ret = poll(pollfds, 1, 1000);
-
 
58
		if(ret < 0) {
-
 
59
			return 1;
-
 
60
		} else if(ret == 0) {
-
 
61
		} else if(ret > 0 && pollfds[0].revents & POLLIN) {
-
 
62
			struct sockaddr_un claddr;
-
 
63
			int clen = sizeof(claddr);
-
 
64
 
-
 
65
			int csock = accept(sock, (struct sockaddr*)&claddr, &clen);
-
 
66
			if(csock < 0) return 1;
-
 
67
 
-
 
68
			if(fork() == 0) {
-
 
69
				strsend(csock, "* READY\n");
-
 
70
				ms_handle_socket(csock);
-
 
71
				close(csock);
-
 
72
				_exit(0);
-
 
73
			} else {
-
 
74
				close(csock);
-
 
75
			}
-
 
76
		}
-
 
77
	}
14
	return 0;
78
	return 0;
15
}
79
}