From c6bfb4d945965a2ec67c3d743979ad7436089772 Mon Sep 17 00:00:00 2001 From: Aviv Eyal Date: Mon, 27 Nov 2017 22:00:09 +0200 Subject: [PATCH] Update node type --- examples/multipro/echo.go | 5 +++-- examples/multipro/main.go | 10 +++++----- examples/multipro/node.go | 10 +++++----- examples/multipro/pb/p2p.proto | 2 +- .../protocol-multiplexing-with-multicodecs/README.md | 1 + 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/multipro/echo.go b/examples/multipro/echo.go index 5392e8e9..60e83c50 100644 --- a/examples/multipro/echo.go +++ b/examples/multipro/echo.go @@ -45,8 +45,9 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) { log.Printf("%s: Received echo request from %s. Message: %s", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.Message) - // send response to sender log.Printf("%s: Sending echo response to %s. Message id: %s...", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.MessageData.Id) + + // send response to request send using the message string he provided resp := &p2p.EchoResponse{ MessageData: NewMessageData(e.host.ID().String(), data.MessageData.Id, false), Message: data.Message} @@ -85,7 +86,7 @@ func (e *EchoProtocol) onEchoResponse(s inet.Stream) { assert.True(req.Message == data.Message, nil, "Expected echo to respond with request message") - log.Printf("%s: Received Echo response from %s. Message id:%s. Message: %s.", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.MessageData.Id, data.Message) + log.Printf("%s: Received echo response from %s. Message id:%s. Message: %s.", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.MessageData.Id, data.Message) e.done <- true } diff --git a/examples/multipro/main.go b/examples/multipro/main.go index 53efce6c..97218255 100644 --- a/examples/multipro/main.go +++ b/examples/multipro/main.go @@ -18,7 +18,7 @@ import ( func makeRandomNode(port int, done chan bool) *Node { // Ignoring most errors for brevity // See echo example for more details and better implementation - priv, pub, _ := crypto.GenerateKeyPair(crypto.RSA, 2048) + priv, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256) pid, _ := peer.IDFromPublicKey(pub) listen, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)) peerStore := ps.NewPeerstore() @@ -47,10 +47,10 @@ func main() { log.Printf("This is a conversation between %s and %s\n", h1.host.ID(), h2.host.ID()) // send messages using the protocols - h1.pingProtocol.Ping(h2) - h2.pingProtocol.Ping(h1) - h1.echoProtocol.Echo(h2) - h2.echoProtocol.Echo(h1) + h1.Ping(h2) + h2.Ping(h1) + h1.Echo(h2) + h2.Echo(h1) // block until all responses have been processed for i := 0; i < 4; i++ { diff --git a/examples/multipro/node.go b/examples/multipro/node.go index 7892bfbb..e490b5cb 100644 --- a/examples/multipro/node.go +++ b/examples/multipro/node.go @@ -44,14 +44,14 @@ func NewMessageData(nodeId string, messageId string, gossip bool) *p2p.MessageDa // Node type - implements one or more p2p protocols type Node struct { - host host.Host // lib-p2p host - pingProtocol *PingProtocol // ping protocol impl - echoProtocol *EchoProtocol // echo protocol impl + host host.Host // lib-p2p host + *PingProtocol // ping protocol impl + *EchoProtocol // echo protocol impl } // create a new node with its implemented protocols func NewNode(host host.Host, done chan bool) *Node { return &Node{host: host, - pingProtocol: NewPingProtocol(host, done), - echoProtocol: NewEchoProtocol(host, done)} + PingProtocol: NewPingProtocol(host, done), + EchoProtocol: NewEchoProtocol(host, done)} } diff --git a/examples/multipro/pb/p2p.proto b/examples/multipro/pb/p2p.proto index 62df22d2..13d31254 100644 --- a/examples/multipro/pb/p2p.proto +++ b/examples/multipro/pb/p2p.proto @@ -10,7 +10,7 @@ message MessageData { 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 string nodeId = 5; // id of node that created the message (not the peer that may have sent it) - string sign = 6; // signature of message data + method specific data by message authoring node + bytes sign = 6; // signature of message data + method specific data by message authoring node } //// ping protocol diff --git a/examples/protocol-multiplexing-with-multicodecs/README.md b/examples/protocol-multiplexing-with-multicodecs/README.md index 21793387..0001f60b 100644 --- a/examples/protocol-multiplexing-with-multicodecs/README.md +++ b/examples/protocol-multiplexing-with-multicodecs/README.md @@ -1,3 +1,4 @@ + # Protocol Multiplexing using multicodecs with libp2p This examples shows how to use multicodecs (i.e. json) to encode and transmit information between LibP2P hosts using LibP2P Streams.