Commit ed0b5a24 authored by Zhu Yong's avatar Zhu Yong
Browse files

tweak handler and callback

parent 87e6a8c5
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -12,31 +12,23 @@ import (
type Callback interface {
	Success(resp *kproto.Command, value []byte)
	Failure(status Status)
	Done() bool
	Status() Status
}

// Generic Callback, can be used for all MessageType which doesn't require data from Kinetic drive.
// And for MessageType that require data from drive, a new struct need to be defined GenericCallback
type GenericCallback struct {
	done   bool
	status Status
}

func (c *GenericCallback) Success(resp *kproto.Command, value []byte) {
	c.done = true
	c.status = Status{Code: OK}
}

func (c *GenericCallback) Failure(status Status) {
	c.done = true
	c.status = status
}

func (c *GenericCallback) Done() bool {
	return c.done
}

func (c *GenericCallback) Status() Status {
	return c.status
}
+2 −4
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import (
)

// ResponseHandler is the handler for XXXXX_RESPONSE message from drive.
// For each operation, a unique ResponseHandler is requried
type ResponseHandler struct {
	callback Callback
	done     bool
@@ -51,11 +52,8 @@ func (h *ResponseHandler) wait() {
	h.cond.L.Unlock()
}

func (h *ResponseHandler) SetCallback(call Callback) {
	h.callback = call
}

// Helper function to build a ResponseHandler with call as the Callback.
// For each operation, a unique ResponseHandler is requried
func NewResponseHandler(call Callback) *ResponseHandler {
	h := &ResponseHandler{callback: call, done: false, cond: sync.NewCond(&sync.Mutex{})}
	return h