Commit 79c62d27 authored by Simon Pastor's avatar Simon Pastor
Browse files

loc-serv and rnis thread safe

parent e7936dc4
Loading
Loading
Loading
Loading
+35 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import (
	"os"
	"strconv"
	"strings"
	"sync"
	"time"

	sbi "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-loc-serv/sbi"
@@ -104,6 +105,7 @@ var sandboxName string
var basePath string
var baseKey string
var sessionMgr *sm.SessionMgr
var mutex sync.Mutex

// Init - Location Service initialization
func Init() (err error) {
@@ -203,7 +205,9 @@ func createClient(notifyPath string) (*clientNotifOMA.APIClient, error) {

func deregisterZoneStatus(subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	zoneStatusSubscriptionMap[subsId] = nil
	mutex.Unlock()
}

func registerZoneStatus(zoneId string, nbOfUsersZoneThreshold int32, nbOfUsersAPThreshold int32, opStatus []OperationStatus, subsIdStr string) {
@@ -227,22 +231,26 @@ func registerZoneStatus(zoneId string, nbOfUsersZoneThreshold int32, nbOfUsersAP
	zoneStatus.NbUsersInZoneThreshold = (int)(nbOfUsersZoneThreshold)
	zoneStatus.NbUsersInAPThreshold = (int)(nbOfUsersAPThreshold)
	zoneStatus.ZoneId = zoneId

	mutex.Lock()
	zoneStatusSubscriptionMap[subsId] = &zoneStatus
	mutex.Unlock()
}

func deregisterZonal(subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	zonalSubscriptionMap[subsId] = ""
	zonalSubscriptionEnteringMap[subsId] = ""
	zonalSubscriptionLeavingMap[subsId] = ""
	zonalSubscriptionTransferringMap[subsId] = ""
	mutex.Unlock()
}

func registerZonal(zoneId string, event []UserEventType, subsIdStr string) {

	subsId, _ := strconv.Atoi(subsIdStr)

	mutex.Lock()
	if event != nil {
		for i := 0; i < len(event); i++ {
			switch event[i] {
@@ -261,20 +269,23 @@ func registerZonal(zoneId string, event []UserEventType, subsIdStr string) {
		zonalSubscriptionTransferringMap[subsId] = zoneId
	}
	zonalSubscriptionMap[subsId] = zoneId
	mutex.Unlock()
}

func deregisterUser(subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	userSubscriptionMap[subsId] = ""
	userSubscriptionEnteringMap[subsId] = ""
	userSubscriptionLeavingMap[subsId] = ""
	userSubscriptionTransferringMap[subsId] = ""
	mutex.Unlock()
}

func registerUser(userAddress string, event []UserEventType, subsIdStr string) {

	subsId, _ := strconv.Atoi(subsIdStr)

	mutex.Lock()
	if event != nil {
		for i := 0; i < len(event); i++ {
			switch event[i] {
@@ -293,6 +304,7 @@ func registerUser(userAddress string, event []UserEventType, subsIdStr string) {
		userSubscriptionTransferringMap[subsId] = userAddress
	}
	userSubscriptionMap[subsId] = userAddress
	mutex.Unlock()
}

func checkNotificationRegistrations(checkType int, param1 string, param2 string, param3 string, param4 string, param5 string) {
@@ -311,6 +323,8 @@ func checkNotificationRegistrations(checkType int, param1 string, param2 string,

func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersInAPStr string, nbUsersInZoneStr string) {

	mutex.Lock()

	//check all that applies
	for subsId, zoneStatus := range zoneStatusSubscriptionMap {
		if zoneStatus == nil {
@@ -338,6 +352,7 @@ func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersIn
					subsIdStr := strconv.Itoa(subsId)
					jsonInfo, _ := rc.JSONGetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".")
					if jsonInfo == "" {
						mutex.Unlock()
						return
					}

@@ -363,10 +378,13 @@ func checkNotificationRegisteredZoneStatus(zoneId string, apId string, nbUsersIn
			}
		}
	}
	mutex.Unlock()
}

func checkNotificationRegisteredUsers(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {

	mutex.Lock()

	//check all that applies
	for subsId, value := range userSubscriptionMap {
		if value == userId {
@@ -374,6 +392,7 @@ func checkNotificationRegisteredUsers(oldZoneId string, newZoneId string, oldApI
			subsIdStr := strconv.Itoa(subsId)
			jsonInfo, _ := rc.JSONGetEntry(baseKey+typeUserSubscription+":"+subsIdStr, ".")
			if jsonInfo == "" {
				mutex.Unlock()
				return
			}

@@ -422,6 +441,7 @@ func checkNotificationRegisteredUsers(oldZoneId string, newZoneId string, oldApI
			}
		}
	}
	mutex.Unlock()
}

func sendNotification(notifyUrl string, ctx context.Context, subscriptionId string, notification clientNotifOMA.TrackingNotification) {
@@ -472,6 +492,8 @@ func sendStatusNotification(notifyUrl string, ctx context.Context, subscriptionI

func checkNotificationRegisteredZones(oldZoneId string, newZoneId string, oldApId string, newApId string, userId string) {

	mutex.Lock()

	//check all that applies
	for subsId, value := range zonalSubscriptionMap {

@@ -550,6 +572,7 @@ func checkNotificationRegisteredZones(oldZoneId string, newZoneId string, oldApI
			}
		}
	}
	mutex.Unlock()
}

func usersGet(w http.ResponseWriter, r *http.Request) {
@@ -1304,6 +1327,8 @@ func cleanUp() {
	nextUserSubscriptionIdAvailable = 1
	nextZoneStatusSubscriptionIdAvailable = 1

	mutex.Lock()

	zonalSubscriptionEnteringMap = map[int]string{}
	zonalSubscriptionLeavingMap = map[int]string{}
	zonalSubscriptionTransferringMap = map[int]string{}
@@ -1316,6 +1341,8 @@ func cleanUp() {

	zoneStatusSubscriptionMap = map[int]*ZoneStatusCheck{}

	mutex.Unlock()

	updateStoreName("")
}

@@ -1440,6 +1467,7 @@ func zoneStatusReInit() {
	_ = rc.ForEachJSONEntry(keyName, populateZoneStatusList, &zoneList)

	maxZoneStatusSubscriptionId := 0
	mutex.Lock()
	for _, zone := range zoneList.ZoneStatusSubscription {
		resourceUrl := strings.Split(zone.ResourceURL, "/")
		subscriptionId, err := strconv.Atoi(resourceUrl[len(resourceUrl)-1])
@@ -1471,6 +1499,7 @@ func zoneStatusReInit() {
			zoneStatusSubscriptionMap[subscriptionId] = &zoneStatus
		}
	}
	mutex.Unlock()
	nextZoneStatusSubscriptionIdAvailable = maxZoneStatusSubscriptionId + 1
}

@@ -1482,6 +1511,7 @@ func zonalTrafficReInit() {
	_ = rc.ForEachJSONEntry(keyName, populateZonalTrafficList, &zoneList)

	maxZonalSubscriptionId := 0
	mutex.Lock()
	for _, zone := range zoneList.ZonalTrafficSubscription {
		resourceUrl := strings.Split(zone.ResourceURL, "/")
		subscriptionId, err := strconv.Atoi(resourceUrl[len(resourceUrl)-1])
@@ -1506,6 +1536,7 @@ func zonalTrafficReInit() {
			zonalSubscriptionMap[subscriptionId] = zone.ZoneId
		}
	}
	mutex.Unlock()
	nextZonalSubscriptionIdAvailable = maxZonalSubscriptionId + 1
}

@@ -1517,6 +1548,7 @@ func userTrackingReInit() {
	_ = rc.ForEachJSONEntry(keyName, populateUserTrackingList, &userList)

	maxUserSubscriptionId := 0
	mutex.Lock()
	for _, user := range userList.UserTrackingSubscription {
		resourceUrl := strings.Split(user.ResourceURL, "/")
		subscriptionId, err := strconv.Atoi(resourceUrl[len(resourceUrl)-1])
@@ -1541,5 +1573,6 @@ func userTrackingReInit() {
			userSubscriptionMap[subscriptionId] = user.Address
		}
	}
	mutex.Unlock()
	nextUserSubscriptionIdAvailable = maxUserSubscriptionId + 1
}
+60 −10
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import (
	"os"
	"strconv"
	"strings"
	"sync"
	"time"

	sbi "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-rnis/sbi"
@@ -68,6 +69,7 @@ var hostUrl *url.URL
var sandboxName string
var basePath string
var baseKey string
var mutex sync.Mutex

var expiryTicker *time.Ticker

@@ -307,6 +309,7 @@ func createClient(notifyPath string) (*clientNotif.APIClient, error) {
func checkForExpiredSubscriptions() {

	nowTime := int(time.Now().Unix())
	mutex.Lock()
	for expiryTime, subsIndexList := range subscriptionExpiryMap {
		if expiryTime <= nowTime {
			subscriptionExpiryMap[expiryTime] = nil
@@ -337,7 +340,7 @@ func checkForExpiredSubscriptions() {
			}
		}
	}

	mutex.Unlock()
}

func repopulateCcSubscriptionMap(key string, jsonInfo string, userData interface{}) error {
@@ -354,12 +357,14 @@ func repopulateCcSubscriptionMap(key string, jsonInfo string, userData interface
	subsIdStr := selfUrl[len(selfUrl)-1]
	subsId, _ := strconv.Atoi(subsIdStr)

	mutex.Lock()
	ccSubscriptionMap[subsId] = &subscription
	if subscription.ExpiryDeadline != nil {
		intList := subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)]
		intList = append(intList, subsId)
		subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)] = intList
	}
	mutex.Unlock()

	//reinitialisation of next available Id for future subscription request
	if subsId >= nextSubscriptionIdAvailable {
@@ -383,12 +388,14 @@ func repopulateReSubscriptionMap(key string, jsonInfo string, userData interface
	subsIdStr := selfUrl[len(selfUrl)-1]
	subsId, _ := strconv.Atoi(subsIdStr)

	mutex.Lock()
	reSubscriptionMap[subsId] = &subscription
	if subscription.ExpiryDeadline != nil {
		intList := subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)]
		intList = append(intList, subsId)
		subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)] = intList
	}
	mutex.Unlock()

	//reinitialisation of next available Id for future subscription request
	if subsId >= nextSubscriptionIdAvailable {
@@ -412,12 +419,14 @@ func repopulateRrSubscriptionMap(key string, jsonInfo string, userData interface
	subsIdStr := selfUrl[len(selfUrl)-1]
	subsId, _ := strconv.Atoi(subsIdStr)

	mutex.Lock()
	rrSubscriptionMap[subsId] = &subscription
	if subscription.ExpiryDeadline != nil {
		intList := subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)]
		intList = append(intList, subsId)
		subscriptionExpiryMap[int(subscription.ExpiryDeadline.Seconds)] = intList
	}
	mutex.Unlock()

	//reinitialisation of next available Id for future subscription request
	if subsId >= nextSubscriptionIdAvailable {
@@ -434,6 +443,8 @@ func checkCcNotificationRegisteredSubscriptions(appId string, assocId *Associate
		return
	}

	mutex.Lock()

	//check all that applies
	for subsId, sub := range ccSubscriptionMap {

@@ -490,6 +501,7 @@ func checkCcNotificationRegisteredSubscriptions(appId string, assocId *Associate
				subsIdStr := strconv.Itoa(subsId)
				jsonInfo, _ := rc.JSONGetEntry(baseKey+cellChangeSubscriptionType+":"+subsIdStr, ".")
				if jsonInfo == "" {
					mutex.Unlock()
					return
				}

@@ -541,6 +553,7 @@ func checkCcNotificationRegisteredSubscriptions(appId string, assocId *Associate
			}
		}
	}
	mutex.Unlock()
}

func checkReNotificationRegisteredSubscriptions(appId string, assocId *AssociateId, newPlmn *Plmn, oldPlmn *Plmn, qci int32, newCellId string, oldCellId string, erabId int32) {
@@ -550,6 +563,8 @@ func checkReNotificationRegisteredSubscriptions(appId string, assocId *Associate
		return
	}

	mutex.Lock()

	//check all that applies
	for subsId, sub := range reSubscriptionMap {

@@ -600,6 +615,7 @@ func checkReNotificationRegisteredSubscriptions(appId string, assocId *Associate
				subsIdStr := strconv.Itoa(subsId)
				jsonInfo, _ := rc.JSONGetEntry(baseKey+rabEstSubscriptionType+":"+subsIdStr, ".")
				if jsonInfo == "" {
					mutex.Unlock()
					return
				}

@@ -638,6 +654,7 @@ func checkReNotificationRegisteredSubscriptions(appId string, assocId *Associate
			}
		}
	}
	mutex.Unlock()
}

func checkRrNotificationRegisteredSubscriptions(appId string, assocId *AssociateId, newPlmn *Plmn, oldPlmn *Plmn, qci int32, newCellId string, oldCellId string, erabId int32) {
@@ -647,6 +664,8 @@ func checkRrNotificationRegisteredSubscriptions(appId string, assocId *Associate
		return
	}

	mutex.Lock()

	//check all that applies
	for subsId, sub := range rrSubscriptionMap {

@@ -697,6 +716,7 @@ func checkRrNotificationRegisteredSubscriptions(appId string, assocId *Associate
				subsIdStr := strconv.Itoa(subsId)
				jsonInfo, _ := rc.JSONGetEntry(baseKey+rabRelSubscriptionType+":"+subsIdStr, ".")
				if jsonInfo == "" {
					mutex.Unlock()
					return
				}

@@ -733,6 +753,7 @@ func checkRrNotificationRegisteredSubscriptions(appId string, assocId *Associate
			}
		}
	}
	mutex.Unlock()
}

func sendCcNotification(notifyUrl string, ctx context.Context, subscriptionId string, notification clientNotif.CellChangeNotification) {
@@ -866,34 +887,47 @@ func cellChangeSubscriptionsGET(w http.ResponseWriter, r *http.Request) {
}

func isSubscriptionIdRegisteredCc(subsIdStr string) bool {
	var returnVal bool
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	if ccSubscriptionMap[subsId] != nil {
		return true
		returnVal = true
	} else {
		return false
		returnVal = false
	}
	mutex.Unlock()
	return returnVal
}

func isSubscriptionIdRegisteredRe(subsIdStr string) bool {
	subsId, _ := strconv.Atoi(subsIdStr)
	var returnVal bool
	mutex.Lock()
	if reSubscriptionMap[subsId] != nil {
		return true
		returnVal = true
	} else {
		return false
		returnVal = false
	}
	mutex.Unlock()
	return returnVal
}

func isSubscriptionIdRegisteredRr(subsIdStr string) bool {
	subsId, _ := strconv.Atoi(subsIdStr)
	var returnVal bool
	mutex.Lock()
	if rrSubscriptionMap[subsId] != nil {
		return true
		returnVal = true
	} else {
		return false
		returnVal = false
	}
	mutex.Unlock()
	return returnVal
}

func registerCc(cellChangeSubscription *CellChangeSubscription, subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	ccSubscriptionMap[subsId] = cellChangeSubscription
	if cellChangeSubscription.ExpiryDeadline != nil {
		//get current list of subscription meant to expire at this time
@@ -901,12 +935,13 @@ func registerCc(cellChangeSubscription *CellChangeSubscription, subsIdStr string
		intList = append(intList, subsId)
		subscriptionExpiryMap[int(cellChangeSubscription.ExpiryDeadline.Seconds)] = intList
	}

	mutex.Unlock()
	log.Info("New registration: ", subsId, " type: ", cellChangeSubscriptionType)
}

func registerRe(rabEstSubscription *RabEstSubscription, subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	reSubscriptionMap[subsId] = rabEstSubscription
	if rabEstSubscription.ExpiryDeadline != nil {
		//get current list of subscription meant to expire at this time
@@ -914,12 +949,13 @@ func registerRe(rabEstSubscription *RabEstSubscription, subsIdStr string) {
		intList = append(intList, subsId)
		subscriptionExpiryMap[int(rabEstSubscription.ExpiryDeadline.Seconds)] = intList
	}

	mutex.Unlock()
	log.Info("New registration: ", subsId, " type: ", rabEstSubscriptionType)
}

func registerRr(rabRelSubscription *RabRelSubscription, subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	rrSubscriptionMap[subsId] = rabRelSubscription
	if rabRelSubscription.ExpiryDeadline != nil {
		//get current list of subscription meant to expire at this time
@@ -927,25 +963,31 @@ func registerRr(rabRelSubscription *RabRelSubscription, subsIdStr string) {
		intList = append(intList, subsId)
		subscriptionExpiryMap[int(rabRelSubscription.ExpiryDeadline.Seconds)] = intList
	}

	mutex.Unlock()
	log.Info("New registration: ", subsId, " type: ", rabRelSubscriptionType)
}

func deregisterCc(subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	ccSubscriptionMap[subsId] = nil
	mutex.Unlock()
	log.Info("Deregistration: ", subsId, " type: ", cellChangeSubscriptionType)
}

func deregisterRe(subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	reSubscriptionMap[subsId] = nil
	mutex.Unlock()
	log.Info("Deregistration: ", subsId, " type: ", rabEstSubscriptionType)
}

func deregisterRr(subsIdStr string) {
	subsId, _ := strconv.Atoi(subsIdStr)
	mutex.Lock()
	rrSubscriptionMap[subsId] = nil
	mutex.Unlock()
	log.Info("Deregistration: ", subsId, " type: ", rabRelSubscriptionType)
}

@@ -1576,6 +1618,7 @@ func createSubscriptionLinkList(subType string) *SubscriptionLinkList {

	if subType == "" || subType == cellChangeSubscriptionType {
		//loop through cell_change map
		mutex.Lock()
		for _, ccSubscription := range ccSubscriptionMap {
			if ccSubscription != nil {
				var subscription Subscription
@@ -1585,9 +1628,11 @@ func createSubscriptionLinkList(subType string) *SubscriptionLinkList {
				subscriptionLinkList.Subscription = append(subscriptionLinkList.Subscription, subscription)
			}
		}
		mutex.Unlock()
	}
	if subType == "" || subType == rabEstSubscriptionType {
		//loop through cell_change map
		mutex.Lock()
		for _, reSubscription := range reSubscriptionMap {
			if reSubscription != nil {
				var subscription Subscription
@@ -1597,9 +1642,11 @@ func createSubscriptionLinkList(subType string) *SubscriptionLinkList {
				subscriptionLinkList.Subscription = append(subscriptionLinkList.Subscription, subscription)
			}
		}
		mutex.Unlock()
	}
	if subType == "" || subType == rabRelSubscriptionType {
		//loop through cell_change map
		mutex.Lock()
		for _, rrSubscription := range rrSubscriptionMap {
			if rrSubscription != nil {
				var subscription Subscription
@@ -1609,6 +1656,7 @@ func createSubscriptionLinkList(subType string) *SubscriptionLinkList {
				subscriptionLinkList.Subscription = append(subscriptionLinkList.Subscription, subscription)
			}
		}
		mutex.Unlock()
	}

	//no other maps to go through
@@ -1698,9 +1746,11 @@ func cleanUp() {
	nextSubscriptionIdAvailable = 1
	nextAvailableErabId = 1

	mutex.Lock()
	ccSubscriptionMap = map[int]*CellChangeSubscription{}
	reSubscriptionMap = map[int]*RabEstSubscription{}
	rrSubscriptionMap = map[int]*RabRelSubscription{}
	mutex.Unlock()

	subscriptionExpiryMap = map[int][]int{}
	updateStoreName("")