Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Data Transfer #5

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4a59ba6
feat(transfer): first two packets
LizAinslie Jul 5, 2024
1be7154
feat(data): create a DataTypeRegistry type for referencing registered…
LizAinslie Jul 9, 2024
8cce343
chore(lib,example): cleanup imports & dependencies
LizAinslie Jul 9, 2024
d86afd2
feat(transfer,data): init data marshallers in node & pass to incoming…
LizAinslie Jul 9, 2024
5f5936f
feat(example,data): pass data marshallers to outbound connection & ad…
LizAinslie Jul 9, 2024
6d7dcf4
chore(data): wip type fuckery
LizAinslie Jul 9, 2024
11a7c1c
fix(data): rethink data types & marshallers
LizAinslie Jul 11, 2024
d365cce
feat(data,transfer): implement broadcast on node inbound conns
LizAinslie Jul 11, 2024
49f39a1
feat(data,transfer): implement outbound & inbound, swap roles, add ha…
LizAinslie Jul 11, 2024
4a9cf07
fix(build): specify nightly
LizAinslie Jul 11, 2024
1bf2f1e
fix(build): oops! all async
LizAinslie Jul 11, 2024
2cca199
feat(transfer): reorg subscribe in node
LizAinslie Jul 11, 2024
32159f7
feat(transfer): change terminology in preparations for registering ha…
LizAinslie Jul 11, 2024
3a61682
feat(transfer): more name changes, fix async issues
LizAinslie Jul 12, 2024
a3f422b
chore(data, transfer): begin implementing event system for data handlers
LizAinslie Jul 12, 2024
9314483
chore(data,transfer): std::mem::transmute my beloved
LizAinslie Jul 12, 2024
d77cd15
fix(data): example fix & docs
LizAinslie Jul 12, 2024
4bee284
fix(data): example fix & docs
LizAinslie Jul 12, 2024
0e8fa6f
fix(data): box TData since we don't know its type when fetching with …
LizAinslie Jul 12, 2024
88aa6f8
fix(data): clone that box
LizAinslie Jul 12, 2024
0fcb265
chore(lib,example): clippy :3
LizAinslie Jul 22, 2024
52b70e1
fix(example): try from url
LizAinslie Jul 22, 2024
59226d6
fix(packet): ser/de tests passing
LizAinslie Jul 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
[workspace]
resolver = "2"
rust-version = "1.81"

members = [
"crates/*",
"examples/*"
]

[workspace.dependencies]
# Internal Crates
osp_data = { version = "=0.0.1", path = "crates/data" }
osp_protocol = { version = "=0.0.1", path = "crates/protocol" }
osp_server_sdk = { version = "=0.0.1", path = "crates/server" }
osp_client_sdk = { version = "=0.0.1", path = "crates/client" }

# External Crates
url = "2.5.2"
log = "0.4.21"
uuid = { version = "1.9.1", features = ["v4"] }
bytes = { version = "1.6.0" }
tokio = { version = "1", features = ["full"] }
bincode = "2.0.0-rc.3"
openssl = "0.10.64"
9 changes: 0 additions & 9 deletions crates/client/Cargo.toml

This file was deleted.

1 change: 0 additions & 1 deletion crates/client/src/lib.rs

This file was deleted.

13 changes: 6 additions & 7 deletions crates/data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
name = "osp_data"
version = "0.0.1"
edition = "2021"
rust-version = "1.81"

[dependencies]
uuid = { version = "1.9.1", features = ["v4"] }
serde = { version = "1.0.203" }
tokio = { version = "1", features = ["full"] }
tokio-byteorder = "0.3.0"
bytes = "1.6.0"
bincode = "2.0.0-rc.3"
derive-where = "1.2.7"
uuid = { workspace = true }
bytes = { workspace = true }
bincode = { workspace = true }
dyn-clone = "1.0.17"
downcast-rs = "1.2.1"
142 changes: 134 additions & 8 deletions crates/data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,153 @@
#![warn(clippy::complexity)]
#![warn(clippy::correctness)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]
#![warn(clippy::perf)]
#![warn(clippy::style)]
#![warn(clippy::suspicious)]
// followings are from clippy::restriction
#![warn(clippy::missing_errors_doc)]
#![warn(clippy::missing_panics_doc)]
#![warn(clippy::missing_safety_doc)]
#![warn(clippy::unwrap_used)]
#![warn(clippy::format_push_string)]
#![warn(clippy::get_unwrap)]

