forked from zeromq/gyre
/
shout.go
176 lines (145 loc) · 3.77 KB
/
shout.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
package msg
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strconv"
zmq "github.com/pebbe/zmq4"
)
// Shout struct
// Send a multi-part message to a group
type Shout struct {
routingID []byte
version byte
sequence uint16
Group string
Content []byte
}
// NewShout creates new Shout message.
func NewShout() *Shout {
shout := &Shout{}
return shout
}
// String returns print friendly name.
func (s *Shout) String() string {
str := "ZRE_MSG_SHOUT:\n"
str += fmt.Sprintf(" version = %v\n", s.version)
str += fmt.Sprintf(" sequence = %v\n", s.sequence)
str += fmt.Sprintf(" Group = %v\n", s.Group)
str += fmt.Sprintf(" Content = %v\n", s.Content)
return str
}
// Marshal serializes the message.
func (s *Shout) Marshal() ([]byte, error) {
// Calculate size of serialized data
bufferSize := 2 + 1 // Signature and message ID
// version is a 1-byte integer
bufferSize++
// sequence is a 2-byte integer
bufferSize += 2
// Group is a string with 1-byte length
bufferSize++ // Size is one byte
bufferSize += len(s.Group)
// Now serialize the message
tmpBuf := make([]byte, bufferSize)
tmpBuf = tmpBuf[:0]
buffer := bytes.NewBuffer(tmpBuf)
binary.Write(buffer, binary.BigEndian, Signature)
binary.Write(buffer, binary.BigEndian, ShoutID)
// version
value, _ := strconv.ParseUint("2", 10, 1*8)
binary.Write(buffer, binary.BigEndian, byte(value))
// sequence
binary.Write(buffer, binary.BigEndian, s.sequence)
// Group
putString(buffer, s.Group)
return buffer.Bytes(), nil
}
// Unmarshal unmarshals the message.
func (s *Shout) Unmarshal(frames ...[]byte) error {
if frames == nil {
return errors.New("Can't unmarshal empty message")
}
frame := frames[0]
frames = frames[1:]
buffer := bytes.NewBuffer(frame)
// Get and check protocol signature
var signature uint16
binary.Read(buffer, binary.BigEndian, &signature)
if signature != Signature {
return fmt.Errorf("invalid signature %X != %X", Signature, signature)
}
// Get message id and parse per message type
var id uint8
binary.Read(buffer, binary.BigEndian, &id)
if id != ShoutID {
return errors.New("malformed Shout message")
}
// version
binary.Read(buffer, binary.BigEndian, &s.version)
if s.version != 2 {
return errors.New("malformed version message")
}
// sequence
binary.Read(buffer, binary.BigEndian, &s.sequence)
// Group
s.Group = getString(buffer)
// Content
if 0 <= len(frames)-1 {
s.Content = frames[0]
}
return nil
}
// Send sends marshaled data through 0mq socket.
func (s *Shout) Send(socket *zmq.Socket) (err error) {
frame, err := s.Marshal()
if err != nil {
return err
}
socType, err := socket.GetType()
if err != nil {
return err
}
// If we're sending to a ROUTER, we send the routingID first
if socType == zmq.ROUTER {
_, err = socket.SendBytes(s.routingID, zmq.SNDMORE)
if err != nil {
return err
}
}
// Now send the data frame
_, err = socket.SendBytes(frame, zmq.SNDMORE)
if err != nil {
return err
}
// Now send any frame fields, in order
_, err = socket.SendBytes(s.Content, 0)
return err
}
// RoutingID returns the routingID for this message, routingID should be set
// whenever talking to a ROUTER.
func (s *Shout) RoutingID() []byte {
return s.routingID
}
// SetRoutingID sets the routingID for this message, routingID should be set
// whenever talking to a ROUTER.
func (s *Shout) SetRoutingID(routingID []byte) {
s.routingID = routingID
}
// SetVersion sets the version.
func (s *Shout) SetVersion(version byte) {
s.version = version
}
// Version returns the version.
func (s *Shout) Version() byte {
return s.version
}
// SetSequence sets the sequence.
func (s *Shout) SetSequence(sequence uint16) {
s.sequence = sequence
}
// Sequence returns the sequence.
func (s *Shout) Sequence() uint16 {
return s.sequence
}