Ejemplo n.º 1
0
func (s *session) sendCmd(op byte, wr func(*rw.Writer) error, rd func(*rw.Reader) error) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	if err := s.cli.writeCmd(op, s.id, wr); err != nil {
		return err
	}
	if op == requestDbClose {
		return nil
	}
	select {
	case <-s.cli.done:
		return fmt.Errorf("server gone")
	case resp, ok := <-s.in:
		if !ok {
			return ErrClosedConnection
		} else if resp.err != nil {
			return resp.err
		}
		defer resp.Close()
		if rd != nil {
			br := rw.NewReader(resp.ReadCloser.(io.Reader))
			if err := rd(br); err != nil {
				return err
			} else if err = br.Err(); err != nil {
				return err
			}
		}
		return nil
	}
}
Ejemplo n.º 2
0
func (rid *RID) FromStream(r io.Reader) error {
	buf := make([]byte, ridSerializedSize)
	if err := rw.NewReader(r).ReadRawBytes(buf); err != nil {
		return err
	}
	rid.ClusterID = int16(rw.Order.Uint16(buf))
	rid.ClusterPos = int64(rw.Order.Uint64(buf[rw.SizeShort:]))
	return nil
}
Ejemplo n.º 3
0
func (bag *RidBag) FromStream(r io.Reader) error {
	br := rw.NewReader(r)
	first := br.ReadByte()
	if err := br.Err(); err != nil {
		return err
	}
	if first&0x1 != 0 {
		bag.delegate = newEmbeddedRidBag()
	} else {
		bag.delegate = newSBTreeRidBag()
	}
	if first&0x2 != 0 {
		br.ReadRawBytes(bag.id[:])
	}
	if err := br.Err(); err != nil {
		return err
	}
	return bag.delegate.deserializeDelegate(br)
}
Ejemplo n.º 4
0
func TestReadErrorResponseWithSingleException(t *testing.T) {
	buf := new(bytes.Buffer)
	bw := rw.NewWriter(buf)
	bw.WriteByte(byte(1)) // indicates continue of exception class/msg array
	bw.WriteStrings("org.foo.BlargException", "wibble wibble!!")
	bw.WriteByte(byte(0)) // indicates end of exception class/msg array
	bw.WriteBytes([]byte("this is a stacktrace simulator\nEOL"))

	var serverExc error
	serverExc = obinary.ReadErrorResponse(rw.NewReader(buf))

	e, ok := serverExc.(orient.OServerException)
	if !ok {
		t.Fatal("wrong exception type")
	}
	equals(t, 1, len(e.Exceptions))

	equals(t, "org.foo.BlargException", e.Exceptions[0].ExcClass())
	equals(t, "wibble wibble!!", e.Exceptions[0].ExcMessage())
}
Ejemplo n.º 5
0
func TestReadErrorResponseWithMultipleExceptions(t *testing.T) {
	buf := new(bytes.Buffer)
	bw := rw.NewWriter(buf)
	bw.WriteByte(byte(1)) // indicates more exceptions to come
	bw.WriteStrings("org.foo.BlargException", "Too many blorgles!!")
	bw.WriteByte(byte(1)) // indicates more exceptions to come
	bw.WriteStrings("org.foo.FeebleException", "Not enough juice")
	bw.WriteByte(byte(1)) // indicates more exceptions to come
	bw.WriteStrings("org.foo.WobbleException", "Orbital decay")
	bw.WriteByte(byte(0)) // indicates end of exceptions
	bw.WriteBytes([]byte("this is a stacktrace simulator\nEOL"))

	serverExc := obinary.ReadErrorResponse(rw.NewReader(buf))

	e, ok := serverExc.(orient.OServerException)
	if !ok {
		t.Fatal("wrong exception type")
	}

	equals(t, "org.foo.BlargException", e.Exceptions[0].ExcClass())
	equals(t, "Not enough juice", e.Exceptions[1].ExcMessage())
	equals(t, "org.foo.WobbleException", e.Exceptions[2].ExcClass())
	equals(t, "Orbital decay", e.Exceptions[2].ExcMessage())
}
Ejemplo n.º 6
0
// Dial creates a new binary connection to OrientDB server.
// The Client returned is ready to make calls to the OrientDB but has not
// yet established a database session or a session with the OrientDB server.
// After this, the user needs to call either OpenDatabase or CreateServerSession.
func Dial(addr string) (*Client, error) {
	addr, err := validateAddr(addr)
	if err != nil {
		return nil, err
	}
	conn, err := net.DialTimeout("tcp", addr, time.Minute)
	if err != nil {
		return nil, err
	}
	c := &Client{
		addr: addr, conn: conn, done: make(chan struct{}),
		br: bufio.NewReader(conn), bw: bufio.NewWriter(conn),
	}
	c.pr = rw.NewReader(c.br)
	c.pw = rw.NewWriter(c.bw)
	if err := c.handshakeVersion(); err != nil {
		conn.Close()
		return nil, err
	}
	c.sess = make(map[int32]*session)
	c.root = c.newSess(noSessionId)
	go c.run()
	return c, nil
}