Esempio n. 1
0
//
// Delete value
//
func (s *EmbeddedServer) Delete(key string) error {

	id, err := common.NewUUID()
	if err != nil {
		return err
	}

	request := s.factory.CreateRequest(id,
		uint32(common.OPCODE_DELETE),
		key,
		[]byte(""))

	handle := newRequestHandle(request)

	handle.CondVar.L.Lock()
	defer handle.CondVar.L.Unlock()

	// push the request to a channel
	log.Current.Tracef("Handing new request to server. Key %s", key)
	s.state.incomings <- handle

	// This goroutine will wait until the request has been processed.
	handle.CondVar.Wait()
	log.Current.Tracef("Receive Response for request. Key %s", key)

	return handle.Err
}
Esempio n. 2
0
//
// Set value
//
func (s *EmbeddedServer) MakeRequest(op common.OpCode, key string, value []byte) error {

	id, err := common.NewUUID()
	if err != nil {
		return err
	}

	request := s.factory.CreateRequest(id,
		uint32(op),
		key,
		value)

	handle := newRequestHandle(request)

	handle.CondVar.L.Lock()
	defer handle.CondVar.L.Unlock()

	// push the request to a channel
	log.Current.Tracef("EmbeddedServer.MakeRequest(): Handing new request to gometa leader. Key %s", key)
	s.state.incomings <- handle

	// This goroutine will wait until the request has been processed.
	handle.CondVar.Wait()
	log.Current.Tracef("EmbeddedServer.MakeRequest(): Receive Response from gometa leader. Key %s", key)

	return handle.Err
}
Esempio n. 3
0
//
// Handle a new incoming request
//
//func (s *RequestReceiver) NewRequest(message []byte, reply *[]byte) error {
func (s *RequestReceiver) NewRequest(req *Request, reply **Reply) error {

	if s.server.IsDone() {
		return common.NewError(common.SERVER_ERROR, "Server is terminated. Cannot process new request.")
	}

	log.Current.Debugf("RequestReceiver.NewRequest(): Receive request from client")
	log.Current.Debugf("RequestReceiver.NewRequest(): opCode %s key %s value %s", req.OpCode, req.Key, req.Value)

	opCode := common.GetOpCode(req.OpCode)
	if opCode == common.OPCODE_GET {

		result, err := s.server.GetValue(req.Key)
		if err != nil {
			return err
		}
		log.Current.Debugf("RequestReceiver.NewRequest(): Receive response from server, len(value) = %d", len(result))

		*reply = &Reply{Result: result}
		return nil

	} else if opCode == common.OPCODE_ADD ||
		opCode == common.OPCODE_SET ||
		opCode == common.OPCODE_DELETE {

		if req.Value == nil {
			req.Value = ([]byte)("")
		}

		id, err := common.NewUUID()
		if err != nil {
			return err
		}
		request := s.server.factory.CreateRequest(id,
			uint32(common.GetOpCode(req.OpCode)),
			req.Key,
			req.Value)

		handle := newRequestHandle(request)

		handle.CondVar.L.Lock()
		defer handle.CondVar.L.Unlock()

		// push the request to a channel
		log.Current.Debugf("Handing new request to server. Key %s", req.Key)
		s.server.state.incomings <- handle

		// This goroutine will wait until the request has been processed.
		handle.CondVar.Wait()
		log.Current.Debugf("Receive Response for request. Key %s", req.Key)

		*reply = &Reply{Result: nil}
		return handle.Err

	} else {
		return common.NewError(common.CLIENT_ERROR, fmt.Sprintf("Invalid Op code %s", req.OpCode))
	}
}
Esempio n. 4
0
func (s *EmbeddedServer) MakeAsyncRequest(op common.OpCode, key string, value []byte) error {

	id, err := common.NewUUID()
	if err != nil {
		return err
	}

	request := s.factory.CreateRequest(id,
		uint32(op),
		key,
		value)

	handle := newRequestHandle(request)

	// push the request to a channel
	log.Current.Tracef("EmbeddedServer.MakeAsyncRequest(): Handing new request to gometa leader. Key %s", key)
	s.state.incomings <- handle

	return nil
}