Subversion Repositories MLServ

Rev

Rev 4 | Rev 7 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 1
/* $Id: server.c 5 2024-09-25 00:19:35Z nishi $ */
2
 
3
#include "ms_server.h"
4
 
5
#include <stdio.h>
5 nishi 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>
4 nishi 12
 
13
#include <cm_ipc.h>
5 nishi 14
#include <cm_db.h>
4 nishi 15
 
5 nishi 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
}
44
 
4 nishi 45
int ms_handle_server(int argc, char** argv, int start) {
46
	int sock = cm_ipc_create();
47
	if(sock == -1) {
48
		fprintf(stderr, "Failed to create the socket\n");
49
	}
5 nishi 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
	}
4 nishi 78
	return 0;
79
}