Statistics
| Branch: | Revision:

peerstreamer-src / src / pschannel.c @ 58fb2cdc

History | View | Annotate | Download (4.87 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<pschannel.h>
21
#include<ord_set.h>
22
#include<stdint.h>
23
#include<string.h>
24
#include<stdlib.h>
25
#include<stdio.h>
26
#include<tokens.h>
27
#include<debug.h>
28

    
29
int8_t pschannel_cmp(const void *ch1,const  void *ch2)
30
{
31
        const struct pschannel *psc1, *psc2;
32
        int res;
33

    
34
        psc1 = (const struct pschannel *) ch1;
35
        psc2 = (const struct pschannel *) ch2;
36
        res = strcmp(psc1->ipaddr, psc2->ipaddr);
37
        if (res == 0)
38
                res = strcmp(psc1->port, psc2->port);
39
        return res;
40
}
41

    
42
struct pschannel_bucket {
43
        struct ord_set * channels;
44
        char * csvfile;
45
};
46

    
47
struct pschannel_bucket * pschannel_bucket_new(const char * csvfilename)
48
{
49
        struct pschannel_bucket * pb;
50
        pb = (struct pschannel_bucket*) malloc(sizeof(struct pschannel_bucket));
51
        pb->channels = ord_set_new(10, pschannel_cmp);
52
        if (csvfilename)
53
                pb->csvfile = strdup(csvfilename);
54
        else
55
                pb->csvfile = NULL;
56
        return pb;
57
}
58

    
59
uint8_t pschannel_bucket_insert(struct pschannel_bucket * pb, char * name, char * ip, char * port, char * quality, char * sdpfile)
60
{
61
        struct pschannel * ch;
62
        void * res;
63

    
64
        if (pb && name && ip && port && quality && sdpfile)
65
        {
66
                ch = (struct pschannel *) malloc(sizeof(struct pschannel));
67
                memset(ch, 0, sizeof(struct pschannel));
68
                strncpy(ch->name, name, MAX_NAME_LENGTH-1);
69
                strncpy(ch->ipaddr, ip, MAX_IPADDR_LENGTH-1);
70
                strncpy(ch->port, port, MAX_PORT_LENGTH-1);
71
                strncpy(ch->quality, quality, MAX_QUALITY_LENGTH-1);
72
                strncpy(ch->sdpfile, sdpfile, MAX_SDPFILENAME_LENGTH-1);
73

    
74
                res = ord_set_insert(pb->channels, ch, 0);
75
                if (res != ch)  // there is a conflict
76
                        ord_set_insert(pb->channels, ch, 1);  // we just override the current value
77
                return 0;
78
        }
79
        else
80
                return 1;
81
}
82

    
83
const struct pschannel * pschannel_bucket_iter(const struct pschannel_bucket * pb, const struct pschannel * iter)
84
{
85
        const struct pschannel * res = NULL;
86

    
87
        if (pb)
88
                res = (const struct pschannel *) ord_set_iter(pb->channels, (const void *) iter);
89
        return res;
90
}
91

    
92
void pschannel_bucket_destroy(struct pschannel_bucket ** pb)
93
{
94
        if(pb && *pb)
95
        {
96
                ord_set_destroy(&((*pb)->channels), 1);
97
                if ((*pb)->csvfile)
98
                        free((*pb)->csvfile);
99
                free(*pb);
100
                *pb = NULL;
101
        }
102
}
103

    
104
char * pschannel_bucket_to_json(const struct pschannel_bucket * pb)
105
{
106
        char objstr[] = "{\"name\":\"\",\"ipaddr\":\"\",\"port\":\"\",\"quality\":\"\"},";
107
        size_t reslength, i;
108
        char * res = NULL;
109
        const struct pschannel * iter;
110

    
111
        if (pb)
112
        {
113
                reslength = strlen("[]") + (strlen(objstr) + MAX_NAME_LENGTH + 
114
                        MAX_IPADDR_LENGTH + MAX_PORT_LENGTH + MAX_QUALITY_LENGTH) * 
115
                           ord_set_length(pb->channels) + 1;
116

    
117
                res = (char *) malloc (sizeof(char) * reslength);
118

    
119
                res[0] = '[';
120
                i = 1;
121
                for(iter = NULL; (iter = pschannel_bucket_iter(pb, iter));)
122
                {
123
                        if (i > 1)
124
                                res[i++] = ',';
125
                        i += sprintf(res+i, "{\"name\":\"%s\",\"ipaddr\":\"%s\",\"port\":\"%s\",\"quality\":\"%s\"}", 
126
                                        iter->name, iter->ipaddr, iter->port, iter->quality);
127
                }
128
                res[i] = ']'; i++;
129
                res[i] = '\0'; i++;
130
        }
131

    
132
        return res;
133
}
134

    
135
const struct pschannel * pschannel_bucket_find(const struct pschannel_bucket * psb, const char * ipaddr, const char * port)
136
{
137
        struct pschannel ch;
138
        
139
        if (ipaddr && port)
140
        {
141
                strncpy(ch.ipaddr, ipaddr, MAX_IPADDR_LENGTH);
142
                strncpy(ch.port, port, MAX_PORT_LENGTH);
143
                return (const struct pschannel *) ord_set_find(psb->channels, &ch);
144

    
145
        } else
146
                return NULL;
147
}
148

    
149
int8_t pschannel_bucket_loadfile(struct pschannel_bucket * psb)
150
{
151
        int8_t res = -1;
152
        int ans;
153
        FILE *fp;
154
        char line[255];
155
        char ** tokens;
156
        uint32_t ntoks;
157
        
158
        if (psb && psb->csvfile && (fp = fopen(psb->csvfile, "r")))
159
        {
160
                ans = ftrylockfile(fp);
161
                if (ans == 0)
162
                {
163
                        debug("Refresing channel list from file\n");
164
                        ord_set_destroy(&(psb->channels), 1);
165
                        psb->channels = ord_set_new(10, pschannel_cmp);
166
                        while (fgets(line, 255, fp) != NULL)
167
                        {
168
                                tokens = tokens_create(line, ',', &ntoks);
169
                                if (tokens[0][0] != '#' && ntoks == 5)
170
                                        pschannel_bucket_insert(psb, tokens[0], tokens[1], tokens[2], tokens[3], tokens[4]);
171
                                tokens_destroy(&tokens, ntoks);
172
                        }
173
                        res = 0;
174
                        funlockfile(fp);
175
                } else
176
                        res = -2;
177
                fclose(fp);
178
        }
179
        return res;
180
}