// Echo service func echo(socket *zmq.Socket) (err error) { msg, err := socket.RecvMessage(0) if err != nil { return } _, err = socket.SendMessage(msg) return }
func inputData(data string, socket *zmq.Socket) { // Send data through socket _, err := socket.SendBytes([]byte(data), 0) if err != nil { log.Println("Error sending data") } }
// listen on a zmq SUB socket // and shovel messages from it out to the websockets func zmqToWebsocket(subsocket zmq.Socket) { for { address, _ := subsocket.Recv(0) content, _ := subsocket.Recv(0) fmt.Println("received a zmq message") fmt.Println(string(address)) fmt.Println(string(content)) runningRoom.SendMessage(envelope{string(address), string(content)}) } }
// listen on a zmq SUB socket // and shovel messages from it out to the websockets func zmqToWebsocket(subsocket zmq.Socket) { for { address, _ := subsocket.Recv(0) content, _ := subsocket.Recv(0) log.WithFields(log.Fields{ "address": string(address), "content": string(content), }).Info("received a zmq message") runningRoom.SendMessage(envelope{string(address), string(content)}) } }
// Application wants to speak to us, see if it's possible func (bstar *Bstar) voter_ready(socket *zmq.Socket) error { // If server can accept input now, call appl handler bstar.event = client_REQUEST err := bstar.execute_fsm() if err == nil { bstar.voter_fn(socket) } else { // Destroy waiting message, no-one to read it socket.RecvMessage(0) } return nil }
// The RecvKvmsg function reads a key-value message from socket, and returns a new // Kvmsg instance. func RecvKvmsg(socket *zmq.Socket) (kvmsg *Kvmsg, err error) { kvmsg = &Kvmsg{ present: make([]bool, kvmsg_FRAMES), frame: make([]string, kvmsg_FRAMES), } msg, err := socket.RecvMessage(0) if err != nil { return } //fmt.Printf("Recv from %s: %q\n", socket, msg) for i := 0; i < kvmsg_FRAMES && i < len(msg); i++ { kvmsg.frame[i] = msg[i] kvmsg.present[i] = true } return }
func snapshots(socket *zmq.Socket, srv *clonesrv_t) (err error) { msg, err := socket.RecvMessage(0) if err != nil { return } identity := msg[0] // Request is in second frame of message request := msg[1] if request != "ICANHAZ?" { err = errors.New("E: bad request, aborting") return } subtree := msg[2] // Send state socket to client for _, kvmsg := range srv.kvmap { if key, _ := kvmsg.GetKey(); strings.HasPrefix(key, subtree) { socket.Send(identity, zmq.SNDMORE) kvmsg.Send(socket) } } // Now send END message with sequence number log.Println("I: sending shapshot =", srv.sequence) socket.Send(identity, zmq.SNDMORE) kvmsg := kvmsg.NewKvmsg(srv.sequence) kvmsg.SetKey("KTHXBAI") kvmsg.SetBody(subtree) kvmsg.Send(socket) return }
func dump(soc *zmq.Socket) { fmt.Println("----------------------------------------") for { // Process all parts of the message message, _ := soc.Recv(0) // Dump the message as text or binary fmt.Printf("[%03d] ", len(message)) if all_char.MatchString(message) { fmt.Print(message) } else { for i := 0; i < len(message); i++ { fmt.Printf("%02X ", message[i]) } } fmt.Println() more, _ := soc.GetRcvmore() if !more { break } } }
// Send key-value message to socket; any empty frames are sent as such. func (kvmsg *Kvmsg) Send(socket *zmq.Socket) (err error) { //fmt.Printf("Send to %s: %q\n", socket, kvmsg.frame) kvmsg.encode_props() _, err = socket.SendMessage(kvmsg.frame) return }
func set_id(soc *zmq.Socket) { identity := fmt.Sprintf("%04X-%04X", rand.Intn(0x10000), rand.Intn(0x10000)) soc.SetIdentity(identity) }
// send a message to the zmq PUB socket func sendMessage(reqsocket zmq.Socket, e envelope) { serialized_envelope, _ := json.Marshal(e) reqsocket.Send(string(serialized_envelope), 0) // wait for a reply reqsocket.Recv(0) }
func (server *server_t) ping(socket *zmq.Socket) { if time.Now().After(server.ping_at) { socket.SendMessage(server.endpoint, "PING") server.ping_at = time.Now().Add(PING_INTERVAL) } }