Developing lightweight computation at the DSG edge

goclidote.go 5 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
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
51
func AntidoteReadRegisterInBucket(bucketName string, registerName string) string {
52

53
54
55
	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
	errCheck(err, "")
	defer client.Close()
56

57
58
	tx, err := client.StartTransaction()
	errCheck(err, "")
59

60
61
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
62

63
64
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")
65

66
	return string(setVal)
67
68
69
}

// AntidoteSetRegisterInBucket set a LWWW register in a bucket
70
71
72
func AntidoteSetRegisterInBucket(bucketName string, registerName string, itemValue string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
73
	errCheck(err, "")
74
	defer client.Close()
75

76
77
	tx, err := client.StartTransaction()
	errCheck(err, "")
78

79
80
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(registerName)
81

82
83
	err = bucket.Update(tx, antidoteclient.RegPut(key, []byte(itemValue)))
	errCheck(err, "")
84

85
86
87
88
89
	setVal, err := bucket.ReadReg(tx, key)
	errCheck(err, "")

	if string(setVal[0]) == itemValue {
		return true
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
	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
117
func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []string {
118
119
120

	var items []string

121
122
123
	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
	errCheck(err, "")
	defer client.Close()
124

125
126
	tx, err := client.StartTransaction()
	errCheck(err, "")
127

128
129
130
131
132
133
134
135
	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))
136
137
138
139
140
141
	}

	return items
}

// AntidoteRemoveItemFromSetInBucket removes an item from a set in a bucket
142
143
144
func AntidoteRemoveItemFromSetInBucket(bucketName string, setName string, itemValue string) bool {

	client, err := antidoteclient.NewClient(antidoteclient.Host{Name: DatabaseHost, Port: DatabasePort})
145
	errCheck(err, "")
146
	defer client.Close()
147

148
149
	tx, err := client.StartTransaction()
	errCheck(err, "")
150

151
152
	bucket := antidoteclient.Bucket{Bucket: []byte(bucketName)}
	key := antidoteclient.Key(setName)
153

154
155
156
157
158
159
160
161
162
163
164
	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
165
166
167
		}
	}

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

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