-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandler.go
executable file
·78 lines (69 loc) · 1.96 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Copyright 2013-2016 Seagate Technology LLC.
*
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at
* https://mozilla.org/MP:/2.0/.
*
* This program is distributed in the hope that it will be useful,
* but is provided AS-IS, WITHOUT ANY WARRANTY; including without
* the implied warranty of MERCHANTABILITY, NON-INFRINGEMENT or
* FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public
* License for more details.
*
* See www.openkinetic.org for more project information
*/
package kinetic
import (
"sync"
kproto "github.com/Kinetic/kinetic-go/proto"
)
// ResponseHandler is the handler for XXXXX_RESPONSE message from drive.
// For each operation, a unique ResponseHandler is required
type ResponseHandler struct {
callback Callback
done bool
cond *sync.Cond
}
func (h *ResponseHandler) handle(cmd *kproto.Command, value []byte) error {
if h.callback != nil {
if cmd.Status != nil && cmd.Status.Code != nil {
if cmd.GetStatus().GetCode() == kproto.Command_Status_SUCCESS {
h.callback.Success(cmd, value)
} else {
h.callback.Failure(cmd, getStatusFromProto(cmd))
}
} else {
klog.Warn("Other status received")
klog.Info("%v", cmd)
}
}
h.cond.L.Lock()
h.done = true
h.cond.Signal()
h.cond.L.Unlock()
return nil
}
func (h *ResponseHandler) fail(s Status) {
if h.callback != nil {
h.callback.Failure(nil, s)
}
h.cond.L.Lock()
h.done = true
h.cond.Signal()
h.cond.L.Unlock()
}
func (h *ResponseHandler) wait() {
h.cond.L.Lock()
if h.done == false {
h.cond.Wait()
}
h.cond.L.Unlock()
}
// NewResponseHandler is helper function to build a ResponseHandler with call as the Callback.
// For each operation, a unique ResponseHandler is required
func NewResponseHandler(call Callback) *ResponseHandler {
h := &ResponseHandler{callback: call, done: false, cond: sync.NewCond(&sync.Mutex{})}
return h
}