aboutsummaryrefslogtreecommitdiff
path: root/inform/inform.go
diff options
context:
space:
mode:
Diffstat (limited to 'inform/inform.go')
-rw-r--r--inform/inform.go44
1 files changed, 1 insertions, 43 deletions
diff --git a/inform/inform.go b/inform/inform.go
index b159084..5c3ca1c 100644
--- a/inform/inform.go
+++ b/inform/inform.go
@@ -3,13 +3,11 @@ package inform
3import ( 3import (
4 "bytes" 4 "bytes"
5 "encoding/json" 5 "encoding/json"
6 "errors"
7 "fmt" 6 "fmt"
8 "github.com/mitchellh/mapstructure"
9) 7)
10 8
11const ( 9const (
12 PROTOCOL_MAGIC int32 = 1414414933 // TNBU 10 PROTOCOL_MAGIC int32 = 1414414933 // UBNT
13 INFORM_VERSION int32 = 0 11 INFORM_VERSION int32 = 0
14 DATA_VERSION int32 = 1 12 DATA_VERSION int32 = 1
15 13
@@ -91,43 +89,3 @@ func (i *InformWrapper) SetCompressed(c bool) {
91 i.Flags &= COMPRESSED_FLAG 89 i.Flags &= COMPRESSED_FLAG
92 } 90 }
93} 91}
94
95// Decode payload to a map and try to determine the inform type
96func (i *InformWrapper) decodePayload() (map[string]interface{}, string, error) {
97 var m map[string]interface{}
98
99 if err := json.Unmarshal(i.Payload, &m); err != nil {
100 return nil, "", err
101 }
102
103 t, ok := m["_type"]
104 if !ok {
105 return nil, "", errors.New("Message contains no type")
106 }
107
108 st, ok := t.(string)
109 if !ok {
110 return nil, "", errors.New("Message type is not a string")
111 }
112
113 return m, st, nil
114}
115
116// Decode payload JSON as a inform message
117func (i *InformWrapper) JsonMessage() (interface{}, error) {
118 msg, t, err := i.decodePayload()
119 if err != nil {
120 return nil, err
121 }
122
123 switch t {
124 case "noop":
125 var o NoopMessage
126 if err := mapstructure.Decode(msg, &o); err != nil {
127 return nil, err
128 }
129 return o, nil
130 }
131
132 return nil, errors.New(fmt.Sprintf("Message type %s is invalid", t))
133}