2021-01-04 15:34:36 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
|
|
|
|
|
pub mod api;
|
|
|
|
|
|
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
|
|
|
// 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-03-15 18:02:53 +00:00
|
|
|
let mut registration_name = xous::String::<64>::new();
|
|
|
|
|
write!(registration_name, "{}", name).expect("namserver: name probably too long");
|
2021-02-10 07:02:48 +00:00
|
|
|
let request = api::Request::Register(registration_name);
|
2021-03-15 18:02:53 +00:00
|
|
|
let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096));
|
|
|
|
|
let pos = {
|
|
|
|
|
use rkyv::ser::Serializer;
|
|
|
|
|
writer.serialize_value(&request).expect("nameserver: couldn't archive name")
|
|
|
|
|
};
|
2021-02-10 07:02:48 +00:00
|
|
|
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
|
2021-03-15 18:02:53 +00:00
|
|
|
.lend_mut(ns_conn, pos as u32)
|
2021-02-10 07:02:48 +00:00
|
|
|
.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> {
|
|
|
|
|
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-03-15 18:02:53 +00:00
|
|
|
let mut lookup_name = xous::String::<64>::new();
|
|
|
|
|
write!(lookup_name, "{}", name).expect("nameserver: name problably too long");
|
2021-02-10 07:02:48 +00:00
|
|
|
let request = api::Request::Lookup(lookup_name);
|
2021-01-07 10:51:14 +00:00
|
|
|
|
2021-03-15 18:02:53 +00:00
|
|
|
let mut writer = rkyv::ser::serializers::BufferSerializer::new(xous::XousBuffer::new(4096));
|
|
|
|
|
let pos = {
|
|
|
|
|
use rkyv::ser::Serializer;
|
|
|
|
|
writer.serialize_value(&request).expect("nameserver: couldn't archive name")
|
|
|
|
|
};
|
2021-02-10 07:02:48 +00:00
|
|
|
let mut xous_buffer = writer.into_inner();
|
|
|
|
|
|
|
|
|
|
xous_buffer
|
2021-03-15 18:02:53 +00:00
|
|
|
.lend_mut(ns_conn, pos as u32)
|
2021-02-10 07:02:48 +00:00
|
|
|
.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
|
|
|
}
|