The bapao_app_protocal crate provides a high-level interface for handling application requests and responses in the Bapao communication system.
The main struct for handling incoming requests and routing them to appropriate handlers.
T: Fn() -> TransUnitType- A function type that returns aTransUnitType
Creates a new AppListener instance.
Example:
use bapao_app_protocal::AppListener;
let mut listener = AppListener::new();Registers a callback function for a specific route.
Parameters:
key: &'static str- The route path to handlecallback: T- The function to call when this route is requested
Example:
use bapao_app_protocal::{AppListener, TransUnitType};
fn handle_request() -> TransUnitType {
TransUnitType::String("Hello, World!".to_string())
}
let mut listener = AppListener::new();
listener.add("/api/hello", handle_request);Starts the listener and begins processing incoming requests asynchronously.
Example:
use bapao_app_protocal::AppListener;
#[tokio::main]
async fn main() {
let mut listener = AppListener::new();
// Add your routes here
listener.add("/api/status", || {
TransUnitType::String("OK".to_string())
});
// Start listening for requests
listener.listen().await;
}Re-exported from bapao_trans_protocal::trans_content::TransUnitType.
An enum representing the type of data that can be transmitted:
pub enum TransUnitType {
String(String), // Text data
File(Vec<u8>), // Binary file data
}Usage Examples:
use bapao_app_protocal::TransUnitType;
// Return text response
fn text_handler() -> TransUnitType {
TransUnitType::String("Response text".to_string())
}
// Return file response
fn file_handler() -> TransUnitType {
let file_data = std::fs::read("path/to/file.jpg").unwrap();
TransUnitType::File(file_data)
}Here's a complete example of setting up an application with multiple endpoints:
use bapao_app_protocal::{AppListener, TransUnitType};
use std::fs;
// Handler for status endpoint
fn status_handler() -> TransUnitType {
TransUnitType::String("System is running".to_string())
}
// Handler for screenshot endpoint
fn screenshot_handler() -> TransUnitType {
match fs::read("/path/to/screenshot.jpg") {
Ok(data) => TransUnitType::File(data),
Err(_) => TransUnitType::String("Screenshot failed".to_string()),
}
}
// Handler for system info endpoint
fn system_info_handler() -> TransUnitType {
let info = format!(
"{{\"hostname\": \"{}\", \"uptime\": \"{}\"}}",
"localhost",
"24h"
);
TransUnitType::String(info)
}
#[tokio::main]
async fn main() {
let mut listener = AppListener::new();
// Register endpoints
listener.add("/api/status", status_handler);
listener.add("/monitor/pic/shot", screenshot_handler);
listener.add("/system/info", system_info_handler);
println!("Starting Bapao application listener...");
listener.listen().await;
}- Request Registration: Use
add()to register route handlers - Listener Start: Call
listen()to start processing requests - Request Processing: The listener polls for new requests every 10 seconds
- Route Matching: Incoming requests are matched against registered routes
- Handler Execution: The appropriate callback function is executed
- Response Handling: The response is automatically sent back through the transport layer
The application protocol layer handles errors gracefully:
- Invalid routes result in no action (the request is ignored)
- Handler panics are caught by the transport layer
- Network errors are handled by the transport protocol layer
The AppListener is designed to be used in a single-threaded async context. All operations are async-aware and use Tokio for concurrency.