Developing lightweight computation at the DSG edge

goclidote.go 4.84 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
package goclidote

import (
	"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
114
func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []string {
115
116
117

	var items []string

118
119
120
	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
	errCheck(err, "")
	defer client.Close()
121

122
123
	tx, err := client.StartTransaction()
	errCheck(err, "")
124

125
126
127
128
129
130
131
132
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(setName)

	setVal, err := bucket.ReadSet(tx, key)
	errCheck(err, "")

	for _, v := range setVal {
		items = append(items, string(v))
133
134
135
136
137
138
	}

	return items
}

// AntidoteRemoveItemFromSetInBucket removes an item from a set in a bucket
139
140
141
func AntidoteRemoveItemFromSetInBucket(bucketName string, setName string, itemValue string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
142
	errCheck(err, "")
143
	defer client.Close()
144

145
146
	tx, err := client.StartTransaction()
	errCheck(err, "")
147

148
149
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(setName)
150

151
152
153
154
155
156
157
158
159
160
161
	err = bucket.Update(tx, antidoteclient.SetRemove(key, []byte(itemValue)))
	errCheck(err, "")

	setVal, err := bucket.ReadSet(tx, key)
	errCheck(err, "")

	itemNotInSet := true

	for _, v := range setVal {
		if string(v) == itemValue {
			itemNotInSet = false
162
163
164
		}
	}

165
	return itemNotInSet
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
}

// 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)
	}
}