-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.go
294 lines (254 loc) · 6.46 KB
/
frontend.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"encoding/gob"
"flag"
"fmt"
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/kataras/iris/v12"
)
//Struct to store a person's name and age
type Person struct {
Name string
Age int
}
//Struct to store client Message info
type Message struct {
Type string
Receiver string
SenderPortNo string
Pmessage PaxosMessage
Person Person
Id int // An Id of -1 means send all students
}
// Struct to store proposal value info
type ProposalVal struct {
Command string
Id int
Person Person
}
// Struct to store paxos message info
type PaxosMessage struct {
Type string
Id string
Val ProposalVal
UniqId int
}
/*
Creates the html text for the home page based on students
students: slice of Person containing all students
return: html text for homepage
*/
func homepage(students map[int]Person) string {
res := "<h1>Students in Jeff Epstein's Class</h1>\n"
for id, person := range students {
res += fmt.Sprintf("\t<br><a href=\"/person/%d\">%s</a>\n", id, person.Name)
}
res += "<br><br><a href=\"/create\"> Create</a>"
return res
}
/*
Requests a heartbeat from a backend and when received, returns that backend's nodeId
backend: portNo for backend
numOfBackends: total number of backends
return: the nodeId of the backend
*/
func checkBackend(backend string, numOfBackends int) int {
conn, err := net.Dial("tcp", backend)
if err != nil {
fmt.Println(fmt.Sprintf("Detected failure on %s", backend))
return numOfBackends + 1
} else {
enc := gob.NewEncoder(conn)
enc.Encode(Message{Type: "Heartbeat"})
var id int
dec := gob.NewDecoder(conn)
dec.Decode(&id)
conn.Close()
return id
}
}
/* Checks connection every 2s and assigns primary, prints error message if down.
backends: list of port numbers for all backends
primary: string to store the portNo of the primary
mutex: mutex for the primary string
*/
func setPrimary(backends []string, primary *string, mutex sync.RWMutex) {
for {
//fmt.Println(backends)
portNos := make(map[int]string)
for _, backend := range backends {
portNos[checkBackend(backend, len(backends))] = backend
}
if _, ok := portNos[len(backends) + 1]; ok {
fmt.Println("")
}
minId := len(backends) + 1
for id, _ := range portNos {
if id < minId {
minId = id
}
}
if minId != len(backends)+1 {
portNo := portNos[minId]
mutex.RLock()
if portNo != *primary {
mutex.RUnlock()
mutex.Lock()
*primary = portNo
mutex.Unlock()
fmt.Println(fmt.Sprintf("\nCurrent primary: %s\n", portNo))
} else {
mutex.RUnlock()
}
}
time.Sleep(3 * time.Second)
}
}
//Main driver function for fronted
func main() {
portNo := flag.String("listen", "8080", "port number")
backends := flag.String("backend", ":8090,:8091,:8092", "primary port numbers")
flag.Parse()
backendsList := strings.Split(*backends, ",")
var mutex sync.RWMutex
var primary *string
dummy := ""
primary = &dummy
go setPrimary(backendsList, primary, mutex)
app := iris.Default()
app.RegisterView(iris.HTML("./files", ".html"))
// Handler for the homeapage
app.Get("/", func(ctx iris.Context) {
mutex.RLock()
conn, err := net.Dial("tcp", *primary)
mutex.RUnlock()
if err != nil {
fmt.Print(err)
}
defer conn.Close()
enc := gob.NewEncoder(conn)
enc.Encode(Message{Type: "Read", Id: -1})
var students map[int]Person
dec := gob.NewDecoder(conn)
dec.Decode(&students)
ctx.HTML(homepage(students))
})
// Handler for the person view page
app.Get("/person/{num}", func(ctx iris.Context) {
i, err := strconv.Atoi(ctx.Params().Get("num"))
if err != nil {
fmt.Println(err)
}
mutex.RLock()
conn, err := net.Dial("tcp", *primary)
mutex.RUnlock()
if err != nil {
fmt.Println(err)
}
defer conn.Close()
enc := gob.NewEncoder(conn)
enc.Encode(Message{Type: "Read", Id: i})
var student []Person
dec := gob.NewDecoder(conn)
dec.Decode(&student)
if len(student) == 0 {
ctx.View("absent.html")
} else {
ctx.ViewData("Name", student[0].Name)
ctx.ViewData("Age", student[0].Age)
ctx.ViewData("i", i)
ctx.View("personView.html")
}
})
//Handler for deleting a person.
app.Get("/person/{num}/delete", func(ctx iris.Context) {
i, err := strconv.Atoi(ctx.Params().Get("num"))
if err != nil {
fmt.Println(err)
}
mutex.RLock()
conn, err := net.Dial("tcp", *primary)
mutex.RUnlock()
if err != nil {
fmt.Println(err)
}
defer conn.Close()
enc := gob.NewEncoder(conn)
enc.Encode(Message{Type: "Delete", Receiver: "Proposer", Id: i})
ctx.Redirect("/")
})
//Handler for editing a person's information.
app.Get("/person/{num}/edit", func(ctx iris.Context) {
i, err := strconv.Atoi(ctx.Params().Get("num"))
if err != nil {
fmt.Println(err)
}
mutex.RLock()
conn, err := net.Dial("tcp", *primary)
mutex.RUnlock()
if err != nil {
fmt.Println(err)
}
defer conn.Close()
enc := gob.NewEncoder(conn)
enc.Encode(Message{Type: "Read", Id: i})
var student []Person
dec := gob.NewDecoder(conn)
dec.Decode(&student)
ctx.ViewData("Name", student[0].Name)
ctx.ViewData("Age", student[0].Age)
ctx.ViewData("i", i)
ctx.View("edit.html")
})
//Handler for executing changes to a person. It redirects back to the homepage.
app.Post("/update/{num}", func(ctx iris.Context) {
i, err := strconv.Atoi(ctx.Params().Get("num"))
if err != nil {
fmt.Println(err)
}
name := ctx.PostValue("name")
age := ctx.PostValue("age")
ageInt, err := strconv.Atoi(age)
if err != nil {
fmt.Println(err)
}
mutex.RLock()
conn, err := net.Dial("tcp", *primary)
mutex.RUnlock()
if err != nil {
fmt.Println(err)
}
defer conn.Close()
enc := gob.NewEncoder(conn)
enc.Encode(Message{Type: "Update", Receiver: "Proposer", Person: Person{Name: name, Age: ageInt}, Id: i})
ctx.Redirect("/")
})
//Handler for creating a person.
app.Get("/create", func(ctx iris.Context) {
ctx.View("create.html")
})
//Handler for applying changes to internal data after creating a person. It redirects to homepage.
app.Post("/created", func(ctx iris.Context) {
name := ctx.PostValue("name")
age := ctx.PostValue("age")
ageInt, err := strconv.Atoi(age)
if err != nil {
fmt.Println(err)
}
mutex.RLock()
conn, err := net.Dial("tcp", *primary)
mutex.RUnlock()
if err != nil {
fmt.Println(err)
}
defer conn.Close()
enc := gob.NewEncoder(conn)
enc.Encode(Message{Type: "Create", Receiver: "Proposer", Person: Person{Name: name, Age: ageInt}})
ctx.Redirect("/")
})
app.Run(iris.Addr(":" + *portNo))
}