Statistics
| Branch: | Revision:

peerstreamer-src / peerstreamer-ng.c @ c039490c

History | View | Annotate | Download (3.49 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
#include<task_manager.h>
26
#include<context.h>
27
#include<pschannel.h>
28
#include<mg_periodic_task_intfs.h>
29
#include<pstreamer.h>
30

    
31
#include<mongoose.h>
32

    
33

    
34
static uint8_t running = 1;
35

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

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

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

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

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

    
74
void init(struct context *c, int argc, char **argv)
75
{
76
        strncpy(c->http_port, "3000", 16);
77
        signal(SIGINT, sig_exit);
78

    
79
        c->http_opts.enable_directory_listing = "no";
80
        c->http_opts.document_root = "Public/";
81
        c->http_opts.index_files = "index.html,player.html";
82
        set_debug(1);
83

    
84
        c->router = router_create(10);
85
        load_path_handlers(c->router);
86
        c->tm = task_manager_new();
87
        c->pb = pschannel_bucket_new();
88
        c->psm = pstreamer_manager_new(7000);
89
        pschannel_bucket_insert(c->pb, "local_channel", "127.0.0.1", "6000", "300kbps", "127.0.0.1:3000/lchannel.sdp");
90

    
91
        c->mongoose_srv = (struct mg_mgr*) malloc(sizeof(struct mg_mgr));
92
        mg_mgr_init(c->mongoose_srv, c);
93

    
94
        parse_args(c, argc, argv);
95
}
96

    
97
struct mg_mgr * launch_http_task(struct context *c)
98
{
99
        struct mg_connection *nc;
100

    
101
        nc = mg_bind(c->mongoose_srv, c->http_port, ev_handler);
102
        nc->user_data = c;
103
        if (nc == NULL) {
104
                fprintf(stderr, "Error starting server on port %s\n", c->http_port);
105
                exit(1);
106
        }
107
        mg_set_protocol_http_websocket(nc);
108
        task_manager_new_task(c->tm, mongoose_task_reinit, mongoose_task_callback, 1000, (void *)c->mongoose_srv->ifaces[MG_MAIN_IFACE]);
109

    
110
        return c->mongoose_srv;
111
}
112

    
113
void context_deinit(struct context *c)
114
{
115
        router_destroy(&(c->router));
116
        task_manager_destroy(&(c->tm));
117
        pschannel_bucket_destroy(&(c->pb));
118
        pstreamer_manager_destroy(&(c->psm));
119
        mg_mgr_free(c->mongoose_srv);
120
        free(c->mongoose_srv);
121
}
122

    
123
int main(int argc, char** argv)
124
{
125
        static struct context c;
126

    
127
        init(&c, argc, argv);
128

    
129

    
130
        debug("Starting server on port %s\n", c.http_port);
131
        launch_http_task(&c);
132
        while (running)
133
                task_manager_poll(c.tm, 1000);
134

    
135
        debug("\nExiting..\n");
136
        context_deinit(&c);
137
        return 0;
138
}