Mohammed Naser | 3415a2a | 2025-03-06 21:16:12 -0500 | [diff] [blame^] | 1 | use crate::{ |
| 2 | schema::{DatabaseSchema, MonitorRequest, TableUpdate}, |
| 3 | transports::{ipc, tcp}, |
| 4 | }; |
| 5 | use jsonrpsee::{async_client::ClientBuilder, core::client::SubscriptionClientT, proc_macros::rpc}; |
| 6 | use std::{collections::HashMap, path::Path}; |
| 7 | use tokio::net::ToSocketAddrs; |
| 8 | |
| 9 | #[rpc(client)] |
| 10 | pub 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>; |
| 39 | } |
| 40 | |
| 41 | pub async fn connect_tcp( |
| 42 | tcp: impl ToSocketAddrs, |
| 43 | ) -> Result<impl SubscriptionClientT, std::io::Error> { |
| 44 | let (sender, receiver) = tcp::connect(tcp).await?; |
| 45 | |
| 46 | Ok(ClientBuilder::default().build_with_tokio(sender, receiver)) |
| 47 | } |
| 48 | |
| 49 | pub async fn connect_unix( |
| 50 | socket_path: impl AsRef<Path>, |
| 51 | ) -> Result<impl SubscriptionClientT, std::io::Error> { |
| 52 | let (sender, receiver) = ipc::connect(socket_path).await?; |
| 53 | |
| 54 | Ok(ClientBuilder::default().build_with_tokio(sender, receiver)) |
| 55 | } |