Developing lightweight computation at the DSG edge

goclidote.go 4.85 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
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
}

// 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
112
func AntidoteReadItemsFromSetInBucket(bucketName string, setName string) []string {
113
114
115

	var items []string

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

120
121
	tx, err := client.StartTransaction()
	errCheck(err, "")
122

123
124
125
126
127
128
129
130
	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))
131
132
133
134
135
136
	}

	return items
}

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

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

143
144
	tx, err := client.StartTransaction()
	errCheck(err, "")
145

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

149
150
151
152
153
154
155
156
157
158
159
	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
160
161
162
		}
	}

163
	return itemNotInSet
164
165
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
}

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