-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·112 lines (99 loc) · 2.37 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
package main
import (
"fmt"
"github.com/therahulprasad/gomembase/commandInterpreter"
"github.com/therahulprasad/gomembase/config"
"github.com/therahulprasad/gomembase/storage"
"net"
"strconv"
"time"
)
const (
RECV_BUF_LEN = 1024
)
func main() {
storage.Datastore = make(map[string]storage.Node)
defer hello()
// Run garbage cleaner as a separate routine
go garbageCleaner()
ln, err := net.Listen("tcp", ":8080")
if err != nil {
fmt.Println("Error 1")
}
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println("Error 2")
continue
}
fmt.Println("Connection received")
// Handle every connection as separte routine
go handleConnection(conn)
}
}
func hello() {
fmt.Println("World")
}
// Cleanup expired value every minute
func garbageCleaner() {
for _ = range time.Tick(1 * time.Minute) {
storage.Cleanup()
}
}
func handleConnection(conn net.Conn) {
for {
buf := make([]byte, RECV_BUF_LEN)
n, err := conn.Read(buf)
if err != nil {
println("Error reading:", err.Error())
return
}
//println("received ", n, " bytes of data =", string(buf[:n-2]))
// TODO FIX Command Interpreter
command, err := commandInterpreter.ParseCommand(string(buf[:n-2]))
if err != nil {
reply(conn, []byte(err.Error()))
continue
}
if command.CommandType == "SET" {
// Send proper data to SET command
_, err := storage.Set(command.CommandValue["key"], command.CommandValue["value"], command.Options)
if err != nil {
reply(conn, []byte(err.Error()))
} else {
reply(conn, []byte("+OK\r\n"))
}
} else if command.CommandType == "GET" {
data, err := storage.Get(command.CommandValue["key"])
if err != nil {
//error_message := err.Error() + "\r\n"
error_message := "$-1\r\n"
reply(conn, []byte(error_message))
continue
}
// TODO implement output formatter as separate package
output_length := len(data.Value)
output := "$" + strconv.Itoa(output_length) + "\r\n" + data.Value + "\r\n"
reply(conn, []byte(output))
}
}
}
func reply(conn net.Conn, buf []byte) {
//_,err := conn.Write(reverse(buf))
_, err := conn.Write(buf)
if err != nil {
if config.Debug == true {
println("Error send reply:", err.Error())
}
} else {
if config.Debug == true {
//println("Reply sent")
}
}
}
func reverse(buf []byte) (buff []byte) {
for i := 0; i < len(buf); i++ {
buf[i] = buf[len(buf)-i]
}
return buf
}