/
peering1.go
66 lines (57 loc) · 1.46 KB
/
peering1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Broker peering simulation (part 1).
// Prototypes the state flow
//
package main
import (
zmq "github.com/pebbe/zmq4"
"fmt"
"math/rand"
"os"
"time"
)
func main() {
// First argument is this broker's name
// Other arguments are our peers' names
//
if len(os.Args) < 2 {
fmt.Println("syntax: peering1 me {you}...")
os.Exit(1)
}
self := os.Args[1]
fmt.Printf("I: preparing broker at %s...\n", self)
rand.Seed(time.Now().UnixNano())
// Bind state backend to endpoint
statebe, _ := zmq.NewSocket(zmq.PUB)
defer statebe.Close()
statebe.Bind("ipc://" + self + "-state.ipc")
// Connect statefe to all peers
statefe, _ := zmq.NewSocket(zmq.SUB)
defer statefe.Close()
statefe.SetSubscribe("")
for _, peer := range os.Args[2:] {
fmt.Printf("I: connecting to state backend at '%s'\n", peer)
statefe.Connect("ipc://" + peer + "-state.ipc")
}
// The main loop sends out status messages to peers, and collects
// status messages back from peers. The zmq_poll timeout defines
// our own heartbeat:
poller := zmq.NewPoller()
poller.Add(statefe, zmq.POLLIN)
for {
// Poll for activity, or 1 second timeout
sockets, err := poller.Poll(time.Second)
if err != nil {
break
}
// Handle incoming status messages
if len(sockets) == 1 {
msg, _ := statefe.RecvMessage(0)
peer_name := msg[0]
available := msg[1]
fmt.Printf("%s - %s workers free\n", peer_name, available)
} else {
statebe.SendMessage(self, rand.Intn(10))
}
}
}