-
Notifications
You must be signed in to change notification settings - Fork 1
/
TCPserver_go_example.go
222 lines (200 loc) · 4 KB
/
TCPserver_go_example.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
package main
import (
"bufio"
"encoding/gob"
"fmt"
"net"
"os"
"strings"
)
type myElement struct {
Name string
Surname string
Id string
}
const welcome = "Welcome to the Key Value store!\n"
var DATA = make(map[string]myElement)
var DATAFILE = "/tmp/dataFile.gob"
// The handleConnection() function communicates with each TCP client and interprets the client input.
func handleConnection(c net.Conn) {
c.Write([]byte(welcome))
for {
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
command := strings.TrimSpace(netData)
tokens := strings.Fields(command)
switch len(tokens) {
case 0:
continue
case 1:
tokens = append(tokens, "")
tokens = append(tokens, "")
tokens = append(tokens, "")
tokens = append(tokens, "")
case 2:
tokens = append(tokens, "")
tokens = append(tokens, "")
tokens = append(tokens, "")
case 3:
tokens = append(tokens, "")
tokens = append(tokens, "")
case 4:
tokens = append(tokens, "")
}
switch tokens[0] {
case "STOP":
err = save()
if err != nil {
fmt.Println(err)
}
c.Close()
return
case "PRINT":
PRINT(c)
case "DELETE":
if !DELETE(tokens[1]) {
netData := "Delete operation failed!\n"
c.Write([]byte(netData))
} else {
netData := "Delete operation successful!\n"
c.Write([]byte(netData))
}
case "ADD":
n := myElement{tokens[2], tokens[3], tokens[4]}
if !ADD(tokens[1], n) {
netData := "Add operation failed!\n"
c.Write([]byte(netData))
} else {
netData := "Add operation successful!\n"
c.Write([]byte(netData))
}
err = save()
if err != nil {
fmt.Println(err)
}
case "LOOKUP":
n := LOOKUP(tokens[1])
if n != nil {
netData := fmt.Sprintf("%v\n", *n)
c.Write([]byte(netData))
} else {
netData := "Did not find key!\n"
c.Write([]byte(netData))
}
case "CHANGE":
n := myElement{tokens[2], tokens[3], tokens[4]}
if !CHANGE(tokens[1], n) {
netData := "Update operation failed!\n"
c.Write([]byte(netData))
} else {
netData := "Update operation successful!\n"
c.Write([]byte(netData))
}
err = save()
if err != nil {
fmt.Println(err)
}
default:
netData := "Unknown command – please try again!\n"
c.Write([]byte(netData))
}
}
}
func save() error {
fmt.Println("Saving", DATAFILE)
err := os.Remove(DATAFILE)
if err != nil {
fmt.Println(err)
}
saveTo, err := os.Create(DATAFILE)
if err != nil {
fmt.Println("Cannot create", DATAFILE)
return err
}
defer saveTo.Close()
encoder := gob.NewEncoder(saveTo)
err = encoder.Encode(DATA)
if err != nil {
fmt.Println("Cannot save to", DATAFILE)
return err
}
return nil
}
func load() error {
fmt.Println("Loading", DATAFILE)
loadFrom, err := os.Open(DATAFILE)
defer loadFrom.Close()
if err != nil {
fmt.Println("Empty key/value store!")
return err
}
decoder := gob.NewDecoder(loadFrom)
decoder.Decode(&DATA)
return nil
}
func ADD(k string, n myElement) bool {
if k == "" {
return false
}
if LOOKUP(k) == nil {
DATA[k] = n
return true
}
return false
}
func DELETE(k string) bool {
if LOOKUP(k) != nil {
delete(DATA, k)
return true
}
return false
}
func LOOKUP(k string) *myElement {
_, ok := DATA[k]
if ok {
n := DATA[k]
return &n
} else {
return nil
}
}
func CHANGE(k string, n myElement) bool {
DATA[k] = n
return true
}
// The PRINT() function sends data to a TCP client directly, one line at a time.
func PRINT(c net.Conn) {
for k, d := range DATA {
netData := fmt.Sprintf("key: %s value: %v\n", k, d)
c.Write([]byte(netData))
}
}
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a port number!")
return
}
PORT := ":" + arguments[1]
l, err := net.Listen("tcp", PORT)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
err = load()
if err != nil {
fmt.Println(err)
}
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
os.Exit(100)
}
go handleConnection(c)
}
}