Statistics
| Branch: | Revision:

peerstreamer-src / peerstreamer-ng.c @ 9eb656e7

History | View | Annotate | Download (2.68 KB)

1
/*******************************************************************
2
* PeerStreamer-ng is a P2P video streaming application exposing a ReST
3
* interface.
4
* Copyright (C) 2017 Luca Baldesi <luca.baldesi@unitn.it>
5
*
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Affero General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU Affero General Public License for more details.
15
*
16
* You should have received a copy of the GNU Affero General Public License
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*******************************************************************/
19

    
20
#include<signal.h>
21
#include<string.h>
22
#include<router.h>
23
#include<path_handlers.h>
24
#include<debug.h>
25

    
26
#include"mongoose.h"
27

    
28

    
29
static uint8_t running = 1;
30

    
31
struct context{
32
        struct mg_serve_http_opts http_opts;
33
        char http_port[16];
34
        struct router * router;
35
};
36

    
37
void sig_exit(int signo)
38
{
39
        running = 0;
40
}
41

    
42
void parse_args(struct context *c, int argc, char *const* argv)
43
{
44
        int o;
45

    
46
        while ((o = getopt (argc, argv, "qp:")) != -1)
47
                switch (o) {
48
                        case 'p':
49
                                strncpy(c->http_port, optarg, 16);
50
                                break;
51
                        case 'q':
52
                                set_debug(0);
53
                                break;
54
                }
55
}
56

    
57
void ev_handler(struct mg_connection *nc, int ev, void *ev_data)
58
{
59
        struct context *c = nc->mgr->user_data;
60
        struct http_message *hm;
61

    
62
        switch (ev) {
63
                case MG_EV_HTTP_REQUEST:
64
                        hm  = (struct http_message *) ev_data;
65
                        // Try to call a path handler. If it fails serve
66
                        // public contents
67
                        if(router_handle(c->router, nc, hm))
68
                                mg_serve_http(nc, hm, c->http_opts);
69
                        break;
70
                default:
71
                        break;
72
        }
73
}
74

    
75
void init(struct context *c, int argc, char **argv)
76
{
77
        strncpy(c->http_port, "3000", 16);
78
        signal(SIGINT, sig_exit);
79
        c->http_opts.enable_directory_listing = "no";
80
        c->http_opts.document_root = "Public/";
81
        set_debug(1);
82
        c->router = router_create(10);
83
        load_path_handlers(c->router);
84
        parse_args(c, argc, argv);
85
}
86

    
87
int main(int argc, char** argv)
88
{
89
        static struct context c;
90
        struct mg_mgr mgr;
91
        struct mg_connection *nc;
92
        
93
        init(&c, argc, argv);
94
        mg_mgr_init(&mgr, &c);
95

    
96
        debug("Starting server on port %s\n", c.http_port);
97
        nc = mg_bind(&mgr, c.http_port, ev_handler);
98
        if (nc == NULL) {
99
                fprintf(stderr, "Error starting server on port %s\n", c.http_port);
100
                exit(1);
101
        }
102
        mg_set_protocol_http_websocket(nc);
103

    
104
        while(running)
105
                mg_mgr_poll(&mgr, 1000);
106

    
107
        debug("\nExiting..\n");
108
        router_destroy(&(c.router));
109
        mg_mgr_free(&mgr);
110
        return 0;
111
}