-
Notifications
You must be signed in to change notification settings - Fork 1
/
envelope_carrier.go
156 lines (127 loc) · 3.58 KB
/
envelope_carrier.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
package kit
import (
"encoding/json"
"github.com/clubpay/ronykit/kit/utils"
"github.com/goccy/go-reflect"
)
type carrierKind int
const (
incomingCarrier carrierKind = iota + 1
outgoingCarrier
eofCarrier
)
// envelopeCarrier is a serializable message which is used by Cluster component of the
// EdgeServer to send information from one instance to another instance.
type envelopeCarrier struct {
// SessionID is a unique identifier for each remote-execution session.
SessionID string `json:"id"`
// Kind identifies what type of the data this carrier has
Kind carrierKind `json:"kind"`
// OriginID the instance's id of the sender of this message
OriginID string `json:"originID"`
// TargetID the instance's id of the receiver of this message
TargetID string `json:"targetID"`
Data *carrierData `json:"data"`
}
func (ec *envelopeCarrier) FillWithContext(ctx *Context) *envelopeCarrier {
ec.Data = &carrierData{
EnvelopeID: ctx.In().GetID(),
IsREST: ctx.IsREST(),
MsgType: reflect.TypeOf(ctx.In().GetMsg()).String(),
Msg: marshalEnvelopeCarrier(ctx.In().GetMsg()),
ContractID: ctx.ContractID(),
ServiceName: ctx.ServiceName(),
Route: ctx.Route(),
ConnHdr: map[string]string{},
Hdr: map[string]string{},
}
if tp := ctx.sb.tp; tp != nil {
tp.Inject(ctx.ctx, ec.Data)
}
ctx.In().
WalkHdr(func(key, val string) bool {
ec.Data.Hdr[key] = val
return true
})
ctx.Conn().
Walk(func(key string, val string) bool {
ec.Data.ConnHdr[key] = val
return true
})
return ec
}
func (ec *envelopeCarrier) FillWithEnvelope(e *Envelope) *envelopeCarrier {
ec.Data = &carrierData{
EnvelopeID: utils.B2S(e.id),
IsREST: e.ctx.IsREST(),
MsgType: reflect.TypeOf(e.GetMsg()).String(),
Msg: marshalEnvelopeCarrier(e.GetMsg()),
ContractID: e.ctx.ContractID(),
ServiceName: e.ctx.ServiceName(),
Route: e.ctx.Route(),
ConnHdr: map[string]string{},
Hdr: map[string]string{},
}
e.WalkHdr(func(key string, val string) bool {
ec.Data.Hdr[key] = val
return true
})
e.conn.Walk(func(key string, val string) bool {
ec.Data.ConnHdr[key] = val
return true
})
return ec
}
type carrierData struct {
EnvelopeID string `json:"envelopID,omitempty"`
ConnHdr map[string]string `json:"connHdr,omitempty"`
IsREST bool `json:"isREST,omitempty"`
Hdr map[string]string `json:"hdr,omitempty"`
MsgType string `json:"msgType,omitempty"`
Msg []byte `json:"msg,omitempty"`
ContractID string `json:"cid,omitempty"`
ServiceName string `json:"svc,omitempty"`
Route string `json:"route,omitempty"`
}
func (c carrierData) Get(key string) string {
return c.ConnHdr[key]
}
func (c carrierData) Set(key string, value string) {
c.ConnHdr[key] = value
}
func (ec *envelopeCarrier) ToJSON() []byte {
data, _ := json.Marshal(ec)
return data
}
func (ec *envelopeCarrier) FromJSON(data []byte) error {
err := json.Unmarshal(data, ec)
if err != nil {
return err
}
return nil
}
func newEnvelopeCarrier(kind carrierKind, sessionID, originID, targetID string) *envelopeCarrier {
return &envelopeCarrier{
Kind: kind,
SessionID: sessionID,
OriginID: originID,
TargetID: targetID,
}
}
func unmarshalEnvelopeCarrier(data []byte, m Message) {
err := json.Unmarshal(data, m)
if err != nil {
panic(err)
}
}
func marshalEnvelopeCarrier(m Message) []byte {
switch v := m.(type) {
case RawMessage:
return v
}
data, err := json.Marshal(m)
if err != nil {
panic(err)
}
return data
}