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

add sender id to message #207

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ require (
github.com/multiformats/go-multistream v0.1.0
github.com/whyrusleeping/timecache v0.0.0-20160911033111-cfcb2f1abfee
)

go 1.13
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't do this. It must've been added via go?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done automatically be go; please remove this line.

24 changes: 19 additions & 5 deletions pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,17 @@ type PubSubRouter interface {

type Message struct {
*pb.Message
senderID peer.ID
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is available in the RPC struct and will be exposed to routers in the refactoring in #199.
It is also visible to validators as it is passed in the validation function.

}

func (m *Message) GetFrom() peer.ID {
return peer.ID(m.Message.GetFrom())
}

func (m *Message) GetSenderID() peer.ID {
return m.senderID
}

type RPC struct {
pb.RPC

Expand Down Expand Up @@ -522,12 +527,15 @@ func (p *PubSub) doAnnounceRetry(pid peer.ID, topic string, sub bool) {

// notifySubs sends a given message to all corresponding subscribers.
// Only called from processLoop.
func (p *PubSub) notifySubs(msg *pb.Message) {
func (p *PubSub) notifySubs(src peer.ID, msg *pb.Message) {
for _, topic := range msg.GetTopicIDs() {
subs := p.myTopics[topic]
for f := range subs {
select {
case f.ch <- &Message{msg}:
case f.ch <- &Message{
Message: msg,
senderID: src,
}:
default:
log.Infof("Can't deliver message to subscription for topic %s; subscriber too slow", topic)
}
Expand Down Expand Up @@ -616,7 +624,10 @@ func (p *PubSub) handleIncomingRPC(rpc *RPC) {
continue
}

msg := &Message{pmsg}
msg := &Message{
Message: pmsg,
senderID: rpc.from,
}
p.pushMsg(rpc.from, msg)
}

Expand Down Expand Up @@ -664,7 +675,7 @@ func (p *PubSub) pushMsg(src peer.ID, msg *Message) {
}

func (p *PubSub) publishMessage(from peer.ID, pmsg *pb.Message) {
p.notifySubs(pmsg)
p.notifySubs(from, pmsg)
p.rt.Publish(from, pmsg)
}

Expand Down Expand Up @@ -747,7 +758,10 @@ func (p *PubSub) Publish(topic string, data []byte) error {
return err
}
}
p.publish <- &Message{m}
p.publish <- &Message{
Message: m,
senderID: p.host.ID(),
}
return nil
}

Expand Down