-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.go
135 lines (106 loc) · 2.67 KB
/
parser.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
package yagnats
import (
"bufio"
"bytes"
"errors"
"fmt"
"regexp"
"strconv"
)
type Parser func(*bufio.Reader) (Packet, error)
var PARSERS = map[string]Parser{
// PING\s*\r\n
"PING": func(io *bufio.Reader) (Packet, error) {
io.ReadBytes('\n')
return &PingPacket{}, nil
},
// PONG\s*\r\n
"PONG": func(io *bufio.Reader) (Packet, error) {
io.ReadBytes('\n')
return &PongPacket{}, nil
},
// +OK\s*\r\n
"+OK": func(io *bufio.Reader) (Packet, error) {
io.ReadBytes('\n')
return &OKPacket{}, nil
},
// -ERR '(message)'\r\n
"-ERR": func(io *bufio.Reader) (Packet, error) {
bytes, _ := io.ReadBytes('\n')
re := regexp.MustCompile(`\s*'(.*)'\s*\r\n`)
match := re.FindSubmatchIndex(bytes)
if match == nil {
return nil, errors.New("Malformed -ERR message")
}
return &ERRPacket{Message: string(bytes[match[2]:match[3]])}, nil
},
// INFO (payload)\r\n
"INFO": func(io *bufio.Reader) (Packet, error) {
bytes, _ := io.ReadBytes('\n')
re := regexp.MustCompile(`\s*([^\s]+)\s*\r\n`)
match := re.FindSubmatchIndex(bytes)
if match == nil {
return nil, errors.New("Malformed INFO message")
}
return &InfoPacket{Payload: string(bytes[match[2]:match[3]])}, nil
},
// MSG (subject) (subscriber-id) (reply)? (length)\r\n(byte * length)\r\n
"MSG": func(io *bufio.Reader) (Packet, error) {
bytes, _ := io.ReadBytes('\n')
re := regexp.MustCompile(`\s*([^\s]+)\s+(\d+)\s+(([^\s]+)\s+)?(\d+)\s*\r\n`)
matches := re.FindStringSubmatch(string(bytes))
if matches == nil {
return nil, errors.New("Malformed MSG message")
}
subID, _ := strconv.ParseInt(matches[2], 10, 64)
payloadLen, _ := strconv.Atoi(matches[5])
payload, err := readNBytes(payloadLen, io)
if err != nil {
return nil, err
}
io.ReadBytes('\n')
return &MsgPacket{
Subject: matches[1],
SubID: subID,
ReplyTo: matches[4],
Payload: payload,
}, nil
},
}
func Parse(io *bufio.Reader) (val Packet, err error) {
header, err := readWord(io)
if err != nil {
return nil, err
}
parser := PARSERS[string(header)]
if parser == nil {
return nil, errors.New(fmt.Sprintf("Unknown header: %s", header))
}
return parser(io)
}
func readWord(io *bufio.Reader) ([]byte, error) {
word := new(bytes.Buffer)
for {
next, err := io.ReadByte()
if err != nil {
return nil, err
}
switch next {
case ' ', '\t', '\r', '\n':
return word.Bytes(), nil
default:
word.WriteByte(next)
}
}
}
func readNBytes(payloadLen int, io *bufio.Reader) ([]byte, error) {
payload := make([]byte, payloadLen)
for readCount := 0; readCount < payloadLen; {
n, err := io.Read(payload[readCount:])
if err != nil {
return nil, err
}
readCount += n
}
return payload, nil
}