]> git.proxmox.com Git - mirror_ovs.git/blame - lib/jsonrpc.h
tests: Add test suite for packets.h.
[mirror_ovs.git] / lib / jsonrpc.h
CommitLineData
f2129093 1/*
0d11f523 2 * Copyright (c) 2009, 2010 Nicira Networks.
f2129093
BP
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;
0d11f523 28struct pstream;
f2129093
BP
29struct stream;
30\f
31/* API for a JSON-RPC stream. */
32
0d11f523
BP
33/* Default port numbers.
34 *
35 * There is nothing standard about these port numbers. They are simply what
36 * we have chosen. */
37#define JSONRPC_TCP_PORT 6632
38#define JSONRPC_SSL_PORT 6632
39
40int jsonrpc_stream_open(const char *name, struct stream **);
41int jsonrpc_pstream_open(const char *name, struct pstream **);
42
f2129093
BP
43struct jsonrpc *jsonrpc_open(struct stream *);
44void jsonrpc_close(struct jsonrpc *);
45
46void jsonrpc_run(struct jsonrpc *);
47void jsonrpc_wait(struct jsonrpc *);
48
49void jsonrpc_error(struct jsonrpc *, int error);
50int jsonrpc_get_status(const struct jsonrpc *);
51size_t jsonrpc_get_backlog(const struct jsonrpc *);
52const char *jsonrpc_get_name(const struct jsonrpc *);
53
54int jsonrpc_send(struct jsonrpc *, struct jsonrpc_msg *);
55int jsonrpc_recv(struct jsonrpc *, struct jsonrpc_msg **);
56void jsonrpc_recv_wait(struct jsonrpc *);
57
58int jsonrpc_send_block(struct jsonrpc *, struct jsonrpc_msg *);
59int jsonrpc_recv_block(struct jsonrpc *, struct jsonrpc_msg **);
d0632593
BP
60int jsonrpc_transact_block(struct jsonrpc *, struct jsonrpc_msg *,
61 struct jsonrpc_msg **);
f2129093
BP
62
63/* Messages. */
64enum jsonrpc_msg_type {
65 JSONRPC_REQUEST, /* Request. */
66 JSONRPC_NOTIFY, /* Notification. */
67 JSONRPC_REPLY, /* Successful reply. */
68 JSONRPC_ERROR /* Error reply. */
69};
70
71struct jsonrpc_msg {
72 enum jsonrpc_msg_type type;
73 char *method; /* Request or notification only. */
74 struct json *params; /* Request or notification only. */
75 struct json *result; /* Successful reply only. */
76 struct json *error; /* Error reply only. */
77 struct json *id; /* Request or reply only. */
78};
79
80struct jsonrpc_msg *jsonrpc_create_request(const char *method,
20bed8be
BP
81 struct json *params,
82 struct json **idp);
f2129093
BP
83struct jsonrpc_msg *jsonrpc_create_notify(const char *method,
84 struct json *params);
85struct jsonrpc_msg *jsonrpc_create_reply(struct json *result,
86 const struct json *id);
87struct jsonrpc_msg *jsonrpc_create_error(struct json *error,
88 const struct json *id);
89
90const char *jsonrpc_msg_type_to_string(enum jsonrpc_msg_type);
91char *jsonrpc_msg_is_valid(const struct jsonrpc_msg *);
92void jsonrpc_msg_destroy(struct jsonrpc_msg *);
93
94char *jsonrpc_msg_from_json(struct json *, struct jsonrpc_msg **);
95struct json *jsonrpc_msg_to_json(struct jsonrpc_msg *);
dcbb691b
BP
96\f
97/* A JSON-RPC session with reconnection. */
98
99struct jsonrpc_session *jsonrpc_session_open(const char *name);
4931f33a 100struct jsonrpc_session *jsonrpc_session_open_unreliably(struct jsonrpc *);
dcbb691b
BP
101void jsonrpc_session_close(struct jsonrpc_session *);
102
103void jsonrpc_session_run(struct jsonrpc_session *);
104void jsonrpc_session_wait(struct jsonrpc_session *);
105
106size_t jsonrpc_session_get_backlog(const struct jsonrpc_session *);
107const char *jsonrpc_session_get_name(const struct jsonrpc_session *);
108
109int jsonrpc_session_send(struct jsonrpc_session *, struct jsonrpc_msg *);
110struct jsonrpc_msg *jsonrpc_session_recv(struct jsonrpc_session *);
111void jsonrpc_session_recv_wait(struct jsonrpc_session *);
112
4931f33a 113bool jsonrpc_session_is_alive(const struct jsonrpc_session *);
dcbb691b
BP
114bool jsonrpc_session_is_connected(const struct jsonrpc_session *);
115unsigned int jsonrpc_session_get_seqno(const struct jsonrpc_session *);
116void jsonrpc_session_force_reconnect(struct jsonrpc_session *);
f2129093 117
94db5407
BP
118void jsonrpc_session_set_max_backoff(struct jsonrpc_session *,
119 int max_backofF);
120void jsonrpc_session_set_probe_interval(struct jsonrpc_session *,
121 int probe_interval);
122
f2129093 123#endif /* jsonrpc.h */