-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
152 lines (137 loc) · 3.3 KB
/
message.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
package ship
import (
"encoding/json"
"encoding/xml"
"io"
"time"
"github.com/LMF-DHBW/go-eebus/resources"
"golang.org/x/net/websocket"
)
type SMEInstance struct {
role string
connectionState string
Connection *websocket.Conn
closeHandler CloseHandler
Ski string
}
type CmiMessage struct {
MessageType int `json:"MessageType"`
MessageValue int `json:"MessageValue"`
}
type Message struct {
MessageType int `xml:"MessageType"`
MessageValue DataValue
}
type DataValue struct {
Header HeaderType `xml:"header"`
Payload resources.DatagramType `xml:"payload"`
}
type HeaderType struct {
ProtocollId string `xml:"protocollId"`
}
const CMI_TIMEOUT = 10
func (SME *SMEInstance) RecieveTimeout(seconds int) []byte {
ch := make(chan bool)
msgch := make(chan []byte)
go SME.RecieveOnce(func(msg []byte) {
ch <- true
msgch <- msg
})
time.AfterFunc(time.Second*time.Duration(seconds), func() { ch <- false })
if !<-ch {
msgch <- make([]byte, 0)
}
return <-msgch
}
func (SME *SMEInstance) StartCMI() {
if SME.role == "server" {
SME.connectionState = "CMI_STATE_SERVER_WAIT"
msg := SME.RecieveTimeout(CMI_TIMEOUT)
if len(msg) == 0 {
SME.Connection.Close()
return
}
SME.connectionState = "CMI_STATE_SERVER_EVALUATE"
Message := CmiMessage{}
resources.CheckError(json.Unmarshal(msg, &Message))
if Message.MessageType != 0 || Message.MessageValue != 0 {
defer SME.Connection.Close()
}
bytes, err := json.Marshal(CmiMessage{
MessageType: 0,
MessageValue: 0,
})
resources.CheckError(err)
websocket.Message.Send(SME.Connection, bytes)
} else {
SME.connectionState = "CMI_STATE_CLIENT_SEND"
bytes, err := json.Marshal(CmiMessage{
MessageType: 0,
MessageValue: 0,
})
resources.CheckError(err)
websocket.Message.Send(SME.Connection, bytes)
SME.connectionState = "CMI_STATE_CLIENT_WAIT"
msg := SME.RecieveTimeout(CMI_TIMEOUT)
if len(msg) == 0 {
SME.Connection.Close()
return
}
SME.connectionState = "CMI_STATE_CLIENT_EVALUATE"
Message := CmiMessage{}
resources.CheckError(json.Unmarshal(msg, &Message))
if Message.MessageType != 0 || Message.MessageValue != 0 {
SME.Connection.Close()
}
}
}
/* Use XML format
- Notify SPINE node that connection is active
- Recieve and send methods should be available
*/
func (SME *SMEInstance) RecieveOnce(handleFunc handler) {
var msg []byte
err := websocket.Message.Receive(SME.Connection, &msg)
if err != nil {
if err == io.EOF {
return
}
SME.Connection.Close()
SME.closeHandler(SME)
return
}
handleFunc(msg)
}
func (SME *SMEInstance) Recieve(handleFunc dataHandler) {
var msg []byte
for {
err := websocket.Message.Receive(SME.Connection, &msg)
if err != nil {
if err == io.EOF {
break
}
SME.Connection.Close()
SME.closeHandler(SME)
break
}
Message := Message{}
resources.CheckError(xml.Unmarshal(msg, &Message))
if Message.MessageType == 2 {
handleFunc(Message.MessageValue.Payload)
}
}
}
/* Sends messages in json format */
func (SME *SMEInstance) Send(payload resources.DatagramType) {
bytes, err := xml.Marshal(Message{
MessageType: 2,
MessageValue: DataValue{
Header: HeaderType{
ProtocollId: "ee1.0",
},
Payload: payload,
},
})
resources.CheckError(err)
websocket.Message.Send(SME.Connection, bytes)
}