Exemple #1
0
// A blocking function that will infinitely forward multi-part messages between two zmq.Sockets
func Forward(a, b zmq.Socket) {
	for {
		parts, err := a.RecvMultipart(0)
		if err != nil {
			log.Println("Error receiving message on frontend broker", err)
		}

		err = b.SendMultipart(parts, 0)
		if err != nil {
			log.Println("Error sending message on backend broker", err)
		}

		// log.Println("Brokered message:", Stringify(parts))
	}
}
Exemple #2
0
func dump(sink zmq.Socket) {
	parts, err := sink.RecvMultipart(0)
	if err != nil {
		fmt.Println(err)
	}
	for _, msgdata := range parts {
		is_text := true
		fmt.Printf("[%03d] ", len(msgdata))
		for _, char := range msgdata {
			if char < 32 || char > 127 {
				is_text = false
			}
		}
		if is_text {
			fmt.Printf("%s\n", msgdata)
		} else {
			fmt.Printf("%X\n", msgdata)
		}
	}
}
Exemple #3
0
// ReadPb sends any protobuf along a ZMQ socket. This makes sure to bundle our
// type identifier at the beginning of the message.
func ReadPb(sock *zmq.Socket, timeout int) ([]byte, interface{}, error) {
	if timeout > 0 {
		if !WaitForRecv(sock, timeout) {
			return nil, nil, errors.New("recv timeout")
		}
	}

	rresp, err := sock.RecvMultipart(0)
	if err != nil {
		return nil, nil, err
	}

	// If we got a remote address, keep it.
	var remote []byte
	if len(rresp) > 1 {
		remote = rresp[0] // Remote address.
	}
	resp := rresp[len(rresp)-1]

	var pb interface{}
	switch resp[0] {
	case 1:
		pb = &Command{}
	case 2:
		pb = &StillAlive{}
	case 3:
		pb = &CommandFinished{}
	case 4:
		pb = &CommandOutput{}
	default:
		return nil, nil, errors.New(fmt.Sprintf("unknown packet type: %d", resp[0]))
	}

	err = proto.Unmarshal(resp[1:], pb.(proto.Message))
	if err != nil {
		return nil, nil, err
	}
	return remote, pb, nil
}