forked from glycerine/gozbus
/
gozbus.go
144 lines (114 loc) · 2.91 KB
/
gozbus.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// copyright(c) 2013, the gozbus authors.
// license: MIT
//
// gozbus: a messaging system in golang.
// Hey hey hey, "Catch Z Bus!"
//
package main
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
//"time"
capn "github.com/glycerine/go-capnproto"
"github.com/glycerine/gozbus/zbus"
nn "github.com/op/go-nanomsg"
// for parsing command line options:
// https://github.com/spf13/cobra
)
const ZBUS_ADDR = "tcp://127.0.0.1:1776"
func startZBus(nnzbus *nn.Socket, addr string) {
_, err := nnzbus.Bind(addr)
if err != nil {
fmt.Printf("could not bind addr '%s': %v", addr, err)
panic(err)
}
fmt.Printf("[pid %d] gozbus server: startZbus bound endpoint '%s'.\n", os.Getpid(), addr)
}
func recvMsgOnZBus(nnzbus *nn.Socket) {
pid := os.Getpid()
// receive, synchronously so flags == 0
var flags int = 0
heardBuf, err := nnzbus.Recv(flags)
if err != nil {
log.Fatal(err)
}
fmt.Printf("[pid %d] gozbus server: I heard: '%s'.\n", pid, heardBuf)
}
func sayHello(nnzbus *nn.Socket) {
pid := os.Getpid()
msg := "hello from pid " + strconv.Itoa(pid)
nnzbus.Send([]byte(msg), 0)
fmt.Printf("[pid %d] gozbus client: sayHello sent msg '%s'.\n", pid, msg)
}
func sendZDate(nnzbus *nn.Socket) {
// Create Zdate and Write to nnzbus.
//
// Z is our universal type, a discriminated union.
// Hence Z provides run-time type identification
// for all message types defined in zbus.capnp
//
s := capn.NewBuffer(nil)
z := gozbus.NewRootZ(s)
d := gozbus.NewRootZdate(s)
z.SetZdate(d)
d.SetYear(2004)
d.SetMonth(12)
d.SetDay(7)
buf := bytes.Buffer{}
s.WriteTo(&buf)
nnzbus.Send(buf.Bytes(), 0)
fmt.Printf("[pid %d] sendZDate sent ZDate message: year %d, month %d, day %d\n",
os.Getpid(), d.Year(), d.Month(), d.Day())
}
func recvZDate(nnzbus *nn.Socket) {
// Read a Z message that is expected to be a Zdate from nnzbus
myMsg, err := nnzbus.Recv(0)
if err != nil {
log.Fatal(err)
}
buf := bytes.NewBuffer(myMsg)
capMsg, err := capn.ReadFromStream(buf, nil)
if err != nil {
log.Fatal(err)
}
z := gozbus.ReadRootZ(capMsg)
mydate := z.Zdate()
fmt.Printf("[pid %d] recvZDate got ZDate message: year %d, month %d, day %d\n",
os.Getpid(), mydate.Year(), mydate.Month(), mydate.Day())
}
func main() {
var err error
var nnzbus *nn.Socket
pid := os.Getpid()
var isServer bool = false
if len(os.Args) > 1 && os.Args[1] == "--server" {
isServer = true
}
if isServer {
// server code, binds the bus to start it.
nnzbus, err = nn.NewSocket(nn.AF_SP, nn.REP)
if err != nil {
log.Fatal(err)
}
defer nnzbus.Close()
startZBus(nnzbus, ZBUS_ADDR)
recvMsgOnZBus(nnzbus)
sendZDate(nnzbus)
} else {
// client code, connects to the bus.
nnzbus, err = nn.NewSocket(nn.AF_SP, nn.REQ)
if err != nil {
log.Fatal(err)
}
defer nnzbus.Close()
if _, err = nnzbus.Connect(ZBUS_ADDR); err != nil {
log.Fatal(err)
}
sayHello(nnzbus)
recvZDate(nnzbus)
}
fmt.Printf("[pid %d] done.\n", pid)
}