Subversion Repositories MLServ

Rev

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

/* $Id: server.c 5 2024-09-25 00:19:35Z nishi $ */

#include "ms_server.h"

#include <stdio.h>
#include <poll.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

#include <cm_ipc.h>
#include <cm_db.h>

#define strsend(sock, str) send(sock, str, strlen(str), 0)

void ms_handle_socket(int sock) {
        sqlite3* db = cm_db_init();
        if(db == NULL) {
                strsend(sock, "* DBERROR\n");
                return;
        }
        struct pollfd pollfds[1];
        pollfds[0].fd = sock;
        pollfds[0].events = POLLIN | POLLPRI;
        while(1) {
                int ret = poll(pollfds, 1, 1000);
                if(ret < 0) {
                        break;
                } else if(ret == 0) {
                        strsend(sock, "* TIMEOUT\n");
                        break;
                } else {
                        int i;
                        char buffer[512];
                        int len = recv(sock, buffer, 512, 0);
                        for(i = 0; i < len; i++) {
                        }
                }
        }
        sqlite3_close(db);
}

int ms_handle_server(int argc, char** argv, int start) {
        int sock = cm_ipc_create();
        if(sock == -1) {
                fprintf(stderr, "Failed to create the socket\n");
        }
        signal(SIGPIPE, SIG_IGN);
        signal(SIGCHLD, SIG_IGN);
        printf("MLServ server ready\n");
        struct pollfd pollfds[1];
        pollfds[0].fd = sock;
        pollfds[0].events = POLLIN | POLLPRI;
        while(1) {
                int ret = poll(pollfds, 1, 1000);
                if(ret < 0) {
                        return 1;
                } else if(ret == 0) {
                } else if(ret > 0 && pollfds[0].revents & POLLIN) {
                        struct sockaddr_un claddr;
                        int clen = sizeof(claddr);

                        int csock = accept(sock, (struct sockaddr*)&claddr, &clen);
                        if(csock < 0) return 1;

                        if(fork() == 0) {
                                strsend(csock, "* READY\n");
                                ms_handle_socket(csock);
                                close(csock);
                                _exit(0);
                        } else {
                                close(csock);
                        }
                }
        }
        return 0;
}