-
Notifications
You must be signed in to change notification settings - Fork 1
/
uklam.go
183 lines (149 loc) · 3.29 KB
/
uklam.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package uklam
import (
"io/ioutil"
"os"
"sync"
"time"
"github.com/eaciit/toolkit"
)
type IDataWalker interface {
SetHost(string)
Host() string
}
type WalkerStatusEnum int
const (
WalkerIdle WalkerStatusEnum = 0
WalkerRunning = 1
WalkerRunningDone = 10
WalkerStop = 100
)
type FSWalker struct {
sync.RWMutex
Setting *toolkit.M
RefreshDuration time.Duration
CheckFn func(IDataWalker, toolkit.M) *toolkit.Result
WalkFn func(IDataWalker, toolkit.M) *toolkit.Result
EachFn func(IDataWalker, toolkit.M, os.FileInfo, *toolkit.Result)
Status WalkerStatusEnum
chanCommand chan toolkit.M
_host string
log *toolkit.LogEngine
}
var _defaultRefreshDuration time.Duration
func DefaultRefreshDuration() time.Duration {
if _defaultRefreshDuration == 0 {
_defaultRefreshDuration = 1 * time.Millisecond
}
return _defaultRefreshDuration
}
func SetDefaultRefreshDuration(t time.Duration) {
_defaultRefreshDuration = t
}
func NewFS(path string) *FSWalker {
fs := new(FSWalker)
fs._host = path
fs.RefreshDuration = DefaultRefreshDuration()
fs.log, _ = toolkit.NewLog(true, false, "", "", "")
fs.chanCommand = make(chan toolkit.M)
return fs
}
func (fs *FSWalker) SetHost(h string) {
fs._host = h
}
func (fs *FSWalker) Host() string {
return fs._host
}
func (fs *FSWalker) Log() *toolkit.LogEngine {
if fs.log == nil {
fs.log, _ = toolkit.NewLog(true, false, "", "", "")
}
return fs.log
}
func (fs *FSWalker) SetLog(l *toolkit.LogEngine) {
if fs.log != nil {
fs.log.Close()
}
fs.log = l
}
func checkFile(dw IDataWalker, in toolkit.M) *toolkit.Result {
r := toolkit.NewResult()
infos, e := ioutil.ReadDir(dw.Host())
if e != nil {
return r.SetError(e)
}
//toolkit.Println("Files: ", len(infos))
r.Data = infos
return r
}
func (fs *FSWalker) Start() {
if fs.CheckFn == nil {
fs.CheckFn = checkFile
}
if fs.WalkFn == nil {
fs.WalkFn = FSWalkFn
}
ticker := time.NewTicker(fs.RefreshDuration)
go func() {
for {
select {
case m := <-fs.chanCommand:
cmd := m.GetString("command")
if cmd == "stop" {
ticker.Stop()
}
case <-ticker.C:
if fs.Status == WalkerIdle {
r := fs.CheckFn(fs, nil)
if r.Status != toolkit.Status_OK {
fs.log.Error("Check Fail: " + r.Message)
}
fs.Walk()
}
if fs.Status == WalkerRunningDone {
fs.SetIdle()
}
}
}
}()
}
func (fs *FSWalker) NewData() bool {
if fs.Status == WalkerRunning {
return false
}
newData := false
fs.Lock()
fs.Unlock()
return newData
}
func (fs *FSWalker) Walk() error {
if fs.Status != WalkerIdle {
return nil
}
fs.Lock()
fs.Status = WalkerRunning
fs.Unlock()
if fs.WalkFn != nil {
r := fs.CheckFn(fs, nil)
if r.Status != toolkit.Status_OK {
fs.log.Error("Check Fail: " + r.Message)
return toolkit.Errorf("Check fail: %s", r.Message)
}
r = fs.WalkFn(fs, toolkit.M{}.Set("data", r.Data))
if r.Status != toolkit.Status_OK {
fs.log.Error("Walking Fail: " + r.Message)
}
}
fs.Lock()
fs.Status = WalkerRunningDone
fs.Unlock()
return nil
}
func (fs *FSWalker) SetIdle() error {
fs.Lock()
fs.Status = WalkerIdle
fs.Unlock()
return nil
}
func (fs *FSWalker) Stop() {
fs.chanCommand <- toolkit.M{}.Set("command", "stop")
}