This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate reqwest; | |
extern crate serde_json; | |
extern crate serde_derive; | |
extern crate serde; | |
extern crate ws; | |
use ws::{connect, CloseCode}; | |
use serde_json::{json, Deserializer, Value}; | |
use std::sync::mpsc::channel; | |
fn main() { | |
//agent 1 address: HcScJtbGnD4muv4hyi6OTWh7vShMyv5keOHdzB33vdDuruon9OtCEUZqgysf8nz | |
//agent 2 address: HcScjmt9IVp7di5q5hKQkWwU5Bxtev5y3rfGG9simu9svn4d6rjmhMPmxuxusba | |
pub fn get(_agent: String) {// update so it returns a Result. | |
let json = serde_json::json!( | |
{"id": "agent1", | |
"jsonrpc": "2.0", | |
"method": "call", | |
"params": {"instance_id": "test-instance", | |
"zome": "txrx", | |
"function": "get_signal", | |
"args": {"agent_address": _agent, | |
} | |
}}); | |
connect("ws://localhost:3401", |out| { | |
out.send(json.to_string()).unwrap(); //change to expect | |
move |msg| { | |
println!("DHT Entries: {:#?}", msg); | |
out.close(CloseCode::Normal) | |
} | |
}).unwrap(); //change to expect | |
} | |
pub fn id() {// update so it returns a Result | |
let json = serde_json::json!( | |
{"id": "agent1", | |
"jsonrpc": "2.0", | |
"method": "call", | |
"params": {"instance_id": "test-instance", | |
"zome": "txrx", | |
"function": "get_agent_id", | |
"args": {} | |
}}); | |
connect("ws://localhost:3401", |out| { | |
out.send(json.to_string()).unwrap(); //change to expect | |
move |msg| { | |
println!("Address for this User is: {:#?}", msg); | |
out.close(CloseCode::Normal) | |
} | |
}).unwrap(); //change to expect | |
} | |
pub fn set(_setting: String) -> String {// update so this returns a Result | |
let json = serde_json::json!( | |
{"id": "agent1", | |
"jsonrpc": "2.0", | |
"method": "call", | |
"params": {"instance_id": "test-instance", | |
"zome": "txrx", | |
"function": "set_signal", | |
"args": {"signal": _setting,}} | |
}); | |
let (tx, rx) = channel(); | |
let tx1 = &tx; | |
connect("ws://localhost:3401", |out| { | |
out.send(json.to_string()).unwrap();//change to expect | |
move |msg| { | |
println!("setting to the dht: {:#?}", msg); | |
tx1.send(msg).ok(); | |
out.close(CloseCode::Normal) | |
} | |
}).unwrap(); //expect instead | |
rx.recv().unwrap().to_string()//change to expect | |
} | |
id(); //whats my agent id | |
get("HcScJtbGnD4muv4hyi6OTWh7vShMyv5keOHdzB33vdDuruon9OtCEUZqgysf8nz".to_string()); //show the entire dht chain | |
set("first entry".to_string()); //set an entry into the DHT | |
get_last("HcScJtbGnD4muv4hyi6OTWh7vShMyv5keOHdzB33vdDuruon9OtCEUZqgysf8nz".to_string()) //get only the most recent entry that was set() in the dht | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this configuration works when I run the id() and set() functions.
but the get function returns the following:
I want it in the get() function to return the entire chain, it should look like the following:
"{\"jsonrpc\":\"2.0\",\"result\":\"{\\\"Ok\\\":[{\\\"signal\\\":\\\"Norm\\\",\\\"author_id\\\":\\\"HcSCJjCxUn8Jv4fv7x4BYcqY5TmWvvg3ytrb35FYKMtyc6qwBXsae4w84qhkyjz\\\"},{\\\"signal\\\":\\\"Norm\\\",\\\"author_id\\\":\\\"HcSCJjCxUn8Jv4fv7x4BYcqY5TmWvvg3ytrb35FYKMtyc6qwBXsae4w84qhkyjz\\\"},{\\\"signal\\\":\\\"Norm\\\",\\\"author_id\\\":\\\"HcSCJjCxUn8Jv4fv7x4BYcqY5TmWvvg3ytrb35FYKMtyc6qwBXsae4w84qhkyjz\\\"},{\\\"signal\\\":\\\"Norm\\\",\\\"author_id\\\":\\\"HcSCJjCxUn8Jv4fv7x4BYcqY5TmWvvg3ytrb35FYKMtyc6qwBXsae4w84qhkyjz\\\"},{\\\"signal\\\":\\\"med\\\",\\\"author_id\\\":\\\"HcSCJjCxUn8Jv4fv7x4BYcqY5TmWvvg3ytrb35FYKMtyc6qwBXsae4w84qhkyjz\\\"},{\\\"signal\\\":\\\"high\\\",\\\"author_id\\\":\\\"HcSCJjCxUn8Jv4fv7x4BYcqY5TmWvvg3ytrb35FYKMtyc6qwBXsae4w84qhkyjz\\\"}]}\",\"id\":\"txrx\"}",
I want the get_last() function to return the last entry of the chain. As above something like:
"high"