On Wed, 15 Dec 2010 13:49:45 -0800
"Enlightenment SVN" <[email protected]> wrote:

> Log:
> add azy_client_get() for [Rui]: attempt 1
>   
> 
> Author:       discomfitor
> Date:         2010-12-15 13:49:45 -0800 (Wed, 15 Dec 2010)
> New Revision: 55565
> Trac:         http://trac.enlightenment.org/e/changeset/55565
> 
> Modified:
>   trunk/PROTO/azy/src/include/Azy.h trunk/PROTO/azy/src/include/azy_private.h
> trunk/PROTO/azy/src/lib/azy_client.c
> trunk/PROTO/azy/src/lib/azy_client_events.c
> trunk/PROTO/azy/src/lib/azy_content.c
> trunk/PROTO/azy/src/lib/azy_content_json.c 
> 
> Modified: trunk/PROTO/azy/src/include/Azy.h
> ===================================================================
> --- trunk/PROTO/azy/src/include/Azy.h 2010-12-15 21:47:46 UTC (rev
> 55564) +++ trunk/PROTO/azy/src/include/Azy.h  2010-12-15 21:49:45 UTC
> (rev 55565) @@ -472,6 +472,10 @@
>                                         Azy_Content      *content,
>                                         Azy_Net_Transport transport,
>                                         Azy_Content_Cb    cb);
> +   EAPI Azy_Client_Call_Id azy_client_get(Azy_Client       *client,
> +
> const char       *uri,
> +
> Azy_Content_Cb    cb,
> +
> void             *data); EAPI Azy_Client_Call_Id azy_client_send(Azy_Client
> *client, unsigned char *data,
>                                        int            length);
> 
> Modified: trunk/PROTO/azy/src/include/azy_private.h
> ===================================================================
> --- trunk/PROTO/azy/src/include/azy_private.h 2010-12-15 21:47:46 UTC
> (rev 55564) +++ trunk/PROTO/azy/src/include/azy_private.h     2010-12-15
> 21:49:45 UTC (rev 55565) @@ -213,6 +213,7 @@
>     Azy_Client         *client;
>     Azy_Net            *recv;
>     const char         *method;
> +   Azy_Net_Transport   transport;
>     Azy_Content_Cb      callback; //callback set to convert from Azy_Value to
> Return_Type void               *content_data;
>  };
> @@ -270,8 +271,17 @@
>  Eina_Bool azy_server_client_handler_add(Azy_Server                *server,
>                                           int                         type,
>                                           Ecore_Con_Event_Client_Add *ev);
> -void      _azy_event_handler_fake_free(void *data __UNUSED__, void *data2);
> +void      _azy_event_handler_fake_free(void *data, void *data2);
>  
> +Azy_Value *
> +azy_content_unserialize(Azy_Net_Transport type,
> +                        const char   *buf,
> +                        ssize_t       len);
> +
> +Azy_Value *
> +azy_content_unserialize_json(const char *buf,
> +                             ssize_t len __UNUSED__);
> +
>  #ifdef HAVE_XML
>  #ifdef __cplusplus 
>  extern "C" {
> 
> Modified: trunk/PROTO/azy/src/lib/azy_client.c
> ===================================================================
> --- trunk/PROTO/azy/src/lib/azy_client.c      2010-12-15 21:47:46 UTC (rev
> 55564) +++ trunk/PROTO/azy/src/lib/azy_client.c       2010-12-15 21:49:45
> UTC (rev 55565) @@ -503,6 +503,100 @@
>  }
>  
>  /**
> + * @brief Make an HTTP GET request using a connected client
> + * 
> + * This function is used to make a GET request using @p client to @p uri of
> the client's
> + * #Azy_Net object using content-type defined by @p transport and the
> deserialization
> + * function specified by @p cb.
> + * @param client The client (NOT #NULL)
> + * @param uri The uri path to GET
> + * @param cb The deserialization callback to use for the response (NOT #NULL)
> + * @param data The user data to be passed to resulting callbacks
> + * @return The #Azy_Client_Call_Id of the transmission, to be used with
> azy_client_callback_set,
> + * or 0 on failure
> + */
> +Azy_Client_Call_Id
> +azy_client_get(Azy_Client       *client,
> +               const char       *uri,
> +               Azy_Content_Cb    cb,
> +               void             *data)
> +{
> +   Eina_Strbuf *msg;
> +   Azy_Client_Handler_Data *handler_data;
> +
> +   DBG("(client=%p, net=%p, uri=%s)", client, client->net, uri);
> +
> +   if (!AZY_MAGIC_CHECK(client, AZY_MAGIC_CLIENT))
> +     {
> +        AZY_MAGIC_FAIL(client, AZY_MAGIC_CLIENT);
> +        return 0;
> +     }
> +   EINA_SAFETY_ON_NULL_RETURN_VAL(client->net, 0);
> +
> +   if (!client->connected)
> +     {
> +        ERR("Can't perform HTTP GET on closed connection.");
> +        return 0;
> +     }
> +
> +   while (++azy_client_send_id__ < 1);
> +
> +   azy_net_type_set(client->net, AZY_NET_TYPE_GET);
> +   if (uri && uri[0])
> +     azy_net_uri_set(client->net, uri);
> +   else
> +     {
> +        if (client->net->http.req.http_path)
> +          WARN("NULL URI passed, using previously set uri '%s'",
> client->net->http.req.http_path);
> +        else
> +          {
> +             WARN("NULL URI passed, defaulting to \"/\"");
> +             azy_net_uri_set(client->net, "/");
> +          }
> +     }
> +
> +   msg = azy_net_header_create(client->net);
> +   EINA_SAFETY_ON_NULL_GOTO(msg, error);
> +
> +#ifdef ISCOMFITOR
> +   char buf[64];
> +   snprintf(buf, sizeof(buf), "\nSENDING
> >>>>>>>>>>>>>>>>>>>>>>>>\n%%.%zus\n>>>>>>>>>>>>>>>>>>>>>>>>",
> +            eina_strbuf_length_get(msg));
> +   DBG(buf, eina_strbuf_string_get(msg));
> +#endif
> +
> +   EINA_SAFETY_ON_TRUE_GOTO(!ecore_con_server_send(client->net->conn,
> eina_strbuf_string_get(msg), eina_strbuf_length_get(msg)), error);
> +   INFO("Send [1/1] complete! %zu bytes queued for sending.",
> eina_strbuf_length_get(msg));
> +   eina_strbuf_free(msg);
> +   msg = NULL;
> +
> +   ecore_con_server_flush(client->net->conn);
> + 
> +   handler_data = calloc(1, sizeof(Azy_Client_Handler_Data));
> +   EINA_SAFETY_ON_NULL_RETURN_VAL(handler_data, 0);
> +   handler_data->client = client;
> +   handler_data->callback = cb;
> +   handler_data->content_data = data;
> +
> +   handler_data->id = azy_client_send_id__;
> +   AZY_MAGIC_SET(handler_data, AZY_MAGIC_CLIENT_DATA_HANDLER);
> +   if (!client->conns)
> +     {
> +        client->recv =  ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA,
> +
> (Ecore_Event_Handler_Cb)_azy_client_handler_data, handler_data);
> +        ecore_con_server_data_set(client->net->conn, client);
> +     }
> +     
> +   client->conns = eina_list_append(client->conns, handler_data);
> +
> +   DBG("(client=%p, net=%p, handler_data=%p)", client, client->net,
> handler_data);
> +   return azy_client_send_id__;
> +error:
> +   if (msg) eina_strbuf_free(msg);
> +   return 0;
> +}
> +
> +/**
>   * @brief Send arbitrary data to a connected server
>   * 
>   * This function is used to send arbitrary data to a connected server using
> @p client. @@ -572,6 +666,7 @@
>     return 0;
>  }
>  
> +
>  /**
>   * @brief Validate a transmission attempt
>   * 
> 
> Modified: trunk/PROTO/azy/src/lib/azy_client_events.c
> ===================================================================
> --- trunk/PROTO/azy/src/lib/azy_client_events.c       2010-12-15 21:47:46
> UTC (rev 55564) +++ trunk/PROTO/azy/src/lib/azy_client_events.c
> 2010-12-15 21:49:45 UTC (rev 55565) @@ -24,6 +24,7 @@
>  #include "azy_private.h"
>  
>  static void _azy_client_handler_call_free(Azy_Client *client, Azy_Content
> *content); +static Eina_Bool _azy_client_handler_get(Azy_Client_Handler_Data
> *handler_data); static Eina_Bool
> _azy_client_handler_call(Azy_Client_Handler_Data *handler_data); static void
> _azy_client_handler_data_free(Azy_Client_Handler_Data *data); static
> Eina_Bool _azy_client_recv_timer(Azy_Client_Handler_Data *handler_data); @@
> -81,7 +82,67 @@ free(handler_data);
>  }
>  
> +/* FIXME: code dupication */
>  static Eina_Bool
> +_azy_client_handler_get(Azy_Client_Handler_Data *handler_data)
> +{
> +   void *ret = NULL;
> +   Azy_Content *content;
> +   Azy_Client_Return_Cb cb;
> +   Azy_Client *client;
> +
> +   DBG("(handler_data=%p, client=%p, net=%p)", handler_data,
> handler_data->client, handler_data->recv); +
> +   client = handler_data->client;
> +   handler_data->recv->transport =
> azy_events_net_transport_get(azy_net_header_get(handler_data->recv,
> "content-type"));
> +   content = azy_content_new(NULL);
> +
> +   EINA_SAFETY_ON_NULL_RETURN_VAL(content, ECORE_CALLBACK_RENEW);
> +
> +   content->data = handler_data->content_data;
> +
> +   content->retval = azy_content_unserialize(handler_data->recv->transport,
> (const char*)handler_data->recv->buffer, handler_data->recv->size);
> +   if (!content->retval)
> +     azy_content_error_faultmsg_set(content, AZY_CLIENT_ERROR_MARSHALIZER,
> "Call return parsing failed.");
> +   else if (handler_data->callback && content->retval &&
> (!handler_data->callback(content->retval, &ret)))
> +     azy_content_error_faultmsg_set(content, AZY_CLIENT_ERROR_MARSHALIZER,
> "Call return value demarshalization failed."); +
> +   if (azy_content_error_is_set(content))
> +     {
> +        char buf[64];
> +        snprintf(buf, sizeof(buf),
> "%s:\n<<<<<<<<<<<<<\n%%.%llis\n<<<<<<<<<<<<<", handler_data->method,
> handler_data->recv->size);
> +        ERR(buf, handler_data->recv->buffer);
> +     }
> +
> +   content->id = handler_data->id;
> +   content->ret = ret;
> +   content->recv_net = handler_data->recv;
> +   handler_data->recv = NULL;
> +
> +   _azy_client_handler_data_free(handler_data);
> +
> +   cb = eina_hash_find(client->callbacks, &content->id);
> +   if (cb)
> +     {
> +        Eina_Error ret;
> +        ret = cb(client, content, content->ret);
> +        
> +        ecore_event_add(AZY_CLIENT_RESULT, &ret,
> (Ecore_End_Cb)_azy_event_handler_fake_free, NULL);
> +        eina_hash_del_by_key(client->callbacks, &content->id);
> +        _azy_client_handler_call_free(client, content);
> +     }
> +   else
> +     {
> +       if (!azy_content_error_is_set(content))
> +         ecore_event_add(AZY_CLIENT_RETURN, content,
> (Ecore_End_Cb)_azy_client_handler_call_free, client);
> +       else
> +         ecore_event_add(AZY_CLIENT_ERROR, content,
> (Ecore_End_Cb)_azy_client_handler_call_free, client);
> +     }
> +
> +   return ECORE_CALLBACK_RENEW;
> +}
> +
> +static Eina_Bool
>  _azy_client_handler_call(Azy_Client_Handler_Data *handler_data)
>  {
>     void *ret = NULL;
> @@ -97,7 +158,8 @@
>     snprintf(buf, sizeof(buf),
> "RECEIVED:\n<<<<<<<<<<<<<\n%%.%llis\n<<<<<<<<<<<<<",
> handler_data->recv->size); INFO(buf, handler_data->recv->buffer); #endif
> -
> +   /* handle HTTP GET request */
> +   if (!handler_data->method) return _azy_client_handler_get(handler_data);
>     INFO("Running RPC for %s", handler_data->method);
>     handler_data->recv->transport =
> azy_events_net_transport_get(azy_net_header_get(handler_data->recv,
> "content-type")); content = azy_content_new(handler_data->method);
> 
> Modified: trunk/PROTO/azy/src/lib/azy_content.c
> ===================================================================
> --- trunk/PROTO/azy/src/lib/azy_content.c     2010-12-15 21:47:46 UTC (rev
> 55564) +++ trunk/PROTO/azy/src/lib/azy_content.c      2010-12-15 21:49:45
> UTC (rev 55565) @@ -49,6 +49,39 @@
>     return EINA_TRUE;
>  }
>  
> +/*
> + * @brief Convert the xml/json parameters to #Azy_Value
> + * 
> + * This function converts a block of xml/json (based on @p type)
> + */
> +Azy_Value *
> +azy_content_unserialize(Azy_Net_Transport type,
> +                        const char   *buf,
> +                        ssize_t       len)
> +{
> +   EINA_SAFETY_ON_NULL_RETURN_VAL(buf, NULL);
> +   EINA_SAFETY_ON_TRUE_RETURN_VAL(!buf[0], NULL);
> +   EINA_SAFETY_ON_TRUE_RETURN_VAL(!len, NULL);
> +
> +   if (type == AZY_NET_TRANSPORT_JSON)
> +     return azy_content_unserialize_json(buf, len);
> +
> +
> +   ERR("UNSUPPORTED TYPE PASSED! FIXME!");
> +   return NULL;
> +#if 0
> +   if (type == AZY_NET_TRANSPORT_XML)
> +     {
> +#ifdef HAVE_XML
> +        return azy_content_unserialize_xml(content, buf, len);
> +#else
> +        ERR("%s", eina_error_msg_get(AZY_ERROR_XML_UNSUPPORTED));
> +        return NULL;
> +#endif
> +     }
> +#endif
> +}
> +
>  /**
>   * @brief Create a new content object
>   * 
> 
> Modified: trunk/PROTO/azy/src/lib/azy_content_json.c
> ===================================================================
> --- trunk/PROTO/azy/src/lib/azy_content_json.c        2010-12-15 21:47:46 UTC
> (rev 55564) +++ trunk/PROTO/azy/src/lib/azy_content_json.c    2010-12-15
> 21:49:45 UTC (rev 55565) @@ -240,6 +240,24 @@
>     return EINA_TRUE;
>  }
>  
> +Azy_Value *
> +azy_content_unserialize_json(const char *buf,
> +                             ssize_t len __UNUSED__)
> +{
> +   cJSON *object;
> +
> +   EINA_SAFETY_ON_NULL_RETURN_VAL(buf, NULL);
> +
> +   if (!(object = cJSON_Parse(buf)))
> +     {
> +        ERR("%s", eina_error_msg_get(AZY_ERROR_REQUEST_JSON_OBJECT));
> +        return NULL;
> +     }
> +
> +   return azy_value_unserialize_json(object);
> +}
> +
> +
>  Eina_Bool
>  azy_content_unserialize_request_json(Azy_Content *content,
>                                        const char *buf,
> 
> 
> ------------------------------------------------------------------------------
> Lotusphere 2011
> Register now for Lotusphere 2011 and learn how
> to connect the dots, take your collaborative environment
> to the next level, and enter the era of Social Business.
> http://p.sf.net/sfu/lotusphere-d2d
> _______________________________________________
> enlightenment-svn mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/enlightenment-svn
Going to need an address to test this with (something that returns a json
object like you'll be parsing normally) so I can make a demo client.

-- 
Mike Blumenkrantz
Zentific: We run the three-legged race individually.

------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
enlightenment-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/enlightenment-devel

Reply via email to