Update node type

This commit is contained in:
Aviv Eyal 2017-11-27 22:00:09 +02:00 committed by Steven Allen
parent f0b185695a
commit c6bfb4d945
5 changed files with 15 additions and 13 deletions

View File

@ -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
}

View File

@ -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++ {

View File

@ -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)}
}

View File

@ -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

View File

@ -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.