Skip to content

Commit

Permalink
Merge pull request #30 from CoLearn-Dev/local_registry
Browse files Browse the repository at this point in the history
- local registry
- fix update_registries
  • Loading branch information
stneng authored Nov 12, 2022
2 parents 34631fa + 0059670 commit 510a03c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "colink"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
description = "CoLink Rust SDK"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion examples/protocol_greetings_with_instant_server.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(unused_variables)]
use colink::{extensions::instant_server::InstantServer, CoLink, Participant, ProtocolEntry};
use colink::{
extensions::instant_server::{InstantRegistry, InstantServer},
CoLink, Participant, ProtocolEntry,
};

struct Initiator;
#[colink::async_trait]
Expand Down Expand Up @@ -33,6 +36,8 @@ impl ProtocolEntry for Receiver {

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
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?;
Expand Down
30 changes: 30 additions & 0 deletions src/extensions/instant_server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{utils::get_colink_home, CoLink};
use rand::Rng;
use std::{
fs::File,
path::Path,
process::{Child, Command, Stdio},
};
Expand Down Expand Up @@ -103,3 +104,32 @@ impl InstantServer {
CoLink::new(&format!("http://127.0.0.1:{}", self.port), &self.host_token)
}
}

pub struct InstantRegistry {
_instant_server: InstantServer,
}

impl Drop for InstantRegistry {
fn drop(&mut self) {
let colink_home = get_colink_home().unwrap();
let registry_file = Path::new(&colink_home).join("reg_config");
std::fs::remove_file(&registry_file).unwrap();
}
}

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("reg_config");
let _file = File::options()
.write(true)
.create_new(true)
.open(&registry_file)
.unwrap();
is.get_colink().switch_to_generated_user().await.unwrap();
Self {
_instant_server: is,
}
}
}
4 changes: 3 additions & 1 deletion src/extensions/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

0 comments on commit 510a03c

Please sign in to comment.