From c51d5034d527578da70bdd41b9ce13f28455c598 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Wed, 8 Nov 2017 17:37:56 +0100 Subject: Added events decoder helpers. --- lib/agent/webserver.go | 2 +- lib/apiv1/events.go | 47 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/agent/webserver.go b/lib/agent/webserver.go index b75abd4..13572b4 100644 --- a/lib/agent/webserver.go +++ b/lib/agent/webserver.go @@ -233,8 +233,8 @@ func (s *WebServer) socketHandler(c *gin.Context) { so.On("disconnection", func() { s.Log.Debugf("WS disconnected (WSID=%s, SID=%s)", so.Id(), sess.ID) - s.sessions.UpdateIOSocket(sess.ID, nil) s.events.UnRegister(apiv1.EVTAll, sess.ID) + s.sessions.UpdateIOSocket(sess.ID, nil) }) }) diff --git a/lib/apiv1/events.go b/lib/apiv1/events.go index dde2710..cdd0889 100644 --- a/lib/apiv1/events.go +++ b/lib/apiv1/events.go @@ -1,5 +1,10 @@ package apiv1 +import ( + "encoding/json" + "fmt" +) + // EventRegisterArgs is the parameters (json format) of /events/register command type EventRegisterArgs struct { Name string `json:"name"` @@ -12,17 +17,17 @@ type EventUnRegisterArgs struct { ID int `json:"id"` } -// Events definitions +// Events Type definitions const ( // EventTypePrefix Used as event prefix EventTypePrefix = "event:" // following by event type // Supported Events type EVTAll = EventTypePrefix + "all" - EVTServerConfig = EventTypePrefix + "server-config" // data type apiv1.ServerCfg - EVTProjectAdd = EventTypePrefix + "project-add" // data type apiv1.ProjectConfig - EVTProjectDelete = EventTypePrefix + "project-delete" // data type apiv1.ProjectConfig - EVTProjectChange = EventTypePrefix + "project-state-change" // data type apiv1.ProjectConfig + EVTServerConfig = EventTypePrefix + "server-config" // type EventMsg with Data type apiv1.ServerCfg + EVTProjectAdd = EventTypePrefix + "project-add" // type EventMsg with Data type apiv1.ProjectConfig + EVTProjectDelete = EventTypePrefix + "project-delete" // type EventMsg with Data type apiv1.ProjectConfig + EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type apiv1.ProjectConfig ) // EVTAllList List of all supported events @@ -33,9 +38,39 @@ var EVTAllList = []string{ EVTProjectChange, } -// EventMsg Message send +// EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function) type EventMsg struct { Time string `json:"time"` Type string `json:"type"` Data interface{} `json:"data"` } + +// DecodeServerCfg Helper to decode Data field type ServerCfg +func (e *EventMsg) DecodeServerCfg() (ServerCfg, error) { + p := ServerCfg{} + if e.Type != EVTServerConfig { + return p, fmt.Errorf("Invalid type") + } + d, err := json.Marshal(e.Data) + if err == nil { + err = json.Unmarshal(d, &p) + } + return p, err +} + +// DecodeProjectConfig Helper to decode Data field type ProjectConfig +func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) { + var err error + p := ProjectConfig{} + switch e.Type { + case EVTProjectAdd, EVTProjectChange, EVTProjectDelete: + d := []byte{} + d, err = json.Marshal(e.Data) + if err == nil { + err = json.Unmarshal(d, &p) + } + default: + err = fmt.Errorf("Invalid type") + } + return p, err +} -- cgit 1.2.3-korg