Applied code review
[platform/core/system/edge-orchestration.git] / src / servicemgr / service_map.go
1 package servicemgr
2
3 import (
4         "os/exec"
5         "sync/atomic"
6 )
7
8 // CreateServiceMap function
9 func CreateServiceMap(cmd *exec.Cmd, serviceName string, appName string) (uint64, chan MsgFormat, chan bool) {
10         serviceID := getServiceIdx()
11         msgChan := make(chan MsgFormat, IPCMsgChan)
12         ctlChan := make(chan bool)
13
14         value := make(map[string]interface{})
15
16         value[KeyAppName] = appName
17         value[KeyServiceName] = serviceName
18         value[KeyCmd] = cmd
19         value[KeyMsgChan] = msgChan
20         value[KeyCtlChan] = ctlChan
21
22         ServiceMap.Set(serviceID, value)
23
24         return serviceID, msgChan, ctlChan
25 }
26
27 // InitServiceMap function
28 func InitServiceMap() {
29         ServiceMap = ConcurrentMap{items: make(map[uint64]interface{})}
30 }
31
32 // DeleteServiceMap function
33 func DeleteServiceMap(serviceID uint64) {
34         ServiceMap.Remove(serviceID)
35 }
36
37 // GetAppName function
38 func GetAppName(serviceID uint64) (string, error) {
39         value, _ := ServiceMap.Get(serviceID)
40         if value == nil {
41                 return "", errInvalidService
42         }
43
44         valueList := value.(map[string]interface{})
45         return valueList[KeyAppName].(string), nil
46 }
47
48 // GetServiceName function
49 func GetServiceName(serviceID uint64) (string, error) {
50         value, _ := ServiceMap.Get(serviceID)
51         if value == nil {
52                 return "", errInvalidService
53         }
54
55         valueList := value.(map[string]interface{})
56         return valueList[KeyServiceName].(string), nil
57 }
58
59 // GetCmd function
60 func GetCmd(serviceID uint64) (*exec.Cmd, error) {
61         value, _ := ServiceMap.Get(serviceID)
62         if value == nil {
63                 return nil, errInvalidService
64         }
65
66         valueList := value.(map[string]interface{})
67         return valueList[KeyCmd].(*exec.Cmd), nil
68 }
69
70 // SetCmd function
71 func SetCmd(serviceID uint64, cmd *exec.Cmd) error {
72         value, _ := ServiceMap.Get(serviceID)
73         if value == nil {
74                 return errInvalidService
75         }
76
77         valueList := value.(map[string]interface{})
78         valueList[KeyCmd] = cmd
79         return nil
80 }
81
82 // GetMsgChan function
83 func GetMsgChan(serviceID uint64) (chan MsgFormat, error) {
84         value, _ := ServiceMap.Get(serviceID)
85         if value == nil {
86                 return nil, errInvalidService
87         }
88
89         valueList := value.(map[string]interface{})
90         return valueList[KeyMsgChan].(chan MsgFormat), nil
91 }
92
93 // GetCtlChan function
94 func GetCtlChan(serviceID uint64) (chan bool, error) {
95         value, _ := ServiceMap.Get(serviceID)
96         if value == nil {
97                 return nil, errInvalidService
98         }
99
100         valueList := value.(map[string]interface{})
101         return valueList[KeyCtlChan].(chan bool), nil
102 }
103
104 // getServiceIdx() is for getting global serviceID
105 func getServiceIdx() uint64 {
106         atomic.AddUint64(&ServiceIdx, 1)
107         return atomic.LoadUint64(&ServiceIdx)
108 }