Skip to content

Commit

Permalink
few adaptations to use latest nwaku master
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Jan 8, 2025
1 parent 863d3ae commit 4cdbacd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 21 deletions.
13 changes: 12 additions & 1 deletion examples/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ async fn main() -> Result<(), Error> {
println!("Message Received in NODE 2: {}", msg);
println!("::::::::::::::::::::::::::::::::::::::::::::::::::::");
}
WakuEvent::RelayTopicHealthChange(_evt) => {} // do nothing
WakuEvent::RelayTopicHealthChange(_evt) => {
// dbg!("Relay topic change evt", evt);
}
WakuEvent::ConnectionChange(_evt) => {
// dbg!("Conn change evt", evt);
}
WakuEvent::Unrecognized(err) => panic!("Unrecognized waku event: {:?}", err),
_ => panic!("event case not expected"),
};
Expand All @@ -64,6 +69,12 @@ async fn main() -> Result<(), Error> {
println!("Message Received in NODE 1: {}", msg);
println!("::::::::::::::::::::::::::::::::::::::::::::::::::::");
}
WakuEvent::RelayTopicHealthChange(_evt) => {
// dbg!("Relay topic change evt", evt);
}
WakuEvent::ConnectionChange(_evt) => {
// dbg!("Conn change evt", evt);
}
WakuEvent::Unrecognized(err) => panic!("Unrecognized waku event: {:?}", err),
_ => panic!("event case not expected"),
};
Expand Down
31 changes: 18 additions & 13 deletions examples/tic-tac-toe-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ impl TicTacToeApp<Initialized> {
}
}
},
WakuEvent::RelayTopicHealthChange(_evt) => {}, // do nothing
WakuEvent::RelayTopicHealthChange(_evt) => {
// dbg!("Relay topic change evt", evt);
},
WakuEvent::ConnectionChange(_evt) => {
// dbg!("Conn change evt", evt);
},
WakuEvent::Unrecognized(err) => panic!("Unrecognized waku event: {:?}", err),
_ => panic!("event case not expected"),
};
Expand All @@ -89,14 +94,14 @@ impl TicTacToeApp<Initialized> {
let waku = self.waku.start().await.expect("waku should start");

// Subscribe to desired topic using the relay protocol
// waku.relay_subscribe(&self.game_topic).await.expect("waku should subscribe");
waku.relay_subscribe(&self.game_topic).await.expect("waku should subscribe");

// Example filter subscription. This is needed in edge nodes (resource-restricted devices)
// Nodes usually use either relay or lightpush/filter protocols

let ctopic = WakuContentTopic::new("waku", "2", "tictactoegame", Encoding::Proto);
let content_topics = vec![ctopic];
waku.filter_subscribe(&self.game_topic, content_topics).await.expect("waku should subscribe");
// let ctopic = WakuContentTopic::new("waku", "2", "tictactoegame", Encoding::Proto);
// let content_topics = vec![ctopic];
// waku.filter_subscribe(&self.game_topic, content_topics).await.expect("waku should subscribe");

// End filter example ----------------------------------------

Expand Down Expand Up @@ -137,18 +142,18 @@ impl TicTacToeApp<Running> {
false,
);

// if let Ok(msg_hash) = self.waku.relay_publish_message(&message, &self.game_topic, None).await {
// dbg!(format!("message hash published: {}", msg_hash));
// }
if let Ok(msg_hash) = self.waku.relay_publish_message(&message, &self.game_topic, None).await {
dbg!(format!("message hash published: {}", msg_hash));
}

// Example lightpush publish message. This is needed in edge nodes (resource-restricted devices)
// Nodes usually use either relay or lightpush/filter protocols
//
let msg_hash_ret = self.waku.lightpush_publish_message(&message, &self.game_topic).await;
match msg_hash_ret {
Ok(msg_hash) => println!("Published message hash {:?}", msg_hash.to_string()),
Err(error) => println!("Failed to publish with lightpush: {}", error)
}
// let msg_hash_ret = self.waku.lightpush_publish_message(&message, &self.game_topic).await;
// match msg_hash_ret {
// Ok(msg_hash) => println!("Published message hash {:?}", msg_hash.to_string()),
// Err(error) => println!("Failed to publish with lightpush: {}", error)
// }
// End example lightpush publish message
}

Expand Down
10 changes: 6 additions & 4 deletions examples/toy-chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ impl App<Initialized> {
}
}
},
WakuEvent::RelayTopicHealthChange(_evt) => {} // do nothing
WakuEvent::RelayTopicHealthChange(_evt) => {
// dbg!("Relay topic change evt", evt);
},
WakuEvent::ConnectionChange(_evt) => {
// dbg!("Conn change evt", evt);
},
WakuEvent::Unrecognized(err) => eprintln!("Unrecognized waku event: {:?}", err),
_ => eprintln!("event case not expected"),
};
Expand All @@ -120,9 +125,6 @@ impl App<Initialized> {

let waku = self.waku.start().await?;

let pubsub_topic = PubsubTopic::new(DEFAULT_PUBSUB_TOPIC);
waku.relay_subscribe(&pubsub_topic).await?;

Ok(App {
input: self.input,
nick: self.nick,
Expand Down
33 changes: 31 additions & 2 deletions waku-bindings/src/node/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum WakuEvent {
#[serde(rename = "relay_topic_health_change")]
RelayTopicHealthChange(TopicHealthEvent),

#[serde(rename = "connection_change")]
ConnectionChange(ConnectionChangeEvent),

Unrecognized(serde_json::Value),
}

Expand All @@ -39,7 +42,7 @@ pub struct WakuMessageEvent {
pub waku_message: WakuMessage,
}

/// Type of `event` field for a `message` event
/// Type of `event` field for a `topic health` event
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TopicHealthEvent {
Expand All @@ -49,10 +52,20 @@ pub struct TopicHealthEvent {
pub topic_health: String,
}

/// Type of `event` field for a `connection change` event
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ConnectionChangeEvent {
/// The pubsub topic on which the message was received
pub peer_id: String,
/// The message hash
pub peer_event: String,
}

#[cfg(test)]
mod tests {
use crate::WakuEvent;
use crate::WakuEvent::RelayTopicHealthChange;
use crate::WakuEvent::{ConnectionChange, RelayTopicHealthChange};

#[test]
fn deserialize_message_event() {
Expand All @@ -73,4 +86,20 @@ mod tests {
_ => panic!("Expected RelayTopicHealthChange event, but got {:?}", evt),
}
}

#[test]
fn deserialize_connection_change_event() {
let s = "{\"eventType\":\"connection_change\", \"peerId\":\"16Uiu2HAmAR24Mbb6VuzoyUiGx42UenDkshENVDj4qnmmbabLvo31\",\"peerEvent\":\"Joined\"}";
let evt: WakuEvent = serde_json::from_str(s).unwrap();
match evt {
ConnectionChange(conn_change_event) => {
assert_eq!(
conn_change_event.peer_id,
"16Uiu2HAmAR24Mbb6VuzoyUiGx42UenDkshENVDj4qnmmbabLvo31"
);
assert_eq!(conn_change_event.peer_event, "Joined");
}
_ => panic!("Expected RelayTopicHealthChange event, but got {:?}", evt),
}
}
}

0 comments on commit 4cdbacd

Please sign in to comment.