src/server/daemon/httplistener.c

Thu, 23 Feb 2012 13:10:04 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 23 Feb 2012 13:10:04 +0100
changeset 24
1a7853a4257e
parent 23
a2c8fc23c90e
child 25
5dee29c7c530
permissions
-rw-r--r--

removed some NSPR dependencies

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2011 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "../public/nsapi.h"

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#include <stdbool.h>
#include <pthread.h>

#include "../ucx/map.h"
#include "httplistener.h"

#include "session.h"
#include "configmanager.h"

UcxMap *listener_map = NULL;

int start_all_listener() {
    ServerConfiguration *conf = cfgmgr_get_server_config();
    UcxList *ls = conf->listeners;
    while(ls) {
        HttpListener *listener = ls->data;
        http_listener_start(listener);
        ls = ls->next;
    }

    return 0;
}


HttpListener* http_listener_new(ListenerConfig *conf) {
    if(listener_map == NULL) {
        listener_map = ucx_map_new(16);
    }

    HttpListener *fl = ucx_map_sstr_get(listener_map, conf->name);
    if(fl != NULL) {
        return fl;
    }

    HttpListener *listener = malloc(sizeof(HttpListener));
    listener->name = conf->name;
    listener->session_handler = create_basic_session_handler();
    listener->nacceptors = conf->nacceptors;
    ucx_map_sstr_put(listener_map, listener->name, listener);

    struct sockaddr_in servaddr;   /* server address */

    /* init address structure */
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(conf->port);

    /* create socket */
    if((listener->server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("Error: http_listener_new: socket");
        return NULL;
    }

    int o = 1;
    setsockopt(
            listener->server_socket,
            SOL_SOCKET, SO_REUSEADDR,
            &o,
            sizeof(int));

    /* bind server socket to address */
    if(bind(listener->server_socket, (struct sockaddr*)&servaddr, sizeof(servaddr))){
        perror("Error: http_listener_new: bind");
        printf("port: %d\n", conf->port);
        return NULL;
    }

    /* create acceptors */
    listener->acceptors = calloc(listener->nacceptors, sizeof(void*));
    for (int i=0;i<listener->nacceptors;i++) {
        listener->acceptors[i] = acceptor_new(listener);
    }

    return listener;
}

int http_listener_start(HttpListener *listener) {
    printf("INFO: start listener\n");

    if (listen(listener->server_socket, 256) == -1) {
        perror("Error: http_listener_start: listen");
        return -1;
    }

    /* start acceptor threads */
    for (int i=0;i<listener->nacceptors;i++) {
        acceptor_start(listener->acceptors[i]);
    }

    return 0;
}



Acceptor* acceptor_new(HttpListener *listener) {
    Acceptor *acceptor = malloc(sizeof(Acceptor));
    acceptor->listener = listener;
    return acceptor;
}

void acceptor_start(Acceptor *a) {
    if(pthread_create(
            &a->tid,
            NULL,
            (void*(*)(void*))acceptor_thread,
            a) != 0)
    {
        perror("Error: acceptor_start: pthread_create");
    }
}

void* acceptor_thread(Acceptor *acceptor) {
    HttpListener *listener = acceptor->listener;

    for (;;) {
        /* accept connections */
        struct sockaddr_in ca;
        socklen_t length = sizeof(ca);
        int clientfd;

        /* accept a connection */
        clientfd = accept(
                listener->server_socket,
                (struct sockaddr*)&ca,
                &length);
        if (clientfd == -1) {
            perror("Error: acceptor_thread: accept");
            continue;
        }
        
        /* create Connection object */
        Connection *conn = malloc(sizeof(Connection));
        conn->address = ca;
        conn->fd = clientfd;
        conn->listener = listener;

        /* enqueue the connection */
        listener->session_handler->enqueue_connection(
                listener->session_handler,
                conn);

        /* ready for new connection */
    }
}

mercurial