Can now connect to Omikron :C
Some util
This commit is contained in:
parent
e15b2b407a
commit
8637be0a08
9 changed files with 391 additions and 78 deletions
16
src/main.rs
16
src/main.rs
|
|
@ -1,8 +1,12 @@
|
|||
use std::time::Duration;
|
||||
use json::{self, JsonValue};
|
||||
use tokio::time::sleep;
|
||||
use uuid::Uuid;
|
||||
|
||||
mod data;
|
||||
mod omikron;
|
||||
mod util;
|
||||
mod users;
|
||||
|
||||
use crate::omikron::omikronConnection::{OmikronConnection};
|
||||
use crate::data::communication::{CommunicationValue, LogLevel, LogValue, CommunicationType, DataTypes};
|
||||
|
|
@ -12,5 +16,17 @@ fn main() {
|
|||
let omikron = OmikronConnection::new();
|
||||
rt.block_on(async {
|
||||
omikron.connect().await;
|
||||
|
||||
omikron.send_message(
|
||||
CommunicationValue::new(
|
||||
CommunicationType::Identification
|
||||
)
|
||||
.add_data(DataTypes::UserIds, json::JsonValue::String(Uuid::new_v4().to_string()))
|
||||
.add_data(DataTypes::IotaId, json::JsonValue::String(Uuid::new_v4().to_string()))
|
||||
.to_json()
|
||||
.to_string()
|
||||
.as_mut()
|
||||
).await;
|
||||
omikron.close().await;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,23 @@
|
|||
use futures_util::{SinkExt, StreamExt};
|
||||
use tokio_tungstenite::tungstenite::Utf8Bytes;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::time::{sleep, Duration};
|
||||
use tokio_tungstenite::{
|
||||
client_async,
|
||||
connect_async,
|
||||
tungstenite::protocol::Message,
|
||||
MaybeTlsStream,
|
||||
WebSocketStream,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
use json::JsonValue;
|
||||
|
||||
use crate::{
|
||||
data::communication::CommunicationValue,
|
||||
data::communication::CommunicationType,
|
||||
data::communication::DataTypes
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OmikronConnection {
|
||||
|
|
@ -108,8 +114,7 @@ impl OmikronConnection {
|
|||
pub async fn send_message(&self, msg: &str) {
|
||||
let mut guard = self.writer.lock().await;
|
||||
if let Some(writer) = guard.as_mut() {
|
||||
let utf8: Utf8Bytes = Utf8Bytes::from(msg.to_string());
|
||||
if let Err(e) = writer.send(Message::Text(utf8)).await {
|
||||
if let Err(e) = writer.send(Message::Text(msg.to_string())).await {
|
||||
println!("[Omikron] Send failed: {}", e);
|
||||
}
|
||||
}
|
||||
|
|
@ -130,18 +135,4 @@ impl OmikronConnection {
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let omikron = OmikronConnection::new();
|
||||
omikron.connect().await;
|
||||
|
||||
let identification = r#"{
|
||||
"type": "identification",
|
||||
"iota_id": "iota-12345",
|
||||
"user_ids": ["user1", "user2"]
|
||||
}"#;
|
||||
|
||||
omikron.send_message(identification).await;
|
||||
|
||||
loop {
|
||||
sleep(Duration::from_secs(30)).await;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,141 @@
|
|||
use json::{self, JsonValue};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use axum::Json;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Contact {
|
||||
pub user_id: Option<Uuid>,
|
||||
pub user_name: Option<String>,
|
||||
pub last_message_at: Option<i64>,
|
||||
pub user_status: UserStatus,
|
||||
pub about: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum UserStatus {
|
||||
Online,
|
||||
DoNotDisturb,
|
||||
WC,
|
||||
Away,
|
||||
UserOffline,
|
||||
IotaOffline,
|
||||
}
|
||||
|
||||
impl Default for Contact {
|
||||
fn default() -> Self {
|
||||
let now = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as i64;
|
||||
Contact {
|
||||
user_id: None,
|
||||
user_name: None,
|
||||
last_message_at: Some(now),
|
||||
user_status: UserStatus::UserOffline,
|
||||
about: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Contact {
|
||||
pub fn new_with_time(last_message_at: i64, user_id: Uuid) -> Self {
|
||||
Contact {
|
||||
user_id: Some(user_id),
|
||||
user_name: None,
|
||||
last_message_at: Some(last_message_at),
|
||||
user_status: UserStatus::UserOffline,
|
||||
about: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(user_id: Uuid) -> Self {
|
||||
Contact {
|
||||
user_id: Some(user_id),
|
||||
user_name: None,
|
||||
last_message_at: None,
|
||||
user_status: UserStatus::UserOffline,
|
||||
about: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_json(&self) -> JsonValue {
|
||||
let mut obj = self.to_json();
|
||||
if let Some(id) = &self.user_id {
|
||||
obj["userID"] = JsonValue::from(id.to_string());
|
||||
}
|
||||
if let Some(name) = &self.user_name {
|
||||
obj["userName"] = JsonValue::from(name.as_str());
|
||||
}
|
||||
if let Some(ts) = &self.last_message_at {
|
||||
obj["lastMessageAt"] = JsonValue::from(ts.to_string());
|
||||
}
|
||||
obj
|
||||
}
|
||||
|
||||
pub fn from_string(s: &str) -> Contact {
|
||||
let parsed: JsonValue = JsonValue::from(s);
|
||||
Self::from_json(&parsed)
|
||||
}
|
||||
|
||||
pub fn from_json(o: &JsonValue) -> Contact {
|
||||
let user_id = o["userID"]
|
||||
.as_str()
|
||||
.and_then(|s| Uuid::parse_str(s).ok());
|
||||
|
||||
let user_name = o["userName"].as_str().map(|s| s.to_string());
|
||||
|
||||
let last_message_at = o["lastMessageAt"].as_i64();
|
||||
|
||||
Contact {
|
||||
user_id,
|
||||
user_name,
|
||||
last_message_at,
|
||||
user_status: UserStatus::UserOffline, // default
|
||||
about: None,
|
||||
}
|
||||
}
|
||||
pub fn info(&self) -> JsonValue {
|
||||
let mut obj = self.to_json();
|
||||
if let Some(id) = &self.user_id {
|
||||
obj["userID"] = JsonValue::from(id.to_string());
|
||||
}
|
||||
if let Some(name) = &self.user_name {
|
||||
obj["userName"] = JsonValue::from(name.as_str());
|
||||
}
|
||||
obj
|
||||
}
|
||||
|
||||
// getters & setters
|
||||
pub fn get_about(&self) -> Option<&String> {
|
||||
self.about.as_ref()
|
||||
}
|
||||
|
||||
pub fn set_about(&mut self, about: String) {
|
||||
self.about = Some(about);
|
||||
}
|
||||
|
||||
pub fn get_user_id(&self) -> Option<Uuid> {
|
||||
self.user_id
|
||||
}
|
||||
|
||||
pub fn set_user_id(&mut self, id: Uuid) {
|
||||
self.user_id = Some(id);
|
||||
}
|
||||
|
||||
pub fn get_user_name(&self) -> Option<&String> {
|
||||
self.user_name.as_ref()
|
||||
}
|
||||
|
||||
pub fn set_user_name(&mut self, name: String) {
|
||||
self.user_name = Some(name);
|
||||
}
|
||||
|
||||
pub fn get_user_status(&self) -> &UserStatus {
|
||||
&self.user_status
|
||||
}
|
||||
|
||||
pub fn set_user_status(&mut self, status: UserStatus) {
|
||||
self.user_status = status;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
use json::{self, Value};
|
||||
use json::{self, array, object, JsonValue};
|
||||
use std::fs::{self, File};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
|
||||
use json::{self, Value};
|
||||
use std::string::String;
|
||||
use json::{self, array, JsonValue};
|
||||
use std::fs::{self, File};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
use axum::Json;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::users::Contact::Contact; // assuming you have a Contact struct in a module
|
||||
use crate::users::contact;
|
||||
use crate::users::contact::Contact;
|
||||
// assuming you have a Contact struct in a module
|
||||
|
||||
pub struct ChatsUtil;
|
||||
|
||||
|
|
@ -40,8 +43,8 @@ impl ChatsUtil {
|
|||
};
|
||||
|
||||
for i in 0..contacts.len() {
|
||||
if contacts[i]["userID"].as_str() == Some(&contact.user_id.to_string()) {
|
||||
contacts.remove(i);
|
||||
if contacts[i]["userID"].as_str() == Some(&contact.user_id.unwrap().to_string()) {
|
||||
contacts.remove(stringify!("{}", i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -62,7 +65,7 @@ impl ChatsUtil {
|
|||
for i in 0..contacts.len() {
|
||||
if let Some(uid) = contacts[i]["userID"].as_str() {
|
||||
if Uuid::parse_str(uid).ok()? == user_id {
|
||||
return Contact::from_json(&contacts[i]);
|
||||
return Option::from(Contact::from_json(&contacts[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -70,7 +73,7 @@ impl ChatsUtil {
|
|||
None
|
||||
}
|
||||
|
||||
pub fn get_users(storage_owner: Uuid) -> Value {
|
||||
pub fn get_users(storage_owner: Uuid) -> JsonValue {
|
||||
let dir = format!("/users/{}/contacts/", storage_owner);
|
||||
let file_name = "contacts.json";
|
||||
let s = Self::load_file(&dir, file_name);
|
||||
|
|
@ -79,9 +82,8 @@ impl ChatsUtil {
|
|||
if !s.is_empty() {
|
||||
if let Ok(contacts) = json::parse(&s) {
|
||||
for i in 0..contacts.len() {
|
||||
if let Some(c) = Contact::from_json(&contacts[i]) {
|
||||
contacts_out.push(c.info()).unwrap();
|
||||
}
|
||||
let c = Contact::from_json(&contacts[i]);
|
||||
contacts_out.push(c.to_json()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@ use std::fs::{self, File};
|
|||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::files::Files;
|
||||
|
||||
pub struct ConfigUtil {
|
||||
pub config: JsonValue,
|
||||
pub unique: bool,
|
||||
|
|
@ -37,13 +35,12 @@ impl ConfigUtil {
|
|||
}
|
||||
|
||||
pub fn load(&mut self) {
|
||||
let s = Self::load_file(Files::MAIN);
|
||||
let s = Self::load_file("config.json");
|
||||
if !s.is_empty() {
|
||||
self.config = json::parse(&s).unwrap_or(JsonValue::new_object());
|
||||
}
|
||||
if self.config.has("port") {
|
||||
if let Some(port) = self.config["port"].as_i32() {
|
||||
// Set community manager port
|
||||
if self.config.has_key("ssl_port") {
|
||||
if let Some(port) = self.config["ssl_port"].as_i32() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -60,6 +57,6 @@ impl ConfigUtil {
|
|||
}
|
||||
|
||||
pub fn save(&self) -> std::io::Result<()> {
|
||||
Self::save_file(Files::MAIN, &self.config.dump())
|
||||
Self::save_file("config.json", &self.config.dump())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use sysinfo::{System};
|
||||
use std::fs::{self, File};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
|
@ -142,21 +143,14 @@ impl FileUtil {
|
|||
}
|
||||
|
||||
pub fn get_used_ram() -> String {
|
||||
// Rust has no direct Runtime like Java.
|
||||
// As a placeholder, read process memory usage from sysinfo.
|
||||
use sysinfo::{System, SystemExt, ProcessExt};
|
||||
|
||||
let mut sys = System::new_all();
|
||||
sys.refresh_all();
|
||||
if let Some(process) = sys.process(process::id() as i32) {
|
||||
let used = process.memory() * 1024; // kB to bytes
|
||||
let total = sys.total_memory() * 1024;
|
||||
return format!(
|
||||
"{}/{}",
|
||||
Self::design_byte(used),
|
||||
Self::design_byte(total)
|
||||
);
|
||||
}
|
||||
"Unknown".to_string()
|
||||
let used = sys.used_memory() * 1024; // kB to bytes
|
||||
let total = sys.total_memory() * 1024;
|
||||
format!(
|
||||
"{}/{}",
|
||||
Self::design_byte(used),
|
||||
Self::design_byte(total)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue