f0ac27bc94045d4f04623587e764a50a7521b034
[sliver-openvswitch.git] / lib / jsonrpc.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "jsonrpc.h"
20
21 #include <assert.h>
22 #include <errno.h>
23
24 #include "byteq.h"
25 #include "dynamic-string.h"
26 #include "json.h"
27 #include "list.h"
28 #include "ofpbuf.h"
29 #include "poll-loop.h"
30 #include "queue.h"
31 #include "reconnect.h"
32 #include "stream.h"
33 #include "timeval.h"
34
35 #define THIS_MODULE VLM_jsonrpc
36 #include "vlog.h"
37 \f
38 struct jsonrpc {
39     struct stream *stream;
40     char *name;
41     int status;
42
43     /* Input. */
44     struct byteq input;
45     struct json_parser *parser;
46     struct jsonrpc_msg *received;
47
48     /* Output. */
49     struct ovs_queue output;
50     size_t backlog;
51 };
52
53 /* Rate limit for error messages. */
54 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
55
56 static void jsonrpc_received(struct jsonrpc *);
57 static void jsonrpc_cleanup(struct jsonrpc *);
58
59 struct jsonrpc *
60 jsonrpc_open(struct stream *stream)
61 {
62     struct jsonrpc *rpc;
63
64     assert(stream != NULL);
65
66     rpc = xzalloc(sizeof *rpc);
67     rpc->name = xstrdup(stream_get_name(stream));
68     rpc->stream = stream;
69     byteq_init(&rpc->input);
70     queue_init(&rpc->output);
71
72     return rpc;
73 }
74
75 void
76 jsonrpc_close(struct jsonrpc *rpc)
77 {
78     if (rpc) {
79         jsonrpc_cleanup(rpc);
80         free(rpc->name);
81         free(rpc);
82     }
83 }
84
85 void
86 jsonrpc_run(struct jsonrpc *rpc)
87 {
88     if (rpc->status) {
89         return;
90     }
91
92     stream_run(rpc->stream);
93     while (!queue_is_empty(&rpc->output)) {
94         struct ofpbuf *buf = rpc->output.head;
95         int retval;
96
97         retval = stream_send(rpc->stream, buf->data, buf->size);
98         if (retval >= 0) {
99             rpc->backlog -= retval;
100             ofpbuf_pull(buf, retval);
101             if (!buf->size) {
102                 ofpbuf_delete(queue_pop_head(&rpc->output));
103             }
104         } else {
105             if (retval != -EAGAIN) {
106                 VLOG_WARN_RL(&rl, "%s: send error: %s",
107                              rpc->name, strerror(-retval));
108                 jsonrpc_error(rpc, -retval);
109             }
110             break;
111         }
112     }
113 }
114
115 void
116 jsonrpc_wait(struct jsonrpc *rpc)
117 {
118     if (!rpc->status) {
119         stream_run_wait(rpc->stream);
120         if (!queue_is_empty(&rpc->output)) {
121             stream_send_wait(rpc->stream);
122         }
123     }
124 }
125
126 int
127 jsonrpc_get_status(const struct jsonrpc *rpc)
128 {
129     return rpc->status;
130 }
131
132 size_t
133 jsonrpc_get_backlog(const struct jsonrpc *rpc)
134 {
135     return rpc->status ? 0 : rpc->backlog;
136 }
137
138 const char *
139 jsonrpc_get_name(const struct jsonrpc *rpc)
140 {
141     return rpc->name;
142 }
143
144 static void
145 jsonrpc_log_msg(const struct jsonrpc *rpc, const char *title,
146                 const struct jsonrpc_msg *msg)
147 {
148     if (VLOG_IS_DBG_ENABLED()) {
149         struct ds s = DS_EMPTY_INITIALIZER;
150         if (msg->method) {
151             ds_put_format(&s, ", method=\"%s\"", msg->method);
152         }
153         if (msg->params) {
154             ds_put_cstr(&s, ", params=");
155             json_to_ds(msg->params, 0, &s);
156         }
157         if (msg->result) {
158             ds_put_cstr(&s, ", result=");
159             json_to_ds(msg->result, 0, &s);
160         }
161         if (msg->error) {
162             ds_put_cstr(&s, ", error=");
163             json_to_ds(msg->error, 0, &s);
164         }
165         if (msg->id) {
166             ds_put_cstr(&s, ", id=");
167             json_to_ds(msg->id, 0, &s);
168         }
169         VLOG_DBG("%s: %s %s%s", rpc->name, title,
170                  jsonrpc_msg_type_to_string(msg->type), ds_cstr(&s));
171         ds_destroy(&s);
172     }
173 }
174
175 /* Always takes ownership of 'msg', regardless of success. */
176 int
177 jsonrpc_send(struct jsonrpc *rpc, struct jsonrpc_msg *msg)
178 {
179     struct ofpbuf *buf;
180     struct json *json;
181     size_t length;
182     char *s;
183
184     if (rpc->status) {
185         jsonrpc_msg_destroy(msg);
186         return rpc->status;
187     }
188
189     jsonrpc_log_msg(rpc, "send", msg);
190
191     json = jsonrpc_msg_to_json(msg);
192     s = json_to_string(json, 0);
193     length = strlen(s);
194     json_destroy(json);
195
196     buf = xmalloc(sizeof *buf);
197     ofpbuf_use(buf, s, length);
198     buf->size = length;
199     queue_push_tail(&rpc->output, buf);
200     rpc->backlog += length;
201
202     if (rpc->output.n == 1) {
203         jsonrpc_run(rpc);
204     }
205     return rpc->status;
206 }
207
208 int
209 jsonrpc_recv(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
210 {
211     *msgp = NULL;
212     if (rpc->status) {
213         return rpc->status;
214     }
215
216     while (!rpc->received) {
217         if (byteq_is_empty(&rpc->input)) {
218             size_t chunk;
219             int retval;
220
221             chunk = byteq_headroom(&rpc->input);
222             retval = stream_recv(rpc->stream, byteq_head(&rpc->input), chunk);
223             if (retval < 0) {
224                 if (retval == -EAGAIN) {
225                     return EAGAIN;
226                 } else {
227                     VLOG_WARN_RL(&rl, "%s: receive error: %s",
228                                  rpc->name, strerror(-retval));
229                     jsonrpc_error(rpc, -retval);
230                     return rpc->status;
231                 }
232             } else if (retval == 0) {
233                 VLOG_INFO_RL(&rl, "%s: connection closed", rpc->name);
234                 jsonrpc_error(rpc, EOF);
235                 return EOF;
236             }
237             byteq_advance_head(&rpc->input, retval);
238         } else {
239             size_t n, used;
240
241             if (!rpc->parser) {
242                 rpc->parser = json_parser_create(0);
243             }
244             n = byteq_tailroom(&rpc->input);
245             used = json_parser_feed(rpc->parser,
246                                     (char *) byteq_tail(&rpc->input), n);
247             byteq_advance_tail(&rpc->input, used);
248             if (json_parser_is_done(rpc->parser)) {
249                 jsonrpc_received(rpc);
250                 if (rpc->status) {
251                     return rpc->status;
252                 }
253             }
254         }
255     }
256
257     *msgp = rpc->received;
258     rpc->received = NULL;
259     return 0;
260 }
261
262 void
263 jsonrpc_recv_wait(struct jsonrpc *rpc)
264 {
265     if (rpc->status || rpc->received || !byteq_is_empty(&rpc->input)) {
266         poll_immediate_wake();
267     } else {
268         stream_recv_wait(rpc->stream);
269     }
270 }
271
272 /* Always takes ownership of 'msg', regardless of success. */
273 int
274 jsonrpc_send_block(struct jsonrpc *rpc, struct jsonrpc_msg *msg)
275 {
276     int error;
277
278     error = jsonrpc_send(rpc, msg);
279     if (error) {
280         return error;
281     }
282
283     for (;;) {
284         jsonrpc_run(rpc);
285         if (queue_is_empty(&rpc->output) || rpc->status) {
286             return rpc->status;
287         }
288         jsonrpc_wait(rpc);
289         poll_block();
290     }
291 }
292
293 int
294 jsonrpc_recv_block(struct jsonrpc *rpc, struct jsonrpc_msg **msgp)
295 {
296     for (;;) {
297         int error = jsonrpc_recv(rpc, msgp);
298         if (error != EAGAIN) {
299             return error;
300         }
301
302         jsonrpc_run(rpc);
303         jsonrpc_wait(rpc);
304         jsonrpc_recv_wait(rpc);
305         poll_block();
306     }
307 }
308
309 /* Always takes ownership of 'request', regardless of success. */
310 int
311 jsonrpc_transact_block(struct jsonrpc *rpc, struct jsonrpc_msg *request,
312                        struct jsonrpc_msg **replyp)
313 {
314     struct jsonrpc_msg *reply = NULL;
315     struct json *id;
316     int error;
317
318     id = json_clone(request->id);
319     error = jsonrpc_send_block(rpc, request);
320     if (!error) {
321         for (;;) {
322             error = jsonrpc_recv_block(rpc, &reply);
323             if (error
324                 || (reply->type == JSONRPC_REPLY
325                     && json_equal(id, reply->id))) {
326                 break;
327             }
328             jsonrpc_msg_destroy(reply);
329         }
330     }
331     *replyp = error ? NULL : reply;
332     json_destroy(id);
333     return error;
334 }
335
336 static void
337 jsonrpc_received(struct jsonrpc *rpc)
338 {
339     struct jsonrpc_msg *msg;
340     struct json *json;
341     char *error;
342
343     json = json_parser_finish(rpc->parser);
344     rpc->parser = NULL;
345     if (json->type == JSON_STRING) {
346         VLOG_WARN_RL(&rl, "%s: error parsing stream: %s",
347                      rpc->name, json_string(json));
348         jsonrpc_error(rpc, EPROTO);
349         json_destroy(json);
350         return;
351     }
352
353     error = jsonrpc_msg_from_json(json, &msg);
354     if (error) {
355         VLOG_WARN_RL(&rl, "%s: received bad JSON-RPC message: %s",
356                      rpc->name, error);
357         free(error);
358         jsonrpc_error(rpc, EPROTO);
359         return;
360     }
361
362     jsonrpc_log_msg(rpc, "received", msg);
363     rpc->received = msg;
364 }
365
366 void
367 jsonrpc_error(struct jsonrpc *rpc, int error)
368 {
369     assert(error);
370     if (!rpc->status) {
371         rpc->status = error;
372         jsonrpc_cleanup(rpc);
373     }
374 }
375
376 static void
377 jsonrpc_cleanup(struct jsonrpc *rpc)
378 {
379     stream_close(rpc->stream);
380     rpc->stream = NULL;
381
382     json_parser_abort(rpc->parser);
383     rpc->parser = NULL;
384
385     jsonrpc_msg_destroy(rpc->received);
386     rpc->received = NULL;
387
388     queue_clear(&rpc->output);
389     rpc->backlog = 0;
390 }
391 \f
392 static struct jsonrpc_msg *
393 jsonrpc_create(enum jsonrpc_msg_type type, const char *method,
394                 struct json *params, struct json *result, struct json *error,
395                 struct json *id)
396 {
397     struct jsonrpc_msg *msg = xmalloc(sizeof *msg);
398     msg->type = type;
399     msg->method = method ? xstrdup(method) : NULL;
400     msg->params = params;
401     msg->result = result;
402     msg->error = error;
403     msg->id = id;
404     return msg;
405 }
406
407 static struct json *
408 jsonrpc_create_id(void)
409 {
410     static unsigned int id;
411     return json_integer_create(id++);
412 }
413
414 struct jsonrpc_msg *
415 jsonrpc_create_request(const char *method, struct json *params,
416                        struct json **idp)
417 {
418     struct json *id = jsonrpc_create_id();
419     if (idp) {
420         *idp = json_clone(id);
421     }
422     return jsonrpc_create(JSONRPC_REQUEST, method, params, NULL, NULL, id);
423 }
424
425 struct jsonrpc_msg *
426 jsonrpc_create_notify(const char *method, struct json *params)
427 {
428     return jsonrpc_create(JSONRPC_NOTIFY, method, params, NULL, NULL, NULL);
429 }
430
431 struct jsonrpc_msg *
432 jsonrpc_create_reply(struct json *result, const struct json *id)
433 {
434     return jsonrpc_create(JSONRPC_REPLY, NULL, NULL, result, NULL,
435                            json_clone(id));
436 }
437
438 struct jsonrpc_msg *
439 jsonrpc_create_error(struct json *error, const struct json *id)
440 {
441     return jsonrpc_create(JSONRPC_REPLY, NULL, NULL, NULL, error,
442                            json_clone(id));
443 }
444
445 const char *
446 jsonrpc_msg_type_to_string(enum jsonrpc_msg_type type)
447 {
448     switch (type) {
449     case JSONRPC_REQUEST:
450         return "request";
451
452     case JSONRPC_NOTIFY:
453         return "notification";
454
455     case JSONRPC_REPLY:
456         return "reply";
457
458     case JSONRPC_ERROR:
459         return "error";
460     }
461     return "(null)";
462 }
463
464 char *
465 jsonrpc_msg_is_valid(const struct jsonrpc_msg *m)
466 {
467     const char *type_name;
468     unsigned int pattern;
469
470     if (m->params && m->params->type != JSON_ARRAY) {
471         return xstrdup("\"params\" must be JSON array");
472     }
473
474     switch (m->type) {
475     case JSONRPC_REQUEST:
476         pattern = 0x11001;
477         break;
478
479     case JSONRPC_NOTIFY:
480         pattern = 0x11000;
481         break;
482
483     case JSONRPC_REPLY:
484         pattern = 0x00101;
485         break;
486
487     case JSONRPC_ERROR:
488         pattern = 0x00011;
489         break;
490
491     default:
492         return xasprintf("invalid JSON-RPC message type %d", m->type);
493     }
494
495     type_name = jsonrpc_msg_type_to_string(m->type);
496     if ((m->method != NULL) != ((pattern & 0x10000) != 0)) {
497         return xasprintf("%s must%s have \"method\"",
498                          type_name, (pattern & 0x10000) ? "" : " not");
499
500     }
501     if ((m->params != NULL) != ((pattern & 0x1000) != 0)) {
502         return xasprintf("%s must%s have \"params\"",
503                          type_name, (pattern & 0x1000) ? "" : " not");
504
505     }
506     if ((m->result != NULL) != ((pattern & 0x100) != 0)) {
507         return xasprintf("%s must%s have \"result\"",
508                          type_name, (pattern & 0x100) ? "" : " not");
509
510     }
511     if ((m->error != NULL) != ((pattern & 0x10) != 0)) {
512         return xasprintf("%s must%s have \"error\"",
513                          type_name, (pattern & 0x10) ? "" : " not");
514
515     }
516     if ((m->id != NULL) != ((pattern & 0x1) != 0)) {
517         return xasprintf("%s must%s have \"id\"",
518                          type_name, (pattern & 0x1) ? "" : " not");
519
520     }
521     return NULL;
522 }
523
524 void
525 jsonrpc_msg_destroy(struct jsonrpc_msg *m)
526 {
527     if (m) {
528         free(m->method);
529         json_destroy(m->params);
530         json_destroy(m->result);
531         json_destroy(m->error);
532         json_destroy(m->id);
533         free(m);
534     }
535 }
536
537 static struct json *
538 null_from_json_null(struct json *json)
539 {
540     if (json && json->type == JSON_NULL) {
541         json_destroy(json);
542         return NULL;
543     }
544     return json;
545 }
546
547 char *
548 jsonrpc_msg_from_json(struct json *json, struct jsonrpc_msg **msgp)
549 {
550     struct json *method = NULL;
551     struct jsonrpc_msg *msg = NULL;
552     struct shash *object;
553     char *error;
554
555     if (json->type != JSON_OBJECT) {
556         error = xstrdup("message is not a JSON object");
557         goto exit;
558     }
559     object = json_object(json);
560
561     method = shash_find_and_delete(object, "method");
562     if (method && method->type != JSON_STRING) {
563         error = xstrdup("method is not a JSON string");
564         goto exit;
565     }
566
567     msg = xzalloc(sizeof *msg);
568     msg->method = method ? xstrdup(method->u.string) : NULL;
569     msg->params = null_from_json_null(shash_find_and_delete(object, "params"));
570     msg->result = null_from_json_null(shash_find_and_delete(object, "result"));
571     msg->error = null_from_json_null(shash_find_and_delete(object, "error"));
572     msg->id = null_from_json_null(shash_find_and_delete(object, "id"));
573     msg->type = (msg->result ? JSONRPC_REPLY
574                  : msg->error ? JSONRPC_ERROR
575                  : msg->id ? JSONRPC_REQUEST
576                  : JSONRPC_NOTIFY);
577     if (!shash_is_empty(object)) {
578         error = xasprintf("message has unexpected member \"%s\"",
579                           shash_first(object)->name);
580         goto exit;
581     }
582     error = jsonrpc_msg_is_valid(msg);
583     if (error) {
584         goto exit;
585     }
586
587 exit:
588     json_destroy(method);
589     json_destroy(json);
590     if (error) {
591         jsonrpc_msg_destroy(msg);
592         msg = NULL;
593     }
594     *msgp = msg;
595     return error;
596 }
597
598 struct json *
599 jsonrpc_msg_to_json(struct jsonrpc_msg *m)
600 {
601     struct json *json = json_object_create();
602
603     if (m->method) {
604         json_object_put(json, "method", json_string_create_nocopy(m->method));
605     }
606
607     if (m->params) {
608         json_object_put(json, "params", m->params);
609     }
610
611     if (m->result) {
612         json_object_put(json, "result", m->result);
613     } else if (m->type == JSONRPC_ERROR) {
614         json_object_put(json, "result", json_null_create());
615     }
616
617     if (m->error) {
618         json_object_put(json, "error", m->error);
619     } else if (m->type == JSONRPC_REPLY) {
620         json_object_put(json, "error", json_null_create());
621     }
622
623     if (m->id) {
624         json_object_put(json, "id", m->id);
625     } else if (m->type == JSONRPC_NOTIFY) {
626         json_object_put(json, "id", json_null_create());
627     }
628
629     free(m);
630
631     return json;
632 }
633 \f
634 /* A JSON-RPC session with reconnection. */
635
636 struct jsonrpc_session {
637     struct reconnect *reconnect;
638     struct jsonrpc *rpc;
639     struct stream *stream;
640     struct pstream *pstream;
641     unsigned int seqno;
642 };
643
644 /* Creates and returns a jsonrpc_session to 'name', which should be a string
645  * acceptable to stream_open() or pstream_open().
646  *
647  * If 'name' is an active connection method, e.g. "tcp:127.1.2.3", the new
648  * jsonrpc_session connects and reconnects, with back-off, to 'name'.
649  *
650  * If 'name' is a passive connection method, e.g. "ptcp:", the new
651  * jsonrpc_session listens for connections to 'name'.  It maintains at most one
652  * connection at any given time.  Any new connection causes the previous one
653  * (if any) to be dropped. */
654 struct jsonrpc_session *
655 jsonrpc_session_open(const char *name)
656 {
657     struct jsonrpc_session *s;
658
659     s = xmalloc(sizeof *s);
660     s->reconnect = reconnect_create(time_msec());
661     reconnect_set_name(s->reconnect, name);
662     reconnect_enable(s->reconnect, time_msec());
663     s->rpc = NULL;
664     s->stream = NULL;
665     s->pstream = NULL;
666     s->seqno = 0;
667
668     if (!pstream_verify_name(name)) {
669         reconnect_set_passive(s->reconnect, true, time_msec());
670     }
671
672     return s;
673 }
674
675 /* Creates and returns a jsonrpc_session that is initially connected to
676  * 'jsonrpc'.  If the connection is dropped, it will not be reconnected. */
677 struct jsonrpc_session *
678 jsonrpc_session_open_unreliably(struct jsonrpc *jsonrpc)
679 {
680     struct jsonrpc_session *s;
681
682     s = xmalloc(sizeof *s);
683     s->reconnect = reconnect_create(time_msec());
684     reconnect_set_name(s->reconnect, jsonrpc_get_name(jsonrpc));
685     reconnect_set_max_tries(s->reconnect, 0);
686     reconnect_connected(s->reconnect, time_msec());
687     s->rpc = jsonrpc;
688     s->stream = NULL;
689     s->pstream = NULL;
690     s->seqno = 0;
691
692     return s;
693 }
694
695 void
696 jsonrpc_session_close(struct jsonrpc_session *s)
697 {
698     if (s) {
699         jsonrpc_close(s->rpc);
700         reconnect_destroy(s->reconnect);
701         stream_close(s->stream);
702         pstream_close(s->pstream);
703         free(s);
704     }
705 }
706
707 static void
708 jsonrpc_session_disconnect(struct jsonrpc_session *s)
709 {
710     if (s->rpc) {
711         jsonrpc_error(s->rpc, EOF);
712         jsonrpc_close(s->rpc);
713         s->rpc = NULL;
714         s->seqno++;
715     } else if (s->stream) {
716         stream_close(s->stream);
717         s->stream = NULL;
718         s->seqno++;
719     }
720 }
721
722 static void
723 jsonrpc_session_connect(struct jsonrpc_session *s)
724 {
725     const char *name = reconnect_get_name(s->reconnect);
726     int error;
727
728     jsonrpc_session_disconnect(s);
729     if (!reconnect_is_passive(s->reconnect)) {
730         error = stream_open(name, &s->stream);
731         if (!error) {
732             reconnect_connecting(s->reconnect, time_msec());
733         }
734     } else {
735         error = s->pstream ? 0 : pstream_open(name, &s->pstream);
736         if (!error) {
737             reconnect_listening(s->reconnect, time_msec());
738         }
739     }
740
741     if (error) {
742         reconnect_connect_failed(s->reconnect, time_msec(), error);
743     }
744     s->seqno++;
745 }
746
747 void
748 jsonrpc_session_run(struct jsonrpc_session *s)
749 {
750     if (s->pstream) {
751         struct stream *stream;
752         int error;
753
754         error = pstream_accept(s->pstream, &stream);
755         if (!error) {
756             if (s->rpc || s->stream) {
757                 VLOG_INFO_RL(&rl,
758                              "%s: new connection replacing active connection",
759                              reconnect_get_name(s->reconnect));
760                 jsonrpc_session_disconnect(s);
761             }
762             reconnect_connected(s->reconnect, time_msec());
763             s->rpc = jsonrpc_open(stream);
764         } else if (error != EAGAIN) {
765             reconnect_listen_error(s->reconnect, time_msec(), error);
766             pstream_close(s->pstream);
767             s->pstream = NULL;
768         }
769     }
770
771     if (s->rpc) {
772         int error;
773
774         jsonrpc_run(s->rpc);
775         error = jsonrpc_get_status(s->rpc);
776         if (error) {
777             reconnect_disconnected(s->reconnect, time_msec(), 0);
778             jsonrpc_session_disconnect(s);
779         }
780     } else if (s->stream) {
781         int error;
782
783         stream_run(s->stream);
784         error = stream_connect(s->stream);
785         if (!error) {
786             reconnect_connected(s->reconnect, time_msec());
787             s->rpc = jsonrpc_open(s->stream);
788             s->stream = NULL;
789         } else if (error != EAGAIN) {
790             reconnect_connect_failed(s->reconnect, time_msec(), error);
791             stream_close(s->stream);
792             s->stream = NULL;
793         }
794     }
795
796     switch (reconnect_run(s->reconnect, time_msec())) {
797     case RECONNECT_CONNECT:
798         jsonrpc_session_connect(s);
799         break;
800
801     case RECONNECT_DISCONNECT:
802         reconnect_disconnected(s->reconnect, time_msec(), 0);
803         jsonrpc_session_disconnect(s);
804         break;
805
806     case RECONNECT_PROBE:
807         if (s->rpc) {
808             struct json *params;
809             struct jsonrpc_msg *request;
810
811             params = json_array_create_empty();
812             request = jsonrpc_create_request("echo", params, NULL);
813             json_destroy(request->id);
814             request->id = json_string_create("echo");
815             jsonrpc_send(s->rpc, request);
816         }
817         break;
818     }
819 }
820
821 void
822 jsonrpc_session_wait(struct jsonrpc_session *s)
823 {
824     if (s->rpc) {
825         jsonrpc_wait(s->rpc);
826     } else if (s->stream) {
827         stream_run_wait(s->stream);
828         stream_connect_wait(s->stream);
829     }
830     if (s->pstream) {
831         pstream_wait(s->pstream);
832     }
833     reconnect_wait(s->reconnect, time_msec());
834 }
835
836 size_t
837 jsonrpc_session_get_backlog(const struct jsonrpc_session *s)
838 {
839     return s->rpc ? jsonrpc_get_backlog(s->rpc) : 0;
840 }
841
842 const char *
843 jsonrpc_session_get_name(const struct jsonrpc_session *s)
844 {
845     return reconnect_get_name(s->reconnect);
846 }
847
848 /* Always takes ownership of 'msg', regardless of success. */
849 int
850 jsonrpc_session_send(struct jsonrpc_session *s, struct jsonrpc_msg *msg)
851 {
852     if (s->rpc) {
853         return jsonrpc_send(s->rpc, msg);
854     } else {
855         jsonrpc_msg_destroy(msg);
856         return ENOTCONN;
857     }
858 }
859
860 struct jsonrpc_msg *
861 jsonrpc_session_recv(struct jsonrpc_session *s)
862 {
863     if (s->rpc) {
864         struct jsonrpc_msg *msg;
865         jsonrpc_recv(s->rpc, &msg);
866         if (msg) {
867             reconnect_received(s->reconnect, time_msec());
868             if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
869                 /* Echo request.  Send reply. */
870                 struct jsonrpc_msg *reply;
871
872                 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
873                 jsonrpc_session_send(s, reply);
874             } else if (msg->type == JSONRPC_REPLY
875                 && msg->id && msg->id->type == JSON_STRING
876                 && !strcmp(msg->id->u.string, "echo")) {
877                 /* It's a reply to our echo request.  Suppress it. */
878             } else {
879                 return msg;
880             }
881             jsonrpc_msg_destroy(msg);
882         }
883     }
884     return NULL;
885 }
886
887 void
888 jsonrpc_session_recv_wait(struct jsonrpc_session *s)
889 {
890     if (s->rpc) {
891         jsonrpc_recv_wait(s->rpc);
892     }
893 }
894
895 bool
896 jsonrpc_session_is_alive(const struct jsonrpc_session *s)
897 {
898     return s->rpc || s->stream || reconnect_get_max_tries(s->reconnect);
899 }
900
901 bool
902 jsonrpc_session_is_connected(const struct jsonrpc_session *s)
903 {
904     return s->rpc != NULL;
905 }
906
907 unsigned int
908 jsonrpc_session_get_seqno(const struct jsonrpc_session *s)
909 {
910     return s->seqno;
911 }
912
913 void
914 jsonrpc_session_force_reconnect(struct jsonrpc_session *s)
915 {
916     reconnect_force_reconnect(s->reconnect, time_msec());
917 }