diff -r 1fdbf4170ef4 -r b8bf95b39952 src/server/httplistener.c --- a/src/server/httplistener.c Sun Jan 08 15:46:47 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,178 +0,0 @@ -/* - * 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 "nsapi.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "map.h" -#include "httplistener.h" - -#include "session.h" - -hashmap_t *listener_map = NULL; - - -int start_all_listener() { - HttpListener *listener = get_http_listener("default"); - http_listener_start(listener); - - return 0; -} - -HttpListener* get_http_listener(char *name) { - return hashmap_get(listener_map, sstr(name)); -} - - -HttpListener* http_listener_new(ListenerConfig *conf) { - HttpListener *listener = malloc(sizeof(HttpListener)); - listener->session_handler = create_basic_session_handler(); - listener->nacceptors = conf->nacceptors; - - 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"); - return NULL; - } - - /* create acceptors */ - listener->acceptors = calloc(listener->nacceptors, sizeof(void*)); - for (int i=0;inacceptors;i++) { - listener->acceptors[i] = acceptor_new(listener); - } - - if(listener_map == NULL) { - listener_map = hashmap_new(8); - } - hashmap_put(listener_map, sstr(conf->name), listener); - return listener; -} - -int http_listener_start(HttpListener *listener) { - printf("INFO: start listener\n"); - - if (listen(listener->server_socket, 16) == -1) { - perror("Error: http_listener_start: listen"); - return -1; - } - - /* start acceptor threads */ - for (int i=0;inacceptors;i++) { - acceptor_start(listener->acceptors[i]); - } -} - - - -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; - - /* enqueue the connection */ - listener->session_handler->enqueue_connection( - listener->session_handler, - conn); - - /* ready for new connection */ - } -}