From 1e7b4863aa6a87fc67d162d2dd8ed6a3770f77cc Mon Sep 17 00:00:00 2001 From: stneng Date: Thu, 10 Nov 2022 04:31:23 +0000 Subject: [PATCH 1/3] local registry --- Cargo.toml | 2 +- README.md | 2 +- .../protocol_greetings_with_instant_server.rs | 6 +++- src/extensions/instant_server.rs | 28 +++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0da2e6b..ef2ac8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "colink" -version = "0.2.2" +version = "0.2.3" edition = "2021" description = "CoLink Rust SDK" license = "MIT" diff --git a/README.md b/README.md index 607e0c4..6d8b194 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ CoLink SDK helps both application adnd protocol developers access the functional Add this to your Cargo.toml: ```toml [dependencies] -colink = "0.2.1" +colink = "0.2.3" ``` ## Getting Started diff --git a/examples/protocol_greetings_with_instant_server.rs b/examples/protocol_greetings_with_instant_server.rs index 1762bd0..94088de 100644 --- a/examples/protocol_greetings_with_instant_server.rs +++ b/examples/protocol_greetings_with_instant_server.rs @@ -1,5 +1,8 @@ #![allow(unused_variables)] -use colink::{extensions::instant_server::InstantServer, CoLink, Participant, ProtocolEntry}; +use colink::{ + extensions::instant_server::{InstantServer, LocalRegistry}, + CoLink, Participant, ProtocolEntry, +}; struct Initiator; #[colink::async_trait] @@ -33,6 +36,7 @@ impl ProtocolEntry for Receiver { #[tokio::main] async fn main() -> Result<(), Box> { + let lr = LocalRegistry::new().await; let is0 = InstantServer::new(); let is1 = InstantServer::new(); let cl0 = is0.get_colink().switch_to_generated_user().await?; diff --git a/src/extensions/instant_server.rs b/src/extensions/instant_server.rs index 264e4da..964f7b1 100644 --- a/src/extensions/instant_server.rs +++ b/src/extensions/instant_server.rs @@ -1,6 +1,8 @@ use crate::{utils::get_colink_home, CoLink}; use rand::Rng; use std::{ + fs::File, + io::Write, path::Path, process::{Child, Command, Stdio}, }; @@ -103,3 +105,29 @@ impl InstantServer { CoLink::new(&format!("http://127.0.0.1:{}", self.port), &self.host_token) } } + +pub struct LocalRegistry { + _instant_server: InstantServer, +} + +impl Drop for LocalRegistry { + fn drop(&mut self) { + let colink_home = get_colink_home().unwrap(); + let registry_file = Path::new(&colink_home).join("registry.conf"); + std::fs::remove_file(®istry_file).unwrap(); + } +} + +impl LocalRegistry { + pub async fn new() -> Self { + let is = InstantServer::new(); + let colink_home = get_colink_home().unwrap(); + let registry_file = Path::new(&colink_home).join("registry.conf"); + let mut file = File::create(®istry_file).unwrap(); + file.write_all(b"new_registry").unwrap(); + is.get_colink().switch_to_generated_user().await.unwrap(); + Self { + _instant_server: is, + } + } +} From ab03ef4741a31b716da35285a04e1a03bd4cb185 Mon Sep 17 00:00:00 2001 From: stneng Date: Thu, 10 Nov 2022 06:30:48 +0000 Subject: [PATCH 2/3] fix update_registries --- src/extensions/registry.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/extensions/registry.rs b/src/extensions/registry.rs index 35c3572..3a8976b 100644 --- a/src/extensions/registry.rs +++ b/src/extensions/registry.rs @@ -15,8 +15,10 @@ impl crate::application::CoLink { }]; let mut payload = vec![]; registries.encode(&mut payload).unwrap(); - self.run_task("registry", &payload, &participants, false) + let task_id = self + .run_task("registry", &payload, &participants, false) .await?; + self.wait_task(&task_id).await?; Ok(()) } } From 0059670c9203bd824cc830961f319da94436a45e Mon Sep 17 00:00:00 2001 From: stneng Date: Thu, 10 Nov 2022 23:55:09 +0000 Subject: [PATCH 3/3] renaming; create -> create_new --- .../protocol_greetings_with_instant_server.rs | 5 +++-- src/extensions/instant_server.rs | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/examples/protocol_greetings_with_instant_server.rs b/examples/protocol_greetings_with_instant_server.rs index 94088de..e478e8c 100644 --- a/examples/protocol_greetings_with_instant_server.rs +++ b/examples/protocol_greetings_with_instant_server.rs @@ -1,6 +1,6 @@ #![allow(unused_variables)] use colink::{ - extensions::instant_server::{InstantServer, LocalRegistry}, + extensions::instant_server::{InstantRegistry, InstantServer}, CoLink, Participant, ProtocolEntry, }; @@ -36,7 +36,8 @@ impl ProtocolEntry for Receiver { #[tokio::main] async fn main() -> Result<(), Box> { - let lr = LocalRegistry::new().await; + let ir = InstantRegistry::new().await; + // The instant server will automatically use the instant registry when there is one. let is0 = InstantServer::new(); let is1 = InstantServer::new(); let cl0 = is0.get_colink().switch_to_generated_user().await?; diff --git a/src/extensions/instant_server.rs b/src/extensions/instant_server.rs index 964f7b1..39e6eda 100644 --- a/src/extensions/instant_server.rs +++ b/src/extensions/instant_server.rs @@ -2,7 +2,6 @@ use crate::{utils::get_colink_home, CoLink}; use rand::Rng; use std::{ fs::File, - io::Write, path::Path, process::{Child, Command, Stdio}, }; @@ -106,25 +105,28 @@ impl InstantServer { } } -pub struct LocalRegistry { +pub struct InstantRegistry { _instant_server: InstantServer, } -impl Drop for LocalRegistry { +impl Drop for InstantRegistry { fn drop(&mut self) { let colink_home = get_colink_home().unwrap(); - let registry_file = Path::new(&colink_home).join("registry.conf"); + let registry_file = Path::new(&colink_home).join("reg_config"); std::fs::remove_file(®istry_file).unwrap(); } } -impl LocalRegistry { +impl InstantRegistry { pub async fn new() -> Self { let is = InstantServer::new(); let colink_home = get_colink_home().unwrap(); - let registry_file = Path::new(&colink_home).join("registry.conf"); - let mut file = File::create(®istry_file).unwrap(); - file.write_all(b"new_registry").unwrap(); + let registry_file = Path::new(&colink_home).join("reg_config"); + let _file = File::options() + .write(true) + .create_new(true) + .open(®istry_file) + .unwrap(); is.get_colink().switch_to_generated_user().await.unwrap(); Self { _instant_server: is,