Developing lightweight computation at the DSG edge

goclidote.go 4.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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)
	}
}