@@ -8,39 +8,62 @@ use std::collections::HashMap;
88use std:: future:: Future ;
99use std:: pin:: Pin ;
1010
11- /// Async callback type for request/response protocols
11+ /// Async callback type for request/response protocol commands
1212pub type RequestCallback = fn (
1313 & str , // identity
1414 & str , // bind_alias
15+ & str , // protocol (e.g., "mail.fastn.com")
16+ & str , // command (e.g., "settings.add-forwarding")
1517 & PathBuf , // protocol_dir
1618 serde_json:: Value , // request
1719) -> Pin < Box < dyn Future < Output = Result < serde_json:: Value , Box < dyn std:: error:: Error + Send + Sync > > > + Send > > ;
1820
19- /// Async callback type for streaming protocols
21+ /// Async callback type for streaming protocol commands
2022pub type StreamCallback = fn (
2123 & str , // identity
2224 & str , // bind_alias
25+ & str , // protocol (e.g., "filetransfer.fastn.com")
26+ & str , // command (e.g., "transfer.large-file")
2327 & PathBuf , // protocol_dir
2428 serde_json:: Value , // initial_data
2529) -> Pin < Box < dyn Future < Output = Result < ( ) , Box < dyn std:: error:: Error + Send + Sync > > > + Send > > ;
2630
2731/// Multi-identity server builder that discovers and serves all configured protocols
2832pub struct ServeAllBuilder {
2933 fastn_home : PathBuf ,
30- request_callbacks : HashMap < String , RequestCallback > ,
31- stream_callbacks : HashMap < String , StreamCallback > ,
34+ request_callbacks : HashMap < String , RequestCallback > , // Key: "protocol.command"
35+ stream_callbacks : HashMap < String , StreamCallback > , // Key: "protocol.command"
3236}
3337
3438impl ServeAllBuilder {
35- /// Register a request/response callback for a protocol
36- pub fn handle_requests ( mut self , protocol_name : & str , callback : RequestCallback ) -> Self {
37- self . request_callbacks . insert ( protocol_name. to_string ( ) , callback) ;
39+ /// Register a request/response callback for a protocol command
40+ ///
41+ /// # Arguments
42+ /// * `protocol` - Protocol name (e.g., "mail.fastn.com", "echo.fastn.com")
43+ /// * `command` - Command name (e.g., "get-mails", "send-mail", "basic-echo")
44+ /// * `callback` - Handler function
45+ ///
46+ /// # Example
47+ /// ```rust,no_run
48+ /// fastn_p2p::serve_all()
49+ /// .handle_requests("mail.fastn.com", "get-mails", get_mails_handler)
50+ /// .handle_requests("mail.fastn.com", "send-mail", send_mail_handler)
51+ /// ```
52+ pub fn handle_requests ( mut self , protocol : & str , command : & str , callback : RequestCallback ) -> Self {
53+ let key = format ! ( "{}.{}" , protocol, command) ;
54+ self . request_callbacks . insert ( key, callback) ;
3855 self
3956 }
4057
41- /// Register a streaming callback for a protocol
42- pub fn handle_streams ( mut self , protocol_name : & str , callback : StreamCallback ) -> Self {
43- self . stream_callbacks . insert ( protocol_name. to_string ( ) , callback) ;
58+ /// Register a streaming callback for a protocol command
59+ ///
60+ /// # Arguments
61+ /// * `protocol` - Protocol name (e.g., "filetransfer.fastn.com")
62+ /// * `command` - Command name (e.g., "large-file", "media-stream")
63+ /// * `callback` - Handler function
64+ pub fn handle_streams ( mut self , protocol : & str , command : & str , callback : StreamCallback ) -> Self {
65+ let key = format ! ( "{}.{}" , protocol, command) ;
66+ self . stream_callbacks . insert ( key, callback) ;
4467 self
4568 }
4669
@@ -124,21 +147,27 @@ pub fn serve_all() -> ServeAllBuilder {
124147 }
125148}
126149
127- /// Echo request handler callback
150+ /// Echo request handler callback for basic-echo command
128151pub fn echo_request_handler (
129152 identity : & str ,
130153 bind_alias : & str ,
154+ protocol : & str ,
155+ command : & str ,
131156 protocol_dir : & PathBuf ,
132157 request : serde_json:: Value ,
133158) -> Pin < Box < dyn Future < Output = Result < serde_json:: Value , Box < dyn std:: error:: Error + Send + Sync > > > + Send > > {
134159 let identity = identity. to_string ( ) ;
135160 let bind_alias = bind_alias. to_string ( ) ;
161+ let protocol = protocol. to_string ( ) ;
162+ let command = command. to_string ( ) ;
136163 let protocol_dir = protocol_dir. clone ( ) ;
137164
138165 Box :: pin ( async move {
139166 println ! ( "💬 Echo handler called:" ) ;
140167 println ! ( " Identity: {}" , identity) ;
141168 println ! ( " Bind alias: {}" , bind_alias) ;
169+ println ! ( " Protocol: {}" , protocol) ;
170+ println ! ( " Command: {}" , command) ;
142171 println ! ( " Protocol dir: {}" , protocol_dir. display( ) ) ;
143172
144173 // Parse request
@@ -154,7 +183,7 @@ pub fn echo_request_handler(
154183
155184 // Create response
156185 let response = serde_json:: json!( {
157- "echoed" : format!( "Echo from {}: {}" , identity, message)
186+ "echoed" : format!( "Echo from {} ({}) : {}" , identity, command , message)
158187 } ) ;
159188
160189 println ! ( "📤 Echo response: {}" , response) ;
0 commit comments