-
Notifications
You must be signed in to change notification settings - Fork 0
/
srhudp.go
327 lines (295 loc) · 12 KB
/
srhudp.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package middleware
import (
"encoding/binary"
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
"github.com/gfads/midarch/pkg/gmidarch/development/messages"
"github.com/gfads/midarch/pkg/gmidarch/development/messages/miop"
"github.com/gfads/midarch/pkg/shared"
"github.com/gfads/midarch/pkg/shared/lib"
)
// @Type: SRHUDP
// @Behaviour: Behaviour = I_Accept -> I_Receive -> InvR.e1 -> TerR.e1 -> I_Send -> Behaviour
type SRHUDP struct{}
func (s SRHUDP) availableConnectionFromPool(clientsPtr *[]*messages.Client) (bool, int) {
clients := *clientsPtr
//lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total clients", len(clients))
if len(clients) < 1 { //shared.MAX_NUMBER_OF_CONNECTIONS { // UDP don't open different connections
client := messages.Client{
Ip: "",
Connection: nil,
UDPConnection: nil,
}
*clientsPtr = append(clients, &client)
lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total Clients", len(*clientsPtr))
lib.PrintlnDebug("len(clients) < 1")
return true, len(*clientsPtr) - 1
}
for idx, client := range clients {
if idx >= 1 { //shared.MAX_NUMBER_OF_CONNECTIONS
lib.PrintlnDebug("idx >= 1")
break
}
if client == nil {
lib.PrintlnDebug("client == nil")
client := messages.Client{
Ip: "",
Connection: nil,
UDPConnection: nil,
}
clients[idx] = &client
return true, idx
}
if client.Connection != nil {
lib.PrintlnDebug("Zerou Connection")
client.Ip = ""
client.Connection.Close()
client.Connection = nil
return true, idx
}
if client.UDPConnection == nil {
lib.PrintlnDebug("UDPConnection == nil")
return true, idx
}
}
return false, -1
}
func (s SRHUDP) I_Accept(id string, msg *messages.SAMessage, info *interface{}, reset *bool) {
//lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "SRHUDP Version Not adapted")
infoTemp := *info
srhInfo := infoTemp.(*messages.SRHInfo)
// srhInfo.Counter++
//lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total Cons", len(srhInfo.Clients))
//lib.PrintlnDebug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Counter", srhInfo.Counter)
var servAddr *net.UDPAddr
var err error
// check if a listen has been already created
//if srhInfo.UDPConnection == nil { // no listen created
// servAddr, err = net.ResolveUDPAddr("udp4", srhInfo.EndPoint.Host+":"+srhInfo.EndPoint.Port)
// if err != nil {
// shared.ErrorHandler(shared.GetFunction(), err.Error())
// }
// //srhInfo.Ln, err = net.ListenUDP("udp4", servAddr)
// //if err != nil {
// // shared.ErrorHandler(shared.GetFunction(), err.Error())
// //}
// srhInfo.UDPConnection, err = net.ListenUDP("udp4", servAddr) //, err := srhInfo.Ln.Accept()
// if err != nil {
// shared.ErrorHandler(shared.GetFunction(), err.Error())
// }
//}
//lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total Clients out", len(srhInfo.Clients))
connectionAvailable, availableConenctionIndex := s.availableConnectionFromPool(&srhInfo.Clients)
//lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total Clients out", len(srhInfo.Clients))
if !connectionAvailable {
time.Sleep(1 * time.Millisecond)
//log.Println("------------------------------>", shared.GetFunction(), "end", "SRHUDP Version Not adapted - No connection available")
return
}
//go func() {
client := srhInfo.Clients[availableConenctionIndex]
lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Clients Index", availableConenctionIndex)
lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Clients", srhInfo.Clients, srhInfo.Clients[availableConenctionIndex])
// Accept connections
//conn, err := net.ListenUDP("udp4", servAddr) //, err := srhInfo.Ln.Accept()
//if err != nil {
// shared.ErrorHandler(shared.GetFunction(), err.Error())
//}
if client == nil {
*reset = true
return
}
if client.UDPConnection == nil { // no listen created
servAddr, err = net.ResolveUDPAddr("udp4", srhInfo.EndPoint.Host+":"+srhInfo.EndPoint.Port)
if err != nil {
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
//srhInfo.Ln, err = net.ListenUDP("udp4", servAddr)
//if err != nil {
// shared.ErrorHandler(shared.GetFunction(), err.Error())
//}
client.UDPConnection, err = net.ListenUDP("udp4", servAddr) //, err := srhInfo.Ln.Accept()
if err != nil {
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
}
srhInfo.Conns = append(srhInfo.Conns, client.UDPConnection)
//srhInfo.CurrentConn = conn
client.Ip = "" //conn.RemoteAddr().String() UDP dont start with RemoteAddr
//client.UDPConnection = srhInfo.UDPConnection
lib.PrintlnDebug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Connected Client", client)
// Update info
*info = srhInfo
// Start goroutine
go s.handler(info, availableConenctionIndex)
//}()
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "end", "SRHUDP Version Not adapted")
return
}
func (s SRHUDP) I_Receive(id string, msg *messages.SAMessage, info *interface{}, reset *bool) {
//lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "SRHUDP Version Not adapted")
//lib.PrintlnDebug(shared.GetFunction(), "HERE")
infoTemp := *info
srhInfo := infoTemp.(*messages.SRHInfo)
select {
case tempMsgReceived := <-srhInfo.RcvedMessages:
{
// Receive message from handlers
//srhInfo.CurrentConn = tempMsgReceived.Conn
// Update info
*info = srhInfo
msg.Payload = tempMsgReceived.Msg
lib.PrintlnDebug("SRHUDP Version Not adapted: tempMsgReceived >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", tempMsgReceived)
//if isNewConnection, miopPacket := s.isNewConnection(tempMsgReceived.Msg); isNewConnection { // TODO dcruzb: move to I_Receive
// fmt.Println("SRHUDP Version Not adapted: tempMsgReceived >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", miopPacket)
// *reset = true
// return
//}
msg.ToAddr = tempMsgReceived.ToAddress
}
default:
{
*reset = true
return
}
}
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "end", "SRHUDP Version Not adapted")
return
}
func (s SRHUDP) I_Send(id string, msg *messages.SAMessage, info *interface{}, reset *bool) {
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "SRHUDP Version Not adapted")
infoTemp := *info
srhInfo := infoTemp.(*messages.SRHInfo)
lib.PrintlnDebug("msg.ToAddr", msg.ToAddr, "srhInfo.Clients", srhInfo.Clients)
client := srhInfo.GetClientFromAddr(msg.ToAddr, srhInfo.Clients)
conn := client.UDPConnection //srhInfo.CurrentConn
if conn == nil {
fmt.Println("SRHUDP.send reset when conn == nil")
*reset = true
return
}
lib.PrintlnDebug("UDP conn:", conn)
msgTemp := msg.Payload.([]byte)
addr := strings.Split(msg.ToAddr, ":")
ip := net.ParseIP(addr[0])
port, _ := strconv.Atoi(addr[1])
s.send(conn, &net.UDPAddr{IP: ip, Port: port}, msgTemp)
// update info
*info = srhInfo
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "end", "SRHUDP Version Not adapted")
return
}
func (s SRHUDP) send(conn *net.UDPConn, addr *net.UDPAddr, msgTemp []byte) {
// send message's size
size := make([]byte, shared.SIZE_OF_MESSAGE_SIZE, shared.SIZE_OF_MESSAGE_SIZE)
binary.LittleEndian.PutUint32(size, uint32(len(msgTemp)))
_, err := conn.WriteTo(size, addr)
if err != nil {
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
//json := Jsonmarshaller{}
//unmarshalledMsg := json.Unmarshall(msgTemp)
//lib.PrintlnDebug("<<<<<<<<<<<< <<<<<<<<<< <<<<<<<<< SRHUDP Version Not adapted => Msg: ", unmarshalledMsg.Bd.RepBody.OperationResult)
// send message
_, err = conn.WriteTo(msgTemp, addr)
if err != nil {
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
}
func (s *SRHUDP) handler(info *interface{}, connectionIndex int) {
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "SRHUDP Version Not adapted")
infoTemp := *info
srhInfo := infoTemp.(*messages.SRHInfo)
conn := srhInfo.Clients[connectionIndex].UDPConnection //CurrentConn
executeForever := srhInfo.ExecuteForever
for {
if !*executeForever {
break
}
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "FOR", "SRHUDP Version Not adapted")
size := make([]byte, shared.SIZE_OF_MESSAGE_SIZE, shared.SIZE_OF_MESSAGE_SIZE)
//err := conn.SetReadDeadline(time.Now().Add(10*time.Second))
//if err != nil {
// lib.PrintlnError(shared.GetFunction(), err.Error())
//}
_, addr, err := conn.ReadFromUDP(size)
if len(srhInfo.Clients) == 0 || srhInfo.Clients[connectionIndex] == nil {
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "Handler without client")
break
}
srhInfo.Clients[connectionIndex].Ip = addr.String()
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "Read efetuado", "SRHUDP Version Not adapted")
if err != nil {
if err == io.EOF ||
strings.Contains(err.Error(), "use of closed network connection") ||
strings.Contains(err.Error(), "i/o timeout") {
srhInfo.Clients[connectionIndex] = nil
lib.PrintlnDebug("Não Vai matar o app EOF", err)
break
} else if err != nil && err != io.EOF {
lib.PrintlnDebug("Vai matar o app, erro mas não EOF. Error:", err)
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
}
// receive message
msgTemp := make([]byte, binary.LittleEndian.Uint32(size))
//err = conn.SetReadDeadline(time.Now().Add(10*time.Second))
//if err != nil {
// lib.PrintlnError(shared.GetFunction(), err.Error())
//}
_, err = conn.Read(msgTemp)
if err != nil {
if err == io.EOF ||
strings.Contains(err.Error(), "use of closed network connection") ||
strings.Contains(err.Error(), "i/o timeout") {
srhInfo.Clients[connectionIndex] = nil
lib.PrintlnDebug("Não Vai matar o app EOF")
break
} else if err != nil && err != io.EOF {
lib.PrintlnDebug("Vai matar o app, erro mas não EOF")
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
}
if changeProtocol, miopPacket := s.isAdapt(msgTemp); changeProtocol {
if miopPacket.Bd.ReqBody.Body[2] == "Ok" {
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "Received Ok to Adapt", "SRHUDP Version Not adapted")
break
}
}
if isNewConnection, miopPacket := s.isNewConnection(msgTemp); isNewConnection { // TODO dcruzb: move to I_Receive
lib.PrintlnDebug("Is New Connection")
miopPacket := miop.CreateReqPacket("Connect", []interface{}{miopPacket.Bd.ReqBody.Body[0], "Ok"}, miopPacket.Bd.ReqBody.Body[0].(int)) // idx is the Connection ID
msgPayload := Jsonmarshaller{}.Marshall(miopPacket)
lib.PrintlnDebug("Before send")
s.send(conn, addr, msgPayload)
lib.PrintlnDebug("After send")
//if miopPacket.Bd.ReqBody.Body[2] == "Ok" {
// lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "Received Ok to Adapt", "SRHUDP Version Not adapted")
// break
//}
continue
}
lib.PrintlnDebug("SRHUDP Version Not adapted: handler >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> received message")
rcvMessage := messages.ReceivedMessages{Msg: msgTemp, Conn: nil, ToAddress: addr.String()}
if !*executeForever {
break
}
srhInfo.RcvedMessages <- rcvMessage
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "FOR end", "SRHUDP Version Not adapted")
}
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "end", "SRHUDP Version Not adapted")
}
func (s SRHUDP) isAdapt(msgFromServer []byte) (bool, miop.MiopPacket) {
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "CRHTCP Version Not adapted")
miop := Jsonmarshaller{}.Unmarshall(msgFromServer)
return miop.Bd.ReqHeader.Operation == "ChangeProtocol", miop
}
func (s SRHUDP) isNewConnection(msgFromServer []byte) (bool, miop.MiopPacket) {
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "CRHTCP Version Not adapted")
miop := Jsonmarshaller{}.Unmarshall(msgFromServer)
return miop.Bd.ReqHeader.Operation == "Connect", miop
}