blob: 0cf39e9567bb9088c2e7d196f7e087a7341f6ca2 [file] [log] [blame]
Mohammed Naser3415a2a2025-03-06 21:16:12 -05001use crate::{
2 schema::{DatabaseSchema, MonitorRequest, TableUpdate},
3 transports::{ipc, tcp},
4};
5use jsonrpsee::{async_client::ClientBuilder, core::client::SubscriptionClientT, proc_macros::rpc};
6use std::{collections::HashMap, path::Path};
7use tokio::net::ToSocketAddrs;
8
9#[rpc(client)]
10pub trait Rpc {
11 /// 4.1.1. List Databases
12 ///
13 /// This operation retrieves an array whose elements are the names of the
14 /// databases that can be accessed over this management protocol
15 /// connection.
16 #[method(name = "list_dbs")]
17 async fn list_databases(&self) -> Result<Vec<String>, ErrorObjectOwned>;
18
19 /// 4.1.2. Get Schema
20 ///
21 /// This operation retrieves a <database-schema> that describes hosted
22 /// database <db-name>.
23 #[method(name = "get_schema")]
24 async fn get_schema(&self, db_name: &str) -> Result<DatabaseSchema, ErrorObjectOwned>;
25
26 /// 4.1.5. Monitor
27 ///
28 /// The "monitor" request enables a client to replicate tables or subsets
29 /// of tables within an OVSDB database by requesting notifications of
30 /// changes to those tables and by receiving the complete initial state
31 /// of a table or a subset of a table.
32 #[method(name = "monitor")]
33 async fn monitor(
34 &self,
35 db_name: &str,
36 matcher: Option<&str>,
37 requests: HashMap<String, MonitorRequest>,
38 ) -> Result<TableUpdate<serde_json::Value>, ErrorObjectOwned>;
Mohammed Naser6730d712025-03-12 17:21:02 -040039
40 /// 4.1.11. Echo
41 ///
42 /// The "echo" method can be used by both clients and servers to verify
43 /// the liveness of a database connection. It MUST be implemented by
44 /// both clients and servers.
45 #[method(name = "echo")]
46 async fn echo(
47 &self,
48 data: Vec<serde_json::Value>,
49 ) -> Result<Vec<serde_json::Value>, ErrorObjectOwned>;
Mohammed Naser3415a2a2025-03-06 21:16:12 -050050}
51
52pub async fn connect_tcp(
53 tcp: impl ToSocketAddrs,
54) -> Result<impl SubscriptionClientT, std::io::Error> {
55 let (sender, receiver) = tcp::connect(tcp).await?;
56
57 Ok(ClientBuilder::default().build_with_tokio(sender, receiver))
58}
59
60pub async fn connect_unix(
61 socket_path: impl AsRef<Path>,
62) -> Result<impl SubscriptionClientT, std::io::Error> {
63 let (sender, receiver) = ipc::connect(socket_path).await?;
64
65 Ok(ClientBuilder::default().build_with_tokio(sender, receiver))
66}