-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobmarshaller.go
57 lines (48 loc) · 1.42 KB
/
gobmarshaller.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
package middleware
import (
"bytes"
"encoding/gob"
"github.com/gfads/midarch/pkg/gmidarch/development/messages"
"github.com/gfads/midarch/pkg/gmidarch/development/messages/miop"
"github.com/gfads/midarch/pkg/shared"
)
// @Type: Gobmarshaller
// @Behaviour: Behaviour = InvP.e1 -> I_Process -> TerP.e1 -> Behaviour
type Gobmarshaller struct{}
func (g Gobmarshaller) I_Process(id string, msg *messages.SAMessage, info *interface{}, reset *bool) {
gob.Register(messages.FunctionalRequest{})
gob.Register(messages.AOR{}) // TODO - perhaps put in init()
req := msg.Payload.(messages.FunctionalRequest)
op := req.Op
switch op {
case "marshall":
r := g.Marshall(req.Params[0])
msg.Payload = messages.FunctionalReply{Rep: r}
case "unmarshall":
temp := req.Params[0].([]byte)
r := g.Unmarshall(temp)
msg.Payload = messages.FunctionalReply{Rep: r}
default:
shared.ErrorHandler(shared.GetFunction(), "Operation '"+op+"' not supported by Marshaller!")
}
}
func (Gobmarshaller) Marshall(m interface{}) []byte {
var b bytes.Buffer
enc := gob.NewEncoder(&b)
err := enc.Encode(m)
if err != nil {
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
return b.Bytes()
}
func (Gobmarshaller) Unmarshall(m []byte) interface{} {
r := new(miop.MiopPacket)
var b bytes.Buffer
b.Write(m)
dec := gob.NewDecoder(&b)
err := dec.Decode(r)
if err != nil {
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
return *r
}