-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
235 lines (212 loc) · 4.66 KB
/
main.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
package main
import (
"flag"
"fmt"
"log/slog"
"net"
"strconv"
"strings"
"time"
)
const DEFAULT_PORT = 6379
// Commands
const (
PING = "PING"
ECHO = "ECHO"
SET = "SET"
GET = "GET"
)
// Returns
const (
PONG = "PONG"
OK = "OK"
)
const NULLBULKSTRING = "$-1\r\n"
type Config struct {
lnAddr string
}
type Server struct {
cfg Config
ln net.Listener
peers map[*Peer]struct{}
addPeerChan chan *Peer
delPeerChan chan *Peer
msgChan chan Message
db map[string]Data
}
type Peer struct {
conn net.Conn
msgChan chan Message
deletePeerChan chan *Peer
db *map[string]Data
}
type Data struct {
value string
px int
}
type Message struct {
cmd []byte
peer Peer
}
func NewMessage(peer Peer, msg []byte) Message {
return Message{
cmd: msg,
peer: peer,
}
}
func NewPeer(conn net.Conn, msgChan chan Message, delete chan *Peer, db *map[string]Data) *Peer {
return &Peer{
conn: conn,
msgChan: msgChan,
deletePeerChan: delete,
db: db,
}
}
func (p *Peer) reader() {
buf := make([]byte, 1024)
for {
defer func() {
p.deletePeerChan <- p
p.conn.Close()
}()
n, err := p.conn.Read(buf)
if err != nil {
return
}
msgBuf := make([]byte, n)
copy(msgBuf, buf[:n])
p.msgChan <- NewMessage(*p, msgBuf)
}
}
func NewServer(c Config) *Server {
if len(c.lnAddr) == 0 {
c.lnAddr = fmt.Sprintf(":%d", DEFAULT_PORT)
}
return &Server{
cfg: c,
peers: make(map[*Peer]struct{}),
addPeerChan: make(chan *Peer),
delPeerChan: make(chan *Peer),
msgChan: make(chan Message),
db: make(map[string]Data),
}
}
func (s *Server) Start() error {
ln, err := net.Listen("tcp", s.cfg.lnAddr)
if err != nil {
return err
}
s.ln = ln
go s.handlePeersLoop()
return s.AcceptLoop()
}
func (s *Server) handlePeersLoop() {
for {
select {
case peer := <-s.addPeerChan:
s.peers[peer] = struct{}{}
case msg := <-s.msgChan:
HandleMessage(msg)
case peer := <-s.delPeerChan:
delete(s.peers, peer)
}
}
}
func HandleMessage(msg Message) {
splits := strings.Split(string(msg.cmd), "\r\n")
inputBulkStrings := []string{}
// commandLen := "0"
// Command Parser
for i, v := range splits {
if v == "" {
continue
}
if strings.Contains(v, "*") {
// commandLen = strings.Split(v, "*")[1]
// println("len", commandLen)
}
if strings.Contains(v, "$") {
inputBulkStrings = append(inputBulkStrings, splits[i+1])
}
}
// Command Handler
for i, v := range inputBulkStrings {
switch strings.ToUpper(v) {
case PING:
msg.peer.conn.Write([]byte(bulkString(PONG)))
case ECHO:
if i+1 < len(inputBulkStrings) {
echoReturn := inputBulkStrings[i+1]
msg.peer.conn.Write([]byte(bulkString(echoReturn)))
} else {
msg.peer.conn.Write([]byte(bulkString("ECHO Command requires a string argument")))
}
case SET:
px := -1
if i+4 < len(inputBulkStrings) {
if inputBulkStrings[i+3] == "px" {
x, err := strconv.Atoi(inputBulkStrings[i+4])
if err != nil {
msg.peer.conn.Write([]byte(bulkString("Invalid px value")))
} else {
px = x
}
}
}
if i+2 < len(inputBulkStrings) {
key := inputBulkStrings[i+1]
value := inputBulkStrings[i+2]
(*msg.peer.db)[key] = Data{value: value, px: px}
msg.peer.conn.Write([]byte(bulkString(OK)))
} else {
msg.peer.conn.Write([]byte(bulkString("SET Command requires a key and value argument")))
}
if px > 0 {
go func() {
time.Sleep(time.Duration(px) * time.Millisecond)
delete((*msg.peer.db), inputBulkStrings[i+1])
}()
}
case GET:
if i+1 < len(inputBulkStrings) {
key := inputBulkStrings[i+1]
value, ok := (*msg.peer.db)[key]
if ok {
msg.peer.conn.Write([]byte(bulkString(value.value)))
} else {
msg.peer.conn.Write([]byte(NULLBULKSTRING))
}
} else {
msg.peer.conn.Write([]byte(bulkString("GET Command requires a key argument")))
}
// default:
// msg.peer.conn.Write([]byte(bulkString("Unknown Command")))
}
}
}
func bulkString(s string) string {
return fmt.Sprintf("$%d\r\n%s\r\n", len(s), s)
}
func (s *Server) AcceptLoop() error {
for {
conn, err := s.ln.Accept()
if err != nil {
return err
}
go s.handleConn(conn)
}
}
func (s *Server) handleConn(conn net.Conn) {
peer := NewPeer(conn, s.msgChan, s.delPeerChan, &s.db)
s.addPeerChan <- peer
peer.reader()
}
func main() {
port := flag.Int("port", DEFAULT_PORT, "port to listen on")
flag.Parse()
print(fmt.Sprintf("Running on port: %d\n", *port))
s := NewServer(Config{lnAddr: fmt.Sprintf(":%d", *port)})
if err := s.Start(); err != nil {
slog.Error("Error starting server: %v", err)
}
}