]> git.proxmox.com Git - mirror_ovs.git/blame - lib/jsonrpc.h
vconn-stream: Factor out port defaults into public helper functions.
[mirror_ovs.git] / lib / jsonrpc.h
CommitLineData
f2129093
BP
1/*
2 * Copyright (c) 2009 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#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
26struct json;
27struct jsonrpc_msg;
28struct stream;
29\f
30/* API for a JSON-RPC stream. */
31
32struct jsonrpc *jsonrpc_open(struct stream *);
33void jsonrpc_close(struct jsonrpc *);
34
35void jsonrpc_run(struct jsonrpc *);
36void jsonrpc_wait(struct jsonrpc *);
37
38void jsonrpc_error(struct jsonrpc *, int error);
39int jsonrpc_get_status(const struct jsonrpc *);
40size_t jsonrpc_get_backlog(const struct jsonrpc *);
41const char *jsonrpc_get_name(const struct jsonrpc *);
42
43int jsonrpc_send(struct jsonrpc *, struct jsonrpc_msg *);
44int jsonrpc_recv(struct jsonrpc *, struct jsonrpc_msg **);
45void jsonrpc_recv_wait(struct jsonrpc *);
46
47int jsonrpc_send_block(struct jsonrpc *, struct jsonrpc_msg *);
48int jsonrpc_recv_block(struct jsonrpc *, struct jsonrpc_msg **);
d0632593
BP
49int jsonrpc_transact_block(struct jsonrpc *, struct jsonrpc_msg *,
50 struct jsonrpc_msg **);
f2129093
BP
51
52/* Messages. */
53enum jsonrpc_msg_type {
54 JSONRPC_REQUEST, /* Request. */
55 JSONRPC_NOTIFY, /* Notification. */
56 JSONRPC_REPLY, /* Successful reply. */
57 JSONRPC_ERROR /* Error reply. */
58};
59
60struct jsonrpc_msg {
61 enum jsonrpc_msg_type type;
62 char *method; /* Request or notification only. */
63 struct json *params; /* Request or notification only. */
64 struct json *result; /* Successful reply only. */
65 struct json *error; /* Error reply only. */
66 struct json *id; /* Request or reply only. */
67};
68
69struct jsonrpc_msg *jsonrpc_create_request(const char *method,
20bed8be
BP
70 struct json *params,
71 struct json **idp);
f2129093
BP
72struct jsonrpc_msg *jsonrpc_create_notify(const char *method,
73 struct json *params);
74struct jsonrpc_msg *jsonrpc_create_reply(struct json *result,
75 const struct json *id);
76struct jsonrpc_msg *jsonrpc_create_error(struct json *error,
77 const struct json *id);
78
79const char *jsonrpc_msg_type_to_string(enum jsonrpc_msg_type);
80char *jsonrpc_msg_is_valid(const struct jsonrpc_msg *);
81void jsonrpc_msg_destroy(struct jsonrpc_msg *);
82
83char *jsonrpc_msg_from_json(struct json *, struct jsonrpc_msg **);
84struct json *jsonrpc_msg_to_json(struct jsonrpc_msg *);
dcbb691b
BP
85\f
86/* A JSON-RPC session with reconnection. */
87
88struct jsonrpc_session *jsonrpc_session_open(const char *name);
4931f33a 89struct jsonrpc_session *jsonrpc_session_open_unreliably(struct jsonrpc *);
dcbb691b
BP
90void jsonrpc_session_close(struct jsonrpc_session *);
91
92void jsonrpc_session_run(struct jsonrpc_session *);
93void jsonrpc_session_wait(struct jsonrpc_session *);
94
95size_t jsonrpc_session_get_backlog(const struct jsonrpc_session *);
96const char *jsonrpc_session_get_name(const struct jsonrpc_session *);
97
98int jsonrpc_session_send(struct jsonrpc_session *, struct jsonrpc_msg *);
99struct jsonrpc_msg *jsonrpc_session_recv(struct jsonrpc_session *);
100void jsonrpc_session_recv_wait(struct jsonrpc_session *);
101
4931f33a 102bool jsonrpc_session_is_alive(const struct jsonrpc_session *);
dcbb691b
BP
103bool jsonrpc_session_is_connected(const struct jsonrpc_session *);
104unsigned int jsonrpc_session_get_seqno(const struct jsonrpc_session *);
105void jsonrpc_session_force_reconnect(struct jsonrpc_session *);
f2129093
BP
106
107#endif /* jsonrpc.h */