diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-05-21 23:00:36 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-05-21 23:00:36 -0400 |
commit | 51692a582543f0182dc335df3fe89a5a08b293e6 (patch) | |
tree | 0a6dafae89979dea04b052d0b79816f3ab3a795c /timers.go | |
parent | 08307fb84160602da12ba4d1b118c859e8a73cdb (diff) |
Organize modules: timers, os
Diffstat (limited to 'timers.go')
-rw-r--r-- | timers.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/timers.go b/timers.go new file mode 100644 index 000000000..3a1efa103 --- /dev/null +++ b/timers.go @@ -0,0 +1,38 @@ +package main + +import ( + "github.com/golang/protobuf/proto" + "time" +) + +func InitTimers() { + Sub("timers", func(buf []byte) []byte { + msg := &Msg{} + check(proto.Unmarshal(buf, msg)) + switch msg.Payload.(type) { + case *Msg_TimerStart: + payload := msg.GetTimerStart() + return HandleTimerStart(payload.Id, payload.Interval, payload.Duration) + default: + panic("[timers] Unexpected message " + string(buf)) + } + }) +} + +func HandleTimerStart(id int32, interval bool, duration int32) []byte { + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(time.Duration(duration) * time.Millisecond) + payload, err := proto.Marshal(&Msg{ + Payload: &Msg_TimerReady{ + TimerReady: &TimerReadyMsg{ + Id: id, + }, + }, + }) + check(err) + Pub("timers", payload) + }() + return nil +} |