Apply go formatting
This commit is contained in:
parent
54aa54a59a
commit
3f926c52eb
|
@ -10,9 +10,9 @@ import (
|
|||
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
|
||||
|
||||
uuid "github.com/google/uuid"
|
||||
"github.com/ipfs/go-ipfs/thirdparty/assert"
|
||||
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
|
||||
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
|
||||
"github.com/ipfs/go-ipfs/thirdparty/assert"
|
||||
)
|
||||
|
||||
// pattern: /protocol-name/request-or-response-message/version
|
||||
|
@ -21,7 +21,7 @@ const echoResponse = "/echo/echoresp/0.0.1"
|
|||
|
||||
type EchoProtocol struct {
|
||||
host host.Host // local host
|
||||
requests map[string] *p2p.EchoRequest // used to access request data from response handlers
|
||||
requests map[string]*p2p.EchoRequest // used to access request data from response handlers
|
||||
done chan bool // only for demo purposes to hold main from terminating
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,6 @@ func NewEchoProtocol(host host.Host, done chan bool) *EchoProtocol {
|
|||
|
||||
// remote peer requests handler
|
||||
func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
|
||||
|
||||
// get request data
|
||||
data := &p2p.EchoRequest{}
|
||||
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))
|
||||
|
|
|
@ -55,4 +55,4 @@ func main() {
|
|||
for i := 0; i < 4; i++ {
|
||||
<-done
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
const clientVersion = "go-p2p-node/0.0.1"
|
||||
|
||||
// helper method - writes a protobuf go data object to a network stream
|
||||
// data - address of protobuf go data object to send
|
||||
// data - reference of protobuf go data object to send (not the object itself)
|
||||
// s - network stream to write the data to
|
||||
func sendDataObject(data interface{}, s inet.Stream) bool {
|
||||
writer := bufio.NewWriter(s)
|
||||
|
@ -35,12 +35,11 @@ func sendDataObject(data interface{}, s inet.Stream) bool {
|
|||
// nodeId - message author id
|
||||
// messageId - unique for requests, copied from request for responses
|
||||
func NewMessageData(nodeId string, messageId string, gossip bool) *p2p.MessageData {
|
||||
return &p2p.MessageData{
|
||||
ClientVersion: clientVersion,
|
||||
NodeId: nodeId,
|
||||
Timestamp: time.Now().Unix(),
|
||||
Id: messageId,
|
||||
Gossip: gossip}
|
||||
return &p2p.MessageData{ClientVersion: clientVersion,
|
||||
NodeId: nodeId,
|
||||
Timestamp: time.Now().Unix(),
|
||||
Id: messageId,
|
||||
Gossip: gossip}
|
||||
}
|
||||
|
||||
// Node type - implements one or more p2p protocols
|
||||
|
@ -50,9 +49,9 @@ type Node struct {
|
|||
echoProtocol *EchoProtocol // echp protocl imp
|
||||
}
|
||||
|
||||
// create a new node with its supported protocols
|
||||
// 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)}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ const pingResponse = "/ping/pingresp/0.0.1"
|
|||
type PingProtocol struct {
|
||||
host host.Host // local host
|
||||
requests map[string]*p2p.PingRequest // used to access request data from response handlers
|
||||
done chan bool // only for demo purposes to hold main from terminating
|
||||
done chan bool // only for demo purposes to stop main from terminating
|
||||
}
|
||||
|
||||
func NewPingProtocol(host host.Host, done chan bool) *PingProtocol {
|
||||
|
@ -106,7 +106,7 @@ func (p *PingProtocol) Ping(node *Node) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// store request so response handler has access to it
|
||||
// store ref request so response handler has access to it
|
||||
p.requests[req.MessageData.Id] = req
|
||||
log.Printf("%s: Ping to: %s was sent. Message Id: %s, Message: %s", p.host.ID(), node.host.ID(), req.MessageData.Id, req.Message)
|
||||
return true
|
||||
|
|
Loading…
Reference in New Issue