summaryrefslogtreecommitdiffstats
path: root/src/mycelium.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mycelium.rs')
-rw-r--r--src/mycelium.rs51
1 files changed, 33 insertions, 18 deletions
diff --git a/src/mycelium.rs b/src/mycelium.rs
index e623659..d2deebe 100644
--- a/src/mycelium.rs
+++ b/src/mycelium.rs
@@ -42,14 +42,16 @@ struct InboundMessage {
payload: String,
}
-#[derive(Debug, Serialize)]
+// Deserialize only for debugging
+#[derive(Debug, Serialize, Deserialize)]
struct OutboundMessage {
dst: Destination,
topic: String,
payload: String,
}
-#[derive(Debug, Serialize)]
+// Deserialize only for debugging
+#[derive(Debug, Serialize, Deserialize)]
struct Destination {
pk: String,
}
@@ -554,7 +556,7 @@ impl Transport for MyceliumTransport {
}
async fn send_encoded(&self, message: &EncodedMessage) -> Result<()> {
- tracing::debug!(payload_bytes = message.body.len(), "Sending message");
+ tracing::trace!(payload_bytes = message.body.len(), "Sending message");
let response = self
.client
.post(self.messages_endpoint.clone())
@@ -576,16 +578,28 @@ impl Transport for MyceliumTransport {
// Mycelium's message ID, for GET /api/v1/messages/status/{id} when chasing
// a message that never arrived.
if tracing::enabled!(tracing::Level::DEBUG) {
- if let Ok(body) = response
+ let Ok(body) = response
.bytes()
.await
.map_err(anyhow::Error::from)
.and_then(|bytes| Ok(serde_json::from_slice::<serde_json::Value>(&bytes)?))
- {
- if let Some(id) = body.get("id").and_then(|id| id.as_str()) {
- tracing::debug!(peer = %message.peer, message_id = id, "Mycelium accepted message");
- }
- }
+ else {
+ tracing::debug!(to = %message.peer, "Sent message (couldn't decode response from Mycelium REST API)");
+ return Ok(());
+ };
+ let Ok(outbound) = serde_json::from_slice::<OutboundMessage>(&message.body) else {
+ tracing::debug!(to = %message.peer, body_len = %message.body.len(), "Sent message (couldn't parse outbound message)");
+ return Ok(());
+ };
+ let Ok(parsed) = decode_payload(&outbound.topic, &outbound.payload) else {
+ tracing::debug!(to = %message.peer, message = ?outbound, "Sent message (couldn't decode outbound message)");
+ return Ok(());
+ };
+ let Some(id) = body.get("id").and_then(|id| id.as_str()) else {
+ tracing::debug!(to = %message.peer, message = ?parsed, "Sent message (couldn't read id from Mycelium REST API response)");
+ return Ok(());
+ };
+ tracing::debug!(to = %message.peer, message = ?parsed, id = %id, "Sent message");
}
Ok(())
}
@@ -651,14 +665,15 @@ impl Transport for MyceliumTransport {
}
};
- tracing::debug!(
- id = %inbound_message.id,
- payload_bytes = raw_bytes.len(),
- "Received inbound Mycelium message"
- );
-
match parse_message(&inbound_message) {
Ok((sender, parsed)) => {
+ tracing::debug!(
+ from = %inbound_message.src_pk,
+ message = ?parsed,
+ id = %inbound_message.id,
+ //payload_bytes = raw_bytes.len(),
+ "Received message"
+ );
if let Err(error) = handler(sender, parsed).await {
tracing::warn!(error = %format!("{error:#}"), "Failed to process inbound message");
}
@@ -694,7 +709,8 @@ fn encode_payload(payload: &Vec<u8>) -> String {
BASE64.encode(payload)
}
-fn decode_payload(topic: &str, payload: &str) -> Result<ParsedMessage, TransportError> {
+fn decode_payload(topic_encoded: &str, payload: &str) -> Result<ParsedMessage, TransportError> {
+ let topic: &str = &decode_topic(topic_encoded)?;
let bytes = BASE64.decode(payload)?;
match topic {
TOPIC_HELLO => {
@@ -722,8 +738,7 @@ fn parse_message(message: &InboundMessage) -> Result<(String, ParsedMessage), Tr
return Err(TransportError::NoSender);
}
- let topic = decode_topic(&message.topic)?;
- let parsed = decode_payload(&topic, &message.payload)?;
+ let parsed = decode_payload(&message.topic, &message.payload)?;
Ok((sender, parsed))
}