/
clientLab3.go
169 lines (136 loc) · 4.62 KB
/
clientLab3.go
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
package main
import (
"encoding/json"
"fmt"
"hash/crc32"
"io/ioutil"
"net/http"
"os"
"sort"
"strconv"
//"strings"
)
//Consistency Hashing Uses CRC32
var crc32q *crc32.Table
var ConsistencyCircle map[uint32]string
var serversList []string
//Key Value Store
type KeyValueStore struct {
Key int64 `json:"key"`
Value string `json:"value"`
}
type KeysStructureStore struct {
Keys []KeyValueStore
}
func main() {
ConsistencyCircle = make(map[uint32]string)
//All servers
serversList = []string{
"http://localhost:3000/",
"http://localhost:3001/",
"http://localhost:3002/"}
crc32q = crc32.MakeTable(0xD5828281)
//Server selection based
for i := 0; i < len(serversList); i++ {
value := crc32.Checksum([]byte(serversList[i]), crc32q)
ConsistencyCircle[value] = serversList[i]
}
cmdArgs := os.Args[1:]
//Client function
Client(cmdArgs)
}
func Client(CmdArgs []string) {
var Result interface{}
if len(CmdArgs) == 3 {
httpMethod := CmdArgs[0]
keyVal, _ := strconv.ParseInt(CmdArgs[1], 10, 64)
serverHostName := get(keyVal, ConsistencyCircle)
keyStr := strconv.Itoa(int(keyVal))
value := CmdArgs[2]
if httpMethod == "PUT" {
client := &http.Client{}
fmt.Println("Server Selected=>",serverHostName)
request, _ := http.NewRequest("PUT", serverHostName+"keys/"+keyStr+"/"+value, nil)
response, err := client.Do(request)
if err != nil {
fmt.Println("Error ")
}
defer response.Body.Close()
fmt.Println(response.StatusCode)
}
} else if len(CmdArgs) == 2 {
httpMethod := CmdArgs[0]
keyVal, _ := strconv.ParseInt(CmdArgs[1], 10, 64)
keyStr := strconv.Itoa(int(keyVal))
serverHostName := get(keyVal, ConsistencyCircle)
if httpMethod == "GET" {
client := &http.Client{}
request, _ := http.NewRequest("GET", serverHostName+"keys/"+keyStr, nil)
response, err := client.Do(request)
if err != nil {
fmt.Println("Error")
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
var Result interface{}
_ = json.Unmarshal(body, &Result)
var keyValue KeyValueStore
keyyy := Result.(map[string]interface{})["key"].(float64)
valuue := Result.(map[string]interface{})["value"].(string)
keyValue.Key = int64(keyyy)
keyValue.Value = valuue
Output, _ := json.Marshal(keyValue)
fmt.Println("\n" + string(Output) + "\n")
} else {
fmt.Println("Invalid HTTP Method")
}
} else if len(CmdArgs) == 0 {
client := &http.Client{}
var AllKey KeysStructureStore
for i := 0; i < len(serversList); i++ {
serverHostName := serversList[i]
request, _ := http.NewRequest("GET", serverHostName+"keys", nil)
response, err := client.Do(request)
if err != nil {
fmt.Println("Error")
}
defer response.Body.Close()
body, _ := ioutil.ReadAll(response.Body)
_ = json.Unmarshal(body, &Result)
if Result != nil {
keyValArr := Result.([]interface{})
for i := 0; i < len(keyValArr); i++ {
var keyValue KeyValueStore
keyyy := keyValArr[i].(map[string]interface{})["key"].(float64)
valuue := keyValArr[i].(map[string]interface{})["value"].(string)
keyValue.Key = int64(keyyy)
keyValue.Value = valuue
AllKey.Keys = append(AllKey.Keys, keyValue)
}
}
}
Output, _ := json.Marshal(AllKey.Keys)
fmt.Println("\n" + string(Output) + "\n")
}
}
func get(key int64, ConsistencyCircle map[uint32]string) string {
keyStr := strconv.Itoa(int(key))
var Allkeys []int
var HashValue uint32
if len(ConsistencyCircle) == 0 {
return ""
} else {
hashfunc := crc32.Checksum([]byte(keyStr), crc32q)
for k := range ConsistencyCircle {
Allkeys = append(Allkeys, int(k))
}
sort.Ints(Allkeys)
for i := 0; i < len(Allkeys); i++ {
if Allkeys[i] >= int(hashfunc) {
HashValue = uint32(Allkeys[i])
break
}
}
return ConsistencyCircle[HashValue]
}
}