2021-01-04 15:34:36 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
|
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
use core::convert::TryInto;
|
2021-01-07 10:51:14 +00:00
|
|
|
use core::fmt::Write;
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-01-07 13:53:08 +00:00
|
|
|
pub fn register_name(name: &str) -> Result<xous::SID, xous::Error> {
|
2021-02-10 07:02:48 +00:00
|
|
|
use rkyv::Write;
|
|
|
|
|
// Ensure we have a connection to the nameserver. If one exists, this is a no-op.
|
2021-01-07 10:51:14 +00:00
|
|
|
let ns_id = xous::SID::from_bytes(b"xous-name-server").unwrap();
|
|
|
|
|
let ns_conn = xous::connect(ns_id).unwrap();
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
let mut registration_name = api::XousServerName::default();
|
|
|
|
|
write!(registration_name, "{}", name).unwrap();
|
|
|
|
|
let request = api::Request::Register(registration_name);
|
2021-02-12 17:42:31 +00:00
|
|
|
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
|
2021-02-10 07:02:48 +00:00
|
|
|
let pos = writer
|
|
|
|
|
.archive(&request)
|
|
|
|
|
.expect("couldn't archive request");
|
|
|
|
|
let mut xous_buffer = writer.into_inner();
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
xous_buffer
|
|
|
|
|
.lend_mut(ns_conn, pos.try_into().unwrap())
|
|
|
|
|
.expect("nameserver registration failure");
|
2021-01-07 13:53:08 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
let archived = unsafe { rkyv::archived_value::<api::Request>(xous_buffer.as_ref(), pos) };
|
|
|
|
|
|
|
|
|
|
if let rkyv::Archived::<api::Request>::SID(sid) = archived {
|
|
|
|
|
let sid = sid.into();
|
|
|
|
|
xous::create_server_with_sid(sid).expect("can't auto-register server");
|
|
|
|
|
Ok(sid)
|
2021-02-17 15:29:20 +00:00
|
|
|
} else if let rkyv::Archived::<api::Request>::Failure = archived {
|
|
|
|
|
return Err(xous::Error::InternalError);
|
2021-01-07 13:53:08 +00:00
|
|
|
} else {
|
2021-02-10 07:02:48 +00:00
|
|
|
panic!("Invalid response from the server -- corruption occurred");
|
2021-01-07 13:53:08 +00:00
|
|
|
}
|
2021-01-07 10:51:14 +00:00
|
|
|
}
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-01-07 10:51:14 +00:00
|
|
|
/// note: if this throws an AccessDenied error, you can retry with a request_authenticat_connection() call (to be written)
|
|
|
|
|
pub fn request_connection(name: &str) -> Result<xous::CID, xous::Error> {
|
2021-02-10 07:02:48 +00:00
|
|
|
use rkyv::Write;
|
2021-01-07 10:51:14 +00:00
|
|
|
let ns_id = xous::SID::from_bytes(b"xous-name-server").unwrap();
|
|
|
|
|
let ns_conn = xous::connect(ns_id).unwrap();
|
2021-01-04 18:28:27 +00:00
|
|
|
|
2021-02-10 07:02:48 +00:00
|
|
|
let mut lookup_name = api::XousServerName::default();
|
|
|
|
|
write!(lookup_name, "{}", name).unwrap();
|
|
|
|
|
let request = api::Request::Lookup(lookup_name);
|
|
|
|
|
// let mut lookup = api::Lookup::default();
|
|
|
|
|
// write!(lookup.name, "{}", name).unwrap();
|
|
|
|
|
// let request = api::Request::Lookup(lookup);
|
2021-01-07 10:51:14 +00:00
|
|
|
|
2021-02-12 17:42:31 +00:00
|
|
|
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
|
2021-02-10 07:02:48 +00:00
|
|
|
let pos = writer
|
|
|
|
|
.archive(&request)
|
|
|
|
|
.expect("couldn't archive request");
|
|
|
|
|
let mut xous_buffer = writer.into_inner();
|
|
|
|
|
|
|
|
|
|
xous_buffer
|
|
|
|
|
.lend_mut(ns_conn, pos.try_into().unwrap())
|
|
|
|
|
.expect("nameserver lookup failure!");
|
|
|
|
|
|
|
|
|
|
let archived = unsafe { rkyv::archived_value::<api::Request>(xous_buffer.as_ref(), pos) };
|
|
|
|
|
match archived {
|
|
|
|
|
rkyv::Archived::<api::Request>::CID(cid) => Ok(*cid),
|
|
|
|
|
rkyv::Archived::<api::Request>::AuthenticateRequest(_) => Err(xous::Error::AccessDenied),
|
|
|
|
|
_ => Err(xous::Error::ServerNotFound),
|
2021-01-07 10:51:14 +00:00
|
|
|
}
|
2021-01-07 21:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-08 18:12:01 +00:00
|
|
|
/// note: you probably want to use this one, to avoid synchronization issues on startup as servers register asynhcronously
|
|
|
|
|
pub fn request_connection_blocking(name: &str) -> Result<xous::CID, xous::Error> {
|
|
|
|
|
loop {
|
2021-02-10 07:02:48 +00:00
|
|
|
match request_connection(name) {
|
|
|
|
|
Ok(val) => return Ok(val),
|
|
|
|
|
Err(xous::Error::AccessDenied) => return Err(xous::Error::AccessDenied),
|
|
|
|
|
_ => (),
|
2021-01-08 18:12:01 +00:00
|
|
|
}
|
2021-02-10 07:02:48 +00:00
|
|
|
xous::yield_slice();
|
2021-01-08 18:12:01 +00:00
|
|
|
}
|
2021-01-12 10:44:02 +00:00
|
|
|
}
|