diff --git a/examples/multipro/echo.go b/examples/multipro/echo.go index e7ddc6c5ce49f30b8941a94079e92947ce90034b..409bc390945fdf9ca08a08972078d9cdf88ef2da 100644 --- a/examples/multipro/echo.go +++ b/examples/multipro/echo.go @@ -10,7 +10,7 @@ import ( p2p "github.com/avive/go-libp2p/examples/multipro/pb" uuid "github.com/google/uuid" - "github.com/ipfs/go-ipfs/thirdparty/assert" + assert "github.com/ipfs/go-ipfs/thirdparty/assert" protobufCodec "github.com/multiformats/go-multicodec/protobuf" "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host" ) diff --git a/examples/multipro/main.go b/examples/multipro/main.go index 58331c3072d31e0fb61fec29594a438e3d8d69f6..13d690e5fbfca8a455e86709fd5fd8a6db2d0af2 100644 --- a/examples/multipro/main.go +++ b/examples/multipro/main.go @@ -46,7 +46,7 @@ func main() { log.Printf("This is a conversation between %s and %s\n", h1.ID(), h2.ID()) - // test implemented protocols + // send messages using the protocols h1.Ping(h2.Host) h2.Ping(h1.Host) h1.Echo(h2.Host) diff --git a/examples/multipro/node.go b/examples/multipro/node.go index 2ffe5352754674c1cd74ca0969ba932cc91f419b..978106bd123fe8ecb1fd8f4c5afdbd7eac11d0c5 100644 --- a/examples/multipro/node.go +++ b/examples/multipro/node.go @@ -1,13 +1,17 @@ package main import ( + "log" + "time" + "bufio" + p2p "github.com/avive/go-libp2p/examples/multipro/pb" "github.com/gogo/protobuf/proto" host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host" peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer" crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto" - "log" - "time" + protobufCodec "github.com/multiformats/go-multicodec/protobuf" + inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net" ) // node client version @@ -115,7 +119,6 @@ func (n *Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyD // helper method - generate message data shared between all node's p2p protocols // messageId: unique for requests, copied from request for responses func (n *Node) NewMessageData(messageId string, gossip bool) *p2p.MessageData { - // Add protobufs bin data for message author public key // this is useful for authenticating messages forwarded by a node authored by another node nodePubKey, err := n.Peerstore().PubKey(n.ID()).Bytes() @@ -131,3 +134,18 @@ func (n *Node) NewMessageData(messageId string, gossip bool) *p2p.MessageData { Id: messageId, Gossip: gossip} } + +// helper method - writes a protobuf go data object to a network stream +// data: reference of protobuf go data object to send (not the object itself) +// s: network stream to write the data to +func sendProtoMessage(data proto.Message, s inet.Stream) bool { + writer := bufio.NewWriter(s) + enc := protobufCodec.Multicodec(nil).Encoder(writer) + err := enc.Encode(data) + if err != nil { + log.Println(err) + return false + } + writer.Flush() + return true +} diff --git a/examples/multipro/protocol.go b/examples/multipro/protocol.go deleted file mode 100644 index 2fc0527e2af882b0d8371fa89c739ba8015823eb..0000000000000000000000000000000000000000 --- a/examples/multipro/protocol.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "bufio" - "github.com/gogo/protobuf/proto" - protobufCodec "github.com/multiformats/go-multicodec/protobuf" - inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net" - "log" -) - -// helper method - writes a protobuf go data object to a network stream -// data: reference of protobuf go data object to send (not the object itself) -// s: network stream to write the data to -func sendProtoMessage(data proto.Message, s inet.Stream) bool { - writer := bufio.NewWriter(s) - enc := protobufCodec.Multicodec(nil).Encoder(writer) - err := enc.Encode(data) - if err != nil { - log.Println(err) - return false - } - writer.Flush() - return true -}