[svn r307] Finished getEpg via HTTP+XML. trunk
authormorphbr
Thu Jan 25 21:40:30 2007 +0000 (2007-01-25)
branchtrunk
changeset 306bbf456814771
parent 305 9d4af01c6a2f
child 307 0a8fe4427e41
[svn r307] Finished getEpg via HTTP+XML.
gmyth/src/gmyth_http.c
gmyth/src/gmyth_http.h
gmyth/src/gmyth_util.c
gmyth/src/gmyth_util.h
gmyth/tests/http.c
     1.1 --- a/gmyth/src/gmyth_http.c	Thu Jan 25 18:17:40 2007 +0000
     1.2 +++ b/gmyth/src/gmyth_http.c	Thu Jan 25 21:40:30 2007 +0000
     1.3 @@ -40,12 +40,6 @@
     1.4  #include "gmyth_debug.h"
     1.5  #include "gmyth_socket.h"
     1.6  
     1.7 -struct MemoryStruct 
     1.8 -{
     1.9 -  char *memory;
    1.10 -  size_t size;
    1.11 -};
    1.12 -
    1.13  
    1.14  xmlXPathObjectPtr
    1.15  getnodeset(xmlDocPtr doc, xmlChar *xpath)
    1.16 @@ -89,31 +83,77 @@
    1.17      return result;
    1.18  }
    1.19  
    1.20 -void get_Channel_List (xmlNodePtr nodeTab, GMythEpg* epg)
    1.21 +/** Retrieves the Progam List from the Channel
    1.22 + *
    1.23 + * @param nodeTab A pointer to a node inside the XML
    1.24 + * @return A GSList containing a list of all the programs
    1.25 + */
    1.26 +GSList* get_Program_List(xmlNodePtr node)
    1.27 +{
    1.28 +	GSList* program_list = NULL;
    1.29 +	
    1.30 +	while (node != NULL)
    1.31 +	{
    1.32 +		if (g_ascii_strcasecmp((char *)node->name, "text") != 0)
    1.33 +		{
    1.34 +			GMythProgram* program = (GMythProgram*)g_malloc(sizeof(struct _GMythProgram));
    1.35 +
    1.36 +			program->title = g_string_new((char *)xmlGetProp(node, (xmlChar *)"title"));
    1.37 +			program->subtitle = g_string_new((char *)xmlGetProp(node, (xmlChar *)"subtitle"));
    1.38 +			program->catType = g_string_new((char *)xmlGetProp(node, (xmlChar *)"catType"));
    1.39 +    		program->category = g_string_new((char *)xmlGetProp(node, (xmlChar *)"category"));
    1.40 +			
    1.41 +			sscanf ((char *)xmlGetProp(node, (xmlChar *)"repeat"), "%d", &(program->repeat));
    1.42 +
    1.43 +			program->startTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(node, \
    1.44 +																		(xmlChar *)"startTime"));
    1.45 +			program->endTime = gmyth_util_string_to_time_val ((char *)xmlGetProp(node, \
    1.46 +																		(xmlChar *)"endTime"));
    1.47 +
    1.48 +			program_list = g_slist_append(program_list, program);
    1.49 +		}
    1.50 +
    1.51 +		node = node->next;
    1.52 +	}
    1.53 +
    1.54 +	return program_list;
    1.55 +}
    1.56 +
    1.57 +/** Retrieves the Channel List from the ProgramGuide
    1.58 + *
    1.59 + * @param node A pointer to a node inside the XML
    1.60 + * @param epg The struct where is the current epg
    1.61 + * @return The epg from "param" updated
    1.62 + */
    1.63 +void get_Channel_List (xmlNodePtr node, GMythEpg* epg)
    1.64  {
    1.65  	int i;
    1.66  	epg->channelList = NULL;
    1.67  
    1.68 -	for(i=1; i <= epg->numOfChannels; i++)
    1.69 +	for (i=1; i <= epg->numOfChannels; i++)
    1.70  	{
    1.71  		GMythChannel* channel = (GMythChannel*)g_malloc(sizeof(struct _GMythChannel));
    1.72  		
    1.73 -		channel->channelName = g_string_new((char *)xmlGetProp(nodeTab, (xmlChar *)"channelName"));
    1.74 -		channel->chanNum = g_string_new((char *)xmlGetProp(nodeTab, (xmlChar *)"chanNum"));
    1.75 +		channel->channelName = g_string_new((char *)xmlGetProp(node, (xmlChar *)"channelName"));
    1.76 +		channel->chanNum = g_string_new((char *)xmlGetProp(node, (xmlChar *)"chanNum"));
    1.77  
    1.78 -		sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"chanId"), "%d", &(channel->chanId));
    1.79 -		sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"callSign"), "%d", &(channel->callSign));
    1.80 +		sscanf ((char *)xmlGetProp(node, (xmlChar *)"chanId"), "%d", &(channel->chanId));
    1.81 +		sscanf ((char *)xmlGetProp(node, (xmlChar *)"callSign"), "%d", &(channel->callSign));
    1.82 +
    1.83 +		channel->programList = get_Program_List(node->children);
    1.84  
    1.85  		epg->channelList = g_slist_append(epg->channelList, channel);
    1.86 -		//get_Program_List(nodeTab, channel);
    1.87 -		//TODO: programlist
    1.88  	}
    1.89  }
    1.90  
    1.91 +/** Retrieves the properties from the ProgramGuide
    1.92 + *
    1.93 + * @param nodeTab A pointer to a node inside the XML
    1.94 + * @param epg The struct where is the current epg
    1.95 + * @return The epg from "param" updated
    1.96 + */
    1.97  void get_ProgramGuide_Properties (xmlNodePtr nodeTab, GMythEpg* epg)
    1.98  {
    1.99 -	//TODO: alloc GTimeVal -> it's loosing the value...
   1.100 -	//		gmyth_util_string_to_time_val does not alloc !
   1.101  	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"startChanId"), "%d", &(epg->startChanId));
   1.102  	sscanf ((char *)xmlGetProp(nodeTab, (xmlChar *)"endChanId"), "%d", &(epg->endChanId));
   1.103  
   1.104 @@ -133,22 +173,20 @@
   1.105  	get_Channel_List(nodeTab->children->next->children->next, epg);
   1.106  }
   1.107  
   1.108 -/** Retrieves the Eletronic Program Guide from the backend
   1.109 +/** Aux function to retrieve the Eletronic Program Guide
   1.110   *
   1.111   * @param doc An XML document (xmlDocPtr)
   1.112   * @return The epg
   1.113   */
   1.114 -void getEpg (xmlDocPtr doc)
   1.115 +void getEpg (xmlDocPtr doc, GMythEpg* epg)
   1.116  {
   1.117      xmlXPathObjectPtr result;
   1.118      xmlNodeSetPtr nodeset;
   1.119      xmlChar *keyword;
   1.120 -	GMythEpg epg;
   1.121  
   1.122      int i;
   1.123      result = getXPath((xmlChar *)"/*", doc);
   1.124  
   1.125 -
   1.126      if (result) 
   1.127      {
   1.128          nodeset = result->nodesetval;
   1.129 @@ -156,13 +194,48 @@
   1.130  		{
   1.131  			keyword = (xmlChar*)nodeset->nodeTab[i]->name;
   1.132  			if (g_ascii_strcasecmp((char *)keyword, "ProgramGuide") == 0)
   1.133 -				get_ProgramGuide_Properties(nodeset->nodeTab[i], &epg);
   1.134 +				get_ProgramGuide_Properties(nodeset->nodeTab[i], epg);
   1.135  		}
   1.136          xmlXPathFreeObject (result);
   1.137      }
   1.138  
   1.139  }
   1.140  
   1.141 +/** Retrieves the Eletronic Program Guide from the backend
   1.142 + *
   1.143 + * @param doc An XML document (xmlDocPtr)
   1.144 + * @return The epg
   1.145 + */
   1.146 +GMythEpg retrieve_epg (GMythBackendInfo *backend_info, int port, \
   1.147 +						GTimeVal* StartTime, GTimeVal* EndTime, \
   1.148 +						gint StartChanId, gint NumOfChannels, \
   1.149 +						gchar* Details)
   1.150 +{
   1.151 +	GMythEpg epg;
   1.152 +	MemoryStruct chunk;
   1.153 +
   1.154 +    chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
   1.155 +    chunk.size = 0;    /* no data at this point */
   1.156 +
   1.157 +	gchar* starttime = (gchar*)g_malloc(sizeof(gchar)*20);
   1.158 +	starttime = gmyth_util_time_to_mythformat_from_time_val(StartTime);
   1.159 +
   1.160 +	gchar* endtime = (gchar*)g_malloc(sizeof(gchar)*20);
   1.161 +	endtime = gmyth_util_time_to_mythformat_from_time_val(EndTime);
   1.162 +
   1.163 +	GString* command = g_string_new("");
   1.164 +	g_string_printf(command, "getProgramGuide?StartTime=%s&EndTime=%s&StartChanId=%d"
   1.165 +							 "&NumOfChannels=%d&Details=%s", starttime, endtime, \
   1.166 +							 StartChanId, NumOfChannels, Details);
   1.167 +
   1.168 +	chunk = gmyth_http_request(backend_info, command);
   1.169 +    xmlDocPtr doc = XMLParse(chunk.memory, strlen(chunk.memory));
   1.170 +    getEpg(doc, &epg);
   1.171 +    free(chunk.memory);
   1.172 +
   1.173 +	return epg;
   1.174 +}
   1.175 +
   1.176  /* Aux functions got from libcurl */
   1.177  void *myrealloc (void *ptr, size_t size)
   1.178  {
   1.179 @@ -177,16 +250,18 @@
   1.180  size_t
   1.181  WriteMemoryCallback (void *ptr, size_t size, size_t nmemb, void *data)
   1.182  {
   1.183 -  size_t realsize = size * nmemb;
   1.184 -  struct MemoryStruct *mem = (struct MemoryStruct *)data;
   1.185 +	size_t realsize = size * nmemb;
   1.186 +	MemoryStruct *mem = (struct _MemoryStruct *)data;
   1.187  
   1.188 -  mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
   1.189 -  if (mem->memory) {
   1.190 -    memcpy(&(mem->memory[mem->size]), ptr, realsize);
   1.191 -    mem->size += realsize;
   1.192 -    mem->memory[mem->size] = 0;
   1.193 -  }
   1.194 -  return realsize;
   1.195 +	mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
   1.196 +	if (mem->memory) 
   1.197 +	{
   1.198 +		memcpy(&(mem->memory[mem->size]), ptr, realsize);
   1.199 +		mem->size += realsize;
   1.200 +		mem->memory[mem->size] = 0;
   1.201 +	}
   1.202 +
   1.203 +	return realsize;
   1.204  }
   1.205  
   1.206  /** Create a String containing the URL with the command
   1.207 @@ -225,19 +300,17 @@
   1.208   * @return A string with the response from the server
   1.209   *          NULL if there is no response.
   1.210   */
   1.211 -GString 
   1.212 -*gmyth_http_request (GMythBackendInfo *backend_info, GString *command)
   1.213 +MemoryStruct gmyth_http_request (GMythBackendInfo *backend_info, GString *command)
   1.214  {
   1.215 -	
   1.216      LIBXML_TEST_VERSION
   1.217  
   1.218      size_t size = strlen(backend_info->hostname) + strlen(command->str) + 13;
   1.219      gchar *URL = (gchar *)g_malloc(sizeof(gchar)*size);
   1.220 -    g_snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str);     
   1.221 +    g_snprintf(URL, size+1, "http://%s:6544/%s", backend_info->hostname, command->str);
   1.222  
   1.223      CURL *curl_handle;
   1.224      
   1.225 -    struct MemoryStruct chunk;
   1.226 +    MemoryStruct chunk;
   1.227      
   1.228      chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
   1.229      chunk.size = 0;    /* no data at this point */
   1.230 @@ -265,16 +338,6 @@
   1.231      
   1.232      /* cleanup curl stuff */
   1.233      curl_easy_cleanup(curl_handle);
   1.234 -    
   1.235 -    GString *response = NULL;
   1.236  
   1.237 -    if(chunk.memory)
   1.238 -    {
   1.239 -        xmlDocPtr doc = XMLParse(chunk.memory, strlen(chunk.memory));
   1.240 -        getEpg(doc);
   1.241 -        response = g_string_new(chunk.memory);
   1.242 -        free(chunk.memory);
   1.243 -    }
   1.244 -
   1.245 -    return response;
   1.246 +    return chunk;
   1.247  }
     2.1 --- a/gmyth/src/gmyth_http.h	Thu Jan 25 18:17:40 2007 +0000
     2.2 +++ b/gmyth/src/gmyth_http.h	Thu Jan 25 21:40:30 2007 +0000
     2.3 @@ -48,12 +48,18 @@
     2.4  G_BEGIN_DECLS
     2.5  
     2.6  #define MYTH_PORT_STATUS 6544
     2.7 -#define BUFLEN 2048
     2.8  
     2.9  typedef struct _GMythPacket     GMythPacket;
    2.10  typedef struct _GMythProgram    GMythProgram;
    2.11  typedef struct _GMythChannel	GMythChannel;
    2.12  typedef struct _GMythEpg		GMythEpg;
    2.13 +typedef struct _MemoryStruct MemoryStruct;
    2.14 +
    2.15 +struct _MemoryStruct
    2.16 +{
    2.17 +  char *memory;
    2.18 +  size_t size;
    2.19 +};
    2.20  
    2.21  struct _GMythPacket
    2.22  {
    2.23 @@ -72,8 +78,6 @@
    2.24  	GTimeVal* endTime;
    2.25  };
    2.26  
    2.27 -//gmyth_util_string_to_time_val ( const gchar* time_str )
    2.28 -
    2.29  struct _GMythChannel
    2.30  {
    2.31  	GString* channelName;
    2.32 @@ -98,8 +102,8 @@
    2.33  	GSList* channelList;
    2.34  };
    2.35  
    2.36 -GString* gmyth_http_create_command(GString *command, ...);
    2.37 -GString *gmyth_http_request (GMythBackendInfo *backend_info, GString *command);
    2.38 +GMythEpg retrieve_epg(GMythBackendInfo *backend_info, int port, GTimeVal* StartTime, GTimeVal* EndTime, gint StartChanId, gint NumOfChannels, gchar* Details);
    2.39 +MemoryStruct gmyth_http_request (GMythBackendInfo *backend_info, GString *command);
    2.40  
    2.41  G_END_DECLS
    2.42  
     3.1 --- a/gmyth/src/gmyth_util.c	Thu Jan 25 18:17:40 2007 +0000
     3.2 +++ b/gmyth/src/gmyth_util.c	Thu Jan 25 21:40:30 2007 +0000
     3.3 @@ -153,7 +153,7 @@
     3.4  }
     3.5  
     3.6  /** Converts a time_t struct in a GString at ISO standard format 
     3.7 - * (e.g. 2006-07-20T09:56:41).
     3.8 + * (e.g. 2006-07-20 09:56:41).
     3.9   * 
    3.10   * The returned GString memory should be deallocated from 
    3.11   * the calling function.
    3.12 @@ -171,6 +171,22 @@
    3.13  	return result;	
    3.14  }
    3.15  
    3.16 +/** Converts a time_t struct in a GString at ISO standard format 2 
    3.17 + * (e.g. 2006-07-20T09:56:41).
    3.18 + * 
    3.19 + * The returned GString memory should be deallocated from 
    3.20 + * the calling function.
    3.21 + *
    3.22 + * @param time_value the GTimeValue to be converted
    3.23 + * @return GString* the converted isoformat string 
    3.24 + */
    3.25 +gchar*
    3.26 +gmyth_util_time_to_mythformat_from_time_val ( const GTimeVal* time )
    3.27 +{
    3.28 +	gchar *result = gmyth_util_time_to_isoformat_from_time_val_fmt( "%Y-%m-%dT%H:%M:%S", time );
    3.29 +	return result;	
    3.30 +}
    3.31 +
    3.32  /** Converts a time_t struct in a GString at ISO standard format 
    3.33   * (e.g. 2006-07-20T09:56:41).
    3.34   * 
     4.1 --- a/gmyth/src/gmyth_util.h	Thu Jan 25 18:17:40 2007 +0000
     4.2 +++ b/gmyth/src/gmyth_util.h	Thu Jan 25 21:40:30 2007 +0000
     4.3 @@ -45,10 +45,11 @@
     4.4  gchar* 			gmyth_util_time_to_isoformat_from_time_val_fmt ( const gchar *fmt_string, const GTimeVal* time_val );
     4.5  
     4.6  GTimeVal		*gmyth_util_string_to_time_val_fmt ( const gchar *fmt_string, const gchar* time_str );
     4.7 -                                         
     4.8 +
     4.9  GTimeVal		*gmyth_util_string_to_time_val ( const gchar* time_str );
    4.10  
    4.11  gchar 			*gmyth_util_time_to_isoformat_from_time_val( const GTimeVal *time);
    4.12 +gchar			*gmyth_util_time_to_mythformat_from_time_val ( const GTimeVal* time );
    4.13  
    4.14  gchar				*gmyth_util_time_to_string_only_date ( const GTimeVal* time );
    4.15  
     5.1 --- a/gmyth/tests/http.c	Thu Jan 25 18:17:40 2007 +0000
     5.2 +++ b/gmyth/tests/http.c	Thu Jan 25 21:40:30 2007 +0000
     5.3 @@ -14,12 +14,11 @@
     5.4      gmyth_backend_info_set_hostname (backend_info, "localhost");
     5.5      gmyth_backend_info_set_port (backend_info, 6543);
     5.6      
     5.7 -    GString *command = g_string_new(argv[1]);
     5.8 +    GTimeVal* start = gmyth_util_string_to_time_val("2006-01-01T00:00");
     5.9 +    GTimeVal* end = gmyth_util_string_to_time_val("2007-01-01T00:00");
    5.10 +    GMythEpg epg;
    5.11 +    epg  = retrieve_epg(backend_info, 6544, start, end, 0, 2, "True");
    5.12  
    5.13 -    //GString *new_command = gmyth_http_create_command(command, "ChanId", "1001", "StartTime", "2006-12-20T00:05:00", NULL);
    5.14 -    GString *result = gmyth_http_request(backend_info, command);
    5.15 -
    5.16 -    printf("%s", result->str);
    5.17  
    5.18      return 0;    
    5.19  }