if err != nil {
panic(err)
}
- //close only on panic, otherwise leave open:
+ //close only on later panic, otherwise leave open:
defer func(){ if r:= recover(); r != nil { ctx.Close(); panic(r) } }()
- cmd_sock, err := ctx.Socket(zmq.Req)
- if err != nil {
- panic(err)
- }
- defer func() { if r:= recover(); r != nil { cmd_sock.Close(); panic(r) } }()
+ if len(cmd_port) > 0 {
+ cmd_sock, err := ctx.Socket(zmq.Req)
+ if err != nil {
+ panic(err)
+ }
+ defer func() { if r:= recover(); r != nil { cmd_sock.Close(); panic(r) } }()
- cmd_sock.SetRecvTimeout(2 * time.Second)
- cmd_sock.SetSendTimeout(2 * time.Second)
+ cmd_sock.SetRecvTimeout(2 * time.Second)
+ cmd_sock.SetSendTimeout(2 * time.Second)
- sub_sock, err := ctx.Socket(zmq.Sub)
- if err != nil {
- panic(err)
+ if err = cmd_sock.Connect(cmd_port); err != nil {
+ panic(err)
+ }
+
+ cmd_chans = cmd_sock.ChannelsBuffer(10)
+ go zmqsHandleError(cmd_chans)
+ } else {
+ cmd_chans = nil
}
- defer func() { if r:= recover(); r != nil { sub_sock.Close(); panic(r) } }()
- if err = cmd_sock.Connect(cmd_port); err != nil {
- panic(err)
- }
+ if len(sub_port) > 0 {
+ sub_sock, err := ctx.Socket(zmq.Sub)
+ if err != nil {
+ panic(err)
+ }
+ defer func() { if r:= recover(); r != nil { sub_sock.Close(); panic(r) } }()
- sub_sock.Subscribe(subfilter)
+ sub_sock.Subscribe(subfilter)
- if err = sub_sock.Connect(sub_port); err != nil {
- panic(err)
- }
+ if err = sub_sock.Connect(sub_port); err != nil {
+ panic(err)
+ }
- cmd_chans = cmd_sock.ChannelsBuffer(10)
- sub_chans = cmd_sock.ChannelsBuffer(10)
+ sub_chans = sub_sock.ChannelsBuffer(10)
+ go zmqsHandleError(sub_chans)
+ } else {
+ sub_chans = nil
+ }
- go zmqsHandleError(cmd_chans, sub_chans)
return
}
-func zmqsHandleError(cmd_chans, sub_chans *zmq.Channels) {
- for {
- select {
- case cmd_error := <- cmd_chans.Errors():
- cmd_chans.Close()
- sub_chans.Close()
- panic(cmd_error)
- case sub_error := <- sub_chans.Errors():
- cmd_chans.Close()
- sub_chans.Close()
- panic(sub_error)
- }
+func zmqsHandleError(chans *zmq.Channels) {
+ for error := range(chans.Errors()) {
+ chans.Close()
+ panic(error)
}
-}
\ No newline at end of file
+}
if err != nil {
panic(err)
}
- //close only on panic, otherwise leave open:
+ //close only on later panic, otherwise leave open:
defer func(){ if r:= recover(); r != nil { ctx.Close(); panic(r) } }()
- cmd_sock, err := ctx.Socket(zmq.Rep)
- if err != nil {
- panic(err)
- }
- defer func() { if r:= recover(); r != nil { cmd_sock.Close(); panic(r) } }()
+ if len(cmd_port) > 0 {
+ cmd_sock, err := ctx.Socket(zmq.Rep)
+ if err != nil {
+ panic(err)
+ }
+ defer func() { if r:= recover(); r != nil { cmd_sock.Close(); panic(r) } }()
- cmd_sock.SetRecvTimeout(2 * time.Second)
- cmd_sock.SetSendTimeout(2 * time.Second)
+ cmd_sock.SetRecvTimeout(2 * time.Second)
+ cmd_sock.SetSendTimeout(2 * time.Second)
- pub_sock, err := ctx.Socket(zmq.Pub)
- if err != nil {
- panic(err)
+ if err = cmd_sock.Bind(cmd_port); err != nil {
+ panic(err)
+ }
+
+ cmd_chans = cmd_sock.ChannelsBuffer(10)
+ go zmqsHandleError(cmd_chans)
+ } else {
+ cmd_chans = nil
}
- defer func() { if r:= recover(); r != nil { pub_sock.Close(); panic(r) } }()
- if err = cmd_sock.Bind(cmd_port); err != nil { // "tcp://*:5555"
- panic(err)
- }
+ if len(pub_port) > 0 {
+ pub_sock, err := ctx.Socket(zmq.Pub)
+ if err != nil {
+ panic(err)
+ }
+ defer func() { if r:= recover(); r != nil { pub_sock.Close(); panic(r) } }()
- if err = pub_sock.Bind(pub_port); err != nil { // "tcp://*:5556"
- panic(err)
+ if err = pub_sock.Bind(pub_port); err != nil {
+ panic(err)
+ }
+
+ pub_chans = pub_sock.ChannelsBuffer(10)
+ go zmqsHandleError(pub_chans)
+ } else {
+ pub_chans = nil
}
- cmd_chans = cmd_sock.ChannelsBuffer(10)
- pub_chans = cmd_sock.ChannelsBuffer(10)
- go zmqsHandleError(cmd_chans, pub_chans)
return
}
-func zmqsHandleError(cmd_chans, pub_chans *zmq.Channels) {
- for {
- select {
- case cmd_error := <- cmd_chans.Errors():
- cmd_chans.Close()
- pub_chans.Close()
- panic(cmd_error)
- case pub_error := <- pub_chans.Errors():
- cmd_chans.Close()
- pub_chans.Close()
- panic(pub_error)
- }
+func zmqsHandleError(chans *zmq.Channels) {
+ for error := range(chans.Errors()) {
+ chans.Close()
+ panic(error)
}
}
\ No newline at end of file