summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-08 10:53:39 +0100
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-08 10:53:39 +0100
commit0a5ab2ca1ab89e6e667b7f9c7bdffdff036ca9b7 (patch)
treebdc803f2d02347540d221bb9fc9f8c4b6e748481
parent02aec942b44eecd2ea9b311bb4ba2d60cce21e9a (diff)
Unregistered events when client WS session is closed.
-rw-r--r--lib/agent/events.go17
-rw-r--r--lib/agent/project-st.go4
-rw-r--r--lib/agent/webserver.go2
-rw-r--r--lib/apiv1/events.go8
4 files changed, 17 insertions, 14 deletions
diff --git a/lib/agent/events.go b/lib/agent/events.go
index 2684ff5..9ff72ac 100644
--- a/lib/agent/events.go
+++ b/lib/agent/events.go
@@ -7,13 +7,6 @@ import (
"github.com/iotbzh/xds-agent/lib/apiv1"
)
-var _EVTAllList = []string{
- apiv1.EVTServerConfig,
- apiv1.EVTProjectAdd,
- apiv1.EVTProjectDelete,
- apiv1.EVTProjectChange,
-}
-
// EventDef Definition on one event
type EventDef struct {
sids map[string]int
@@ -28,7 +21,7 @@ type Events struct {
// NewEvents creates an instance of Events
func NewEvents(ctx *Context) *Events {
evMap := make(map[string]*EventDef)
- for _, ev := range _EVTAllList {
+ for _, ev := range apiv1.EVTAllList {
evMap[ev] = &EventDef{
sids: make(map[string]int),
}
@@ -41,12 +34,12 @@ func NewEvents(ctx *Context) *Events {
// GetList returns the list of all supported events
func (e *Events) GetList() []string {
- return _EVTAllList
+ return apiv1.EVTAllList
}
// Register Used by a client/session to register to a specific (or all) event(s)
func (e *Events) Register(evName, sessionID string) error {
- evs := _EVTAllList
+ evs := apiv1.EVTAllList
if evName != apiv1.EVTAll {
if _, ok := e.eventsMap[evName]; !ok {
return fmt.Errorf("Unsupported event type name")
@@ -61,7 +54,7 @@ func (e *Events) Register(evName, sessionID string) error {
// UnRegister Used by a client/session to unregister event(s)
func (e *Events) UnRegister(evName, sessionID string) error {
- evs := _EVTAllList
+ evs := apiv1.EVTAllList
if evName != apiv1.EVTAll {
if _, ok := e.eventsMap[evName]; !ok {
return fmt.Errorf("Unsupported event type name")
@@ -95,7 +88,7 @@ func (e *Events) Emit(evName string, data interface{}) error {
so := e.webServer.sessions.IOSocketGet(sid)
if so == nil {
if firstErr == nil {
- firstErr = fmt.Errorf("IOSocketGet return nil")
+ firstErr = fmt.Errorf("IOSocketGet return nil (SID=%v)", sid)
}
continue
}
diff --git a/lib/agent/project-st.go b/lib/agent/project-st.go
index cd55656..e2cd3cb 100644
--- a/lib/agent/project-st.go
+++ b/lib/agent/project-st.go
@@ -143,7 +143,7 @@ func (p *STProject) _cbServerFolderChanged(pData interface{}, data interface{})
p.folder.DataCloudSync.STSvrStatus = evt.Folder.Status
if err := p.events.Emit(apiv1.EVTProjectChange, p.server.FolderToProject(*p.folder)); err != nil {
- p.Log.Warningf("Cannot notify project change: %v", err)
+ p.Log.Warningf("Cannot notify project change (from server): %v", err)
}
}
return nil
@@ -182,7 +182,7 @@ func (p *STProject) _cbLocalSTEvents(ev st.Event, data *st.EventsCBData) {
p.folder.DataCloudSync.STLocStatus = sts
if err := p.events.Emit(apiv1.EVTProjectChange, p.server.FolderToProject(*p.folder)); err != nil {
- p.Log.Warningf("Cannot notify project change: %v", err)
+ p.Log.Warningf("Cannot notify project change (local): %v", err)
}
}
}
diff --git a/lib/agent/webserver.go b/lib/agent/webserver.go
index d15236d..b75abd4 100644
--- a/lib/agent/webserver.go
+++ b/lib/agent/webserver.go
@@ -11,6 +11,7 @@ import (
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
+ "github.com/iotbzh/xds-agent/lib/apiv1"
)
// WebServer .
@@ -233,6 +234,7 @@ 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)
})
})
diff --git a/lib/apiv1/events.go b/lib/apiv1/events.go
index da9a2af..dde2710 100644
--- a/lib/apiv1/events.go
+++ b/lib/apiv1/events.go
@@ -25,6 +25,14 @@ const (
EVTProjectChange = EventTypePrefix + "project-state-change" // data type apiv1.ProjectConfig
)
+// EVTAllList List of all supported events
+var EVTAllList = []string{
+ EVTServerConfig,
+ EVTProjectAdd,
+ EVTProjectDelete,
+ EVTProjectChange,
+}
+
// EventMsg Message send
type EventMsg struct {
Time string `json:"time"`