File: epgchannel.c

package info (click to toggle)
dvbstreamer 2.1.0-2.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,680 kB
  • sloc: ansic: 42,160; sh: 10,230; python: 519; makefile: 371
file content (185 lines) | stat: -rw-r--r-- 5,957 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
Copyright (C) 2006  Adam Charrett

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

epgchannel.c

Used to send EPG information to interested parties.

*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>
#include <limits.h>
#include <time.h>

#include "main.h"
#include "types.h"

#include "list.h"
#include "logging.h"
#include "objects.h"
#include "messageq.h"
#include "epgchannel.h"

/*******************************************************************************
* Defines                                                                      *
*******************************************************************************/                
#define CHECK_LISTENERS() \
    do{ \
        int count;\
        pthread_mutex_lock(&EPGChannelMutex); \
        count = ListCount(EPGChannelListeners); \
        pthread_mutex_unlock(&EPGChannelMutex);\
        if (count == 0) \
        {\
            LogModule(LOG_DIARRHEA, EPGCHANNEL, "Not creating message, no listeners!");\
            return 0;\
        }\
    }while(0)
    
/*******************************************************************************
* Prototypes                                                                   *
*******************************************************************************/
static void EPGChannelMessageDestructor(void *ptr);
static void EPGChannelSendMessage(EPGChannelMessage_t *msg);

/*******************************************************************************
* Global variables                                                             *
*******************************************************************************/
static pthread_mutex_t EPGChannelMutex = PTHREAD_MUTEX_INITIALIZER;
static const char EPGCHANNEL[] = "EPGDChannel";
static List_t *EPGChannelListeners;
/*******************************************************************************
* Global functions                                                             *
*******************************************************************************/
int EPGChannelInit(void)
{
    ObjectRegisterTypeDestructor(EPGChannelMessage_t, EPGChannelMessageDestructor);
    EPGChannelListeners = ListCreate();
    return 0;
}


int EPGChannelDeInit()
{
    ListFree(EPGChannelListeners, (void(*)(void*))MessageQDestroy);
    return 0;
}

int EPGChannelRegisterListener(MessageQ_t msgQ)
{
    pthread_mutex_lock(&EPGChannelMutex);
    ListAdd(EPGChannelListeners, msgQ);
    pthread_mutex_unlock(&EPGChannelMutex);
    return 0;
}

int EPGChannelUnregisterListener(MessageQ_t msgQ)
{
    pthread_mutex_lock(&EPGChannelMutex);
    ListRemove(EPGChannelListeners, msgQ);
    pthread_mutex_unlock(&EPGChannelMutex);
    return 0;
}


int EPGChannelNewEvent(EPGEventRef_t *eventRef, struct tm *startTime, struct tm *endTime, bool ca)
{
    EPGChannelMessage_t *msg;

    CHECK_LISTENERS();
    
    msg = ObjectCreateType(EPGChannelMessage_t);
    msg->type = EPGChannelMessageType_Event;
    msg->eventRef = *eventRef;
    msg->data.event.startTime = *startTime;
    msg->data.event.endTime = *endTime;
    msg->data.event.ca = ca;
    EPGChannelSendMessage(msg);
    return 0;
}


int EPGChannelNewRating(EPGEventRef_t *eventRef, char *system, char *rating)
{
    EPGChannelMessage_t *msg;

    CHECK_LISTENERS();
    
    msg = ObjectCreateType(EPGChannelMessage_t);
    msg->type = EPGChannelMessageType_Rating;
    msg->eventRef = *eventRef;
    msg->data.rating.system = strdup(system);
    msg->data.rating.rating = strdup(rating);
    EPGChannelSendMessage(msg);
    return 0;
}


int EPGChannelNewDetail(EPGEventRef_t *eventRef, char *lang, char * name, char *value)
{
    EPGChannelMessage_t *msg;    
    EPGEventDetail_t *eventDetail;
    CHECK_LISTENERS();

    msg = ObjectCreateType(EPGChannelMessage_t);
    msg->type = EPGChannelMessageType_Detail;
    msg->eventRef = *eventRef;
    memcpy(msg->data.detail.lang, lang, sizeof(eventDetail->lang));
    msg->data.detail.name = strdup(name);
    msg->data.detail.value = strdup(value);
    EPGChannelSendMessage(msg);
    return 0;
}


/*******************************************************************************
* Local Functions                                                              *
*******************************************************************************/
static void EPGChannelSendMessage(EPGChannelMessage_t *msg)
{
    ListIterator_t iterator;
    pthread_mutex_lock(&EPGChannelMutex);
    for (ListIterator_Init(iterator, EPGChannelListeners); ListIterator_MoreEntries(iterator); ListIterator_Next(iterator))
    {
        MessageQ_t msgQ = (MessageQ_t)ListIterator_Current(iterator);
        MessageQSend(msgQ, msg);
    }
    pthread_mutex_unlock(&EPGChannelMutex);
    ObjectRefDec(msg);
}

static void EPGChannelMessageDestructor(void *ptr)
{
    EPGChannelMessage_t *msg = ptr;
    switch(msg->type)
    {
        case EPGChannelMessageType_Event:
            break;
        case EPGChannelMessageType_Detail:
            free(msg->data.detail.name);
            free(msg->data.detail.value);            
            break;
        case EPGChannelMessageType_Rating:
            free(msg->data.rating.system);
            free(msg->data.rating.rating);            
            break;
    }
}