Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
3f926c52
Commit
3f926c52
authored
Nov 27, 2017
by
Aviv Eyal
Committed by
Steven Allen
Feb 21, 2018
Browse files
Apply go formatting
parent
54aa54a5
Changes
4
Show whitespace changes
Inline
Side-by-side
examples/multipro/echo.go
View file @
3f926c52
...
...
@@ -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
))
...
...
examples/multipro/main.go
View file @
3f926c52
examples/multipro/node.go
View file @
3f926c52
...
...
@@ -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,8 +35,7 @@ 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
,
return
&
p2p
.
MessageData
{
ClientVersion
:
clientVersion
,
NodeId
:
nodeId
,
Timestamp
:
time
.
Now
()
.
Unix
(),
Id
:
messageId
,
...
...
@@ -50,7 +49,7 @@ type Node struct {
echoProtocol
*
EchoProtocol
// echp protocl imp
}
// create a new node with its
suppor
ted protocols
// create a new node with its
implemen
ted protocols
func
NewNode
(
host
host
.
Host
,
done
chan
bool
)
*
Node
{
return
&
Node
{
host
:
host
,
pingProtocol
:
NewPingProtocol
(
host
,
done
),
...
...
examples/multipro/ping.go
View file @
3f926c52
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment