Skip to content
Browse files

Moved the mutex into a new client variable.

  • Loading branch information...
1 parent 8a5a416 commit 24bdde2b91044ec6d40641d0d722d9f01710adb4 @dov committed Dec 30, 2011
Showing with 92 additions and 18 deletions.
  1. +1 −0 .gitignore
  2. +4 −0 ChangeLog
  3. +1 −1 SConstruct
  4. +9 −0 glib-jsonrpc-client.h
  5. +9 −0 glib-jsonrpc-json.h
  6. +46 −13 glib-jsonrpc-server.c
  7. +17 −1 glib-jsonrpc-server.h
  8. +5 −3 test-glib-jsonrpc-server.c
View
1 .gitignore
@@ -1,5 +1,6 @@
/.sconsign.dblite
*.o
+*.bak
*.a
/test-glib-jsonrpc-server
/test-glib-jsonrpc-client
View
4 ChangeLog
@@ -0,0 +1,4 @@
+2011-12-30 Dov Grobgeld <dov.grobgeld@gmail.com>
+
+ * glib-jsonrpc-server.c, glib-jsonrpc-client.c: Moved the mutex into a new client variable.
+
View
2 SConstruct
@@ -9,7 +9,7 @@ lib = env.Library("glib-jsonrpc",
"glib-jsonrpc-json.c"])
env.Program("test-glib-jsonrpc-server",
- ["test-glib-jsonrpc-server"],
+ ["test-glib-jsonrpc-server.c"],
LIBS = lib + env['LIBS'])
env.Program("test-glib-jsonrpc-client",
View
9 glib-jsonrpc-client.h
@@ -24,6 +24,10 @@
#include "json-glib/json-glib.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef struct {
} GLibJsonRpcClient;
@@ -36,4 +40,9 @@ int glib_jsonrpc_client_call(GLibJsonRpcClient *client,
// output
JsonNode **response);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif /* GLIB_JSONRPC_CLIENT */
View
9 glib-jsonrpc-json.h
@@ -23,6 +23,10 @@
#include <json-glib/json-glib.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
// Allocate and convert a json node to a string
gchar *glib_jsonrpc_json_to_string(JsonNode *node);
@@ -32,4 +36,9 @@ JsonNode *glib_jsonrpc_json_csv_to_json_array(const char *string);
// A general json parser
JsonNode *glib_jsonrpc_json_string_to_json_node(const gchar *str);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif /* GLIB_JSONRPC_JSON */
View
59 glib-jsonrpc-server.c
@@ -29,8 +29,6 @@ typedef struct {
GSocketService *service;
GHashTable *command_hash;
GString *req_string;
- GMutex *mutex;
- JsonNode *reply;
gboolean allow_non_loopback_connections;
// This is set whenever an asynchronous command is being run. Only
@@ -45,6 +43,35 @@ typedef struct {
JsonNode *reply;
} command_hash_value_t;
+typedef struct {
+ GMutex *mutex;
+ GLibJsonRpcServer *server;
+ JsonNode *reply;
+} GLibJsonRpcAsyncQueryPrivate;
+
+static GLibJsonRpcAsyncQueryPrivate *glib_jsonrpc_server_query_new(GLibJsonRpcServer *server)
+{
+ GLibJsonRpcAsyncQueryPrivate *query = g_new0(GLibJsonRpcAsyncQueryPrivate, 1);
+ query->mutex = g_mutex_new();
+ query->server = server;
+ query->reply = NULL;
+ g_mutex_lock(query->mutex);
+
+ return query;
+}
+
+static void glib_jsonrpc_async_query_free(GLibJsonRpcAsyncQueryPrivate *query)
+{
+ g_mutex_unlock(query->mutex);
+ g_mutex_free(query->mutex); // Why does this crash?
+ g_free(query);
+}
+
+GLibJsonRpcServer *glib_jsonrpc_async_query_get_server(GLibJsonRpcAsyncQuery *_query)
+{
+ GLibJsonRpcAsyncQueryPrivate *query = (GLibJsonRpcAsyncQueryPrivate*)_query;
+ return query->server;
+}
static JsonNode* create_fault_response(int error_num, const char *message, int id)
{
@@ -204,18 +231,27 @@ handler (GThreadedSocketService *service,
response = create_fault_response(-2, "Busy!",id);
else
{
- jsonrpc_server->async_busy = TRUE;
+ // With the embedding of the mutex in the query we should
+ // be able to handle more than one connection, so there
+ // is no need to protect against a busy state.
+ // jsonrpc_server->async_busy = TRUE;
+ GLibJsonRpcAsyncQueryPrivate *query = glib_jsonrpc_server_query_new((GLibJsonRpcServer*)jsonrpc_server);
// Create a secondary main loop
(*command_val->async_callback)((GLibJsonRpcServer*)jsonrpc_server,
+ (GLibJsonRpcAsyncQuery*)query,
method,
params,
command_val->user_data);
- // Lock on a mutex
- g_mutex_lock(jsonrpc_server->mutex);
- response = create_response(jsonrpc_server->reply, id);
+ // Lock on a mutex - Should probably be changed to a condition variable
+ g_mutex_lock(query->mutex);
+
+ response = create_response(query->reply, id);
jsonrpc_server->async_busy = FALSE;
+
+ // Erase the query
+ glib_jsonrpc_async_query_free(query);
}
}
else
@@ -299,8 +335,6 @@ GLibJsonRpcServer *glib_jsonrpc_server_new(int port)
jsonrpc_server->command_hash = g_hash_table_new(g_str_hash,
g_str_equal);
- jsonrpc_server->mutex = g_mutex_new();
- g_mutex_lock(jsonrpc_server->mutex);
return (GLibJsonRpcServer*)jsonrpc_server;
}
@@ -358,13 +392,12 @@ int glib_jsonrpc_server_register_async_command(GLibJsonRpcServer *jsonrpc_server
return 0;
}
-int glib_jsonrpc_server_send_async_response(GLibJsonRpcServer *_server,
+int glib_jsonrpc_server_send_async_response(GLibJsonRpcAsyncQuery *_query,
JsonNode *reply)
{
- GLibJsonRpcServerPrivate *server = (GLibJsonRpcServerPrivate *)_server;
- server->reply = reply;
-
- g_mutex_unlock(server->mutex);
+ GLibJsonRpcAsyncQueryPrivate *query = (GLibJsonRpcAsyncQueryPrivate *)_query;
+ query->reply = reply;
+ g_mutex_unlock(query->mutex);
return TRUE;
}
View
18 glib-jsonrpc-server.h
@@ -24,16 +24,27 @@
#include "json-glib/json-glib.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef struct {
} GLibJsonRpcServer;
+// Queries use for async commands
+typedef struct {
+} GLibJsonRpcAsyncQuery;
+
+GLibJsonRpcServer *glib_jsonrpc_async_query_get_server(GLibJsonRpcAsyncQuery *query);
+
typedef int (GLibJsonRpcCommandCallback)(GLibJsonRpcServer *server,
const char *method,
JsonNode *params,
JsonNode **response,
gpointer user_data);
typedef int (GLibJsonRpcCommandAsyncCallback)(GLibJsonRpcServer *server,
+ GLibJsonRpcAsyncQuery *query,
const char *method,
JsonNode *params,
gpointer user_data);
@@ -51,11 +62,16 @@ int glib_jsonrpc_server_register_async_command(GLibJsonRpcServer *jsonrpc_server
GLibJsonRpcCommandAsyncCallback *async_callback,
gpointer user_data);
// The json node ownershap is transfered.
-int glib_jsonrpc_server_send_async_response(GLibJsonRpcServer *server,
+int glib_jsonrpc_server_send_async_response(GLibJsonRpcAsyncQuery *query,
JsonNode *response);
void glib_jsonrpc_server_set_allow_non_loopback_connections(GLibJsonRpcServer *_server,
gboolean allow_non_loop_back_connections);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif /* GLIB-JSON-RPC-SERVER */
View
8 test-glib-jsonrpc-server.c
@@ -74,21 +74,22 @@ int cmd_echo(GLibJsonRpcServer *server,
// Global variables for this test only
gboolean async_pong(gpointer data)
{
- GLibJsonRpcServer *server = (GLibJsonRpcServer*)data;
+ GLibJsonRpcAsyncQuery *query = (GLibJsonRpcAsyncQuery*)data;
JsonNode *response = json_node_new(JSON_NODE_VALUE);
json_node_set_string(response, "async_pong");
- glib_jsonrpc_server_send_async_response(server,
+ glib_jsonrpc_server_send_async_response(query,
response);
return 0;
}
int cmd_async_ping(GLibJsonRpcServer *server,
+ GLibJsonRpcAsyncQuery *query,
const char *method,
JsonNode *params,
gpointer user_data)
{
- g_timeout_add_seconds(2, async_pong, server);
+ g_timeout_add_seconds(2, async_pong, query);
return 0;
}
@@ -123,3 +124,4 @@ int main(int argc, char **argv)
exit(0);
}
+

0 comments on commit 24bdde2

Please sign in to comment.
Something went wrong with that request. Please try again.