Statistics
| Branch: | Revision:

peerstreamer-src / src / periodic_task_intfs.c @ 3af4c8d7

History | View | Annotate | Download (4.65 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<periodic_task_intfs.h>
21
#include<mongoose.h>
22
#include<psinstance.h>
23
#include<pstreamer_event.h>
24
#include<pschannel.h>
25
#include<pstreamer.h>
26
#include<debug.h>
27

    
28
void add_fd_to_fdset(void * handler, int sock, char set)
29
{
30
        struct periodic_task * pt;
31

    
32
        pt = (struct periodic_task *) handler;
33

    
34
        switch (set)
35
        {
36
                case 'r':
37
                        periodic_task_readfd_add(pt, sock);
38
                        break;
39
                case 'w':
40
                        periodic_task_writefd_add(pt, sock);
41
                        break;
42
                case 'e':
43
                        periodic_task_errfd_add(pt, sock);
44
                        break;
45
                default:
46
                        fprintf(stderr, "[ERROR] sock insertion\n");
47
        }
48
}
49

    
50
uint8_t pstreamer_purge_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
51
{
52
        struct pstreamer_manager * psm;
53

    
54
        psm = (struct pstreamer_manager *) periodic_task_get_data(pt);
55
        if (ret == 0)
56
                pstreamer_manager_remove_orphans(psm, 10);
57
        return 0;
58
}
59

    
60
uint8_t pschannel_csvfile_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
61
{
62
        struct pschannel_bucket * pb;
63

    
64
        pb = (struct pschannel_bucket *) periodic_task_get_data(pt);
65
        if (ret == 0)
66
                return pschannel_bucket_loadfile(pb);
67
        return 1;
68
}
69

    
70
uint8_t pstreamer_topology_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
71
{
72
        struct psinstance * ps;
73
        ps = (struct psinstance *) periodic_task_get_data(pt);
74
        if (ret == 0)
75
        {
76
                //debug("Topology update\n");
77
                psinstance_topology_update(ps);
78
        }
79
        return 0;
80
}
81

    
82
uint8_t pstreamer_offer_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
83
{
84
        struct psinstance * ps;
85
        ps = (struct psinstance *) periodic_task_get_data(pt);
86
        if (ret == 0)
87
        {
88
                debug("Offer time\n");
89
                psinstance_send_offer(ps);
90
        }
91
        return 0;
92
}
93

    
94
uint8_t pstreamer_offer_task_reinit(struct periodic_task * pt)
95
{
96
        struct psinstance * ps;
97
        ps = (struct psinstance *) periodic_task_get_data(pt);
98
        periodic_task_set_remaining_time(pt, psinstance_offer_interval(ps));
99
        return 0;
100
}
101

    
102
uint8_t pstreamer_msg_handling_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
103
{
104
        struct psinstance * ps;
105

    
106
        ps = (struct psinstance *) periodic_task_get_data(pt);
107
        if (ret > 0)  // we do not consider timeouts, we just want to handle data ready
108
        {
109
                debug("Received a message\n");
110
                psinstance_handle_msg(ps);
111
        }
112
        //if (ret == 0)
113
        //        debug("PStreamer message handling timeout\n");
114
        return 0;
115
}
116

    
117
uint8_t pstreamer_msg_handling_task_reinit(struct periodic_task * pt)
118
{
119
        struct psinstance * ps;
120
        ps = (struct psinstance *) periodic_task_get_data(pt);
121

    
122
        periodic_task_flush_fdsets(pt);
123
        pstreamer_register_fds(ps, add_fd_to_fdset, (void*)pt);
124
        return 0;
125
}
126

    
127

    
128
uint8_t mongoose_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
129
{
130
        struct mg_iface *iface;
131

    
132
        iface = (struct mg_iface *) periodic_task_get_data(pt);
133

    
134
        //if (ret == 0)
135
        //        debug("Mongoose timeout\n");
136
        return mongoose_select_action(iface, ret, readfds, writefds, errfds);
137
}
138

    
139

    
140
uint8_t mongoose_task_reinit(struct periodic_task * pt)
141
{
142
        struct mg_iface *iface;
143
        timeout timeout_ms;
144

    
145
        iface = (struct mg_iface *) periodic_task_get_data(pt);
146

    
147
        timeout_ms = periodic_task_get_remaining_time(pt);
148
        if (timeout_ms == 0)
149
                timeout_ms = periodic_task_reset_timeout(pt);
150

    
151
        periodic_task_flush_fdsets(pt);
152

    
153
        timeout_ms = mongoose_select_init(iface, add_fd_to_fdset, (void*) pt, timeout_ms);
154
        periodic_task_set_remaining_time(pt, timeout_ms);
155

    
156
        return 0;
157
}
158

    
159
uint8_t pstreamer_inject_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
160
{
161
        struct psinstance * ps;
162
        ps = (struct psinstance *) periodic_task_get_data(pt);
163
        if (ret == 0)
164
        {
165
                debug("Chunk seeding time\n");
166
                psinstance_inject_chunk(ps);
167
        }
168
        return 0;
169
}