Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
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
7 years ago
by
Aviv Eyal
Committed by
Steven Allen
7 years ago
Browse files
Options
Download
Email Patches
Plain Diff
Apply go formatting
parent
54aa54a5
master
2018-Q4-OKR
feat/protobuf
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-nza0mn
jenkinsfile
multistream-ping
punching
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
examples/multipro/echo.go
+2
-3
examples/multipro/echo.go
examples/multipro/main.go
+1
-1
examples/multipro/main.go
examples/multipro/node.go
+9
-10
examples/multipro/node.go
examples/multipro/ping.go
+2
-2
examples/multipro/ping.go
with
14 additions
and
16 deletions
+14
-16
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
))
...
...
This diff is collapsed.
Click to expand it.
examples/multipro/main.go
View file @
3f926c52
...
...
@@ -55,4 +55,4 @@ func main() {
for
i
:=
0
;
i
<
4
;
i
++
{
<-
done
}
}
\ No newline at end of file
}
This diff is collapsed.
Click to expand it.
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,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
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
),
echoProtocol
:
NewEchoProtocol
(
host
,
done
)}
pingProtocol
:
NewPingProtocol
(
host
,
done
),
echoProtocol
:
NewEchoProtocol
(
host
,
done
)}
}
This diff is collapsed.
Click to expand it.
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
...
...
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help