Developing lightweight computation at the DSG edge

Commit 73e9cd34 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[assign] Use antidote to read the devices list


Signed-off-by: default avatarRoger Pueyo Centelles <roger.pueyo@guifi.net>
parent 757b0113
......@@ -14,6 +14,7 @@ import (
"time"
"github.com/golang/glog"
"lightkone.guifi.net/uc-monitor-go-test/apidote"
"lightkone.guifi.net/uc-monitor-go-test/cnml"
)
......@@ -72,18 +73,16 @@ var globalAssignCheckInterval = 5 //60
var globalAssignCheckIntervalHelp = "Interval (in seconds) in which to check for changes in the global devices assignation list"
//Global variables
var allDevs []cnml.DeviceAddresses //The global list with all the fetched devices and their IPv4 addresses
var allDevs []cnml.DeviceIpv4Adddresses //The global list with all the fetched devices and their IPv4 addresses
var allDevsChecksum string //The checksum of the last global devices list and IPv4 addresses fetched
var localAssign []cnml.DeviceAddresses //The list of locally assigned devices and their IPv4 addresses
var localAssign []cnml.DeviceIpv4Adddresses //The list of locally assigned devices and their IPv4 addresses
var localAssignChecksum string //The checksum of the local assigned devices list
var globalAssign []cnml.DeviceMonitorAssignation //The global assignation list of devices<=>monitors
var globalAssignChecksums []cnml.AssignChecksum //The checksum of the foreign assigned devices list
func main() {
//Initialize the service (read the configuration flags, etc.)
initialize()
}
func initialize() {
......@@ -139,7 +138,9 @@ func initialize() {
}
//Import the devices and IPv4 addresses list from the JSON file, and the checksum
allDevs = readDevsaddsFile(fmt.Sprintf("%s/%s", tmpDir, devsFile), ERROR)
//allDevs = readDevsaddsFile(fmt.Sprintf("%s/%s", tmpDir, devsFile), ERROR)
allDevs = apidote.ReadDevicesFromAntidote()
setCurrentAllDevsChecksum()
//Import the global devices assignation lists
......@@ -175,8 +176,8 @@ func initializeTimers() {
}
//Read a devices and IPv4 addresses list from a JSON file
func readDevsaddsFile(filename string, errLevel int) []cnml.DeviceAddresses {
var newdevs []cnml.DeviceAddresses
func readDevsaddsFile(filename string, errLevel int) []cnml.DeviceIpv4Adddresses {
var newdevs []cnml.DeviceIpv4Adddresses
jdevsaddrsFile, err := os.Open(filename)
errCheck(err, errLevel, fmt.Sprintf("Error opening file %s", filename))
......@@ -194,7 +195,7 @@ func readDevsaddsFile(filename string, errLevel int) []cnml.DeviceAddresses {
//Unmarshal the JSON objects to DeviceAddresses objects
for _, v := range lines {
var devaddr cnml.DeviceAddresses
var devaddr cnml.DeviceIpv4Adddresses
err := json.Unmarshal([]byte(v), &devaddr)
errCheck(err, INFO, "TODO")
newdevs = append(newdevs, devaddr)
......@@ -204,7 +205,7 @@ func readDevsaddsFile(filename string, errLevel int) []cnml.DeviceAddresses {
return newdevs
}
func updateDevsaddsFile(filename string, currdevsadds []cnml.DeviceAddresses) []cnml.DeviceAddresses {
func updateDevsaddsFile(filename string, currdevsadds []cnml.DeviceIpv4Adddresses) []cnml.DeviceIpv4Adddresses {
fdevs := readDevsaddsFile(filename, ERROR)
......@@ -259,7 +260,8 @@ func checkAllDevsChecksum() {
if allDevsChecksum != readChecksumFile(fmt.Sprintf("%s/%s", tmpDir, devsChecksumFile), ERROR) {
fmt.Println("SHA256 has changed since last check")
setCurrentAllDevsChecksum()
allDevs = readDevsaddsFile(fmt.Sprintf("%s/%s", tmpDir, devsFile), ERROR)
//allDevs = readDevsaddsFile(fmt.Sprintf("%s/%s", tmpDir, devsFile), ERROR)
allDevs = apidote.ReadDevicesFromAntidote()
return
}
fmt.Println("SHA256 has not changed since last check")
......@@ -390,7 +392,7 @@ func reassignDevs() {
fmt.Println(len(localAssign), "devices currently assigned (maximum:", maxDevs, "devices)")
assgnchanges := false
var deldevs []cnml.DeviceAddresses
var deldevs []cnml.DeviceIpv4Adddresses
newassigndevs := localAssign
// Check if all the devices assigned are still in the global list
......@@ -419,7 +421,7 @@ func reassignDevs() {
fmt.Println("Assigning", toassign, "new devices")
//Create a list with all the unassigned devices
var unassigned []cnml.DeviceAddresses
var unassigned []cnml.DeviceIpv4Adddresses
for _, v := range allDevs {
if !isDeviceInSlice(v, localAssign) {
unassigned = append(unassigned, v)
......@@ -427,14 +429,14 @@ func reassignDevs() {
}
//Create a list with all the unassigned devices that are assigned via web to this monitor
var assignweb []cnml.DeviceAddresses
for _, v := range unassigned {
if fmt.Sprint(v.GraphServer) == ID {
assignweb = append(assignweb, v)
} else {
}
}
var assignweb []cnml.DeviceIpv4Adddresses
// for _, v := range unassigned {
// // if fmt.Sprint(v.GraphServer) == ID {
// // assignweb = append(assignweb, v)
// // } else {
// // }
//
// }
fmt.Println(len(unassigned), "devices unassigned, of which", len(assignweb), "are assigned via web")
......@@ -478,7 +480,7 @@ func reassignDevs() {
}
}
func posDeviceInSlice(device cnml.DeviceAddresses, slice []cnml.DeviceAddresses) int {
func posDeviceInSlice(device cnml.DeviceIpv4Adddresses, slice []cnml.DeviceIpv4Adddresses) int {
for k, v := range slice {
if device.ID == v.ID {
return k
......@@ -487,7 +489,7 @@ func posDeviceInSlice(device cnml.DeviceAddresses, slice []cnml.DeviceAddresses)
return -1
}
func isDeviceInSlice(device cnml.DeviceAddresses, slice []cnml.DeviceAddresses) bool {
func isDeviceInSlice(device cnml.DeviceIpv4Adddresses, slice []cnml.DeviceIpv4Adddresses) bool {
if posDeviceInSlice(device, slice) >= 0 {
return true
}
......@@ -527,7 +529,7 @@ func isMonitorInSlice(id string, slice []cnml.AssignChecksum) bool {
return false
}
func rmDeviceFromSlice(device cnml.DeviceAddresses, slice []cnml.DeviceAddresses) []cnml.DeviceAddresses {
func rmDeviceFromSlice(device cnml.DeviceIpv4Adddresses, slice []cnml.DeviceIpv4Adddresses) []cnml.DeviceIpv4Adddresses {
if isDeviceInSlice(device, slice) {
pos := posDeviceInSlice(device, slice)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment