-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging.go
125 lines (102 loc) · 2.5 KB
/
messaging.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
package chik
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"reflect"
"github.com/gochik/chik/types"
"github.com/gofrs/uuid"
)
type Message struct {
sender uuid.UUID
receiver uuid.UUID
command *types.Command
}
// NewMessage creates a new message
func NewMessage(receiver uuid.UUID, command *types.Command) *Message {
return &Message{
sender: uuid.Nil,
receiver: receiver,
command: command,
}
}
// ParseMessage handles incoming data and creates a Message object
func ParseMessage(reader io.Reader) (*Message, error) {
message := Message{}
var length uint32
err := binary.Read(reader, binary.BigEndian, &length)
if err != nil {
return nil, err
}
if length < 16*2 {
return nil, fmt.Errorf("Message too short, must be at least 32 bytes, got: %d", length)
}
err = binary.Read(reader, binary.BigEndian, &message.sender)
if err != nil {
return nil, err
}
err = binary.Read(reader, binary.BigEndian, &message.receiver)
if err != nil {
return nil, err
}
datalength := length - 16*2
if datalength > 0 {
data := make([]byte, datalength)
err := binary.Read(reader, binary.BigEndian, &data)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &message.command)
if err != nil {
return nil, err
}
}
return &message, nil
}
// SenderUUID returns the sender identity
func (m *Message) SenderUUID() uuid.UUID {
return m.sender
}
// ReceiverUUID returns the receiver identity
func (m *Message) ReceiverUUID() (uuid.UUID, error) {
return m.receiver, nil
}
// Command returns message content as a Command object
func (m *Message) Command() *types.Command {
return m.command
}
// Bytes returns the binary rapresentation of the message
func (m *Message) Bytes() ([]byte, error) {
data, err := json.Marshal(m.command)
if err != nil {
return []byte{}, err
}
length := uint32(16*2 + len(data))
buffer := bytes.Buffer{}
binary.Write(&buffer, binary.BigEndian, length)
binary.Write(&buffer, binary.BigEndian, m.sender)
binary.Write(&buffer, binary.BigEndian, m.receiver)
binary.Write(&buffer, binary.BigEndian, data)
return buffer.Bytes(), nil
}
// Equal compares two messages
func Equal(v1, v2 *Message) bool {
if v1 != nil && v2 != nil {
if v1.sender != v2.sender {
return false
}
if v1.receiver != v2.receiver {
return false
}
if !reflect.DeepEqual(v1.command, v2.command) {
return false
}
return true
}
return v1 == v2
}
func (m Message) String() string {
return fmt.Sprintf("{%v, %v, %v}", m.sender, m.receiver, m.command)
}