Commit 01e1a19c authored by Aviv Eyal's avatar Aviv Eyal Committed by Steven Allen
Browse files

updated readme and comments

parent 380ae70d
# Protocol Multiplexing using rpc-style multicodecs, protobufs with libp2p # Protocol Multiplexing using rpc-style multicodecs, protobufs with libp2p
This examples shows how to use multicodecs (i.e. protobufs) to encode and transmit information between LibP2P hosts using LibP2P Streams. This examples shows how to use multicodecs (i.e. protobufs) to encode and transmit information between LibP2P hosts using LibP2P Streams.
Multicodecs present a common interface, making it very easy to swap the codec implementation if needed. Multicodecs present a common interface, making it very easy to swap the codec implementation if needed.
This example expects that you area already familiar with the [echo example](https://github.com/libp2p/go-libp2p/tree/master/examples/echo). This example expects that you area already familiar with the [echo example](https://github.com/libp2p/go-libp2p/tree/master/examples/echo).
## Build ## Build
Compile the .proto files with the protobufs go compiler: Compile the .proto files using the protobufs go compiler:
``` ```
protoc --go_out=. ./p2p.proto protoc --go_out=. ./p2p.proto
...@@ -31,8 +29,9 @@ From `multipro` base source folder: ...@@ -31,8 +29,9 @@ From `multipro` base source folder:
## Details ## Details
The example creates two LibP2P Hosts. Host1 opens a stream to Host2. Host2 has an `StreamHandler` to deal with the incoming stream. This is covered in the `echo` example. 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).
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.
Both hosts simulate a conversation. But rather than sending raw messages on the stream, each message in the conversation is encoded under a `json` object (using the `json` multicodec). For example:
The stream lasts until one of the sides closes it when the HangUp field is `true`.
...@@ -47,22 +47,13 @@ func main() { ...@@ -47,22 +47,13 @@ func main() {
log.Printf("This is a conversation between %s and %s\n", h1.host.ID(), h2.host.ID()) log.Printf("This is a conversation between %s and %s\n", h1.host.ID(), h2.host.ID())
// send a ping from h1 to h2
h1.pingProtocol.Ping(h2) h1.pingProtocol.Ping(h2)
// pause main until pong was received and processed by h2
<-done
// send a ping from h1 to h2
h2.pingProtocol.Ping(h1) h2.pingProtocol.Ping(h1)
<-done
h1.echoProtocol.Echo(h2) h1.echoProtocol.Echo(h2)
<-done
h2.echoProtocol.Echo(h1) h2.echoProtocol.Echo(h1)
// block until all responses have been processed
for i := 0; i < 4; i++ {
<-done <-done
}
} }
\ No newline at end of file
...@@ -7,7 +7,7 @@ message MessageData { ...@@ -7,7 +7,7 @@ message MessageData {
// shared between all requests // shared between all requests
string clientVersion = 1; // client version string clientVersion = 1; // client version
int64 timestamp = 2; // unix time int64 timestamp = 2; // unix time
string id = 3; // allows requesters to match response with a request string id = 3; // allows requesters to use request data when processing a response
bool gossip = 4; // true to have receiver peer gossip the message to neighbors bool gossip = 4; // true to have receiver peer gossip the message to neighbors
string nodeId = 5; // id of node that created the message (not the one that may have relayed it) string nodeId = 5; // id of node that created the message (not the one that may have relayed it)
string sign = 6; // signature of message data + method specific data by message authoring node string sign = 6; // signature of message data + method specific data by message authoring node
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment