Skip to content

Commit b019b25

Browse files
committed
fakeFS support 'status' command
1 parent bbfbc28 commit b019b25

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

test/fakeFS/fakeFsWorker.go

+63
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,18 @@ const (
3737
BufLen = 4096
3838
SerializePlain = 0
3939
SerializeJson = 1
40+
FSStatusReply = `UP %d years, %d days, %d hours, %d minutes, %d seconds, %d milliseconds, %d microseconds
41+
FreeSWITCH (Version 1.8.7 git 6047ebd 2019-07-02 20:06:09Z 64bit) is ready
42+
0 session(s) since startup
43+
0 session(s) - peak 0, last 5min 0
44+
0 session(s) per Sec out of max 30, peak 0, last 5min 0
45+
1000 session(s) max
46+
min idle cpu 0.00/99.73
47+
Current Stack Size/Max 240K/8192K`
4048
)
4149

4250
type Worker struct {
51+
startTime time.Time
4352
stop bool
4453
conn net.Conn
4554
pass string
@@ -60,6 +69,7 @@ func NewWorker(conn net.Conn, pass, uuid string, events chan *Event) *Worker {
6069
resUuid = tryUuid
6170
}
6271
return &Worker{
72+
startTime: time.Now(),
6373
conn: conn,
6474
pass: pass,
6575
events: make([]string, 0),
@@ -160,6 +170,12 @@ func (fs *Worker) processCommand(s string) {
160170
fs.customEvents = append(fs.customEvents, events[i])
161171
}
162172
}
173+
case "status":
174+
Y, M, D, H, m, sec := diff(time.Now(), fs.startTime)
175+
status := fmt.Sprintf(FSStatusReply, Y, M, D, H, m, sec, 0)
176+
if _, err := fs.conn.Write([]byte(status)); err != nil {
177+
fs.stop = true
178+
}
163179
default:
164180
if _, err := fs.conn.Write([]byte(FsErrCommandNotFound)); err != nil {
165181
fs.stop = true
@@ -220,3 +236,50 @@ func (fs *Worker) sendMessage(buf string) error {
220236
}
221237
return nil
222238
}
239+
240+
func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
241+
if a.Location() != b.Location() {
242+
b = b.In(a.Location())
243+
}
244+
if a.After(b) {
245+
a, b = b, a
246+
}
247+
y1, M1, d1 := a.Date()
248+
y2, M2, d2 := b.Date()
249+
250+
h1, m1, s1 := a.Clock()
251+
h2, m2, s2 := b.Clock()
252+
253+
year = int(y2 - y1)
254+
month = int(M2 - M1)
255+
day = int(d2 - d1)
256+
hour = int(h2 - h1)
257+
min = int(m2 - m1)
258+
sec = int(s2 - s1)
259+
260+
// Normalize negative values
261+
if sec < 0 {
262+
sec += 60
263+
min--
264+
}
265+
if min < 0 {
266+
min += 60
267+
hour--
268+
}
269+
if hour < 0 {
270+
hour += 24
271+
day--
272+
}
273+
if day < 0 {
274+
// days in month:
275+
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
276+
day += 32 - t.Day()
277+
month--
278+
}
279+
if month < 0 {
280+
month += 12
281+
year--
282+
}
283+
284+
return
285+
}

0 commit comments

Comments
 (0)