Developing lightweight computation at the DSG edge

Commit e10c714e authored by Roger Pueyo Centelles's avatar Roger Pueyo Centelles
Browse files

[fetch] Add antidoteRemoveItemFromSetInBucket function



Add a function to remove an item from a set in a bucket. Also remove
http requests from previous antidote-related functions and use those
recently added instead.
Signed-off-by: default avatarRoger Pueyo Centelles <roger.pueyo@guifi.net>
parent 41d2e679
......@@ -537,56 +537,19 @@ func getZonesInZoneExc(zone Zone) []Zone {
// Remove a device by its ID from Antidote using João's REST server
func removeDeviceFromAntidote(device DeviceIpv4Adddresses) bool {
request := fmt.Sprintf("http://localhost:3000/set/remove/guifi/devices/%d", device.ID)
//fmt.Println("Saving device", ID, "to AntidoteDB")
response, err := http.Get(request)
errCheck(err)
//fmt.Println(response)
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err)
restReply := string(bodyBytes)
if restReply == "ok" {
//fmt.Println("Success!")
}
return true
}
return false
return antidoteRemoveItemFromSetInBucket("guifi", "devices", fmt.Sprintf("%d", device.ID))
}
// Read all the devices from Antidote using João's REST server
func readDevicesFromAntidote() []DeviceIpv4Adddresses {
var devices []DeviceIpv4Adddresses
request := fmt.Sprintf("http://localhost:3000/set/read/guifi/devices")
response, err := http.Get(request)
errCheck(err)
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err)
var stringRestReply []string
err = json.Unmarshal(bodyBytes, &stringRestReply)
for _, v := range antidoteReadItemsFromSetInBucket("guifi", "devices") {
thisDevID, err := strconv.Atoi(v)
errCheck(err)
for _, v := range stringRestReply {
thisDevID, err := strconv.Atoi(v)
errCheck(err)
var thisDev DeviceIpv4Adddresses
thisDev.ID = thisDevID
devices = append(devices, thisDev)
//println(v, thisDevID)
}
var thisDev DeviceIpv4Adddresses
thisDev.ID = thisDevID
devices = append(devices, thisDev)
}
return devices
......@@ -594,12 +557,39 @@ func readDevicesFromAntidote() []DeviceIpv4Adddresses {
// Save a device ID to Antidote using João's REST server
func saveDeviceToAntidote(ID int) bool {
request := fmt.Sprintf("http://localhost:3000/set/add/guifi/devices/%d", ID)
//fmt.Println("Saving device", ID, "to AntidoteDB")
return antidoteAddItemToSetInBucket("guifi", "devices", fmt.Sprintf("%d", ID))
}
// // Save a device ID and an IPv4 address to Antidote using João's REST server
// func saveDeviceIpv4ToAntidote(ID int, Ipv4 string) bool {
// request := fmt.Sprintf("http://localhost:3000/set/add/guifi/device-%d/%s", ID, Ipv4)
// //fmt.Println("Saving address", Ipv4, "in device", ID, "to AntidoteDB")
// response, err := http.Get(request)
// errCheck(err)
//
// //fmt.Println(response)
// defer response.Body.Close()
//
// if response.StatusCode == http.StatusOK {
// bodyBytes, err := ioutil.ReadAll(response.Body)
// errCheck(err)
// restReply := string(bodyBytes)
//
// if restReply == "ok" {
// //fmt.Println("Success!")
// }
// return true
// }
//
// return false
// }
// Antidote: save an item to a set in a bucket
func antidoteAddItemToSetInBucket(bucket string, set string, item string) bool {
request := fmt.Sprintf("http://localhost:3000/set/add/%s/%s/%s", bucket, set, item)
response, err := http.Get(request)
errCheck(err)
//fmt.Println(response)
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
......@@ -608,41 +598,37 @@ func saveDeviceToAntidote(ID int) bool {
restReply := string(bodyBytes)
if restReply == "ok" {
//fmt.Println("Success!")
return true
}
return true
}
}
return false
}
// Save a device ID and an IPv4 address to Antidote using João's REST server
func saveDeviceIpv4ToAntidote(ID int, Ipv4 string) bool {
request := fmt.Sprintf("http://localhost:3000/set/add/guifi/device-%d/%s", ID, Ipv4)
//fmt.Println("Saving address", Ipv4, "in device", ID, "to AntidoteDB")
// Antidote: read items from a set in a bucket
func antidoteReadItemsFromSetInBucket(bucket string, set string) []string {
request := fmt.Sprintf("http://localhost:3000/set/read/%s/%s", bucket, set)
response, err := http.Get(request)
errCheck(err)
//fmt.Println(response)
var items []string
defer response.Body.Close()
if response.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
errCheck(err)
restReply := string(bodyBytes)
if restReply == "ok" {
//fmt.Println("Success!")
}
return true
err = json.Unmarshal(bodyBytes, &items)
errCheck(err)
}
return false
return items
}
// Antidote: save an item to a set in a bucket
func antidoteAddItemToSetInBucket(bucket string, set string, item string) bool {
request := fmt.Sprintf("http://localhost:3000/set/add/%s/%s/%s", bucket, set, item)
// Antidote: remove an item from a set in a bucket
func antidoteRemoveItemFromSetInBucket(bucket string, set string, item string) bool {
request := fmt.Sprintf("http://localhost:3000/set/remove/%s/%s/%s", bucket, set, item)
response, err := http.Get(request)
errCheck(err)
......@@ -654,31 +640,11 @@ func antidoteAddItemToSetInBucket(bucket string, set string, item string) bool {
restReply := string(bodyBytes)
if restReply == "ok" {
return true
}
return true
}
return false
}
// Antidote: read items from a set in a bucket
func antidoteReadItemsFromSetInBucket(bucket string, set string) []string {
request := fmt.Sprintf("http://localhost:3000/set/read/%s/%s", 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
return false
}
// Check if an int item is in an ints array
......
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