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

Make sendProtoMessage a method

parent 3eb465bf
...@@ -47,6 +47,6 @@ The idea is to use lib-p2p protocol multiplexing on a per-message basis. ...@@ -47,6 +47,6 @@ The idea is to use lib-p2p protocol multiplexing on a per-message basis.
5. Full access to request data when processing a response. 5. Full access to request data when processing a response.
## Author ## Author
@avive @avive
...@@ -80,7 +80,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) { ...@@ -80,7 +80,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
return return
} }
ok := sendProtoMessage(resp, s) ok := e.node.sendProtoMessage(resp, s)
if ok { if ok {
log.Printf("%s: Echo response to %s sent.", s.Conn().LocalPeer().String(), s.Conn().RemotePeer().String()) log.Printf("%s: Echo response to %s sent.", s.Conn().LocalPeer().String(), s.Conn().RemotePeer().String())
...@@ -143,7 +143,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool { ...@@ -143,7 +143,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool {
return false return false
} }
ok := sendProtoMessage(req, s) ok := e.node.sendProtoMessage(req, s)
if !ok { if !ok {
return false return false
......
package main package main
import ( import (
"bufio"
"log" "log"
"time" "time"
"bufio"
p2p "github.com/avive/go-libp2p/examples/multipro/pb" p2p "github.com/avive/go-libp2p/examples/multipro/pb"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host" host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer" peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto" crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net" inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
) )
...@@ -37,7 +37,6 @@ func NewNode(host host.Host, done chan bool) *Node { ...@@ -37,7 +37,6 @@ func NewNode(host host.Host, done chan bool) *Node {
// message: a protobufs go data object // message: a protobufs go data object
// data: common p2p message data // data: common p2p message data
func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool { func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool {
// store a temp ref to signature and remove it from message data // store a temp ref to signature and remove it from message data
// sign is a string to allow easy reset to zero-value (empty string) // sign is a string to allow easy reset to zero-value (empty string)
sign := data.Sign sign := data.Sign
...@@ -138,7 +137,7 @@ func (n *Node) NewMessageData(messageId string, gossip bool) *p2p.MessageData { ...@@ -138,7 +137,7 @@ func (n *Node) NewMessageData(messageId string, gossip bool) *p2p.MessageData {
// helper method - writes a protobuf go data object to a network stream // 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) // data: reference of protobuf go data object to send (not the object itself)
// s: network stream to write the data to // s: network stream to write the data to
func sendProtoMessage(data proto.Message, s inet.Stream) bool { func (n *Node) sendProtoMessage(data proto.Message, s inet.Stream) bool {
writer := bufio.NewWriter(s) writer := bufio.NewWriter(s)
enc := protobufCodec.Multicodec(nil).Encoder(writer) enc := protobufCodec.Multicodec(nil).Encoder(writer)
err := enc.Encode(data) err := enc.Encode(data)
......
...@@ -6,12 +6,11 @@ import ( ...@@ -6,12 +6,11 @@ import (
"fmt" "fmt"
"log" "log"
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
p2p "github.com/avive/go-libp2p/examples/multipro/pb" p2p "github.com/avive/go-libp2p/examples/multipro/pb"
uuid "github.com/google/uuid" uuid "github.com/google/uuid"
protobufCodec "github.com/multiformats/go-multicodec/protobuf" protobufCodec "github.com/multiformats/go-multicodec/protobuf"
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host" "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
) )
// pattern: /protocol-name/request-or-response-message/version // pattern: /protocol-name/request-or-response-message/version
...@@ -76,7 +75,7 @@ func (p *PingProtocol) onPingRequest(s inet.Stream) { ...@@ -76,7 +75,7 @@ func (p *PingProtocol) onPingRequest(s inet.Stream) {
return return
} }
ok := sendProtoMessage(resp, s) ok := p.node.sendProtoMessage(resp, s)
if ok { if ok {
log.Printf("%s: Ping response to %s sent.", s.Conn().LocalPeer().String(), s.Conn().RemotePeer().String()) log.Printf("%s: Ping response to %s sent.", s.Conn().LocalPeer().String(), s.Conn().RemotePeer().String())
...@@ -136,7 +135,7 @@ func (p *PingProtocol) Ping(host host.Host) bool { ...@@ -136,7 +135,7 @@ func (p *PingProtocol) Ping(host host.Host) bool {
return false return false
} }
ok := sendProtoMessage(req, s) ok := p.node.sendProtoMessage(req, s)
if !ok { if !ok {
return false return false
......
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