Skip to content

Commit

Permalink
move JsonMessage to jormungandr-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ecioppettini committed Jul 3, 2020
1 parent 2caba0d commit 48f32bc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 33 deletions.
2 changes: 1 addition & 1 deletion jormungandr-lib/Cargo.toml
Expand Up @@ -23,6 +23,7 @@ thiserror = "1.0"
poldercast = "0.13.1"
hex = "0.4"
multiaddr = "0.3.1"
serde_json = "1.0"

[dev-dependencies]
rand = "0.7"
Expand All @@ -33,5 +34,4 @@ chain-crypto = { path = "../chain-deps/chain-crypto", features = [ "property-
chain-core = { path = "../chain-deps/chain-core" }
ed25519-bip32 = "0.3"
serde_yaml = "0.8"
serde_json = "1.0"
bincode = "1.2"
1 change: 1 addition & 0 deletions jormungandr-lib/src/interfaces/mod.rs
Expand Up @@ -9,6 +9,7 @@ mod config;
mod fragment_log;
mod leadership_log;
mod linear_fee;
pub mod notifier;
mod old_address;
mod peer_stats;
mod ratio;
Expand Down
14 changes: 14 additions & 0 deletions jormungandr-lib/src/interfaces/notifier.rs
@@ -0,0 +1,14 @@
use crate::crypto::hash::Hash;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub enum JsonMessage {
NewBlock(Hash),
NewTip(Hash),
}

impl Into<String> for JsonMessage {
fn into(self) -> String {
serde_json::to_string(&self).unwrap()
}
}
25 changes: 7 additions & 18 deletions jormungandr/src/notifier/mod.rs
Expand Up @@ -3,6 +3,7 @@ use crate::utils::async_msg::{channel, MessageBox, MessageQueue};
use crate::utils::task::TokioServiceInfo;
use chain_impl_mockchain::header::HeaderId;
use futures::{SinkExt, StreamExt};
use jormungandr_lib::interfaces::notifier::JsonMessage;
use serde::{Serialize, Serializer};
use slog::Logger;
use std::sync::atomic::{AtomicUsize, Ordering};
Expand Down Expand Up @@ -107,10 +108,11 @@ async fn process_message(
msg: Message,
mut disconnected: MessageBox<usize>,
) {
let warp_msg = JsonMessage::from(msg).into();
let dead = async move { notify_all(clients, warp_msg).await };
let warp_msg = warp::ws::Message::text(JsonMessage::from(msg));

for id in dead.await {
let dead = notify_all(clients, warp_msg).await;

for id in dead {
disconnected.send(id).await.unwrap_or_else(|err| {
error!(
logger,
Expand Down Expand Up @@ -160,21 +162,8 @@ async fn handle_disconnected(
impl From<Message> for JsonMessage {
fn from(msg: Message) -> JsonMessage {
match msg {
Message::NewBlock(inner) => JsonMessage::NewBlock(inner),
Message::NewTip(inner) => JsonMessage::NewTip(inner),
Message::NewBlock(inner) => JsonMessage::NewBlock(inner.into()),
Message::NewTip(inner) => JsonMessage::NewTip(inner.into()),
}
}
}

fn to_hex<S>(key: &HeaderId, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&key.to_string())
}

impl Into<warp::ws::Message> for JsonMessage {
fn into(self) -> warp::ws::Message {
warp::ws::Message::text(serde_json::to_string(&self).unwrap())
}
}
@@ -1,5 +1,4 @@
use jormungandr_lib::crypto::hash::Hash;
use serde::Deserialize;
use jormungandr_lib::interfaces::notifier::JsonMessage;
use std::net::SocketAddr;
use std::sync::{Arc, RwLock};
use std::thread::JoinHandle;
Expand Down Expand Up @@ -29,13 +28,6 @@ pub struct JormungandrNotifier {
handles: Vec<JoinHandle<()>>,
}

// TODO: maybe this can be shared with the type in jormungandr (that only implements Serialize)
#[derive(Deserialize, Debug)]
pub enum JsonMessage {
NewBlock(Hash),
NewTip(Hash),
}

pub enum NotifierMessage {
JsonMessage(JsonMessage),
MaxConnectionsReached,
Expand Down
@@ -1,12 +1,9 @@
use crate::common::{
jormungandr::{
notifier::{JsonMessage, NotifierMessage},
ConfigurationBuilder,
},
jormungandr::{notifier::NotifierMessage, ConfigurationBuilder},
startup,
};

use jormungandr_lib::interfaces::ActiveSlotCoefficient;
use jormungandr_lib::interfaces::{notifier::JsonMessage, ActiveSlotCoefficient};
use std::sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc,
Expand Down

0 comments on commit 48f32bc

Please sign in to comment.