Apply go formatting

This commit is contained in:
Aviv Eyal 2017-11-27 20:34:52 +02:00 committed by Steven Allen
parent 54aa54a59a
commit 3f926c52eb
4 changed files with 14 additions and 16 deletions

View File

@ -10,9 +10,9 @@ import (
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net" inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
uuid "github.com/google/uuid" uuid "github.com/google/uuid"
"github.com/ipfs/go-ipfs/thirdparty/assert"
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb" p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec "github.com/multiformats/go-multicodec/protobuf" protobufCodec "github.com/multiformats/go-multicodec/protobuf"
"github.com/ipfs/go-ipfs/thirdparty/assert"
) )
// pattern: /protocol-name/request-or-response-message/version // pattern: /protocol-name/request-or-response-message/version
@ -21,7 +21,7 @@ const echoResponse = "/echo/echoresp/0.0.1"
type EchoProtocol struct { type EchoProtocol struct {
host host.Host // local host 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 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 // remote peer requests handler
func (e *EchoProtocol) onEchoRequest(s inet.Stream) { func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// get request data // get request data
data := &p2p.EchoRequest{} data := &p2p.EchoRequest{}
decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s)) decoder := protobufCodec.Multicodec(nil).Decoder(bufio.NewReader(s))

View File

@ -55,4 +55,4 @@ func main() {
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
<-done <-done
} }
} }

View File

@ -17,7 +17,7 @@ import (
const clientVersion = "go-p2p-node/0.0.1" const clientVersion = "go-p2p-node/0.0.1"
// 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 - 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 // s - network stream to write the data to
func sendDataObject(data interface{}, s inet.Stream) bool { func sendDataObject(data interface{}, s inet.Stream) bool {
writer := bufio.NewWriter(s) writer := bufio.NewWriter(s)
@ -35,12 +35,11 @@ func sendDataObject(data interface{}, s inet.Stream) bool {
// nodeId - message author id // nodeId - message author id
// messageId - unique for requests, copied from request for responses // messageId - unique for requests, copied from request for responses
func NewMessageData(nodeId string, messageId string, gossip bool) *p2p.MessageData { func NewMessageData(nodeId string, messageId string, gossip bool) *p2p.MessageData {
return &p2p.MessageData{ return &p2p.MessageData{ClientVersion: clientVersion,
ClientVersion: clientVersion, NodeId: nodeId,
NodeId: nodeId, Timestamp: time.Now().Unix(),
Timestamp: time.Now().Unix(), Id: messageId,
Id: messageId, Gossip: gossip}
Gossip: gossip}
} }
// Node type - implements one or more p2p protocols // Node type - implements one or more p2p protocols
@ -50,9 +49,9 @@ type Node struct {
echoProtocol *EchoProtocol // echp protocl imp 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 { func NewNode(host host.Host, done chan bool) *Node {
return &Node{host: host, return &Node{host: host,
pingProtocol: NewPingProtocol(host, done), pingProtocol: NewPingProtocol(host, done),
echoProtocol: NewEchoProtocol(host, done)} echoProtocol: NewEchoProtocol(host, done)}
} }

View File

@ -22,7 +22,7 @@ const pingResponse = "/ping/pingresp/0.0.1"
type PingProtocol struct { type PingProtocol struct {
host host.Host // local host host host.Host // local host
requests map[string]*p2p.PingRequest // used to access request data from response handlers 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 { func NewPingProtocol(host host.Host, done chan bool) *PingProtocol {
@ -106,7 +106,7 @@ func (p *PingProtocol) Ping(node *Node) bool {
return false 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 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) 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 return true