Skip to content

Commit

Permalink
Merge pull request #1 from libp2p/feat/multipro-example
Browse files Browse the repository at this point in the history
Feat/multipro example
  • Loading branch information
avive committed Feb 22, 2018
2 parents c3c3852 + 074f2f2 commit bdaced7
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 70 deletions.
2 changes: 1 addition & 1 deletion examples/multipro/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Juan Batiz-Benet
Copyright (c) 2017 Aviv Eyal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion examples/multipro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ From `multipro` base source folder:

The example creates two LibP2P Hosts supporting 2 protocols: ping and echo.

Each protocol consists RPC-style requests and respones and each request and response is a typed protobufs message (and a go data object).
Each protocol consists RPC-style requests and responses and each request and response is a typed protobufs message (and a go data object).

This is a different pattern then defining a whole p2p protocol as one protobuf message with lots of optional fields (as can be observed in various p2p-lib protocols using protobufs such as dht).

Expand Down
15 changes: 8 additions & 7 deletions examples/multipro/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"fmt"
"log"

inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
inet "github.com/libp2p/go-libp2p-net"

p2p "github.com/avive/go-libp2p/examples/multipro/pb"
uuid "github.com/google/uuid"
assert "github.com/ipfs/go-ipfs/thirdparty/assert"
"github.com/libp2p/go-libp2p-host"
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
uuid "github.com/satori/go.uuid"
)

// pattern: /protocol-name/request-or-response-message/version
Expand Down Expand Up @@ -114,7 +113,9 @@ func (e *EchoProtocol) onEchoResponse(s inet.Stream) {
return
}

assert.True(req.Message == data.Message, nil, "Expected echo to respond with request message")
if req.Message != data.Message {
log.Fatalln("Expected echo to respond with request message")
}

log.Printf("%s: Received echo response from %s. Message id:%s. Message: %s.", s.Conn().LocalPeer(), s.Conn().RemotePeer(), data.MessageData.Id, data.Message)
e.done <- true
Expand All @@ -125,7 +126,7 @@ func (e *EchoProtocol) Echo(host host.Host) bool {

// create message data
req := &p2p.EchoRequest{
MessageData: e.node.NewMessageData(uuid.New().String(), false),
MessageData: e.node.NewMessageData(uuid.Must(uuid.NewV4()).String(), false),
Message: fmt.Sprintf("Echo from %s", e.node.ID())}

signature, err := e.node.signProtoMessage(req)
Expand Down
10 changes: 5 additions & 5 deletions examples/multipro/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"log"
"math/rand"

crypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
ps "github.com/libp2p/go-libp2p-peerstore"
swarm "github.com/libp2p/go-libp2p-swarm"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
ps "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore"
swarm "gx/ipfs/QmU219N3jn7QadVCeBUqGnAkwoXoUomrCwDuVQVuL7PB5W/go-libp2p-swarm"
ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
ma "github.com/multiformats/go-multiaddr"
)

// helper method - create a lib-p2p host to listen on a port
Expand Down
11 changes: 6 additions & 5 deletions examples/multipro/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"log"
"time"

p2p "github.com/avive/go-libp2p/examples/multipro/pb"
"github.com/gogo/protobuf/proto"
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"

)

// node client version
Expand Down
80 changes: 36 additions & 44 deletions examples/multipro/pb/p2p.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions examples/multipro/pb/p2p.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ message EchoResponse {
string message = 2;

// ... add any additional message data here....
}

}
10 changes: 5 additions & 5 deletions examples/multipro/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"fmt"
"log"

p2p "github.com/avive/go-libp2p/examples/multipro/pb"
uuid "github.com/google/uuid"
"github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
p2p "github.com/libp2p/go-libp2p/examples/multipro/pb"
protobufCodec "github.com/multiformats/go-multicodec/protobuf"
"gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
inet "gx/ipfs/QmbD5yKbXahNvoMqzeuNyKQA9vAs9fUvJg2GXeWU1fVqY5/go-libp2p-net"
uuid "github.com/satori/go.uuid"
)

// pattern: /protocol-name/request-or-response-message/version
Expand Down Expand Up @@ -116,7 +116,7 @@ func (p *PingProtocol) Ping(host host.Host) bool {
log.Printf("%s: Sending ping to: %s....", p.node.ID(), host.ID())

// create message data
req := &p2p.PingRequest{MessageData: p.node.NewMessageData(uuid.New().String(), false),
req := &p2p.PingRequest{MessageData: p.node.NewMessageData(uuid.Must(uuid.NewV4()).String(), false),
Message: fmt.Sprintf("Ping from %s", p.node.ID())}

// sign the data
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@
"hash": "Qmc14vuKyGqX27RvBhekYytxSFJpaEgQVuVJgKSm69MEix",
"name": "go-smux-multiplex",
"version": "3.0.5"
},
{
"author": "multiformats",
"hash": "QmRDePEiL4Yupq5EkcK3L3ko3iMgYaqUdLu7xc1kqs7dnV",
"name": "go-multicodec",
"version": "0.1.5"
},
{
"author": "satori",
"hash": "QmcBWojPoNh4qm7zvv4qiepvCnnc7ALS9qcp7TNwwxT1gT",
"name": "go.uuid",
"version": "1.1.0"
}
],
"gxVersion": "0.4.0",
Expand Down

0 comments on commit bdaced7

Please sign in to comment.