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

Protolok utils #9

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
571 changes: 422 additions & 149 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions loki-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
loki-shared = { path = "../loki-shared" }
protolok = { path = "../protolok" }
tokio = { version = "1", features = ["full"] }
6 changes: 5 additions & 1 deletion loki-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
fn main() {
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
println!("Hello, world!");
}
14 changes: 0 additions & 14 deletions loki-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
5 changes: 3 additions & 2 deletions protolok/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
base16ct = "0.2"
rand = "0.8.5"
rsa = "0.8"
sha2 = "0.10"
chacha20poly1305 = "0.10"
ed25519-dalek = { version="2.1", features=["rand_core"] }
x25519-dalek = { version="2.0", features=["reusable_secrets"] }
4 changes: 2 additions & 2 deletions protolok/src/channel.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::message::{Message, MessageContent};
use crate::Message;

pub trait Channel<ErrorType, Ctx> {
fn fetch_newest_messages(
&self,
amount: u32,
begin_at_id: Option<u64>,
) -> Result<Vec<Message>, ErrorType>;
fn send_message(&self, message: MessageContent, ctx: Ctx) -> Result<Message, ErrorType>;
fn send_message(&self, message: Message, ctx: Ctx) -> Result<(), ErrorType>;
}
49 changes: 0 additions & 49 deletions protolok/src/crypto.rs

This file was deleted.

74 changes: 74 additions & 0 deletions protolok/src/id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::{
sync::Mutex,
time::{Duration, SystemTime, UNIX_EPOCH},
};

const EPOCH: Duration = Duration::from_millis(1672531200000);

const fn mask(amount: u64) -> u64 {
(1 << amount) - 1
}

pub struct SnowflakeGenerator {
last_evoked: u64,
count: Mutex<u16>,
}

impl SnowflakeGenerator {
pub fn new() -> SnowflakeGenerator {
SnowflakeGenerator {
last_evoked: 0,
count: Mutex::new(0),
}
}

pub fn generate(&mut self, worker_id: u16) -> u64 {
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH + EPOCH)
.expect("Time went backwards")
.as_millis() as u64
& mask(42);

let mut count = self.count.lock().expect("Poisoned mutex");

if self.last_evoked > timestamp {
*count = 0;
self.last_evoked = timestamp;
}

let id = timestamp << 22 | (worker_id as u64 & mask(10)) << 12 | (*count as u64 & mask(12));

*count += 1;

id
}
}

pub trait Object {
fn get_id(&self) -> u64;
fn creation_time(&self) -> SystemTime {
UNIX_EPOCH + EPOCH + Duration::from_millis(self.get_id() >> 22)
}

fn serialize(&self) -> Vec<u8>;
fn deserialize(data: &[u8]) -> Self;
}

#[cfg(test)]
mod tests {
use super::SnowflakeGenerator;

#[test]
fn test_snowflake_count() {
let mut snowflake = SnowflakeGenerator::new();

assert_ne!(snowflake.generate(0), snowflake.generate(0));
}

#[test]
fn test_snowflake_worker() {
let mut snowflake = SnowflakeGenerator::new();

assert_ne!(snowflake.generate(1), snowflake.generate(0));
}
}
153 changes: 0 additions & 153 deletions protolok/src/ids.rs

This file was deleted.

6 changes: 6 additions & 0 deletions protolok/src/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::Object;

pub trait Instance {
/// Gets an object from its Snowflake ID
fn from_object(&self, id: u64) -> impl Object;
}
11 changes: 6 additions & 5 deletions protolok/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
pub mod channel;
pub mod crypto;
pub mod ids;
pub mod message;
pub mod instance;
pub mod place;
pub mod user;
pub mod message;
pub mod id;

pub use channel::*;
pub use ids::*;
pub use message::*;
pub use place::*;
pub use user::*;
pub use message::*;
pub use id::*;
pub use instance::*;
Loading