Skip to content
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

Merged
merged 8 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions network/p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ import (
"github.com/topology-gg/gram/config"
ex "github.com/topology-gg/gram/execution"
"github.com/topology-gg/gram/log"
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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
log.Error("(Network) Failed to serialize message", "message", 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 {
log.Error("(Network) Failed to publish to topic", "topic", p2p.streams[i].name, "error", err)
}
}
Expand Down Expand Up @@ -210,6 +222,12 @@ func (p2p *P2P) p2pMessageHandler(subscription *pubsub.Subscription) {
continue
Copy link
Contributor

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.

Copy link
Contributor Author

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?

}

var msg base.HelloRequest
if err := p2p.serializer.Unmarshal(message.Data, &msg); err != nil {
log.Error("(Network) Failed to deserialize message", "message", err)
continue
}

p2p.executor.Execute(string(message.Message.Data))
}
}
Expand Down
6 changes: 6 additions & 0 deletions proto/interface.go
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
}
13 changes: 13 additions & 0 deletions proto/pbcodec.go
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))
}
Loading