2018-06-13 02:06:59 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2018-12-23 20:34:59 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-10-03 01:49:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/event"
|
2018-06-13 02:06:59 +00:00
|
|
|
)
|
|
|
|
|
2018-07-28 19:53:02 +00:00
|
|
|
// Feed is a one to many subscription feed of the argument type.
|
2018-06-13 02:06:59 +00:00
|
|
|
//
|
|
|
|
// Messages received via p2p protocol are sent to subscribers by these event
|
|
|
|
// feeds. Message consumers should not use event feeds to reply to or broadcast
|
|
|
|
// messages. The p2p server will not relay them to peers. Rather, use the
|
|
|
|
// Send() or Broadcast() method on p2p.Server.
|
|
|
|
//
|
|
|
|
// Event feeds from p2p will always be of type p2p.Message. The message
|
|
|
|
// contains information about the sender, aka the peer, and the message payload
|
|
|
|
// itself.
|
|
|
|
//
|
2018-09-09 22:15:24 +00:00
|
|
|
// feed, err := ps.Feed(&pb.MyMessage{})
|
2018-06-13 02:06:59 +00:00
|
|
|
// ch := make(chan p2p.Message, 100) // Choose a reasonable buffer size!
|
2018-06-13 03:03:13 +00:00
|
|
|
// sub := feed.Subscribe(ch)
|
2018-06-13 02:06:59 +00:00
|
|
|
//
|
|
|
|
// // Wait until my message comes from a peer.
|
|
|
|
// msg := <- ch
|
|
|
|
// fmt.Printf("Message received: %v", msg.Data)
|
2018-09-16 18:02:53 +00:00
|
|
|
func (s *Server) Feed(msg proto.Message) Feed {
|
2018-09-09 22:15:24 +00:00
|
|
|
t := messageType(msg)
|
2018-07-17 18:39:04 +00:00
|
|
|
|
2018-07-25 16:11:15 +00:00
|
|
|
s.mutex.Lock()
|
|
|
|
defer s.mutex.Unlock()
|
2018-06-13 02:06:59 +00:00
|
|
|
if s.feeds[t] == nil {
|
|
|
|
s.feeds[t] = new(event.Feed)
|
|
|
|
}
|
2018-07-25 16:11:15 +00:00
|
|
|
|
2018-06-21 03:03:02 +00:00
|
|
|
return s.feeds[t]
|
2018-06-13 02:06:59 +00:00
|
|
|
}
|
2018-09-16 18:02:53 +00:00
|
|
|
|
|
|
|
// Feed implements one-to-many subscriptions where the carrier of events is a channel.
|
|
|
|
// Values sent to a Feed are delivered to all subscribed channels simultaneously.
|
|
|
|
//
|
|
|
|
// Feeds can only be used with a single type. The type is determined by the first Send or
|
|
|
|
// Subscribe operation. Subsequent calls to these methods panic if the type does not
|
|
|
|
// match.
|
|
|
|
//
|
|
|
|
// Implemented by https://github.com/ethereum/go-ethereum/blob/HEAD/event/feed.go
|
|
|
|
type Feed interface {
|
|
|
|
Subscribe(channel interface{}) event.Subscription
|
|
|
|
Send(value interface{}) (nsent int)
|
|
|
|
}
|