2018-06-13 02:43:19 +00:00
|
|
|
package p2p
|
|
|
|
|
2018-08-29 16:32:54 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/testing"
|
|
|
|
)
|
2018-06-13 02:43:19 +00:00
|
|
|
|
2018-06-13 03:02:07 +00:00
|
|
|
// Feeds can be use to subscribe to any type of message.
|
2018-06-13 02:43:19 +00:00
|
|
|
func ExampleServer_Feed() {
|
|
|
|
s, err := NewServer()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's wait for a puzzle from our peers then try to solve it.
|
2018-09-09 22:15:24 +00:00
|
|
|
feed := s.Feed(&pb.Puzzle{})
|
2018-06-13 02:43:19 +00:00
|
|
|
|
|
|
|
ch := make(chan Message, 5) // Small buffer size. I don't expect many puzzles.
|
|
|
|
sub := feed.Subscribe(ch)
|
|
|
|
|
|
|
|
// Always close these resources.
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
// Wait until we have a puzzle to solve.
|
|
|
|
msg := <-ch
|
2018-08-29 16:32:54 +00:00
|
|
|
puzzle, ok := msg.Data.(*pb.Puzzle)
|
2018-06-13 11:44:45 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
panic("Received a message that wasn't a puzzle!")
|
|
|
|
}
|
2018-06-13 02:43:19 +00:00
|
|
|
|
|
|
|
fmt.Printf("Received puzzle %s from peer %v\n", puzzle, msg.Peer)
|
|
|
|
|
|
|
|
if puzzle.Answer == "fourteen" {
|
|
|
|
fmt.Println("I solved the puzzle!")
|
|
|
|
} else {
|
|
|
|
fmt.Println("The answer isn't \"fourteen\"... giving up")
|
|
|
|
}
|
|
|
|
}
|