Developing lightweight computation at the DSG edge

Commit 618d5591 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[monitor-ping] Add program skeleton



The monitor-ping.go program is initialised with a minimal set of flags
and enters a dummy routine every N seconds.
Signed-off-by: default avatarRoger Pueyo Centelles <roger.pueyo@guifi.net>
parent 3cfeac0d
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"time"
"uc-monitor-go-test/cnml"
"github.com/golang/glog"
)
// Define error levels
// FATAL output code
var FATAL = 1
// ERROR output code
var ERROR = 2
// WARNING output code
var WARNING = 3
// INFO output code
var INFO = 4
// DEFAULTFILEMODE for created files
var DEFAULTFILEMODE os.FileMode = 0755
//Default settings and descriptions
var dbHost = "localhost"
var dbHostHelp = "The hostname or IP address where AntidoteDB is running"
var dbPort = 8087
var dbPortHelp = "The TCP port on which AntidoteDB is listening"
// idNum holds the monitor id, in numberic form
var idNum int
// ID represents the ID of the monitor, in alfanumeric form
var ID = "0"
var idHelp = "Numeric ID of this monitoring instance"
var localAssignCheckInterval = 5 //60
var localAssignCheckIntervalHelp = "Interval (in seconds) in which to check for changes in the local devices assignation list"
// Global data structures
// localAssign contains the list of devices assigned to this monitor and their IPv4 addresses
var localAssign []cnml.DeviceMonitorAssignation
func main() {
//Initialize the service (read the configuration flags, etc.)
initialize()
// Handle interrupt signals in order to terminate gracefully
signalChan := make(chan os.Signal, 1)
cleanupDone := make(chan bool)
signal.Notify(signalChan, os.Interrupt)
go func() {
for _ = range signalChan {
fmt.Println("\nReceived an interrupt, quitting...")
cleanupDone <- true
}
}()
<-cleanupDone
fmt.Println("\nBye!")
}
// The initialize() function manages the execution parameters, sets timers, etc.
func initialize() {
fmt.Println("Initializing...")
// Manage command line flags and execution settings
initializeFlags()
// Initialize periodic timers
initializeTimers()
// The application is now ininitialized; entering the loop
fmt.Println("Initialization done. Entering infinite loop...")
}
func initializeTimers() {
// Initialize the local assignation check timer
startLocalAssignTimer()
}
// The initializeFlags function initializes the command line flags
func initializeFlags() {
//Define and parse command line flags
dbHostPtr := flag.String("db_host", dbHost, dbHostHelp)
dbPortPtr := flag.Int("db_port", dbPort, dbPortHelp)
idInt, _ := strconv.ParseInt(ID, 10, 32)
idPtr := flag.Int("id", int(idInt), idHelp)
localAssignCheckIntervalPtr := flag.Int("localAssignCheckInterval", localAssignCheckInterval, localAssignCheckIntervalHelp)
flag.Parse()
//Service id
if *idPtr > 0 {
idNum = *idPtr
ID = fmt.Sprint(*idPtr)
fmt.Println("Using ID", ID)
} else {
panic("The id flag must be an integer greater than zero.")
}
dbHost = *dbHostPtr
dbPort = *dbPortPtr
if *localAssignCheckIntervalPtr > 0 {
localAssignCheckInterval = *localAssignCheckIntervalPtr
}
}
func startLocalAssignTimer() {
localAssignTicker := time.NewTicker(time.Duration(localAssignCheckInterval) * time.Second)
go func() {
for range localAssignTicker.C {
fmt.Println("Hi!")
}
}()
}
func errCheck(e error, level int, message string) {
if message == "" {
message = "Unknown error"
}
if e != nil {
switch level {
case FATAL:
glog.Fatalln(message, e)
case ERROR:
glog.Errorln(message, e)
case WARNING:
glog.Warningln(message, e)
default:
glog.Infoln(message, e)
}
}
}
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