genev/
rpc.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi::{
20    rpc::{client::RpcClient, jsonrpc::JsonRequest},
21    util::encoding::base64,
22    Result,
23};
24use darkfi_serial::{deserialize, serialize};
25use genevd::GenEvent;
26use log::debug;
27use tinyjson::JsonValue;
28
29pub struct Gen {
30    pub rpc_client: RpcClient,
31}
32
33impl Gen {
34    pub async fn close_connection(&self) {
35        self.rpc_client.stop().await;
36    }
37
38    /// Add a new task.
39    pub async fn add(&self, event: GenEvent) -> Result<()> {
40        let event = JsonValue::String(base64::encode(&serialize(&event)));
41
42        let req = JsonRequest::new("add", JsonValue::Array([event].to_vec()));
43        let rep = self.rpc_client.request(req).await?;
44
45        debug!("Got reply: {:?}", rep);
46        Ok(())
47    }
48
49    /// Get current open tasks ids.
50    pub async fn list(&self) -> Result<Vec<GenEvent>> {
51        let req = JsonRequest::new("list", JsonValue::Array([].to_vec()));
52        let rep = self.rpc_client.request(req).await?;
53
54        debug!("reply: {:?}", rep);
55
56        let bytes: Vec<u8> = base64::decode(rep.get::<String>().unwrap()).unwrap();
57        let events: Vec<GenEvent> = deserialize(&bytes)?;
58
59        Ok(events)
60    }
61}