Statistics
| Branch: | Revision:

peerstreamer-src / src / path_handlers.c @ b87dc7a2

History | View | Annotate | Download (3.47 KB)

1
/*******************************************************************
2
* PeerStreamer-ng is a P2P video streaming application exposing a ReST
3
* interface.
4
* Copyright (C) 2015 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<stdint.h>
21
#include<pschannel.h>
22
#include<router.h>
23
#include<tokens.h>
24
#include<name_lengths.h>
25
#include<debug.h>
26
#include<pschannel.h>
27
#include<context.h>
28
#include<mongoose.h>
29
#include<sdpfile.h>
30

    
31

    
32
char * mg_uri_field(struct http_message *hm, uint8_t pos)
33
{
34
        char * uri;
35
        char ** tokens;
36
        uint32_t ntok;
37

    
38
        uri = malloc((hm->uri.len + 1) * sizeof(char));
39
        strncpy(uri, hm->uri.p, hm->uri.len);
40
        uri[hm->uri.len] = '\0';
41

    
42
        tokens = tokens_create(uri, '/', &ntok);
43
        
44
        strcpy(uri, tokens[pos]);
45
        tokens_destroy(&tokens, ntok);
46

    
47
        return uri;
48
}
49

    
50
void channel_index(struct mg_connection *nc, struct http_message *hm)
51
{
52
        char * channels;
53
        const struct context * c;
54

    
55
        c = (const struct context *) nc->user_data;
56

    
57
        debug("GET request for channels\n");
58
        channels = pschannel_bucket_to_json(c->pb);
59
        debug("\t%s\n", channels);
60

    
61
        mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
62
        mg_printf_http_chunk(nc, "%s", channels);
63
        mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
64

    
65
        free(channels);
66
}
67

    
68
void streamer_create(struct mg_connection *nc, struct http_message *hm)
69
{
70
        const struct context * c;
71
        char ipaddr[MAX_IPADDR_LENGTH];
72
        char port[MAX_PORT_LENGTH];
73
        char * id, *sdpuri;
74
        const struct pstreamer * ps;
75
        const struct pschannel * ch;
76

    
77
        c = (const struct context *) nc->user_data;
78
        mg_get_http_var(&hm->body, "ipaddr", ipaddr, MAX_IPADDR_LENGTH);
79
        mg_get_http_var(&hm->body, "port", port, MAX_PORT_LENGTH);
80

    
81
        id = mg_uri_field(hm, 1);
82

    
83
        debug("POST request for resource %s\n", id);
84
        ch = pschannel_bucket_find(c->pb, ipaddr, port);
85

    
86
        if (ch)
87
        {
88
                debug("Channel: %s\n", ch->name);
89
                ps = pstreamer_manager_create_streamer(c->psm, ipaddr, port, id); 
90
                if(ps)
91
                {
92
                        pstreamer_schedule_tasks((struct pstreamer*)ps, c->tm);
93
                        debug("Streamer instance created\n");
94
                        sdpuri = sdpfile_create(c, ch, ps);
95
                        mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
96
                        mg_printf_http_chunk(nc, "{\"id\":\"%s\",\"name\":\"%s\",\"sdpfile\":\"%s\"}", id, ch->name, sdpuri);
97
                        mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
98
                        free(sdpuri);
99
                } else {
100
                        debug("Streamer could not be launched\n");
101
                        mg_printf(nc, "%s", "HTTP/1.1 409 Conflict\r\nTransfer-Encoding: chunked\r\n\r\n");
102
                        mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
103
                }
104
        } else {
105
                debug("No channel found for socket <%s:%s>\n", ipaddr, port);
106
                mg_printf(nc, "%s", "HTTP/1.1 404 Not Found\r\nTransfer-Encoding: chunked\r\n\r\n");
107
                mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
108
        }
109

    
110
        free(id);
111
}