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

Update node type

parent f0b18569
...@@ -45,8 +45,9 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) { ...@@ -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) 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) 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{ resp := &p2p.EchoResponse{
MessageData: NewMessageData(e.host.ID().String(), data.MessageData.Id, false), MessageData: NewMessageData(e.host.ID().String(), data.MessageData.Id, false),
Message: data.Message} Message: data.Message}
...@@ -85,7 +86,7 @@ func (e *EchoProtocol) onEchoResponse(s inet.Stream) { ...@@ -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") 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 e.done <- true
} }
......
...@@ -18,7 +18,7 @@ import ( ...@@ -18,7 +18,7 @@ import (
func makeRandomNode(port int, done chan bool) *Node { func makeRandomNode(port int, done chan bool) *Node {
// Ignoring most errors for brevity // Ignoring most errors for brevity
// See echo example for more details and better implementation // 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) pid, _ := peer.IDFromPublicKey(pub)
listen, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)) listen, _ := ma.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
peerStore := ps.NewPeerstore() peerStore := ps.NewPeerstore()
...@@ -47,10 +47,10 @@ func main() { ...@@ -47,10 +47,10 @@ 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 messages using the protocols // send messages using the protocols
h1.pingProtocol.Ping(h2) h1.Ping(h2)
h2.pingProtocol.Ping(h1) h2.Ping(h1)
h1.echoProtocol.Echo(h2) h1.Echo(h2)
h2.echoProtocol.Echo(h1) h2.Echo(h1)
// block until all responses have been processed // block until all responses have been processed
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
......
...@@ -44,14 +44,14 @@ func NewMessageData(nodeId string, messageId string, gossip bool) *p2p.MessageDa ...@@ -44,14 +44,14 @@ func NewMessageData(nodeId string, messageId string, gossip bool) *p2p.MessageDa
// Node type - implements one or more p2p protocols // Node type - implements one or more p2p protocols
type Node struct { type Node struct {
host host.Host // lib-p2p host host host.Host // lib-p2p host
pingProtocol *PingProtocol // ping protocol impl *PingProtocol // ping protocol impl
echoProtocol *EchoProtocol // echo protocol impl *EchoProtocol // echo protocol impl
} }
// create a new node with its implemented protocols // create a new node with its implemented protocols
func NewNode(host host.Host, done chan bool) *Node { func NewNode(host host.Host, done chan bool) *Node {
return &Node{host: host, return &Node{host: host,
pingProtocol: NewPingProtocol(host, done), PingProtocol: NewPingProtocol(host, done),
echoProtocol: NewEchoProtocol(host, done)} EchoProtocol: NewEchoProtocol(host, done)}
} }
...@@ -10,7 +10,7 @@ message MessageData { ...@@ -10,7 +10,7 @@ message MessageData {
string id = 3; // allows requesters to use request data when processing a response 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 peer that may have sent it) 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 //// ping protocol
......
# Protocol Multiplexing using multicodecs with libp2p # 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. This examples shows how to use multicodecs (i.e. json) to encode and transmit information between LibP2P hosts using LibP2P Streams.
......
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