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
7ba479f3
Commit
7ba479f3
authored
Mar 02, 2018
by
Steven Allen
Browse files
use pb for protobufs, not p2p
For some reason, this was causing gx to freak out.
parent
bd3e85cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
examples/multipro/echo.go
View file @
7ba479f3
...
@@ -9,7 +9,7 @@ import (
...
@@ -9,7 +9,7 @@ import (
inet
"github.com/libp2p/go-libp2p-net"
inet
"github.com/libp2p/go-libp2p-net"
"github.com/libp2p/go-libp2p-host"
"github.com/libp2p/go-libp2p-host"
p
2p
"github.com/libp2p/go-libp2p/examples/multipro/pb"
p
b
"github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec
"github.com/multiformats/go-multicodec/protobuf"
protobufCodec
"github.com/multiformats/go-multicodec/protobuf"
uuid
"github.com/satori/go.uuid"
uuid
"github.com/satori/go.uuid"
)
)
...
@@ -19,13 +19,13 @@ const echoRequest = "/echo/echoreq/0.0.1"
...
@@ -19,13 +19,13 @@ const echoRequest = "/echo/echoreq/0.0.1"
const
echoResponse
=
"/echo/echoresp/0.0.1"
const
echoResponse
=
"/echo/echoresp/0.0.1"
type
EchoProtocol
struct
{
type
EchoProtocol
struct
{
node
*
Node
// local host
node
*
Node
// local host
requests
map
[
string
]
*
p
2p
.
EchoRequest
// used to access request data from response handlers
requests
map
[
string
]
*
p
b
.
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
}
}
func
NewEchoProtocol
(
node
*
Node
,
done
chan
bool
)
*
EchoProtocol
{
func
NewEchoProtocol
(
node
*
Node
,
done
chan
bool
)
*
EchoProtocol
{
e
:=
EchoProtocol
{
node
:
node
,
requests
:
make
(
map
[
string
]
*
p
2p
.
EchoRequest
),
done
:
done
}
e
:=
EchoProtocol
{
node
:
node
,
requests
:
make
(
map
[
string
]
*
p
b
.
EchoRequest
),
done
:
done
}
node
.
SetStreamHandler
(
echoRequest
,
e
.
onEchoRequest
)
node
.
SetStreamHandler
(
echoRequest
,
e
.
onEchoRequest
)
node
.
SetStreamHandler
(
echoResponse
,
e
.
onEchoResponse
)
node
.
SetStreamHandler
(
echoResponse
,
e
.
onEchoResponse
)
...
@@ -38,7 +38,7 @@ func NewEchoProtocol(node *Node, done chan bool) *EchoProtocol {
...
@@ -38,7 +38,7 @@ func NewEchoProtocol(node *Node, 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
:=
&
p
2p
.
EchoRequest
{}
data
:=
&
p
b
.
EchoRequest
{}
decoder
:=
protobufCodec
.
Multicodec
(
nil
)
.
Decoder
(
bufio
.
NewReader
(
s
))
decoder
:=
protobufCodec
.
Multicodec
(
nil
)
.
Decoder
(
bufio
.
NewReader
(
s
))
err
:=
decoder
.
Decode
(
data
)
err
:=
decoder
.
Decode
(
data
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -59,7 +59,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
...
@@ -59,7 +59,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// send response to the request using the message string he provided
// send response to the request using the message string he provided
resp
:=
&
p
2p
.
EchoResponse
{
resp
:=
&
p
b
.
EchoResponse
{
MessageData
:
e
.
node
.
NewMessageData
(
data
.
MessageData
.
Id
,
false
),
MessageData
:
e
.
node
.
NewMessageData
(
data
.
MessageData
.
Id
,
false
),
Message
:
data
.
Message
}
Message
:
data
.
Message
}
...
@@ -88,7 +88,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
...
@@ -88,7 +88,7 @@ func (e *EchoProtocol) onEchoRequest(s inet.Stream) {
// remote echo response handler
// remote echo response handler
func
(
e
*
EchoProtocol
)
onEchoResponse
(
s
inet
.
Stream
)
{
func
(
e
*
EchoProtocol
)
onEchoResponse
(
s
inet
.
Stream
)
{
data
:=
&
p
2p
.
EchoResponse
{}
data
:=
&
p
b
.
EchoResponse
{}
decoder
:=
protobufCodec
.
Multicodec
(
nil
)
.
Decoder
(
bufio
.
NewReader
(
s
))
decoder
:=
protobufCodec
.
Multicodec
(
nil
)
.
Decoder
(
bufio
.
NewReader
(
s
))
err
:=
decoder
.
Decode
(
data
)
err
:=
decoder
.
Decode
(
data
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -125,7 +125,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool {
...
@@ -125,7 +125,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool {
log
.
Printf
(
"%s: Sending echo to: %s...."
,
e
.
node
.
ID
(),
host
.
ID
())
log
.
Printf
(
"%s: Sending echo to: %s...."
,
e
.
node
.
ID
(),
host
.
ID
())
// create message data
// create message data
req
:=
&
p
2p
.
EchoRequest
{
req
:=
&
p
b
.
EchoRequest
{
MessageData
:
e
.
node
.
NewMessageData
(
uuid
.
Must
(
uuid
.
NewV4
())
.
String
(),
false
),
MessageData
:
e
.
node
.
NewMessageData
(
uuid
.
Must
(
uuid
.
NewV4
())
.
String
(),
false
),
Message
:
fmt
.
Sprintf
(
"Echo from %s"
,
e
.
node
.
ID
())}
Message
:
fmt
.
Sprintf
(
"Echo from %s"
,
e
.
node
.
ID
())}
...
...
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