2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
23 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
28 var timerMutex = &sync.Mutex{}
30 type TimerInfo struct {
31 timerAddress *time.Timer
32 timerFunctionAddress func()
35 type TimerMap struct {
36 timer map[uint16] TimerInfo
39 // This method should run as a constructor
40 func (t *TimerMap) Init() {
41 t.timer = make(map[uint16] TimerInfo)
44 func (t *TimerMap) StartTimer(subId uint16, expireAfterTime time.Duration, timerFunction func(subId uint16)) bool {
46 defer timerMutex.Unlock()
47 if (timerFunction == nil) {
48 xapp.Logger.Error("StartTimer() timerFunc == nil")
52 // Stop timer if there is already timer running with the same id
53 if val, ok := t.timer[subId]; ok {
54 xapp.Logger.Error("StartTimer() old timer found")
55 if val.timerAddress != nil {
56 xapp.Logger.Error("StartTimer() deleting old timer")
57 val.timerAddress.Stop()
59 delete(t.timer, subId)
62 // Store timer + timer function excecutor function and the function to be excecuted when timer expires, in map
63 t.timer[subId] = TimerInfo{timerAddress: time.AfterFunc(expireAfterTime, func(){t.timerFunctionExcecutor(subId)}),
64 timerFunctionAddress: func(){timerFunction(subId)}}
68 func (t *TimerMap) StopTimer(subId uint16) bool {
70 defer timerMutex.Unlock()
71 if val, ok := t.timer[subId]; ok {
72 if val.timerAddress != nil {
73 val.timerAddress.Stop()
74 delete(t.timer, subId)
77 xapp.Logger.Error("StopTimer() timerAddress == nil")
81 xapp.Logger.Info("StopTimer() Timer not found. May be expired or stopped already. subId: %v",subId)
86 func (t *TimerMap) timerFunctionExcecutor(subId uint16) {
88 if val, ok := t.timer[subId]; ok {
89 if val.timerFunctionAddress != nil {
90 // Take local copy of timer function address
91 f := val.timerFunctionAddress
92 // Delete timer instance from map
93 delete(t.timer, subId)
95 // Excecute the timer function
99 xapp.Logger.Error("timerExcecutorFunc() timerFunctionAddress == nil")
104 xapp.Logger.Error("timerExcecutorFunc() Timer not anymore in map. subId: %v",subId)