message_router.rs

Overview

This file implements a MessageRouter that acts as a web server endpoint for receiving, authenticating, and forwarding external messages to a business process (BP) system. It leverages the actix_web framework to expose HTTP services for inbound messages, handles authorization tokens, and manages key cryptographic signing credentials. The router provides asynchronous processing of JSON-based messages, implements token issuance for authenticated requests, and manages internal state via thread-safe synchronization.

The main responsibilities include:


Structures and Implementations

MessageRouterConfig

pub struct MessageRouterConfig {
    pub bp_resolver: Arc<Mutex<dyn BPResolver>>,
    pub owner_wallet_pubkey: Option<String>,
    pub signing_keys: Option<KeyPair>,
}

MessageRouter

#[derive(Clone)]
pub struct MessageRouter {
    pub bind: SocketAddr,
    pub owner_wallet_pubkey: Option<String>,
    pub signing_keys: Option<KeyPair>,
    pub bp_resolver: Arc<Mutex<dyn BPResolver>>,
}

Display implementation for MessageRouter

Provides a human-readable string representation primarily for logging purposes.

impl Display for MessageRouter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MessageRouter bind={}", self.bind)
    }
}

MessageRouter Associated Functions and Methods

new

pub fn new(bind: String, config: MessageRouterConfig) -> Self

config

pub fn config(cfg: &mut web::ServiceConfig, message_router: Arc<MessageRouter>)

dump

pub fn dump(&self)

issue_token

pub fn issue_token(&self) -> Option<Token>

run

pub fn run(&self)

Asynchronous Functions

run

#[actix_web::main]
async fn run(message_router: Arc<MessageRouter>) -> anyhow::Result<()>

actix_ext_messages_handler

async fn actix_ext_messages_handler(
    node_requests: web::Json<serde_json::Value>,
    message_router: Arc<MessageRouter>,
) -> actix_web::Result<web::Json<serde_json::Value>>

Implementation Details and Algorithms


Interactions with Other System Components


Visual Diagram

flowchart TD
A[MessageRouter::run] --> B[Spawn Thread]
B --> C[run async fn]
C --> D[HttpServer::new]
D --> E[App Configuration]
E --> F[Middleware: Logger & Headers]
E --> G[MessageRouter::config]
G --> H[Route: POST ROUTER_URL_PATH]
H --> I[actix_ext_messages_handler]
I --> J[process_ext_messages::run]
J -->|Uses| K["BPResolver (Arc<Mutex>)"]
I -->|Uses| L[MessageRouter instance]
MessageRouter --> M[issue_token]
M --> N[Token creation]

Environment Configuration


Usage Example

use std::sync::Arc;
use parking_lot::Mutex;

// Assume BPResolver implementation and KeyPair initialization
let bp_resolver = Arc::new(Mutex::new(my_bp_resolver));
let config = MessageRouterConfig {
    bp_resolver,
    owner_wallet_pubkey: Some("owner_pubkey_string".to_string()),
    signing_keys: Some(my_keypair),
};

let router = MessageRouter::new("0.0.0.0:8080".to_string(), config);
router.run(); // Starts the server and listens for incoming messages

This example shows basic setup and startup of the message router server binding to all interfaces on port 8080.