Make sendProtoMessage a method
This commit is contained in:
parent
3eb465bffe
commit
05928a9417
|
@ -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.
|
||||
|
||||
## Author
|
||||
|
||||
@avive
|
||||
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
|
|||
return
|
||||
}
|
||||
|
||||
ok := sendProtoMessage(resp, s)
|
||||
ok := e.node.sendProtoMessage(resp, s)
|
||||
|
||||
if ok {
|
||||
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 {
|
|||
return false
|
||||
}
|
||||
|
||||
ok := sendProtoMessage(req, s)
|
||||
ok := e.node.sendProtoMessage(req, s)
|
||||
|
||||
if !ok {
|
||||
return false
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"time"
|
||||
"bufio"
|
||||
|
||||
p2p "github.com/avive/go-libp2p/examples/multipro/pb"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
|
||||
host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
|
||||
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
|
||||
crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
|
||||
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
|
||||
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
|
||||
)
|
||||
|
||||
|
@ -37,7 +37,6 @@ func NewNode(host host.Host, done chan bool) *Node {
|
|||
// message: a protobufs go data object
|
||||
// data: common p2p message data
|
||||
func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData) bool {
|
||||
|
||||
// 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 := data.Sign
|
||||
|
@ -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
|
||||
// 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 {
|
||||
func (n *Node) sendProtoMessage(data proto.Message, s inet.Stream) bool {
|
||||
writer := bufio.NewWriter(s)
|
||||
enc := protobufCodec.Multicodec(nil).Encoder(writer)
|
||||
err := enc.Encode(data)
|
||||
|
|
|
@ -6,12 +6,11 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
|
||||
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
|
||||
|
||||
p2p "github.com/avive/go-libp2p/examples/multipro/pb"
|
||||
uuid "github.com/google/uuid"
|
||||
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
|
||||
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
|
||||
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
|
||||
)
|
||||
|
||||
// pattern: /protocol-name/request-or-response-message/version
|
||||
|
@ -76,7 +75,7 @@ func (p *PingProtocol) onPingRequest(s inet.Stream) {
|
|||
return
|
||||
}
|
||||
|
||||
ok := sendProtoMessage(resp, s)
|
||||
ok := p.node.sendProtoMessage(resp, s)
|
||||
|
||||
if ok {
|
||||
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 {
|
|||
return false
|
||||
}
|
||||
|
||||
ok := sendProtoMessage(req, s)
|
||||
ok := p.node.sendProtoMessage(req, s)
|
||||
|
||||
if !ok {
|
||||
return false
|
||||
|
|
Loading…
Reference in New Issue