Example #1
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
}
Example #2
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
}
Example #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
}
Example #4
0
func BenchClient(b *testing.B, c chan bool, sender libchan.Sender, sendFunc BenchMessageSender, count int) {
	defer close(c)
	for i := 0; i < count; i++ {
		sendFunc(i, sender)
	}
	err := sender.Close()
	if err != nil {
		b.Fatalf("Error closing sender: %s", err)
	}
}
Example #5
0
func (c *channel) copySender(val libchan.Sender) (libchan.Sender, error) {
	recv, send, err := c.CreateNestedReceiver()
	if err != nil {
		return nil, err
	}
	go func() {
		libchan.Copy(val, recv)
		val.Close()
	}()
	return send, nil
}
Example #6
0
func (c *channel) copySendChannel(send libchan.Sender) (*channel, error) {
	recv, sendCopy, err := c.CreateNestedReceiver()
	if err != nil {
		return nil, err
	}
	// Start copying into sender
	go func() {
		libchan.Copy(send, recv)
		send.Close()
	}()
	return sendCopy.(*channel), nil
}
Example #7
0
func BenchProxy(b *testing.B, c chan bool, sender libchan.Sender, receiver libchan.Receiver, count int) {
	defer close(c)
	n, err := libchan.Copy(sender, receiver)
	if err != nil {
		b.Errorf("Error proxying: %s", err)
	}
	err = sender.Close()
	if err != nil {
		b.Errorf("Error closing sender: %s", err)
	}
	if n != count {
		b.Errorf("Wrong proxy count\n\tExpected: %d\n\tActual: %d", count, n)
	}
}
Example #8
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
}
Example #9
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
}
Example #10
0
	"time"

	"./database"

	"github.com/docker/libchan"
	"github.com/docker/libchan/spdy"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

//===== tests =====

var _ = Describe("Libchan server", func() {

	var clientConn net.Conn
	var clientSender libchan.Sender
	var serverListener net.Listener

	var dbDir string

	BeforeEach(func() {
		dbDir = fmt.Sprintf("/tmp/db-%d", os.Getpid())
		db, _ = database.Open(dbDir) // db is global defined in main.go

		var err error
		serverListener, err = net.Listen("tcp", "localhost:9323")
		Ω(err).ShouldNot(HaveOccurred())
		go ServeChan(serverListener)
		time.Sleep(10 * time.Millisecond)

		clientConn, err = net.Dial("tcp", "127.0.0.1:9323")