Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / jsonrpc.h
1 /*
2  * Copyright (c) 2009, 2010, 2012 Nicira, Inc.
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 #ifndef JSONRPC_H
18 #define JSONRPC_H 1
19
20 /* This is an implementation of the JSON-RPC 1.0 specification defined at
21  * http://json-rpc.org/wiki/specification. */
22
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include "openvswitch/types.h"
26
27 struct json;
28 struct jsonrpc_msg;
29 struct pstream;
30 struct reconnect_stats;
31 struct stream;
32 \f
33 /* API for a JSON-RPC stream. */
34
35 /* Default port numbers.
36  *
37  * There is nothing standard about these port numbers.  They are simply what
38  * we have chosen. */
39 #define JSONRPC_TCP_PORT 6632
40 #define JSONRPC_SSL_PORT 6632
41
42 int jsonrpc_stream_open(const char *name, struct stream **, uint8_t dscp);
43 int jsonrpc_pstream_open(const char *name, struct pstream **, uint8_t dscp);
44
45 struct jsonrpc *jsonrpc_open(struct stream *);
46 void jsonrpc_close(struct jsonrpc *);
47
48 void jsonrpc_run(struct jsonrpc *);
49 void jsonrpc_wait(struct jsonrpc *);
50
51 int jsonrpc_get_status(const struct jsonrpc *);
52 size_t jsonrpc_get_backlog(const struct jsonrpc *);
53 const char *jsonrpc_get_name(const struct jsonrpc *);
54
55 int jsonrpc_send(struct jsonrpc *, struct jsonrpc_msg *);
56 int jsonrpc_recv(struct jsonrpc *, struct jsonrpc_msg **);
57 void jsonrpc_recv_wait(struct jsonrpc *);
58
59 int jsonrpc_send_block(struct jsonrpc *, struct jsonrpc_msg *);
60 int jsonrpc_recv_block(struct jsonrpc *, struct jsonrpc_msg **);
61 int jsonrpc_transact_block(struct jsonrpc *, struct jsonrpc_msg *,
62                            struct jsonrpc_msg **);
63
64 /* Messages. */
65 enum jsonrpc_msg_type {
66     JSONRPC_REQUEST,           /* Request. */
67     JSONRPC_NOTIFY,            /* Notification. */
68     JSONRPC_REPLY,             /* Successful reply. */
69     JSONRPC_ERROR              /* Error reply. */
70 };
71
72 struct jsonrpc_msg {
73     enum jsonrpc_msg_type type;
74     char *method;               /* Request or notification only. */
75     struct json *params;        /* Request or notification only. */
76     struct json *result;        /* Successful reply only. */
77     struct json *error;         /* Error reply only. */
78     struct json *id;            /* Request or reply only. */
79 };
80
81 struct jsonrpc_msg *jsonrpc_create_request(const char *method,
82                                            struct json *params,
83                                            struct json **idp);
84 struct jsonrpc_msg *jsonrpc_create_notify(const char *method,
85                                           struct json *params);
86 struct jsonrpc_msg *jsonrpc_create_reply(struct json *result,
87                                          const struct json *id);
88 struct jsonrpc_msg *jsonrpc_create_error(struct json *error,
89                                          const struct json *id);
90
91 const char *jsonrpc_msg_type_to_string(enum jsonrpc_msg_type);
92 char *jsonrpc_msg_is_valid(const struct jsonrpc_msg *);
93 void jsonrpc_msg_destroy(struct jsonrpc_msg *);
94
95 char *jsonrpc_msg_from_json(struct json *, struct jsonrpc_msg **);
96 struct json *jsonrpc_msg_to_json(struct jsonrpc_msg *);
97 \f
98 /* A JSON-RPC session with reconnection. */
99
100 struct jsonrpc_session *jsonrpc_session_open(const char *name);
101 struct jsonrpc_session *jsonrpc_session_open_unreliably(struct jsonrpc *);
102 void jsonrpc_session_close(struct jsonrpc_session *);
103
104 void jsonrpc_session_run(struct jsonrpc_session *);
105 void jsonrpc_session_wait(struct jsonrpc_session *);
106
107 size_t jsonrpc_session_get_backlog(const struct jsonrpc_session *);
108 const char *jsonrpc_session_get_name(const struct jsonrpc_session *);
109
110 int jsonrpc_session_send(struct jsonrpc_session *, struct jsonrpc_msg *);
111 struct jsonrpc_msg *jsonrpc_session_recv(struct jsonrpc_session *);
112 void jsonrpc_session_recv_wait(struct jsonrpc_session *);
113
114 bool jsonrpc_session_is_alive(const struct jsonrpc_session *);
115 bool jsonrpc_session_is_connected(const struct jsonrpc_session *);
116 unsigned int jsonrpc_session_get_seqno(const struct jsonrpc_session *);
117 int jsonrpc_session_get_status(const struct jsonrpc_session *);
118 void jsonrpc_session_get_reconnect_stats(const struct jsonrpc_session *,
119                                          struct reconnect_stats *);
120
121 void jsonrpc_session_force_reconnect(struct jsonrpc_session *);
122
123 void jsonrpc_session_set_max_backoff(struct jsonrpc_session *,
124                                      int max_backofF);
125 void jsonrpc_session_set_probe_interval(struct jsonrpc_session *,
126                                         int probe_interval);
127 void jsonrpc_session_set_dscp(struct jsonrpc_session *,
128                               uint8_t dscp);
129
130 #endif /* jsonrpc.h */