# HG changeset patch # User morphbr # Date 1169761230 0 # Node ID bbf4568147717153be87695f612dae33d2506823 # Parent 9d4af01c6a2f97def49af9a4e443d13845e2b2ff [svn r307] Finished getEpg via HTTP+XML. diff -r 9d4af01c6a2f -r bbf456814771 gmyth/src/gmyth_http.c --- a/gmyth/src/gmyth_http.c Thu Jan 25 18:17:40 2007 +0000 +++ b/gmyth/src/gmyth_http.c Thu Jan 25 21:40:30 2007 +0000 @@ -40,12 +40,6 @@ #include "gmyth_debug.h" #include "gmyth_socket.h" -struct MemoryStruct -{ - char *memory; - size_t size; -}; - xmlXPathObjectPtr getnodeset(xmlDocPtr doc, xmlChar *xpath) @@ -89,31 +83,77 @@ return result; } -void get_Channel_List (xmlNodePtr nodeTab, GMythEpg* epg) +/** Retrieves the Progam List from the Channel + * + * @param nodeTab A pointer to a node inside the XML + * @return A GSList containing a list of all the programs + */ +GSList* get_Program_List(xmlNodePtr node) +{ + GSList* program_list = NULL; + + while (node != NULL) + { + if (g_ascii_strcasecmp((char *)node->name, "text") != 0) + { + GMythProgram* program = (GMythProgram*)g_malloc(sizeof(struct _GMythProgram)); + + program->title = g_string_new((char *)xmlGetProp(node, (xmlChar *)"title")); + program->subtitle = g_string_new((char *)xmlGetProp(node, (xmlChar *)"subtitle")); + program->catType = g_string_new((char *)xmlGetProp(node, (xmlChar *)"catType")); + program->category = g_string_new((char *)xmlGetProp(node, (xmlChar *)"category")); + + sscanf ((char *)xmlGetProp(node, (xmlChar *)"repeat"), "%d", &(program->repeat)); + + program->startTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(node, \ + (xmlChar *)"startTime")); + program->endTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(node, \ + (xmlChar *)"endTime")); + + program_list = g_slist_append(program_list, program); + } + + node = node->next; + } + + return program_list; +} + +/** Retrieves the Channel List from the ProgramGuide + * + * @param node A pointer to a node inside the XML + * @param epg The struct where is the current epg + * @return The epg from "param" updated + */ +void get_Channel_List (xmlNodePtr node, GMythEpg* epg) { int i; epg->channelList = NULL; - for(i=1; i <= epg->numOfChannels; i++) + for (i=1; i <= epg->numOfChannels; i++) { GMythChannel* channel = (GMythChannel*)g_malloc(sizeof(struct _GMythChannel)); - channel->channelName = g_string_new((char *)xmlGetProp(nodeTab, (xmlChar *)"channelName")); - channel->chanNum = g_string_new((char *)xmlGetProp(nodeTab, (xmlChar *)"chanNum")); + channel->channelName = g_string_new((char *)xmlGetProp(node, (xmlChar *)"channelName")); + channel->chanNum = g_string_new((char *)xmlGetProp(node, (xmlChar *)"chanNum")); - sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"chanId"), "%d", &(channel->chanId)); - sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"callSign"), "%d", &(channel->callSign)); + sscanf ((char *)xmlGetProp(node, (xmlChar *)"chanId"), "%d", &(channel->chanId)); + sscanf ((char *)xmlGetProp(node, (xmlChar *)"callSign"), "%d", &(channel->callSign)); + + channel->programList = get_Program_List(node->children); epg->channelList = g_slist_append(epg->channelList, channel); - //get_Program_List(nodeTab, channel); - //TODO: programlist } } +/** Retrieves the properties from the ProgramGuide + * + * @param nodeTab A pointer to a node inside the XML + * @param epg The struct where is the current epg + * @return The epg from "param" updated + */ void get_ProgramGuide_Properties (xmlNodePtr nodeTab, GMythEpg* epg) { - //TODO: alloc GTimeVal -> it's loosing the value... - // gmyth_util_string_to_time_val does not alloc ! sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"startChanId"), "%d", &(epg->startChanId)); sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"endChanId"), "%d", &(epg->endChanId)); @@ -133,22 +173,20 @@ get_Channel_List(nodeTab->children->next->children->next, epg); } -/** Retrieves the Eletronic Program Guide from the backend +/** Aux function to retrieve the Eletronic Program Guide * * @param doc An XML document (xmlDocPtr) * @return The epg */ -void getEpg (xmlDocPtr doc) +void getEpg (xmlDocPtr doc, GMythEpg* epg) { xmlXPathObjectPtr result; xmlNodeSetPtr nodeset; xmlChar *keyword; - GMythEpg epg; int i; result = getXPath((xmlChar *)"/*", doc); - if (result) { nodeset = result->nodesetval; @@ -156,13 +194,48 @@ { keyword = (xmlChar*)nodeset->nodeTab[i]->name; if (g_ascii_strcasecmp((char *)keyword, "ProgramGuide") == 0) - get_ProgramGuide_Properties(nodeset->nodeTab[i], &epg); + get_ProgramGuide_Properties(nodeset->nodeTab[i], epg); } xmlXPathFreeObject (result); } } +/** Retrieves the Eletronic Program Guide from the backend + * + * @param doc An XML document (xmlDocPtr) + * @return The epg + */ +GMythEpg retrieve_epg (GMythBackendInfo *backend_info, int port, \ + GTimeVal* StartTime, GTimeVal* EndTime, \ + gint StartChanId, gint NumOfChannels, \ + gchar* Details) +{ + GMythEpg epg; + MemoryStruct chunk; + + chunk.memory=NULL; /* we expect realloc(NULL, size) to work */ + chunk.size = 0; /* no data at this point */ + + gchar* starttime = (gchar*)g_malloc(sizeof(gchar)*20); + starttime = gmyth_util_time_to_mythformat_from_time_val(StartTime); + + gchar* endtime = (gchar*)g_malloc(sizeof(gchar)*20); + endtime = gmyth_util_time_to_mythformat_from_time_val(EndTime); + + GString* command = g_string_new(""); + g_string_printf(command, "getProgramGuide?StartTime=%s&EndTime=%s&StartChanId=%d" + "&NumOfChannels=%d&Details=%s", starttime, endtime, \ + StartChanId, NumOfChannels, Details); + + chunk = gmyth_http_request(backend_info, command); + xmlDocPtr doc = XMLParse(chunk.memory, strlen(chunk.memory)); + getEpg(doc, &epg); + free(chunk.memory); + + return epg; +} + /* Aux functions got from libcurl */ void *myrealloc (void *ptr, size_t size) { @@ -177,16 +250,18 @@ size_t WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data) { - size_t realsize = size * nmemb; - struct MemoryStruct *mem = (struct MemoryStruct *)data; + size_t realsize = size * nmemb; + MemoryStruct *mem = (struct _MemoryStruct *)data; - mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1); - if (mem->memory) { - memcpy(&(mem->memory[mem->size]), ptr, realsize); - mem->size += realsize; - mem->memory[mem->size] = 0; - } - return realsize; + mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1); + if (mem->memory) + { + memcpy(&(mem->memory[mem->size]), ptr, realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; + } + + return realsize; } /** Create a String containing the URL with the command @@ -225,19 +300,17 @@ * @return A string with the response from the server * NULL if there is no response. */ -GString -*gmyth_http_request (GMythBackendInfo *backend_info, GString *command) +MemoryStruct gmyth_http_request (GMythBackendInfo *backend_info, GString *command) { - LIBXML_TEST_VERSION size_t size = strlen(backend_info->hostname) + strlen(command->str) + 13; gchar *URL = (gchar *)g_malloc(sizeof(gchar)*size); - g_snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str); + g_snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str); CURL *curl_handle; - struct MemoryStruct chunk; + MemoryStruct chunk; chunk.memory=NULL; /* we expect realloc(NULL, size) to work */ chunk.size = 0; /* no data at this point */ @@ -265,16 +338,6 @@ /* cleanup curl stuff */ curl_easy_cleanup(curl_handle); - - GString *response = NULL; - if(chunk.memory) - { - xmlDocPtr doc = XMLParse(chunk.memory, strlen(chunk.memory)); - getEpg(doc); - response = g_string_new(chunk.memory); - free(chunk.memory); - } - - return response; + return chunk; } diff -r 9d4af01c6a2f -r bbf456814771 gmyth/src/gmyth_http.h --- a/gmyth/src/gmyth_http.h Thu Jan 25 18:17:40 2007 +0000 +++ b/gmyth/src/gmyth_http.h Thu Jan 25 21:40:30 2007 +0000 @@ -48,12 +48,18 @@ G_BEGIN_DECLS #define MYTH_PORT_STATUS 6544 -#define BUFLEN 2048 typedef struct _GMythPacket GMythPacket; typedef struct _GMythProgram GMythProgram; typedef struct _GMythChannel GMythChannel; typedef struct _GMythEpg GMythEpg; +typedef struct _MemoryStruct MemoryStruct; + +struct _MemoryStruct +{ + char *memory; + size_t size; +}; struct _GMythPacket { @@ -72,8 +78,6 @@ GTimeVal* endTime; }; -//gmyth_util_string_to_time_val ( const gchar* time_str ) - struct _GMythChannel { GString* channelName; @@ -98,8 +102,8 @@ GSList* channelList; }; -GString* gmyth_http_create_command(GString *command, ...); -GString *gmyth_http_request (GMythBackendInfo *backend_info, GString *command); +GMythEpg retrieve_epg(GMythBackendInfo *backend_info, int port, GTimeVal* StartTime, GTimeVal* EndTime, gint StartChanId, gint NumOfChannels, gchar* Details); +MemoryStruct gmyth_http_request (GMythBackendInfo *backend_info, GString *command); G_END_DECLS diff -r 9d4af01c6a2f -r bbf456814771 gmyth/src/gmyth_util.c --- a/gmyth/src/gmyth_util.c Thu Jan 25 18:17:40 2007 +0000 +++ b/gmyth/src/gmyth_util.c Thu Jan 25 21:40:30 2007 +0000 @@ -153,7 +153,7 @@ } /** Converts a time_t struct in a GString at ISO standard format - * (e.g. 2006-07-20T09:56:41). + * (e.g. 2006-07-20 09:56:41). * * The returned GString memory should be deallocated from * the calling function. @@ -171,6 +171,22 @@ return result; } +/** Converts a time_t struct in a GString at ISO standard format 2 + * (e.g. 2006-07-20T09:56:41). + * + * The returned GString memory should be deallocated from + * the calling function. + * + * @param time_value the GTimeValue to be converted + * @return GString* the converted isoformat string + */ +gchar* +gmyth_util_time_to_mythformat_from_time_val ( const GTimeVal* time ) +{ + gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%dT%H:%M:%S", time ); + return result; +} + /** Converts a time_t struct in a GString at ISO standard format * (e.g. 2006-07-20T09:56:41). * diff -r 9d4af01c6a2f -r bbf456814771 gmyth/src/gmyth_util.h --- a/gmyth/src/gmyth_util.h Thu Jan 25 18:17:40 2007 +0000 +++ b/gmyth/src/gmyth_util.h Thu Jan 25 21:40:30 2007 +0000 @@ -45,10 +45,11 @@ gchar* gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, const GTimeVal* time_val ); GTimeVal *gmyth_util_string_to_time_val_fmt ( const gchar *fmt_string, const gchar* time_str ); - + GTimeVal *gmyth_util_string_to_time_val ( const gchar* time_str ); gchar *gmyth_util_time_to_isoformat_from_time_val( const GTimeVal *time); +gchar *gmyth_util_time_to_mythformat_from_time_val ( const GTimeVal* time ); gchar *gmyth_util_time_to_string_only_date ( const GTimeVal* time ); diff -r 9d4af01c6a2f -r bbf456814771 gmyth/tests/http.c --- a/gmyth/tests/http.c Thu Jan 25 18:17:40 2007 +0000 +++ b/gmyth/tests/http.c Thu Jan 25 21:40:30 2007 +0000 @@ -14,12 +14,11 @@ gmyth_backend_info_set_hostname (backend_info, "localhost"); gmyth_backend_info_set_port (backend_info, 6543); - GString *command = g_string_new(argv[1]); + GTimeVal* start = gmyth_util_string_to_time_val("2006-01-01T00:00"); + GTimeVal* end = gmyth_util_string_to_time_val("2007-01-01T00:00"); + GMythEpg epg; + epg = retrieve_epg(backend_info, 6544, start, end, 0, 2, "True"); - //GString *new_command = gmyth_http_create_command(command, "ChanId", "1001", "StartTime", "2006-12-20T00:05:00", NULL); - GString *result = gmyth_http_request(backend_info, command); - - printf("%s", result->str); return 0; }