Exemplo n.º 1
0
func NewQueue(dst libchan.Sender, size int) *Queue {
	r, w := libchan.Pipe()
	q := &Queue{
		PipeSender: w,
		dst:        dst,
		ch:         make(chan *libchan.Message, size),
	}
	go func() {
		defer close(q.ch)
		for {
			msg, err := r.Receive(libchan.Ret)
			if err != nil {
				r.Close()
				return
			}
			q.ch <- msg
		}
	}()
	go func() {
		for msg := range q.ch {
			_, err := dst.Send(msg)
			if err != nil {
				r.Close()
				return
			}
		}
	}()
	return q
}
Exemplo n.º 2
0
func doEcho(sender libchan.Sender, timeout time.Duration) error {
	txt := "Hello world!"
	replyRecv, replySend := libchan.Pipe()

	req := Request{ER: (*EchoRequest)(&txt), Reply: replySend}

	err := sender.Send(req)
	if err != nil {
		replySend.Close()
		return err
	}

	var reply Reply
	err = replyRecv.Receive(&reply)
	if err != nil {
		return err
	}
	if reply.Code != CodeOK {
		return fmt.Errorf("%s", reply.Error)
	}
	if string(*reply.ER) != txt {
		return fmt.Errorf("echo returned bad message: '%s'", reply.ER)
	}
	return nil
}
Exemplo n.º 3
0
func SendEvents(ch chan FileEvent, sender libchan.Sender) error {
	for ev := range ch {
		if err := sender.Send(ev); err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 4
0
func (r *receiver) SendTo(dst libchan.Sender) (int, error) {
	var n int
	for {
		var rm msgpack.RawMessage
		if err := r.Receive(&rm); err == io.EOF {
			break
		} else if err != nil {
			return n, err
		}

		if err := dst.Send(&rm); err != nil {
			return n, err
		}
		n++
	}
	return n, nil
}
Exemplo n.º 5
0
func (c *channel) SendTo(dst libchan.Sender) (int, error) {
	if c.direction == outbound {
		return 0, ErrWrongDirection
	}
	var n int
	for {
		var rm msgpack.RawMessage
		if err := c.Receive(&rm); err == io.EOF {
			break
		} else if err != nil {
			return n, err
		}

		if err := dst.Send(&rm); err != nil {
			return n, err
		}
		n++
	}
	return n, nil
}
Exemplo n.º 6
0
		time.Sleep(10 * time.Millisecond)

		db.Close()
		os.RemoveAll(dbDir)
	})

	It("responds to echo requests", func() {
		responseRecv, responseSend := libchan.Pipe()

		req := &EchoRequest{
			Cmd:   "echo",
			Text:  "Hello world!",
			Reply: responseSend,
		}

		err := clientSender.Send(req)
		Ω(err).ShouldNot(HaveOccurred())

		response := &EchoReply{}
		err = responseRecv.Receive(response)
		Ω(err).ShouldNot(HaveOccurred())

		//fmt.Println(response.Text)
		Ω(response.Text).Should(Equal(req.Text))
	})

	It("processes subscriptions", func() {
		// Create some initial messages
		now := time.Now().Unix()
		for i := 0; i < 10; i += 1 {
			m := database.RFMessage{At: now + int64(i), Group: byte(2 * i),