Statistics
| Branch: | Revision:

peerstreamer-src / src / periodic_task_intfs.c @ b87dc7a2

History | View | Annotate | Download (3.56 KB)

1 4a486a01 Luca Baldesi
/*******************************************************************
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 c03fc03d Luca Baldesi
20 b87dc7a2 Luca Baldesi
#include<periodic_task_intfs.h>
21 c03fc03d Luca Baldesi
#include<mongoose.h>
22 b87dc7a2 Luca Baldesi
#include<psinstance.h>
23
#include<pstreamer_event.h>
24 c03fc03d Luca Baldesi
25
void add_fd_to_fdset(void * handler, int sock, char set)
26
{
27
        struct periodic_task * pt;
28
29
        pt = (struct periodic_task *) handler;
30
31
        switch (set)
32
        {
33
                case 'r':
34
                        periodic_task_readfd_add(pt, sock);
35
                        break;
36
                case 'w':
37
                        periodic_task_writefd_add(pt, sock);
38
                        break;
39
                case 'e':
40
                        periodic_task_errfd_add(pt, sock);
41
                        break;
42
                default:
43
                        fprintf(stderr, "[ERROR] sock insertion\n");
44
        }
45
}
46
47 b87dc7a2 Luca Baldesi
uint8_t pstreamer_topology_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
48
{
49
        struct psinstance * ps;
50
        ps = (struct psinstance *) periodic_task_get_data(pt);
51
        if (ret == 0)
52
                psinstance_topology_update(ps);
53
        return 0;
54
}
55
56
uint8_t pstreamer_topology_task_reinit(struct periodic_task * pt)
57
{
58
        return 0;
59
}
60
61
uint8_t pstreamer_offer_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
62
{
63
        struct psinstance * ps;
64
        ps = (struct psinstance *) periodic_task_get_data(pt);
65
        if (ret == 0)
66
                psinstance_send_offer(ps);
67
        return 0;
68
}
69
70
uint8_t pstreamer_offer_task_reinit(struct periodic_task * pt)
71
{
72
        struct psinstance * ps;
73
        ps = (struct psinstance *) periodic_task_get_data(pt);
74
        periodic_task_set_remaining_time(pt, psinstance_offer_interval(ps));
75
        return 0;
76
}
77
78
uint8_t pstreamer_msg_handling_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
79
{
80
        struct psinstance * ps;
81
82
        ps = (struct psinstance *) periodic_task_get_data(pt);
83
        if (ret > 0)  // we do not consider timeouts, we just want to handle data ready
84
                psinstance_handle_msg(ps);
85
        return 0;
86
}
87
88
uint8_t pstreamer_msg_handling_task_reinit(struct periodic_task * pt)
89
{
90
        struct psinstance * ps;
91
        ps = (struct psinstance *) periodic_task_get_data(pt);
92
93
        periodic_task_flush_fdsets(pt);
94
        pstreamer_register_fds(ps, add_fd_to_fdset, (void*)pt);
95
        return 0;
96
}
97
98 c03fc03d Luca Baldesi
99
uint8_t mongoose_task_callback(struct periodic_task * pt, int ret, fd_set * readfds, fd_set * writefds, fd_set * errfds)
100
{
101
        struct mg_iface *iface;
102
103
        iface = (struct mg_iface *) periodic_task_get_data(pt);
104
105
        return mongoose_select_action(iface, ret, readfds, writefds, errfds);
106
}
107
108
109
uint8_t mongoose_task_reinit(struct periodic_task * pt)
110
{
111
        struct mg_iface *iface;
112
        timeout timeout_ms;
113
114
        iface = (struct mg_iface *) periodic_task_get_data(pt);
115
116
        timeout_ms = periodic_task_get_remaining_time(pt);
117
        if (timeout_ms == 0)
118
                timeout_ms = periodic_task_reset_timeout(pt);
119
120
        periodic_task_flush_fdsets(pt);
121
122
        timeout_ms = mongoose_select_init(iface, add_fd_to_fdset, (void*) pt, timeout_ms);
123
        periodic_task_set_remaining_time(pt, timeout_ms);
124
125
        return 0;
126
}