-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
126 lines (105 loc) · 2.57 KB
/
client.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
package kece
import (
"bytes"
"errors"
"fmt"
"math"
"net"
"strconv"
"strings"
"time"
)
// Client struct
type Client struct {
ID string
Conn net.Conn
}
// Subscribe client method, this function will used by client to subscribe to specific topic
//TODO
func (client *Client) Subscribe(topic string) {
}
// ClientMessage struct
type ClientMessage struct {
Client *Client
Message []byte
Cmd []byte
Key []byte
Value []byte
Exp time.Duration
}
func processingValue(val string) (value string, expiredValue int, err error) {
var pair = map[string]string{"{": "}", `"`: `"`, "'": "'"}
if _, ok := pair[val]; ok {
err = errors.New(ErrorInvalidArgument)
return
}
// calculate if arguments has expired value (suffix is integer)
decimal := 0
for i := len(val) - 1; i >= 0; i-- {
k := string(val[i])
a, errConv := strconv.Atoi(k)
if errConv != nil {
break
}
expiredValue += a * int(math.Pow10(decimal))
decimal++
}
if expiredValue != 0 && fmt.Sprint(expiredValue) != val {
val = strings.TrimRight(val, fmt.Sprint(expiredValue)) // trim argument with expired value
val = strings.TrimSpace(val)
}
lastChar := string(val[len(val)-1])
value = val
if res, ok := pair[string(val[0])]; ok {
if res == lastChar {
if res == `"` || res == "'" {
value = val[1 : len(val)-1] // remove prefix & suffix string => ex: "test" -> test
}
return
}
err = errors.New(ErrorInvalidArgument)
return
}
if len(strings.Fields(val)) <= 1 { // single string without space and list from pair
return
}
err = errors.New(ErrorInvalidArgument)
return
}
// ValidateMessage function
func (c *ClientMessage) ValidateMessage() error {
message := bytes.TrimSpace(c.Message)
messages := strings.Fields(string(message))
command, ok := commands[messages[0]]
if !ok {
return errors.New(ErrorInvalidCommand)
}
c.Cmd = []byte(messages[0])
c.Key = []byte(messages[1])
if command == "GET" || command == "DEL" || command == "AUTH" {
if len(messages) < 2 || len(messages) > 2 {
return errors.New(ErrorInvalidOperation)
}
}
if command == "SET" {
if len(messages) < 3 {
return errors.New(ErrorInvalidOperation)
}
mess := strings.TrimLeft(string(message), command)
idx := strings.Index(mess, messages[1])
if idx < 0 {
return errors.New(ErrorInvalidArgument)
}
value := strings.TrimSpace(mess[idx+len(messages[1]):])
val, expired, err := processingValue(value)
if err != nil {
return err
}
c.Value = []byte(val)
if expired != 0 {
c.Exp = time.Second * time.Duration(expired)
}
}
c.Message = nil // garbage
return nil
}