Statistics
| Branch: | Revision:

peerstreamer-src / src / periodic_task_intfs.c @ cea8d274

History | View | Annotate | Download (4.75 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_populate_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
        {
67
                pschannel_bucket_reset(pb);
68
                pschannel_bucket_load_local_streams(pb);
69
                pschannel_bucket_save2file(pb);
70
                pschannel_bucket_loadfile(pb);
71
        }
72
        return 1;
73
}
74

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

    
87
uint8_t pstreamer_offer_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
88
{
89
        struct psinstance * ps;
90
        ps = (struct psinstance *) periodic_task_get_data(pt);
91
        if (ret == 0)
92
        {
93
                debug("Offer time\n");
94
                psinstance_send_offer(ps);
95
        }
96
        return 0;
97
}
98

    
99
uint8_t pstreamer_offer_task_reinit(struct periodic_task * pt)
100
{
101
        struct psinstance * ps;
102
        ps = (struct psinstance *) periodic_task_get_data(pt);
103
        periodic_task_set_remaining_time(pt, psinstance_offer_interval(ps));
104
        return 0;
105
}
106

    
107
uint8_t pstreamer_msg_handling_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
108
{
109
        struct psinstance * ps;
110

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

    
122
uint8_t pstreamer_msg_handling_task_reinit(struct periodic_task * pt)
123
{
124
        struct psinstance * ps;
125
        ps = (struct psinstance *) periodic_task_get_data(pt);
126

    
127
        periodic_task_flush_fdsets(pt);
128
        pstreamer_register_fds(ps, add_fd_to_fdset, (void*)pt);
129
        return 0;
130
}
131

    
132

    
133
uint8_t mongoose_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
134
{
135
        struct mg_iface *iface;
136

    
137
        iface = (struct mg_iface *) periodic_task_get_data(pt);
138

    
139
        //if (ret == 0)
140
        //        debug("Mongoose timeout\n");
141
        return mongoose_select_action(iface, ret, readfds, writefds, errfds);
142
}
143

    
144

    
145
uint8_t mongoose_task_reinit(struct periodic_task * pt)
146
{
147
        struct mg_iface *iface;
148
        timeout timeout_ms;
149

    
150
        iface = (struct mg_iface *) periodic_task_get_data(pt);
151

    
152
        timeout_ms = periodic_task_get_remaining_time(pt);
153
        if (timeout_ms == 0)
154
                timeout_ms = periodic_task_reset_timeout(pt);
155

    
156
        periodic_task_flush_fdsets(pt);
157

    
158
        timeout_ms = mongoose_select_init(iface, add_fd_to_fdset, (void*) pt, timeout_ms);
159
        periodic_task_set_remaining_time(pt, timeout_ms);
160

    
161
        return 0;
162
}
163

    
164
uint8_t pstreamer_inject_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
165
{
166
        struct psinstance * ps;
167
        ps = (struct psinstance *) periodic_task_get_data(pt);
168
        if (ret == 0)
169
        {
170
                debug("Chunk seeding time\n");
171
                psinstance_inject_chunk(ps);
172
        }
173
        return 0;
174
}