Skip to content

Commit

Permalink
Add GError parameter to searpc_call_function
Browse files Browse the repository at this point in the history
  • Loading branch information
freeplant committed Nov 20, 2011
1 parent a0e894f commit 90f8ad5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
9 changes: 8 additions & 1 deletion demo/searpc-demo-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <netinet/in.h>
#include <arpa/inet.h>

#include <glib.h>
#include <glib-object.h>

#include "searpc-server.h"
#include "searpc-demo-packet.h"
#define BUFLEN 256
Expand Down Expand Up @@ -47,6 +50,7 @@ main(int argc, char *argv[])
socklen_t clilen;
char buf[BUFLEN];
packet *pac, *pac_ret;

g_type_init();

start_rpc_service();
Expand Down Expand Up @@ -83,6 +87,8 @@ main(int argc, char *argv[])
}

while (1) {
GError *error = NULL;

clilen = sizeof(client_addr);
connfd = accept(listenfd, (struct sockaddr *)&client_addr, &clilen);
if (connfd < 0) {
Expand All @@ -100,7 +106,8 @@ main(int argc, char *argv[])
gsize ret_len;
int fcall_len = ntohs(pac->length);
/* Execute the RPC function */
char *res = searpc_server_call_function (pac->data, fcall_len, &ret_len);
char *res = searpc_server_call_function (pac->data, fcall_len,
&ret_len, &error);

pac_ret = (packet *)buf;
pac_ret->length = htons((uint16_t)ret_len);
Expand Down
15 changes: 8 additions & 7 deletions lib/searpc-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ void
searpc_server_init ()
{
func_table = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, func_item_free);
NULL, (GDestroyNotify)func_item_free);
marshal_table = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, marshal_item_free);
NULL, (GDestroyNotify)marshal_item_free);

/* register buildin marshal functions */
register_marshals(marshal_table);
Expand Down Expand Up @@ -184,18 +184,18 @@ searpc_server_register_function (void *func, const gchar *fname, gchar *signatur

/* Called by RPC transport. */
gchar*
searpc_server_call_function (gchar *func, gsize len, gsize *ret_len)
searpc_server_call_function (gchar *func, gsize len, gsize *ret_len, GError **error)
{
JsonParser *parser;
GError *error = NULL;
JsonNode *root;
JsonArray *array;

g_return_val_if_fail (error == NULL || *error == NULL, NULL);

parser = json_parser_new ();

if (!json_parser_load_from_data (parser, func, len, &error)) {
g_warning ("[SeaRPC] failed to parse RPC call: %s\n", error->message);
g_error_free (error);
if (!json_parser_load_from_data (parser, func, len, error)) {
g_warning ("[SeaRPC] failed to parse RPC call: %s\n", (*error)->message);
g_object_unref (parser);
return NULL;
}
Expand All @@ -207,6 +207,7 @@ searpc_server_call_function (gchar *func, gsize len, gsize *ret_len)
FuncItem *fitem = g_hash_table_lookup(func_table, fname);
if (!fitem) {
g_warning ("[SeaRPC] cannot find function %s.\n", fname);
g_set_error (error, 0, 500, "cannot find function %s.", fname);
return NULL;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/searpc-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ gboolean searpc_server_register_function (void* func,
*
* Returns the serialized representatio of the returned value.
*/
gchar *searpc_server_call_function (gchar *func, gsize len, gsize *ret_len);
gchar *searpc_server_call_function (gchar *func, gsize len, gsize *ret_len,
GError **error);

/**
* searpc_compute_signature:
Expand Down

0 comments on commit 90f8ad5

Please sign in to comment.