-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
288 lines (265 loc) · 6.46 KB
/
server.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
package main
import (
"fmt"
"net"
"os"
_"errors"
"bufio"
"strings"
"time"
_"golang.org/x/sync/semaphore"
"strconv"
)
var SERVER_PORT = 9999
var server_name = ""
type Client struct{
NickName string
num string
requests []string
in_con net.Conn
}
type Objects struct{
hashtable map[string]int
locktable map[string]string
}
var transToclient map[string]string
var clientToTrans map[string]string
var data Data
var transToData = map[string]map[string]string{}
//var readLocks map[string]string
var locks map[string]*Lock
// make objects global
// handle request
func listener(cli *Client, inbox *Box){
reader := bufio.NewReader(cli.in_con)
// create local request buffer
for {
s, err := reader.ReadString('\n')
fmt.Printf("# recieved string %s\n", s)
if err != nil {
fmt.Println("#Error in listening")
fmt.Printf("# %s", err)
return
}
s = strings.TrimSpace(s)
m := Msg{}
m.Parse(s)
m.from = cli
if (m.GetType() != "BEGIN") {
m.transId = clientToTrans[m.from.num]
}
inbox.enqueue(m)
// msg := strings.Split(s, " ")
}
}
func startListening(inbox *Box, port int) {
// fmt.Println("Started Listening on " + port)
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
// handle error
fmt.Printf("# [ERROR] %s", err)
}
c := 0
for {
conn, err := ln.Accept()
if err != nil {
// handle error
fmt.Printf("# [ERROR] %s", err)
}
cli := Client{in_con:conn}
go listener(&cli, inbox)
c++
}
}
func set(m Msg) {
// m.transId = clientToTrans[m.from.num]
if _, ok := locks[m.obj]; !ok {
// defining lock
locks[m.obj] = &Lock{}
locks[m.obj].init()
}
// if already a writer
if locks[m.obj].isWriter(m.from.num) {
// fmt.Println("updating write")
transToData[m.transId][m.obj] = m.val
m.from.SendMsg("OK")
return
}
if ! locks[m.obj].lockWriter(m.from.num) {
m.from.SendMsg("ABORTED")
return
}
// fmt.Println("got write lock")
transToData[m.transId][m.obj] = m.val
// fmt.Println("transid: %s, obj:%s, val:%s",m.transId,m.obj,transToData[m.transId][m.obj])
m.from.SendMsg("OK")
return
}
func get(m Msg) {
// if lock for the object is not defined
if _, ok := locks[m.obj]; !ok {
// defining lock
locks[m.obj] = &Lock{}
locks[m.obj].init()
}
// if has write lock
if locks[m.obj].isWriter(m.from.num) {
val, _ := transToData[m.transId][m.obj]
s := fmt.Sprintf("%s.%s %s", server_name, m.obj, val)
m.from.SendMsg(s)
return
}
// fmt.Println("is writer not")
// in case the object doesnt exists
if _,ok := data.isPresent(m.obj); !ok {
m.from.SendMsg("NOT FOUND")
return
}
// if already a reader of this object
if locks[m.obj].isReader(m.from.num) {
val,_ := data.isPresent(m.obj)
s := fmt.Sprintf("%s.%s %s", server_name, m.obj, val)
m.from.SendMsg(s)
return
}
// blocking for lock
if ! locks[m.obj].lockReader(m.from.num){
m.from.SendMsg("ABORTED")
return
}
// lock required
val,_ := data.isPresent(m.obj)
s := fmt.Sprintf("%s.%s %s", server_name, m.obj, val)
m.from.SendMsg(s)
// wait to acquire the lock
// fmt.Println("GET done")
return
}
func sendToDeadlock(conn net.Conn, msg string)bool {
_, er := fmt.Fprintf(conn, msg)
if er != nil {
fmt.Printf("Error in talking to deadlock handler\n")
return false
}
reader := bufio.NewReader(conn)
s, err := reader.ReadString('\n')
fmt.Printf("# recieved string %s\n", s)
if err != nil {
fmt.Println("#Error in listening")
fmt.Printf("# %s", err)
return false
}
s = strings.TrimSpace(s)
return s == "NO"
}
func main(){
if len(os.Args) != 3 {
fmt.Println("Need server id as argument. One of {A,B,C,D,E} portnum")
}
server_name = os.Args[1]
// fmt.Printf(server_name)
inbox := Box{}
//var connected_members map[string]*Node
transToclient = make(map[string]string)
clientToTrans = make(map[string]string)
data = Data{}
data.init()
transToData = map[string]map[string]string{}
// readLocks = make(map[string]string)
locks = make(map[string]*Lock)
data.update("x","100")
// fmt.Println("Started Listening on\n")
a,_ := strconv.Atoi(os.Args[2])
go startListening(&inbox, a)
conn_deadlock, err := net.Dial("tcp", "127.0.0.1:9999")
if err != nil {
fmt.Printf("couldn't connect to deadlock server\n")
return
}
for {
m, err := inbox.pop()
//fmt.Println("Got msg\n")
// sleeping if no message in inbox
if err != nil {
time.Sleep(10)
continue
}
switch m.GetType() {
// client telling the server its ID
case "ID":
m.from.num = m.id
break
case "BEGIN":
// if no transaction running from this client
// fmt.Println("BEGIN Received")
// fmt.Println(m.transId + " id")
if _, ok := clientToTrans[m.from.num];!ok {
clientToTrans[m.from.num] = m.transId
transToclient[m.transId] = m.from.num
transToData[m.transId] = map[string]string{}
// transToData[m.transId]["x"] = "100"
for _,v := range locks {
v.kill.Off(m.from.num)
}
m.from.SendMsg("OK")
} else {
m.from.SendMsg("NOT_OK")
}
break
case "GET":
// fmt.Println("GET Received")
if sendToDeadlock(conn_deadlock, fmt.Sprintf("ADD %s %s %s\n",m.from.num,server_name+"."+m.obj,"R")) {
m.from.SendMsg("DEADLOCK")
// fmt.Println("GET Received")
break
}
go get(m)
break
case "SET":
// fmt.Println("SET Received")
if sendToDeadlock(conn_deadlock, fmt.Sprintf("ADD %s %s %s\n",m.from.num,server_name+"."+m.obj,"W")) {
m.from.SendMsg("DEADLOCK")
// fmt.
break
}
go set(m)
break
case "PRECOMMIT":
// do some kind of checking
m.from.SendMsg("OK")
case "COMMIT":
// commiting all the data
for v,k := range transToData[m.transId] {
fmt.Printf("Commiting %s:%s\n",v,k)
data.update(v,k)
}
for _,v := range locks {
v.removeReader(m.from.num)
v.removeWriter(m.from.num)
}
delete(transToData, m.transId)
delete(transToclient, m.transId)
delete(clientToTrans, m.from.num)
// fmt.Println("Sending commit ok")
sendToDeadlock(conn_deadlock, fmt.Sprintf("REMOVE %s\n",m.from.num))
m.from.SendMsg("COMMIT OK")
break
case "ABORT":
for _,v := range locks {
v.kill.On(m.from.num)
v.removeReader(m.from.num)
v.removeWriter(m.from.num)
}
delete(transToData, m.transId)
delete(transToclient, m.transId)
delete(clientToTrans, m.from.num)
sendToDeadlock(conn_deadlock, fmt.Sprintf("REMOVE %s\n",m.from.num))
m.from.SendMsg("ABORTED")
break
// abort everything and clear all
default:
break
}
}
return
}