jsonrpc: New type "jsonrpc_session", which automatically reconnects.
authorBen Pfaff <blp@nicira.com>
Wed, 2 Dec 2009 18:50:18 +0000 (10:50 -0800)
committerBen Pfaff <blp@nicira.com>
Wed, 2 Dec 2009 19:19:03 +0000 (11:19 -0800)
lib/jsonrpc.c
lib/jsonrpc.h

index 09c3d45dbeff5f81cfb0ff949c392357274aaaef..817a35f1d5b0b2822f5066f1a9a03a4068607e95 100644 (file)
@@ -27,7 +27,9 @@
 #include "ofpbuf.h"
 #include "poll-loop.h"
 #include "queue.h"
+#include "reconnect.h"
 #include "stream.h"
+#include "timeval.h"
 
 #define THIS_MODULE VLM_jsonrpc
 #include "vlog.h"
@@ -614,3 +616,185 @@ jsonrpc_msg_to_json(struct jsonrpc_msg *m)
 
     return json;
 }
+\f
+/* A JSON-RPC session with reconnection. */
+
+struct jsonrpc_session {
+    struct reconnect *reconnect;
+    struct jsonrpc *rpc;
+    struct stream *stream;
+    unsigned int seqno;
+};
+
+struct jsonrpc_session *
+jsonrpc_session_open(const char *name)
+{
+    struct jsonrpc_session *s;
+
+    s = xmalloc(sizeof *s);
+    s->reconnect = reconnect_create(time_msec());
+    reconnect_set_name(s->reconnect, name);
+    reconnect_enable(s->reconnect, time_msec());
+    s->rpc = NULL;
+    s->stream = NULL;
+    s->seqno = 0;
+
+    return s;
+}
+
+void
+jsonrpc_session_close(struct jsonrpc_session *s)
+{
+    if (s) {
+        jsonrpc_close(s->rpc);
+        reconnect_destroy(s->reconnect);
+        free(s);
+    }
+}
+
+static void
+jsonrpc_session_disconnect(struct jsonrpc_session *s)
+{
+    reconnect_disconnected(s->reconnect, time_msec(), 0);
+    if (s->rpc) {
+        jsonrpc_error(s->rpc, EOF);
+        jsonrpc_close(s->rpc);
+        s->rpc = NULL;
+        s->seqno++;
+    } else if (s->stream) {
+        stream_close(s->stream);
+        s->stream = NULL;
+        s->seqno++;
+    }
+}
+
+static void
+jsonrpc_session_connect(struct jsonrpc_session *s)
+{
+    int error;
+
+    jsonrpc_session_disconnect(s);
+    error = stream_open(reconnect_get_name(s->reconnect), &s->stream);
+    if (error) {
+        reconnect_connect_failed(s->reconnect, time_msec(), error);
+    } else {
+        reconnect_connecting(s->reconnect, time_msec());
+    }
+    s->seqno++;
+}
+
+void
+jsonrpc_session_run(struct jsonrpc_session *s)
+{
+    if (s->rpc) {
+        int error;
+
+        jsonrpc_run(s->rpc);
+        error = jsonrpc_get_status(s->rpc);
+        if (error) {
+            jsonrpc_session_disconnect(s);
+        }
+    } else if (s->stream) {
+        int error = stream_connect(s->stream);
+        if (!error) {
+            reconnect_connected(s->reconnect, time_msec());
+            s->rpc = jsonrpc_open(s->stream);
+            s->stream = NULL;
+        } else if (error != EAGAIN) {
+            reconnect_connect_failed(s->reconnect, time_msec(), error);
+            stream_close(s->stream);
+            s->stream = NULL;
+        }
+    }
+
+    switch (reconnect_run(s->reconnect, time_msec())) {
+    case RECONNECT_CONNECT:
+        jsonrpc_session_connect(s);
+        break;
+
+    case RECONNECT_DISCONNECT:
+        jsonrpc_session_disconnect(s);
+        break;
+
+    case RECONNECT_PROBE:
+        if (s->rpc) {
+            struct json *params;
+            struct jsonrpc_msg *request;
+
+            params = json_array_create_empty();
+            request = jsonrpc_create_request("echo", params);
+            json_destroy(request->id);
+            request->id = json_string_create("echo");
+            jsonrpc_send(s->rpc, request);
+        }
+        break;
+    }
+}
+
+void
+jsonrpc_session_wait(struct jsonrpc_session *s)
+{
+    if (s->rpc) {
+        jsonrpc_wait(s->rpc);
+    } else if (s->stream) {
+        stream_connect_wait(s->stream);
+    }
+    reconnect_wait(s->reconnect, time_msec());
+}
+
+size_t
+jsonrpc_session_get_backlog(const struct jsonrpc_session *s)
+{
+    return s->rpc ? jsonrpc_get_backlog(s->rpc) : 0;
+}
+
+const char *
+jsonrpc_session_get_name(const struct jsonrpc_session *s)
+{
+    return reconnect_get_name(s->reconnect);
+}
+
+int
+jsonrpc_session_send(struct jsonrpc_session *s, struct jsonrpc_msg *msg)
+{
+    return s->rpc ? jsonrpc_send(s->rpc, msg) : ENOTCONN;
+}
+
+struct jsonrpc_msg *
+jsonrpc_session_recv(struct jsonrpc_session *s)
+{
+    struct jsonrpc_msg *msg = NULL;
+    if (s->rpc) {
+        jsonrpc_recv(s->rpc, &msg);
+        if (msg) {
+            reconnect_received(s->reconnect, time_msec());
+        }
+    }
+    return msg;
+}
+
+void
+jsonrpc_session_recv_wait(struct jsonrpc_session *s)
+{
+    if (s->rpc) {
+        jsonrpc_recv_wait(s->rpc);
+    }
+}
+
+bool
+jsonrpc_session_is_connected(const struct jsonrpc_session *s)
+{
+    return s->rpc != NULL;
+}
+
+unsigned int
+jsonrpc_session_get_seqno(const struct jsonrpc_session *s)
+{
+    return s->seqno;
+}
+
+void
+jsonrpc_session_force_reconnect(struct jsonrpc_session *s)
+{
+    reconnect_force_reconnect(s->reconnect, time_msec());
+}
index e6d11ac511c48d9bf94f4dc92ba24e27846874f6..5d47ac5e6240db032b4fc613239c8cc44e2c497c 100644 (file)
@@ -81,5 +81,24 @@ void jsonrpc_msg_destroy(struct jsonrpc_msg *);
 
 char *jsonrpc_msg_from_json(struct json *, struct jsonrpc_msg **);
 struct json *jsonrpc_msg_to_json(struct jsonrpc_msg *);
+\f
+/* A JSON-RPC session with reconnection. */
+
+struct jsonrpc_session *jsonrpc_session_open(const char *name);
+void jsonrpc_session_close(struct jsonrpc_session *);
+
+void jsonrpc_session_run(struct jsonrpc_session *);
+void jsonrpc_session_wait(struct jsonrpc_session *);
+
+size_t jsonrpc_session_get_backlog(const struct jsonrpc_session *);
+const char *jsonrpc_session_get_name(const struct jsonrpc_session *);
+
+int jsonrpc_session_send(struct jsonrpc_session *, struct jsonrpc_msg *);
+struct jsonrpc_msg *jsonrpc_session_recv(struct jsonrpc_session *);
+void jsonrpc_session_recv_wait(struct jsonrpc_session *);
+
+bool jsonrpc_session_is_connected(const struct jsonrpc_session *);
+unsigned int jsonrpc_session_get_seqno(const struct jsonrpc_session *);
+void jsonrpc_session_force_reconnect(struct jsonrpc_session *);
 
 #endif /* jsonrpc.h */