summaryrefslogtreecommitdiffstats
path: root/hw1/main.c
blob: 80298ac82be69a45e91df62bfeb341cebe1249ab (plain) (blame)
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
#include "common.h"
#include "server.h"
#include "proc.h"

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

request* requestP = NULL;  // point to a list of requests
int maxfd;  // size of open file descriptor table, size of request list

int main(int argc, char** argv) {
    if (argc != 2) {
        fprintf(stderr, "usage: %s [port]\n", argv[0]);
        exit(1);
    }

    // Setup signal handlers
    struct sigaction pipe_action = {
        .sa_handler = SIG_IGN,
        .sa_flags = 0
    };
    sigemptyset(&pipe_action.sa_mask);
    sigaction(SIGPIPE, &pipe_action, NULL);

    // Get file descripter table size
    maxfd = getdtablesize();

    // Initialize server
    server_init((unsigned short) atoi(argv[1]), maxfd);

    // Initialize request table
    requestP = (request*) e_malloc(sizeof(request) * maxfd);

    for (int i = 0; i < maxfd; i++)
        request_init(&requestP[i]);

    requestP[svr.listen_fd].conn_fd = svr.listen_fd;
    strcpy(requestP[svr.listen_fd].host, svr.hostname);

    // Loop for handling connections
    fprintf(stderr,
        "\nstarting on %.80s, port %d, fd %d, maxconn %d...\n",
        svr.hostname, svr.port, svr.listen_fd, maxfd);

    while (procconn(&svr, requestP, maxfd));

    free(requestP);
    return 0;
}