Developing lightweight computation at the DSG edge

Commit 1d248756 authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[goclidote] Port AntidoteAddItemToSetInBucket


Signed-off-by: Roger Pueyo Centelles's avatarRoger Pueyo Centelles <rpueyo@ac.upc.edu>
parent eccb384a
package goclidote
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"uc-monitor-go-test/cnml"
"github.com/mweberUKL/go-antidote"
)
// DatabaseHost defines the host of the AntidoteDB instance to connect to is running
var DatabaseHost = "localhost"
// DatabasePort defines the port of the host where the AntidoteDB instance to connect to is running
var DatabasePort = 8087
var api_host_port = "localhost:3000"
// AntidoteAddItemToSetInBucket save an item to a set in a bucket
func AntidoteAddItemToSetInBucket(bucketName string, setName string, itemValue string) bool {
client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
errCheck(err, "")
defer client.Close()
tx, err := client.StartTransaction()
errCheck(err, "")
bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
key := antidoteclient.Key(setName)
err = bucket.Update(tx, antidoteclient.SetAdd(key, []byte(itemValue)))
errCheck(err, "")
setVal, err := bucket.ReadSet(tx, key)
errCheck(err, "")
for _, v := range setVal {
if string(v) == itemValue {
return true
}
}
return false
}
// AntidoteReadRegisterInBucket read a LWWW register in a bucket
func AntidoteReadRegisterInBucket(bucket string, register string) string {
request := fmt.Sprintf("http://%s/register/read/%s/%s", api_host_port, bucket, register)
response, err := http.Get(request)
errCheck(err, "")
var value string
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err, "")
value = fmt.Sprintf("%s", bodyBytes)
errCheck(err, "")
}
return value
}
// AntidoteSetRegisterInBucket set a LWWW register in a bucket
func AntidoteSetRegisterInBucket(bucket string, register string, value string) bool {
request := fmt.Sprintf("http://%s/register/set/%s/%s/%s", api_host_port, bucket, register, value)
response, err := http.Get(request)
errCheck(err, "")
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err, "")
restReply := string(bodyBytes)
if restReply == "ok" {
return true
}
}
return false
}
// AntidoteRemoveRegisterInBucket remove (set null) a LWWW register in a bucket
func AntidoteRemoveRegisterInBucket(bucket string, register string) bool {
request := fmt.Sprintf("http://%s/register/set/%s/%s/\\'\\'", api_host_port, bucket, register)
response, err := http.Get(request)
errCheck(err, "")
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err, "")
restReply := string(bodyBytes)
if restReply == "ok" {
return true
}
}
return false
}
// AntidoteReadItemsFromSetInBucket read items from a set in a bucket
func AntidoteReadItemsFromSetInBucket(bucket string, set string) []string {
request := fmt.Sprintf("http://%s/set/read/%s/%s", api_host_port, bucket, set)
response, err := http.Get(request)
errCheck(err, "")
var items []string
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err, "")
err = json.Unmarshal(bodyBytes, &items)
errCheck(err, "")
}
return items
}
// AntidoteRemoveItemFromSetInBucket removes an item from a set in a bucket
func AntidoteRemoveItemFromSetInBucket(bucket string, set string, item string) bool {
request := fmt.Sprintf("http://%s/set/remove/%s/%s/%s", api_host_port, bucket, set, item)
response, err := http.Get(request)
errCheck(err, "")
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err, "")
restReply := string(bodyBytes)
if restReply == "ok" {
return true
}
}
return false
}
// ReadDevicesFromAntidote read all the devices from Antidote using João's REST server
func ReadDevicesFromAntidote() []cnml.DeviceIpv4sGraphserver {
var devices []cnml.DeviceIpv4sGraphserver
for _, v := range AntidoteReadItemsFromSetInBucket("guifi", "devices") {
thisDevID, err := strconv.Atoi(v)
errCheck(err, "")
var thisDev cnml.DeviceIpv4sGraphserver
thisDev.ID = thisDevID
thisDev.GraphServer, err = strconv.Atoi(AntidoteReadRegisterInBucket(fmt.Sprintf("device-%d", thisDevID), "graphserver"))
errCheck(err, "")
thisDev.Addresses = AntidoteReadItemsFromSetInBucket(fmt.Sprintf("device-%d", thisDevID), "ipv4s")
devices = append(devices, thisDev)
}
return devices
}
// Panic on error
func errCheck(err error, message string) {
if err != nil {
fmt.Println(message)
panic(err)
}
}
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