Skip to content

Instantly share code, notes, and snippets.

@simwilso
Created January 14, 2020 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simwilso/6a07fd87844039e1e10deeffd6632957 to your computer and use it in GitHub Desktop.
Save simwilso/6a07fd87844039e1e10deeffd6632957 to your computer and use it in GitHub Desktop.
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
}
@simwilso
Copy link
Author

this configuration works when I run the id() and set() functions.
but the get function returns the following:

DHT Entries: Text(
    "{\"type\":\"InstanceStats\",\"instance_stats\":{\"test-instance\":{\"number_held_entries\":0,\"number_held_aspects\":0,\"number_pending_validations\":0,\"number_delayed_validations\":0,\"number_running_zome_calls\":1,\"offline\":false}}}",

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment