-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use protobuf while communicate between peers #30
Changes from 1 commit
37cfa29
f90fa7c
e4125b7
a9808b5
63cf0b2
0796548
be042dd
ae8fc14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,18 +15,21 @@ import ( | |
dutil "github.com/libp2p/go-libp2p/p2p/discovery/util" | ||
"github.com/topology-gg/gram/config" | ||
ex "github.com/topology-gg/gram/execution" | ||
pbcodec "github.com/topology-gg/gram/proto" | ||
"github.com/topology-gg/gram/proto/gen/gram/base" | ||
) | ||
|
||
type P2P struct { | ||
ctx context.Context | ||
errCh chan error | ||
executor ex.Execution | ||
host host.Host | ||
namespace string | ||
maxPeers int | ||
port int | ||
pubsub *pubsub.PubSub | ||
streams []Stream | ||
ctx context.Context | ||
errCh chan error | ||
executor ex.Execution | ||
host host.Host | ||
namespace string | ||
maxPeers int | ||
port int | ||
pubsub *pubsub.PubSub | ||
streams []Stream | ||
serializer pbcodec.Serializer | ||
} | ||
|
||
type Stream struct { | ||
|
@@ -59,16 +62,19 @@ func NewP2P(ctx context.Context, errCh chan error, executor ex.Execution, cfg *c | |
streams[i] = Stream{name: name} | ||
} | ||
|
||
serializer := &pbcodec.ProtoBufSerializer{} | ||
|
||
return &P2P{ | ||
ctx: ctx, | ||
errCh: errCh, | ||
executor: executor, | ||
host: host, | ||
namespace: namespace, | ||
maxPeers: maxPeers, | ||
port: port, | ||
pubsub: gossipsub, | ||
streams: streams, | ||
ctx: ctx, | ||
errCh: errCh, | ||
executor: executor, | ||
host: host, | ||
namespace: namespace, | ||
maxPeers: maxPeers, | ||
port: port, | ||
pubsub: gossipsub, | ||
streams: streams, | ||
serializer: serializer, | ||
}, nil | ||
} | ||
|
||
|
@@ -78,8 +84,14 @@ func (p2p *P2P) Start() { | |
} | ||
|
||
func (p2p *P2P) Publish(message string) { | ||
msg, err := p2p.serializer.Marshal(&base.HelloRequest{Name: message}) | ||
if err != nil { | ||
p2p.errCh <- err | ||
return | ||
} | ||
|
||
for i := range p2p.streams { | ||
if err := p2p.streams[i].topic.Publish(p2p.ctx, []byte(message)); err != nil { | ||
if err := p2p.streams[i].topic.Publish(p2p.ctx, msg); err != nil { | ||
fmt.Println("(Network) Failed to publish to topic:", p2p.streams[i].name, err) | ||
} | ||
} | ||
|
@@ -211,6 +223,12 @@ func (p2p *P2P) p2pMessageHandler(subscription *pubsub.Subscription) { | |
continue | ||
} | ||
|
||
var msg base.HelloRequest | ||
if err := p2p.serializer.Unmarshal(message.Data, &msg); err != nil { | ||
p2p.errCh <- err | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this case the error should be logged and that's it I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha! Also, I've noticed that some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
p2p.executor.Execute(string(message.Message.Data)) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pbcodec | ||
|
||
type Serializer interface { | ||
Marshal(data interface{}) ([]byte, error) | ||
Unmarshal(data []byte, v interface{}) error | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pbcodec | ||
|
||
import "google.golang.org/protobuf/proto" | ||
|
||
type ProtoBufSerializer struct{} | ||
|
||
func (p *ProtoBufSerializer) Marshal(data interface{}) ([]byte, error) { | ||
return proto.Marshal(data.(proto.Message)) | ||
} | ||
|
||
func (p *ProtoBufSerializer) Unmarshal(data []byte, v interface{}) error { | ||
return proto.Unmarshal(data, v.(proto.Message)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, was L221~L224 in affect? I wonder if we still need these after the last refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a node publishes a message, it has already subscribed to the relevant topic. That's why this code needed. no?