1 // (c) Bernhard Tittelbach, 2013
6 zmq "github.com/vaughan0/go-zmq"
10 // ---------- ZeroMQ Code -------------
12 func ZmqsInit(cmd_port, sub_port string) (ctx *zmq.Context, cmd_chans, sub_chans *zmq.Channels) {
14 ctx, err = zmq.NewContext()
18 //close only on later panic, otherwise leave open:
19 defer func(){ if r:= recover(); r != nil { ctx.Close(); panic(r) } }()
21 if len(cmd_port) > 0 {
22 cmd_sock, err := ctx.Socket(zmq.Req)
26 defer func() { if r:= recover(); r != nil { cmd_sock.Close(); panic(r) } }()
28 cmd_sock.SetRecvTimeout(2 * time.Second)
29 cmd_sock.SetSendTimeout(2 * time.Second)
31 if err = cmd_sock.Connect(cmd_port); err != nil {
35 cmd_chans = cmd_sock.ChannelsBuffer(10)
36 go zmqsHandleError(cmd_chans)
41 if len(sub_port) > 0 {
42 sub_sock, err := ctx.Socket(zmq.Sub)
46 defer func() { if r:= recover(); r != nil { sub_sock.Close(); panic(r) } }()
48 sub_sock.Subscribe([]byte{}) //subscribe empty filter -> aka to all messages
50 if err = sub_sock.Connect(sub_port); err != nil {
54 sub_chans = sub_sock.ChannelsBuffer(10)
55 go zmqsHandleError(sub_chans)
63 func zmqsHandleError(chans *zmq.Channels) {
64 for error := range(chans.Errors()) {