Пример #1
0
func (p *UDFProcess) readResponse() (*udf.Response, error) {
	err := udf.ReadMessage(&p.responseBuf, p.stdout, p.response)
	if err != nil {
		return nil, err
	}
	return p.response, nil
}
Пример #2
0
func TestMessage_ReadWriteMultiple(t *testing.T) {
	req := &udf.Request{}
	req.Message = &udf.Request_Keepalive{
		Keepalive: &udf.KeepaliveRequest{
			Time: 42,
		},
	}

	var buf bytes.Buffer

	var count int = 1e4
	for i := 0; i < count; i++ {
		err := udf.WriteMessage(req, &buf)
		if err != nil {
			t.Fatal(err)
		}
	}

	nreq := &udf.Request{}
	var b []byte

	for i := 0; i < count; i++ {
		err := udf.ReadMessage(&b, &buf, nreq)
		if err != nil {
			t.Fatal(err)
		}

		if !reflect.DeepEqual(req, nreq) {
			t.Fatalf("unexpected request: i:%d \ngot %v\nexp %v", i, nreq, req)
		}
	}
}
Пример #3
0
func TestMessage_ReadWrite(t *testing.T) {
	req := &udf.Request{}
	req.Message = &udf.Request_Keepalive{
		Keepalive: &udf.KeepaliveRequest{
			Time: 42,
		},
	}

	var buf bytes.Buffer

	err := udf.WriteMessage(req, &buf)
	if err != nil {
		t.Fatal(err)
	}

	nreq := &udf.Request{}
	var b []byte
	err = udf.ReadMessage(&b, &buf, nreq)
	if err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(req, nreq) {
		t.Errorf("unexpected request: \ngot %v\nexp %v", nreq, req)
	}
}
Пример #4
0
func (c *CommandHelper) readRequests() error {
	defer c.inr.Close()
	defer close(c.Requests)
	buf := bufio.NewReader(c.inr)
	var b []byte
	for {
		req := &udf.Request{}
		err := udf.ReadMessage(&b, buf, req)
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}
		c.Requests <- req
	}
}
Пример #5
0
func (o *IO) readRequests() error {
	defer o.inr.Close()
	defer close(o.Requests)
	buf := bufio.NewReader(o.inr)
	var b []byte
	for {
		req := &udf.Request{}
		err := udf.ReadMessage(&b, buf, req)
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}
		o.Requests <- req
	}
}
Пример #6
0
func (a *Agent) readLoop() error {
	defer a.Handler.Stop()
	defer a.in.Close()
	in := bufio.NewReader(a.in)
	var buf []byte
	request := &udf.Request{}
	for {
		err := udf.ReadMessage(&buf, in, request)
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}

		// Hand message to handler
		var res *udf.Response
		switch msg := request.Message.(type) {
		case *udf.Request_Info:
			info, err := a.Handler.Info()
			if err != nil {
				return err
			}
			res = &udf.Response{}
			res.Message = &udf.Response_Info{
				Info: info,
			}
		case *udf.Request_Init:
			init, err := a.Handler.Init(msg.Init)
			if err != nil {
				return err
			}
			res = &udf.Response{}
			res.Message = &udf.Response_Init{
				Init: init,
			}
		case *udf.Request_Keepalive:
			res = &udf.Response{
				Message: &udf.Response_Keepalive{
					Keepalive: &udf.KeepaliveResponse{
						Time: msg.Keepalive.Time,
					},
				},
			}
		case *udf.Request_Snapshot:
			snapshot, err := a.Handler.Snaphost()
			if err != nil {
				return err
			}
			res = &udf.Response{}
			res.Message = &udf.Response_Snapshot{
				Snapshot: snapshot,
			}
		case *udf.Request_Restore:
			restore, err := a.Handler.Restore(msg.Restore)
			if err != nil {
				return err
			}
			res = &udf.Response{}
			res.Message = &udf.Response_Restore{
				Restore: restore,
			}
		case *udf.Request_Begin:
			err := a.Handler.BeginBatch(msg.Begin)
			if err != nil {
				return err
			}
		case *udf.Request_Point:
			err := a.Handler.Point(msg.Point)
			if err != nil {
				return err
			}
		case *udf.Request_End:
			err := a.Handler.EndBatch(msg.End)
			if err != nil {
				return err
			}
		}
		if res != nil {
			a.outResponses <- res
		}
	}
	return nil
}