-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenvelope.go
242 lines (199 loc) · 5.73 KB
/
envelope.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package lime
import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
)
// Envelope is the base struct to all protocol envelopes.
type Envelope struct {
// ID is the envelope identifier
ID string
// From is the identifier of the sender node of the envelope.
// If a node receives an envelope without this value, it means that the envelope was originated by the remote party.
From Node
// PP is the delegation node. It's an acronym for 'per procurationem'.
// Identifier of a delegate node (a node that received a permission To send on behalf of another).
// Allows a node to send an envelope on behalf of another identity.
PP Node
// To is the identifier of the destination node of the envelope.
// If a node receives an envelope without this value, it means that the envelope is addressed To itself.
To Node
// Metadata holds additional information to be delivered with the envelope.
Metadata map[string]string
}
func (env *Envelope) SetID(id string) *Envelope {
env.ID = id
return env
}
func (env *Envelope) SetNewEnvelopeID() *Envelope {
env.ID = NewEnvelopeID()
return env
}
func (env *Envelope) SetFrom(from Node) *Envelope {
env.From = from
return env
}
func (env *Envelope) SetFromString(s string) *Envelope {
from := ParseNode(s)
return env.SetFrom(from)
}
func (env *Envelope) SetTo(to Node) *Envelope {
env.To = to
return env
}
func (env *Envelope) SetToString(s string) *Envelope {
to := ParseNode(s)
return env.SetTo(to)
}
func (env *Envelope) SetPP(pp Node) *Envelope {
env.PP = pp
return env
}
func (env *Envelope) SetPPString(s string) *Envelope {
pp := ParseNode(s)
return env.SetPP(pp)
}
func (env *Envelope) SetMetadataKeyValue(key string, value string) *Envelope {
if env.Metadata == nil {
env.Metadata = make(map[string]string)
}
env.Metadata[key] = value
return env
}
// Sender returns the envelope sender Node.
func (env *Envelope) Sender() Node {
if env.PP == (Node{}) {
return env.PP
} else {
return env.From
}
}
func (env *Envelope) toRawEnvelope() (*rawEnvelope, error) {
raw := rawEnvelope{}
raw.ID = env.ID
if env.From != (Node{}) {
raw.From = &env.From
}
if env.PP != (Node{}) {
raw.PP = &env.PP
}
if env.To != (Node{}) {
raw.To = &env.To
}
raw.Metadata = env.Metadata
return &raw, nil
}
func (env *Envelope) populate(raw *rawEnvelope) error {
if raw == nil || env == nil {
return nil
}
env.ID = raw.ID
env.Metadata = raw.Metadata
if raw.From != nil {
env.From = *raw.From
}
if raw.PP != nil {
env.PP = *raw.PP
}
if raw.To != nil {
env.To = *raw.To
}
return nil
}
// Reason represents a known reason for events occurred during the client-server
// interactions.
type Reason struct {
Code int `json:"code,omitempty"` // The reason code
Description string `json:"description,omitempty"` // The reason description
}
func (r Reason) String() string {
return fmt.Sprintf("Code: %v - Description: %v", r.Code, r.Description)
}
// NewEnvelopeID generates a new unique envelope ID.
func NewEnvelopeID() string {
return uuid.New().String()
}
// envelope is the base interface for envelopes types.
type envelope interface {
populate(raw *rawEnvelope) error
toRawEnvelope() (*rawEnvelope, error)
}
// rawEnvelope is an intermediate type for marshalling.
type rawEnvelope struct {
// Common envelope properties
ID string `json:"id,omitempty"`
From *Node `json:"from,omitempty"`
PP *Node `json:"pp,omitempty"`
To *Node `json:"to,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
// Shared properties
Reason *Reason `json:"reason,omitempty"` // Shared by Notification and Message
Type *MediaType `json:"type,omitempty"` // Shared by Message and Command
// Message properties
Content *json.RawMessage `json:"content,omitempty"`
// Notification properties
Event *NotificationEvent `json:"event,omitempty"`
// Command properties
Method *CommandMethod `json:"method,omitempty"`
Resource *json.RawMessage `json:"resource,omitempty"`
// RequestCommand properties
URI *URI `json:"uri,omitempty"`
// ResponseCommand properties
Status *CommandStatus `json:"status,omitempty"`
// Session properties
State *SessionState `json:"state,omitempty"`
EncryptionOptions []SessionEncryption `json:"encryptionOptions,omitempty"`
Encryption *SessionEncryption `json:"encryption,omitempty"`
CompressionOptions []SessionCompression `json:"compressionOptions,omitempty"`
Compression *SessionCompression `json:"compression,omitempty"`
SchemeOptions []AuthenticationScheme `json:"schemeOptions,omitempty"`
Scheme *AuthenticationScheme `json:"scheme,omitempty"`
Authentication *json.RawMessage `json:"authentication,omitempty"`
}
func (re *rawEnvelope) envelopeType() (string, error) {
// Determine the envelope type
if re.Method != nil {
if re.URI != nil {
return "RequestCommand", nil
}
if re.Status != nil {
return "ResponseCommand", nil
}
}
if re.Event != nil {
return "Notification", nil
}
if re.Content != nil {
return "Message", nil
}
if re.State != nil {
return "Session", nil
}
return "", errors.New("could not determine the envelope type")
}
func (re *rawEnvelope) toEnvelope() (envelope, error) {
var env envelope
t, err := re.envelopeType()
if err != nil {
return nil, err
}
switch t {
case "RequestCommand":
env = &RequestCommand{}
case "ResponseCommand":
env = &ResponseCommand{}
case "Notification":
env = &Notification{}
case "Message":
env = &Message{}
case "Session":
env = &Session{}
default:
return nil, errors.New("unknown or unsupported envelope type")
}
if err := env.populate(re); err != nil {
return nil, err
}
return env, nil
}