-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
187 lines (166 loc) · 3.2 KB
/
request.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
package main
import (
"bufio"
"fmt"
"net"
"strconv"
"strings"
)
type request struct {
method string
transactionID int
sequenceNum int
contentLength int
data []byte
filename string
}
type response struct {
method string
transactionID int
sequenceNum int
errorCode int
length int
reason string
}
func sendErrorIfItExist(conn net.Conn, err error) bool {
if err != nil {
return true
}
return false
}
func parseHeader(header string) (request, error) {
method := ""
var tid, seqNum, length int
s := strings.Split(header, " ")
if len(s) >= 1 {
method = s[0]
}
if len(s) >= 2 {
i, err := strconv.Atoi(s[1])
if err != nil {
return request{}, err
}
tid = i
}
if len(s) >= 3 {
i, err := strconv.Atoi(s[2])
if err != nil {
return request{}, err
}
if i < 0 {
fmt.Println("should error out")
}
seqNum = i
}
if len(s) >= 4 {
i, err := strconv.Atoi(s[3][:len(s[3])-2])
if err != nil {
fmt.Println(err)
return request{}, err
}
length = i
}
// fmt.Println("header", method)
// fmt.Println("tid", tid)
// fmt.Println("seqnum", seqNum)
// fmt.Println("len", length)
return request{
method: method,
transactionID: tid,
sequenceNum: seqNum,
contentLength: length,
}, nil
}
// helper to parse to stuff
func parsePacket(conn net.Conn) error {
var req request
var err error
r := bufio.NewReader(conn)
header, err := r.ReadString('\n')
if err != nil {
return nil
}
req, err = parseHeader(header)
if err != nil {
return nil
}
switch req.method {
case "NEW_TXN":
tid := strconv.Itoa(handleNewTransaction(req, r))
conn.Write([]byte("ACK " + tid))
case "WRITE":
req = handleWrite(req, r)
case "READ":
req = handleRead(req, r)
conn.Write(req.data)
case "COMMIT":
req = handleCommit(req)
case "ABORT":
req = handleAbort(req)
default:
req = handleError(req)
}
return nil
}
func handleNewTransaction(req request, r *bufio.Reader) int {
// reads the empty line
_, err := r.ReadString('\n')
if err != nil {
return -1
}
filename, err := r.ReadString('\n')
if err != nil {
return -1
}
req.filename = trimSuffix(filename, "\n")
retTID := logNewTransaction(req)
return retTID
}
func handleWrite(req request, r *bufio.Reader) request {
// reads the empty line
_, err := r.ReadString('\n')
if err != nil {
return request{}
}
data, err := r.ReadString('\n')
if err != nil {
return request{}
}
//fmt.Println("data: ", data)
req.data = []byte(data)
logWrite(req)
return req
}
func trimSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
s = s[:len(s)-len(suffix)]
}
return s
}
func handleRead(req request, r *bufio.Reader) request {
// reads the empty line
_, err := r.ReadString('\n')
if err != nil {
return request{}
}
filename, err := r.ReadString('\n')
if err != nil {
return request{}
}
filename = trimSuffix(filename, "\n")
//absPath, _ := filepath.Abs(DIRECTORY + filename)
//fmt.Println(absPath)
req.data = readFile(DIRECTORY, filename)
return req
}
func handleCommit(req request) request {
commit(req)
return req
}
func handleAbort(req request) request {
abort(req)
return req
}
func handleError(req request) request {
return req
}