-
Notifications
You must be signed in to change notification settings - Fork 0
/
srh-https.go
199 lines (173 loc) · 7.67 KB
/
srh-https.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
package middleware
import (
"io"
"strings"
"time"
"github.com/gfads/midarch/pkg/gmidarch/development/messages"
"github.com/gfads/midarch/pkg/gmidarch/development/messages/miop"
"github.com/gfads/midarch/pkg/gmidarch/development/protocols"
"github.com/gfads/midarch/pkg/shared"
"github.com/gfads/midarch/pkg/shared/lib"
)
// @Type: SRHHTTPS
// @Behaviour: Behaviour = I_Accept -> I_Receive -> InvR.e1 -> TerR.e1 -> I_Send -> Behaviour
type SRHHTTPS struct {
// Graph exec.ExecGraph
}
func (s SRHHTTPS) I_Accept(id string, msg *messages.SAMessage, info *interface{}, reset *bool) {
//lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "SRHHTTPS Version 2 adapted")
infoTemp := *info
srhInfo := infoTemp.(*messages.SRHInfo)
// srhInfo.Counter++
//log.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total Cons", len(srhInfo.Clients))
//log.Println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Counter", srhInfo.Counter)
if srhInfo.Protocol == nil {
srhInfo.Protocol = &protocols.HTTPS{}
srhInfo.Protocol.StartServer(srhInfo.EndPoint.Host, srhInfo.EndPoint.Port, 2) //shared.MAX_NUMBER_OF_CONNECTIONS)
lib.PrintlnInfo("SRHHTTPS Server Started")
}
// // check if a listener has already been created
// if srhInfo.Ln == nil { // no listen created
// servAddr, err := net.ResolveTCPAddr("tcp", srhInfo.EndPoint.Host+":"+srhInfo.EndPoint.Port)
// if err != nil {read
// shared.ErrorHandler(shared.GetFunction(), err.Error())
// }
// srhInfo.Ln, err = net.ListenTCP("tcp", servAddr)
// if err != nil {
// shared.ErrorHandler(shared.GetFunction(), err.Error())
// }
// }
//log.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total Clients out", len(srhInfo.Clients))
connectionAvailable, availableConenctionIndex := srhInfo.Protocol.AvailableConnectionFromPool()
//log.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Total Clients out", len(srhInfo.Clients))
if !connectionAvailable {
//lib.PrintlnDebug("------------------------------>", shared.GetFunction(), "end", "SRHHTTPS Version 2 adapted - No connection available")
time.Sleep(1 * time.Millisecond)
return
}
// RPC is already executed concurrently, dont need go func
// go func() {
lib.PrintlnInfo("SRHHTTPS Clients Index", availableConenctionIndex)
client := srhInfo.Protocol.WaitForConnection(availableConenctionIndex)
lib.PrintlnInfo("SRHHTTPS Client", client)
// Update info
*info = srhInfo
// Start goroutine
if client != nil {
go s.handler(info, availableConenctionIndex)
}
// }()
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "end", "SRHHTTPS Version Not adapted")
return
}
func (s SRHHTTPS) I_Receive(id string, msg *messages.SAMessage, info *interface{}, reset *bool) {
// lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "SRHHTTPS 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.PrintlnInfo("SRHHTTPS Version Not adapted: tempMsgReceived", tempMsgReceived)
if isNewConnection, miopPacket := s.isNewConnection(tempMsgReceived.Msg); isNewConnection { // TODO dcruzb: move to I_Receive
lib.PrintlnInfo("SRHHTTPS Version Not adapted: tempMsgReceived >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", miopPacket)
*reset = true
return
}
lib.PrintlnInfo("SRHHTTPS tempMsgReceived.ToAddress", tempMsgReceived.ToAddress)
msg.ToAddr = tempMsgReceived.ToAddress //Chn.RemoteAddr().String()
}
default:
{
*reset = true
return
}
}
lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "end", "SRHHTTPS Version Not adapted")
return
}
func (s SRHHTTPS) I_Send(id string, msg *messages.SAMessage, info *interface{}, reset *bool) {
lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "SRHHTTPS Version Not adapted")
infoTemp := *info
srhInfo := infoTemp.(*messages.SRHInfo)
lib.PrintlnInfo("msg.ToAddr", msg.ToAddr)
client := srhInfo.Protocol.GetClientFromAddr(msg.ToAddr)
if client == nil {
*reset = true
return
}
lib.PrintlnInfo("SRHHTTPS Version Not adapted >>>>> RPC => msg.ToAddr:", msg.ToAddr, "RPC Client:", client) //, "AdaptId:", client.AdaptId) // TODO dcruzb: verify impact of removing AdaptId
msgTemp := msg.Payload.([]byte)
err := client.Send(msgTemp)
if err != nil {
shared.ErrorHandler(shared.GetFunction(), err.Error())
}
// update info
*info = srhInfo
lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "end", "SRHHTTPS Version Not adapted")
return
}
func (s SRHHTTPS) handler(info *interface{}, connectionIndex int) {
lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "SRHHTTPS Version Not adapted")
infoTemp := *info
srhInfo := infoTemp.(*messages.SRHInfo)
// conn := srhInfo.Clients[connectionIndex].Connection //CurrentConn
client := srhInfo.Protocol.GetClient(connectionIndex)
executeForever := srhInfo.ExecuteForever
for {
if !*executeForever {
break
}
lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "FOR", "SRHHTTPS Version Not adapted")
msg, err := client.Receive()
if err != nil {
if err == io.EOF || strings.Contains(err.Error(), "use of closed network connection") {
break
}
}
lib.PrintlnInfo("SRHHTTPS got message")
if changeProtocol, miopPacket := s.isAdapt(msg); changeProtocol {
if miopPacket.Bd.ReqBody.Body[2] == "Ok" {
lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "Received Ok to Adapt", "SRHHTTPS Version Not adapted")
break
}
}
if isNewConnection, _ := s.isNewConnection(msg); isNewConnection { // TODO dcruzb: move to I_Receive
//newConnection = true
lib.PrintlnInfo("RPC 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.PrintlnInfo("RPC Before send")
//s.send(conn, addr, msgPayload)
lib.PrintlnInfo("RPC After send")
//if miopPacket.Bd.ReqBody.Body[2] == "Ok" {
// lib.PrintlnDebug("----------------------------------------->", shared.GetFunction(), "Received Ok to Adapt", "SRHUDP Version Not adapted")
// break
//}
continue
}
rcvMessage := messages.ReceivedMessages{Msg: msg, Conn: nil, ToAddress: srhInfo.Protocol.GetClient(connectionIndex).Address()}
lib.PrintlnInfo("SRHHTTPS Version Not adapted: handler >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> received message")
if !*executeForever {
break
}
srhInfo.RcvedMessages <- rcvMessage
lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "FOR end", "SRHHTTPS Version Not adapted")
}
lib.PrintlnInfo("----------------------------------------->", shared.GetFunction(), "end", "SRHHTTPS Version Not adapted")
}
func (s SRHHTTPS) isAdapt(msgFromServer []byte) (bool, miop.MiopPacket) {
//log.Println("----------------------------------------->", shared.GetFunction(), "CRHTCP Version Not adapted")
miop := Jsonmarshaller{}.Unmarshall(msgFromServer)
return miop.Bd.ReqHeader.Operation == "ChangeProtocol", miop
}
func (s SRHHTTPS) isNewConnection(msgFromServer []byte) (bool, miop.MiopPacket) {
//log.Println("----------------------------------------->", shared.GetFunction(), "CRHTCP Version Not adapted")
miop := Jsonmarshaller{}.Unmarshall(msgFromServer)
return miop.Bd.ReqHeader.Operation == "Connect", miop
}