pub struct CallbackServer { /* private fields */ }Expand description
HTTP callback server for receiving UPnP event notifications.
The CallbackServer binds to a local port and provides an HTTP endpoint
for receiving UPnP NOTIFY requests. It validates UPnP headers and routes
events through an EventRouter to a channel.
§Example
use tokio::sync::mpsc;
use callback_server::{CallbackServer, NotificationPayload};
#[tokio::main]
async fn main() {
let (tx, mut rx) = mpsc::unbounded_channel::<NotificationPayload>();
let server = CallbackServer::new((3400, 3500), tx)
.await
.expect("Failed to create callback server");
println!("Server listening at: {}", server.base_url());
// Process notifications
while let Some(notification) = rx.recv().await {
println!("Received event for subscription: {}", notification.subscription_id);
}
}Implementations§
Source§impl CallbackServer
impl CallbackServer
Sourcepub async fn new(
port_range: (u16, u16),
event_sender: UnboundedSender<NotificationPayload>,
) -> Result<Self, String>
pub async fn new( port_range: (u16, u16), event_sender: UnboundedSender<NotificationPayload>, ) -> Result<Self, String>
Create and start a new unified callback server.
This method creates a single HTTP server that efficiently handles all UPnP event notifications from multiple speakers and services. The server:
- Finds an available port in the specified range
- Detects the local IP address for callback URLs
- Starts an HTTP server to receive all UPnP NOTIFY requests
- Routes events through a unified event router to registered handlers
§Unified Event Stream Processing
The callback server is designed to support the unified event stream processor pattern where a single HTTP endpoint receives events from multiple UPnP services and speakers, then routes them to appropriate handlers based on subscription IDs.
§Arguments
port_range- Range of ports to try binding to (start, end)event_sender- Channel for sending notification payloads to the unified processor
§Returns
Returns the callback server instance or an error if no port could be bound or the local IP address could not be detected.
§Example
let (tx, _rx) = mpsc::unbounded_channel::<NotificationPayload>();
let server = CallbackServer::new((3400, 3500), tx).await.unwrap();
println!("Unified callback server listening at: {}", server.base_url());Sourcepub fn base_url(&self) -> &str
pub fn base_url(&self) -> &str
Get the unified callback URL for subscription registration.
This URL should be used when subscribing to UPnP events from any speaker or service. The unified callback server will route all incoming events based on their subscription IDs to the appropriate handlers.
The format is http://<local_ip>:<port> and this same URL is used for
all subscriptions, enabling the unified event stream processing pattern.
§Example
let callback_url = server.base_url();
println!("Use this URL for all subscriptions: {}", callback_url);Sourcepub fn router(&self) -> &Arc<EventRouter>
pub fn router(&self) -> &Arc<EventRouter>
Get a reference to the event router.
The router can be used to register and unregister subscription IDs for event routing.
§Example
server.router().register("uuid:subscription-123".to_string()).await;