-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
219 lines (178 loc) · 3.63 KB
/
message.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
package rcss
import (
"bytes"
"fmt"
)
type Message struct {
name string
values []string
submessages []Message
}
func (c *Message) AddValues(values ...string) {
c.values = append(c.values, values...)
}
func (c *Message) AddSubmessages(messages ...Message) {
c.submessages = append(c.submessages, messages...)
}
func (c Message) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
b.WriteRune('(')
b.WriteString(c.name)
for _, v := range c.values {
b.WriteString(fmt.Sprintf(" %v", v))
}
for _, msg := range c.submessages {
if mb, err := msg.MarshalBinary(); err != nil {
return nil, err
} else {
b.WriteRune(' ')
b.Write(mb)
}
}
b.WriteRune(')')
return b.Bytes(), nil
}
func (c *Message) UnmarshalBinary(data []byte) error {
c.name = ""
c.values = make([]string, 0)
c.submessages = make([]Message, 0)
r := bytes.NewBuffer(data)
if ch, _, err := r.ReadRune(); err != nil {
return err
} else if '(' != ch {
return fmt.Errorf("corrupted message")
}
var name bytes.Buffer
name:
for ch, _, err := r.ReadRune(); ; ch, _, err = r.ReadRune() {
if err != nil {
return err
}
switch ch {
case ' ':
break name
case ')':
r.UnreadByte()
break name
default:
name.WriteRune(ch)
}
}
c.name = name.String()
var value bytes.Buffer
values:
for ch, _, err := r.ReadRune(); ; ch, _, err = r.ReadRune() {
if err != nil {
return err
}
switch ch {
case '(':
r.UnreadRune()
break values
case ' ':
c.AddValues(value.String())
value.Reset()
case ')':
if value.Len() > 0 {
c.AddValues(value.String())
value.Reset()
}
r.UnreadRune()
break values
default:
value.WriteRune(ch)
}
}
messages:
for ch, _, err := r.ReadRune(); ; ch, _, err = r.ReadRune() {
if err != nil {
return err
}
switch ch {
case '(':
var subbuffer bytes.Buffer
subbuffer.WriteRune(ch)
for hops := 1; hops > 0; {
if ch, _, err := r.ReadRune(); err != nil {
return err
} else {
subbuffer.WriteRune(ch)
switch ch {
case '(':
hops++
case ')':
hops--
}
}
}
var submessage Message
if err := submessage.UnmarshalBinary(subbuffer.Bytes()); err != nil {
return err
} else {
c.AddSubmessages(submessage)
}
case ')':
r.UnreadRune()
break messages
}
}
if ch, _, err := r.ReadRune(); err != nil {
return err
} else if ')' != ch {
return fmt.Errorf("corrupted message")
}
return nil
}
type Marshaler interface {
MarshalRcss() (Message, error)
}
type Unmarshaler interface {
UnmarshalRcss(Message) error
}
func NewMessage(name string) *Message {
return &Message{
name: name,
values: make([]string, 0),
submessages: make([]Message, 0),
}
}
type buffer struct {
title string
value interface{}
name string
format string
vars []buffer
}
func (buf buffer) MarshalRcss() (Message, error) {
msg := NewMessage(buf.name)
for _, v := range buf.vars {
var value string
if len(v.format) > 0 {
value = fmt.Sprintf(v.format, v.value)
} else {
value = fmt.Sprint(v.value)
}
if len(v.name) > 0 {
submsg := NewMessage(v.name)
submsg.AddValues(value)
msg.AddSubmessages(*submsg)
} else {
msg.AddValues(value)
}
}
return *msg, nil
}
func (buf *buffer) UnmarshalRcss(msg Message) error {
for k, v := range buf.vars {
if len(v.format) > 0 {
if _, err := fmt.Sscanf(msg.values[k], v.name, &v.value); err != nil {
return fmt.Errorf("error on parsing %s: %s", v.name, err)
}
} else {
if _, err := fmt.Sscan(msg.values[k], &v.value); err != nil {
return fmt.Errorf("error on parsing %s: %s", v.name, err)
}
}
}
return nil
}