pub mod registry;

use bincode::{Decode, Encode};
use bincode::config::Configuration;
use bincode::error::{DecodeError, EncodeError};

use bytes::{Bytes, BytesMut};

use downcast_rs::{Downcast, DowncastSync, impl_downcast};

use uuid::Uuid;

/// Base type for all OSP data objects
///
/// ## Example implementation
/// ```rust
/// use bincode::{Decode, Encode};
/// use osp_data::{impl_data, Data};
/// use uuid::Uuid;
/// use std::str::FromStr;
///
/// #[derive(Encode, Decode, Clone)]
/// pub struct MyData {
/// test_val: bool,
/// }
///
/// impl_data!(MyData, "995f6806-7c36-4e27-ab03-a422952287b6");
/// ```
pub trait Data : Send + Downcast {
fn get_id_static() -> Uuid where Self : Sized;

fn get_id(&self) -> Uuid where Self : Sized {
Self::get_id_static()
}
}
impl_downcast!(Data);

/// Implement data methods more easily.
/// [`Uuid`], [`std::str::FromStr`] and [`Data`] must be in scope
///
/// **Usage:** (Given some concrete type `YourType`) `impl_data!(YourType, "your-type-uuid");`
#[macro_export]
macro_rules! impl_data {
($t:ident, $id:expr) => {
impl Data for $t {
fn get_id_static() -> Uuid
where
Self: Sized
{
Uuid::from_str($id).unwrap()
}
}
};
}

/// A meta type that contains encode/decode methods for writing [`Data`] to
/// a buffer, handlers assigned to `TData`, and associated markers.
pub struct DataType<TData>
where
TData : Data + ?Sized,
{
handlers: Vec<Box<dyn DataHandler<TData>>>
}

pub trait Data {
fn get_id() -> Uuid where Self : Sized;
impl<TData> DataType<TData>
where
TData : Data + ?Sized
{
#[must_use] pub fn new() -> Self {
Self {
handlers: Vec::new()
}
}

fn decode_from_bytes(buf: &Bytes) -> Result<(Self, usize), DecodeError>
#[must_use] pub fn get_id(&self) -> Uuid
where
Self : Decode + Sized
TData : Sized
{
TData::get_id_static()
}

/// Decode a [`TData`] off a buffer
pub fn decode_from_bytes(&self, buf: &Bytes) -> Result<(Box<TData>, usize), DecodeError>
where
TData : Decode,
{
let config = bincode::config::standard();
let res = bincode::decode_from_slice(buf, config)?;
Ok(res)
let res = bincode::decode_from_slice::<TData, Configuration>(buf, config)?;
Ok((Box::new(res.0), res.1))
}

fn encode_to_bytes(buf: &mut BytesMut, obj: Self) -> Result<usize, EncodeError>
/// Encode a [`TData`] onto a buffer
pub fn encode_to_bytes(&self, buf: &mut BytesMut, obj: TData) -> Result<usize, EncodeError>
where
Self : Encode + Sized
TData : Encode + Sized,
{
let config = bincode::config::standard();
let len = bincode::encode_into_slice(obj, buf, config)?;
Ok(len)
}

/// Run all handlers registered for this type on `obj`
#[allow(clippy::needless_pass_by_value)]
pub fn handle(&self, obj: Box<&TData>)
{
for handler in &self.handlers {
handler.handle(obj.clone());
}
}
}

impl<TData> Default for DataType<TData>
where
TData : Data + ?Sized
{
fn default() -> Self {
Self::new()
}
}

pub trait DataHandler<TData> : DowncastSync + Send + Sync
where
TData : Data + 'static + ?Sized
{
fn handle(&self, obj: Box<&TData>);
}

impl_downcast!(sync DataHandler<TData> where TData : Data + 'static);

impl<TData : Data + ?Sized, F: Fn(Box<&TData>) + Send + Sync + 'static> DataHandler<TData> for F {
fn handle(&self, obj: Box<&TData>) {
self(obj);
}
}


#[cfg(test)]
mod tests {}
Loading