diff --git a/examples/multipro/README.md b/examples/multipro/README.md index 343ac0c61b1ecb76181fcba44a4a5154f1afd8c3..9c5640fc11ec3d6cf457d82c246ecba936651acf 100644 --- a/examples/multipro/README.md +++ b/examples/multipro/README.md @@ -33,10 +33,19 @@ The example creates two LibP2P Hosts supporting 2 protocols: ping and echo. Each protocol consists RPC-style requests and respones and each request and response is a typed protobufs message (and a go data object). -This is a different pattern then defining a whole p2p protocol as 1 protobuf message with lots of optional fields (as can be observed in various p2p-lib protocols using protobufs such as dht). +This is a different pattern then defining a whole p2p protocol as one protobuf message with lots of optional fields (as can be observed in various p2p-lib protocols using protobufs such as dht). The example shows how to match async received responses with their requests. This is useful when processing a response requires access to the request data. +The idea is to use lib-p2p protocol multiplexing on a per-message basis. + +### Features +1. 2 fully implemented protocols using an RPC-like request-response pattern - Ping and Echo +2. Scaffolding for quickly implementing new app-level versioned RPC-like protocols +3. Full authentication of incoming message data by author (who might not be the message's sender peer) +4. Base p2p format in protobufs with fields shared by all protocol messages +5. Full access to request data when processing a response. + ## Author @avive diff --git a/examples/multipro/echo.go b/examples/multipro/echo.go index 82c4185a7abb8b055d1b803d45f50262bc2c63c0..e7ddc6c5ce49f30b8941a94079e92947ce90034b 100644 --- a/examples/multipro/echo.go +++ b/examples/multipro/echo.go @@ -29,6 +29,10 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol { e := EchoProtocol{node: node, requests: make(map[string]*p2p.EchoRequest), done: done} node.SetStreamHandler(echoRequest, e.onEchoRequest) node.SetStreamHandler(echoResponse, e.onEchoResponse) + + // design note: to implement fire-and-forget style messages you may just skip specifying a response callback. + // a fire-and-forget message will just include a request and not specify a response object + return &e } diff --git a/examples/multipro/node.go b/examples/multipro/node.go index 1d712c0955215ac559fc7f86b3b7918f8d0d75aa..2ffe5352754674c1cd74ca0969ba932cc91f419b 100644 --- a/examples/multipro/node.go +++ b/examples/multipro/node.go @@ -35,6 +35,7 @@ func NewNode(host host.Host, done chan bool) *Node { func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool { // store a temp ref to signature and remove it from message data + // sign is a string to allow easy reset to zero-value (empty string) sign := data.Sign data.Sign = "" diff --git a/examples/multipro/pb/p2p.proto b/examples/multipro/pb/p2p.proto index 0c71ebbc5fa216fa963a8edd00b4158eafda4462..53e652154b213b2f5d73de604c014a99ad09da86 100644 --- a/examples/multipro/pb/p2p.proto +++ b/examples/multipro/pb/p2p.proto @@ -30,7 +30,8 @@ message PingResponse { // response specific data string message = 2; - // ... add any data here + + // ... add any additional message data here } //// echo protocol @@ -41,7 +42,8 @@ message EchoRequest { // method specific data string message = 2; - // add any data here.... + + // add any additional message data here.... } message EchoResponse { @@ -49,5 +51,6 @@ message EchoResponse { // response specific data string message = 2; - // ... add any data here -} \ No newline at end of file + + // ... add any additional message data here.... +}