forked from gophergala2016/rex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.go
201 lines (161 loc) · 4.06 KB
/
content.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
package room
import (
"encoding/json"
"github.com/bmatsuo/rex/room/roomtime"
)
// Codec performs serialization for room data.
type Codec interface {
// EncodeMessage serializes v and returns the []byte or any error.
EncodeMessage(v Msg) []byte
// DecodeMessage deserializes b into a Msg and returns it with any error.
DecodeMessage(b []byte) (Msg, error)
// DecodeEvent deserializes b into an Event and returns it with any error.
DecodeEvent(b []byte) (Event, error)
// EncodeEvent serializes v and returns the []byte or any error.
EncodeEvent(v Event) []byte
}
// Content is application data that is transmitted over a bus.
type Content interface {
// Data returns the event content is a slice of bytes (even if the content
// is stored as a string). Applications should use whichever of Data or
// Text methods they need and any translation necessary will happen as fast
// as possible.
Data() []byte
// Text is like Data but returns a string representation of event data and
// may be optmized to do that.
Text() string
}
// Bytes returns a Content containing the b as its data.
func Bytes(b []byte) Content {
return contentBytes(b)
}
// String returns a Content containing the s as its data.
func String(s string) Content {
return contentString(s)
}
type contentBytes []byte
var _ Content = contentBytes(nil)
func (c contentBytes) Data() []byte {
return []byte(c)
}
func (c contentBytes) Text() string {
return string(c)
}
type contentString string
var _ Content = contentString("")
func (c contentString) Data() []byte {
return []byte(c)
}
func (c contentString) Text() string {
return string(c)
}
// Event is a broadcast message from the server to all clients. Unlike Msg an
// event does not have an associated session identifier because it is intended
// for all clients.
type Event interface {
Index() uint64
Time() *roomtime.Time
Content
}
// Msg is a piece of data originating from a client application.
type Msg interface {
// Session returns an identifier for the client (session) that originated
// the message.
Session() string
Time() *roomtime.Time
Content
}
func newEvent(i uint64, c Content, t func() *roomtime.Time) Event {
event := &simpleEvent{i, t(), c}
return event
}
type jsonEvent struct {
I uint64 `json:"index"`
T *roomtime.Time `json:"time"`
D string `json:"data"`
Event `json:"-"`
}
func newJSONEvent(event Event) *jsonEvent {
if event == nil {
return &jsonEvent{}
}
return &jsonEvent{
I: event.Index(),
T: event.Time(),
D: event.Text(),
}
}
func (event *jsonEvent) MarshalJSON() ([]byte, error) {
type E jsonEvent
return json.Marshal((*E)(event))
}
func (event *jsonEvent) UnmarshalJSON(b []byte) error {
type E jsonEvent
err := json.Unmarshal(b, (*E)(event))
if err != nil {
return err
}
event.Event = newEvent(
event.I,
String(event.D),
func() *roomtime.Time { return event.T },
)
return nil
}
type simpleEvent struct {
i uint64
t *roomtime.Time
Content
}
var _ Event = &simpleEvent{}
func (event *simpleEvent) Index() uint64 {
return event.i
}
func (event *simpleEvent) Time() *roomtime.Time {
return event.t
}
type jsonMsg struct {
S string `json:"session"`
T *roomtime.Time `json:"time"`
D string `json:"data"`
Msg `json:"-"`
}
func newJSONMsg(msg Msg) *jsonMsg {
if msg == nil {
return &jsonMsg{}
}
return &jsonMsg{
S: msg.Session(),
T: msg.Time(),
D: msg.Text(),
}
}
func (msg *jsonMsg) MarshalJSON() ([]byte, error) {
type M jsonMsg
return json.Marshal((*M)(msg))
}
func (msg *jsonMsg) UnmarshalJSON(b []byte) error {
type M jsonMsg
err := json.Unmarshal(b, (*M)(msg))
if err != nil {
return err
}
msg.Msg = newMsg(msg.S, String(msg.D), func() *roomtime.Time { return msg.T })
return nil
}
func newMsg(session string, c Content, t func() *roomtime.Time) Msg {
msg := &simpleMsg{session, t(), c}
return msg
}
type simpleMsg struct {
sess string
t *roomtime.Time
Content
}
var _ Msg = &simpleMsg{}
func (msg *simpleMsg) Session() string {
return msg.sess
}
func (msg *simpleMsg) Time() *roomtime.Time {
return msg.t
}