Use go module as dependency tool instead of glide
[src/xds/xds-agent.git] / lib / agent / events.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package agent
19
20 import (
21         "fmt"
22         "time"
23
24         "gerrit.automotivelinux.org/gerrit/src/xds/xds-agent.git/lib/xaapiv1"
25 )
26
27 // EventDef Definition on one event
28 type EventDef struct {
29         sids map[string]int
30 }
31
32 // Events Hold registered events per context
33 type Events struct {
34         *Context
35         eventsMap map[string]*EventDef
36 }
37
38 // NewEvents creates an instance of Events
39 func NewEvents(ctx *Context) *Events {
40         evMap := make(map[string]*EventDef)
41         for _, ev := range xaapiv1.EVTAllList {
42                 evMap[ev] = &EventDef{
43                         sids: make(map[string]int),
44                 }
45         }
46         return &Events{
47                 Context:   ctx,
48                 eventsMap: evMap,
49         }
50 }
51
52 // GetList returns the list of all supported events
53 func (e *Events) GetList() []string {
54         return xaapiv1.EVTAllList
55 }
56
57 // Register Used by a client/session to register to a specific (or all) event(s)
58 func (e *Events) Register(evName, sessionID string) error {
59         evs := xaapiv1.EVTAllList
60         if evName != xaapiv1.EVTAll {
61                 if _, ok := e.eventsMap[evName]; !ok {
62                         return fmt.Errorf("Unsupported event type name")
63                 }
64                 evs = []string{evName}
65         }
66         for _, ev := range evs {
67                 e.eventsMap[ev].sids[sessionID]++
68         }
69         return nil
70 }
71
72 // UnRegister Used by a client/session to unregister event(s)
73 func (e *Events) UnRegister(evName, sessionID string) error {
74         evs := xaapiv1.EVTAllList
75         if evName != xaapiv1.EVTAll {
76                 if _, ok := e.eventsMap[evName]; !ok {
77                         return fmt.Errorf("Unsupported event type name")
78                 }
79                 evs = []string{evName}
80         }
81         for _, ev := range evs {
82                 if _, exist := e.eventsMap[ev].sids[sessionID]; exist {
83                         delete(e.eventsMap[ev].sids, sessionID)
84                         break
85                 }
86         }
87         return nil
88 }
89
90 // Emit Used to manually emit an event
91 func (e *Events) Emit(evName string, data interface{}, fromSid string) error {
92         var firstErr error
93
94         if _, ok := e.eventsMap[evName]; !ok {
95                 return fmt.Errorf("Unsupported event type")
96         }
97
98         e.LogSillyf("Emit Event %s: %v", evName, data)
99
100         firstErr = nil
101         evm := e.eventsMap[evName]
102         for sid := range evm.sids {
103                 so := e.webServer.sessions.IOSocketGet(sid)
104                 if so == nil {
105                         if firstErr == nil {
106                                 firstErr = fmt.Errorf("IOSocketGet return nil (SID=%v)", sid)
107                         }
108                         continue
109                 }
110                 msg := xaapiv1.EventMsg{
111                         Time:          time.Now().String(),
112                         FromSessionID: fromSid,
113                         Type:          evName,
114                         Data:          data,
115                 }
116
117                 if err := (*so).Emit(evName, msg); err != nil {
118                         e.Log.Errorf("WS Emit %v error : %v", evName, err)
119                         if firstErr == nil {
120                                 firstErr = err
121                         }
122                 }
123         }
124
125         return firstErr
126 }