1 // (c) Bernhard Tittelbach, 2013
6 zmq "github.com/vaughan0/go-zmq"
9 // ---------- ZeroMQ Code -------------
11 func ZmqsInit(sub_port string) (ctx *zmq.Context, sub_chans *zmq.Channels) {
13 ctx, err = zmq.NewContext()
17 //close only on later panic, otherwise leave open:
18 defer func(){ if r:= recover(); r != nil { ctx.Close(); panic(r) } }()
20 if len(sub_port) > 0 {
21 sub_sock, err := ctx.Socket(zmq.Sub)
25 defer func() { if r:= recover(); r != nil { sub_sock.Close(); panic(r) } }()
27 sub_sock.Subscribe([]byte{}) //subscribe empty filter -> aka to all messages
29 if err = sub_sock.Connect(sub_port); err != nil {
33 sub_chans = sub_sock.ChannelsBuffer(10)
34 go zmqsHandleError(sub_chans)
42 func zmqsHandleError(chans *zmq.Channels) {
43 for error := range(chans.Errors()) {
49 func ZmqsRequestAnswer(sock *zmq.Socket, request [][]byte) (answer [][]byte) {
50 if err := sock.Send(request); err != nil {
53 parts, err := sock.Recv()
60 func ZmqsAskQuestionsAndClose(ctx *zmq.Context, addr string, questions [][][]byte) [][][]byte {
61 if len(addr) == 0 || ctx == nil { return nil }
63 req_sock, err := ctx.Socket(zmq.Req)
67 defer req_sock.Close()
69 if err = req_sock.Connect(addr); err != nil {
73 rv := make([][][]byte, len(questions))
74 for index, q := range(questions) {
75 rv[index] = ZmqsRequestAnswer(req_sock, q)