-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_connection.go
914 lines (766 loc) · 20.8 KB
/
cloud_connection.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
package cloud
import (
"crypto/tls"
"encoding/binary"
"encoding/json"
"errors"
"github.com/gazercloud/gazernode/common_interfaces"
"github.com/gazercloud/gazernode/logger"
"github.com/gazercloud/gazernode/protocols/nodeinterface"
"github.com/gazercloud/gazernode/settings"
"io/ioutil"
"math/rand"
"net"
"net/http"
"strconv"
"sync"
"time"
)
type Connection struct {
mtx sync.Mutex
addr string
conn net.Conn
httpClient *http.Client
started bool
stopping bool
sessionId string
userName string
password string
nodeId string
requester common_interfaces.Requester
connectionStatus string
loginStatus string
iamStatus string
disconnectedSent bool
calls map[string]int64
callPerSecond float64
receivedBytesPerSecond float64
sentBytesPerSecond float64
callCount int64
receivedBytes int64
sentBytes int64
lastReceivedBytes int64
lastSentBytes int64
lastCallCount int64
lastCallCountDT time.Time
proxyTasks map[string]*ProxyTask
allowIncomingFunctions map[string]bool
}
func NewConnection() *Connection {
var c Connection
c.addr = ""
c.userName = ""
c.password = ""
c.nodeId = ""
c.allowIncomingFunctions = make(map[string]bool)
c.allowIncomingFunctions["unit_state_all"] = true
c.allowIncomingFunctions["unit_state"] = true
c.allowIncomingFunctions["unit_items_values"] = true
c.allowIncomingFunctions["data_item_list"] = true
c.allowIncomingFunctions["unit_start"] = true
c.allowIncomingFunctions["unit_stop"] = true
c.allowIncomingFunctions["public_channel_list"] = true
c.allowIncomingFunctions["public_channel_add"] = true
c.allowIncomingFunctions["public_channel_set_name"] = true
c.allowIncomingFunctions["public_channel_remove"] = true
c.allowIncomingFunctions["public_channel_item_add"] = true
c.allowIncomingFunctions["public_channel_item_remove"] = true
c.allowIncomingFunctions["public_channel_item_state"] = true
c.allowIncomingFunctions["public_channel_start"] = true
c.allowIncomingFunctions["public_channel_stop"] = true
c.allowIncomingFunctions["data_item_list"] = true
c.allowIncomingFunctions["data_item_list_all"] = true
c.allowIncomingFunctions["data_item_write"] = true
c.allowIncomingFunctions["data_item_history"] = true
c.allowIncomingFunctions["data_item_history_chart"] = true
c.allowIncomingFunctions["data_item_remove"] = true
c.calls = make(map[string]int64)
c.proxyTasks = make(map[string]*ProxyTask)
tr := &http.Transport{
TLSClientConfig: &tls.Config{},
}
c.httpClient = &http.Client{Transport: tr, Timeout: 1 * time.Second}
return &c
}
func (c *Connection) SetRequester(requester common_interfaces.Requester) {
c.requester = requester
}
func (c *Connection) Connected() bool {
return c.conn != nil
}
func (c *Connection) CloseConnection() {
c.mtx.Lock()
_ = c.conn.Close()
c.mtx.Unlock()
}
func (c *Connection) Start() {
if c.started {
return
}
c.started = true
c.stopping = false
c.LoadSession()
go c.thConn()
}
func (c *Connection) Started() bool {
return c.started
}
func (c *Connection) Stop() {
if !c.started {
return
}
c.stopping = true
if c.conn != nil {
_ = c.conn.Close()
}
for i := 0; i < 100; i++ {
time.Sleep(10 * time.Millisecond)
if !c.started {
break
}
}
}
func (c *Connection) LoadSession() error {
configString, err := ioutil.ReadFile(settings.ServerDataPath() + "/cloud_session.json")
if err == nil {
var config SessionConfig
err = json.Unmarshal(configString, &config)
if err == nil {
logger.Println("CloudConnection LoadSession:", config.Key)
c.sessionId = config.Key
c.userName = config.UserName
c.nodeId = config.NodeId
} else {
logger.Println("CloudConnection LoadSession unmarshal error:", err)
}
} else {
logger.Println("CloudConnection LoadSession read file error:", err)
}
return err
}
func (c *Connection) SaveSession() error {
var config SessionConfig
config.Key = c.sessionId
config.UserName = c.userName
config.NodeId = c.nodeId
bs, err := json.MarshalIndent(config, "", " ")
if err == nil {
err = ioutil.WriteFile(settings.ServerDataPath()+"/cloud_session.json", bs, 0600)
}
return err
}
func (c *Connection) Login(userName string, password string) {
logger.Println("CloudConnection login", userName, password)
c.mtx.Lock()
c.userName = userName
c.password = password
c.sessionId = ""
c.mtx.Unlock()
c.openSession()
}
func (c *Connection) Logout() {
logger.Println("CloudConnection logout")
c.loginStatus = "logged out"
var err error
type CloseSessionRequest struct {
Key string `json:"key"`
}
var frame BinFrame
var closeSessionRequest CloseSessionRequest
closeSessionRequest.Key = c.sessionId
// Send LogOut frame to the cloud
frame.Header.Src = ""
frame.Header.Dest = ""
frame.Header.Function = "session_close"
frame.Header.TransactionId = ""
frame.Header.SessionId = ""
frame.Data, err = json.Marshal(closeSessionRequest)
c.SendData(&frame)
// Clear local data
c.mtx.Lock()
c.password = ""
c.sessionId = ""
c.mtx.Unlock()
// Save cleared local data
err = c.SaveSession()
if err != nil {
logger.Println("CloudConnection save session error", err)
return
}
logger.Println("CloudConnection logout ok")
}
func (c *Connection) SessionId() string {
c.mtx.Lock()
result := c.sessionId
c.mtx.Unlock()
return result
}
func (c *Connection) UserName() string {
return c.userName
}
func (c *Connection) updateCurrentRepeater() {
logger.Println("updateCurrentRepeater")
c.connectionStatus = "repeater search"
IPs, err := net.LookupIP("home.gazer.cloud")
if err != nil {
logger.Println("updateCurrentRepeater error (LookupIP)", err)
return
}
logger.Println("updateCurrentRepeater IPs:", IPs)
//currentIndex := rand.Int() % len(IPs)
//currentIP := IPs[currentIndex].String()
req, _ := http.NewRequest("GET", "https://home.gazer.cloud/api/request?fn=s-repeater-for-node", nil)
response, err := c.httpClient.Transport.RoundTrip(req)
//link := "https://home.gazer.cloud/api/request?fn=s-repeater-for-node"
//response, err := c.httpClient.Get(link)
if err != nil {
c.connectionStatus = "repeater search error: " + err.Error()
logger.Println("updateCurrentRepeater error (httpClient)", err)
return
}
type RepeaterForNodeResponse struct {
NodeId string `json:"node_id"`
Host string `json:"host"`
}
content, _ := ioutil.ReadAll(response.Body)
_ = response.Body.Close()
logger.Println("Content:", string(content))
var resp RepeaterForNodeResponse
err = json.Unmarshal(content, &resp)
if err != nil {
c.connectionStatus = "repeater search error: " + err.Error()
logger.Println("updateCurrentRepeater error (Unmarshal)", err)
return
}
c.addr = resp.Host + ":1077"
c.connectionStatus = "repeater search complete: " + c.addr
logger.Println("updateCurrentRepeater ok:", resp.Host)
}
func (c *Connection) thConn() {
logger.Println("CloudConnection th begin")
const inputBufferSize = 100 * 1024
inputBuffer := make([]byte, inputBufferSize)
inputBufferOffset := 0
for !c.stopping {
if c.conn == nil {
inputBufferOffset = 0
if len(c.addr) > 0 {
var err error
var conn net.Conn
c.connectionStatus = "connecting"
conn, err = tls.DialWithDialer(&net.Dialer{Timeout: time.Second * 1}, "tcp", c.addr, &tls.Config{})
if err != nil {
c.conn = nil
c.connectionStatus = "connect error:" + err.Error()
logger.Println("CloudConnection th dial error", err)
time.Sleep(100 * time.Millisecond)
c.addr = ""
continue
}
c.connectionStatus = "connected"
logger.Println("Connection connected", c.addr)
c.mtx.Lock()
c.conn = conn
c.mtx.Unlock()
if c.sessionId == "" {
c.openSession()
} else {
c.loginStatus = "ok"
c.regNode()
}
} else {
c.updateCurrentRepeater()
time.Sleep(100 * time.Millisecond)
continue
}
}
if inputBufferOffset >= inputBufferSize {
logger.Println("max buffer size")
c.applyDisconnected()
continue
}
n, err := c.conn.Read(inputBuffer[inputBufferOffset:])
if err != nil {
// connection closed: n = 0; err = EOF
logger.Println("conn read error:", err)
c.applyDisconnected()
continue
}
if n == 0 {
logger.Println("read 0")
c.applyDisconnected()
continue
}
inputBufferOffset += n
needExit := false
processed := 0
for inputBufferOffset-processed >= 4 {
frameLen := int(binary.LittleEndian.Uint32(inputBuffer[processed:]))
if frameLen < 8 || frameLen > inputBufferSize {
logger.Println("wrong frame len", frameLen)
needExit = true
break // critical error
}
unprocessedBufferLen := inputBufferOffset - processed
if unprocessedBufferLen < frameLen {
break // no enough data
}
var frameData BinFrameTask
frameData.Frame, err = UnmarshalBinFrame(inputBuffer[processed : processed+frameLen])
if err != nil {
logger.Println("Error parse frame", err)
} else {
frameData.SessionId = c.sessionId
frameData.Client = c
c.processData(frameData, int64(frameLen))
}
processed += frameLen
}
if needExit {
c.applyDisconnected()
continue
}
if processed > 0 {
copy(inputBuffer, inputBuffer[processed:inputBufferOffset])
inputBufferOffset -= processed
}
}
c.applyDisconnected()
logger.Println("CloudConnection th end")
c.started = false
}
func (c *Connection) applyDisconnected() {
c.mtx.Lock()
if c.conn != nil {
_ = c.conn.Close()
c.conn = nil
}
c.mtx.Unlock()
}
func (c *Connection) SendData(data *BinFrame) (frameLen int64) {
c.mtx.Lock()
conn := c.conn
if conn != nil {
data.Header.SessionId = c.sessionId
frameBytes, _ := data.Marshal()
frameLen += int64(len(frameBytes))
sent := 0
for sent < len(frameBytes) {
n, err := conn.Write(frameBytes)
if err != nil {
break
}
sent += n
}
}
c.mtx.Unlock()
return
}
func (c *Connection) openSession() {
if c.password == "" {
c.loginStatus = "error: no password provided"
logger.Println("CloudConnection openSession - no auth data")
return
}
var err error
type OpenSessionRequest struct {
UserName string `json:"user_name"`
Password string `json:"password"`
}
var frame BinFrame
var openSessionRequest OpenSessionRequest
openSessionRequest.UserName = c.userName
openSessionRequest.Password = c.password
frame.Header.Src = ""
frame.Header.Dest = ""
frame.Header.Function = "session_open"
frame.Header.TransactionId = ""
frame.Header.SessionId = ""
frame.Data, err = json.Marshal(openSessionRequest)
c.loginStatus = "processing"
if err != nil {
c.loginStatus = "error: " + err.Error()
return
}
c.SendData(&frame)
}
func (c *Connection) regNode() {
var err error
type RegNodeRequest struct {
NodeId string `json:"node_id"`
}
if c.nodeId == "" {
c.iamStatus = "no nodeId specified"
return
}
var frame BinFrame
var regNodeRequest RegNodeRequest
regNodeRequest.NodeId = c.nodeId
c.iamStatus = "processing"
frame.Header.Src = ""
frame.Header.Dest = ""
frame.Header.Function = "#iam"
frame.Header.TransactionId = ""
frame.Header.SessionId = ""
frame.Data, err = json.Marshal(regNodeRequest)
if err != nil {
return
}
c.SendData(&frame)
}
func (c *Connection) processData(task BinFrameTask, inputFrameSize int64) {
var err error
if time.Now().Sub(c.lastCallCountDT) > 1*time.Second {
now := time.Now()
period := now.Sub(c.lastCallCountDT)
if c.lastCallCount > 0 {
c.callPerSecond = float64(c.callCount-c.lastCallCount) / period.Seconds()
c.receivedBytesPerSecond = float64(c.receivedBytes-c.lastReceivedBytes) / period.Seconds()
c.sentBytesPerSecond = float64(c.sentBytes-c.lastSentBytes) / period.Seconds()
} else {
c.callPerSecond = 0
}
c.lastCallCount = c.callCount
c.lastReceivedBytes = c.receivedBytes
c.lastSentBytes = c.sentBytes
c.lastCallCountDT = now
}
c.callCount++
c.receivedBytes += inputFrameSize
if task.Frame != nil {
c.mtx.Lock()
if value, ok := c.calls[task.Frame.Header.Function]; ok {
c.calls[task.Frame.Header.Function] = value + 1
} else {
c.calls[task.Frame.Header.Function] = 1
}
c.mtx.Unlock()
}
responseFromNodeReceived := false
c.mtx.Lock()
if tr, ok := c.proxyTasks[task.Frame.Header.TransactionId]; ok {
tr.ResponseText = task.Frame.Data
tr.ResponseReceived = true
responseFromNodeReceived = true
}
c.mtx.Unlock()
if responseFromNodeReceived {
return
}
if task.Frame.Header.Function == "#iam" {
type IAmResponse struct {
Error string `json:"error"`
}
var iamResponse IAmResponse
err = json.Unmarshal(task.Frame.Data, &iamResponse)
if err != nil {
return
}
if iamResponse.Error != "" {
err = errors.New(iamResponse.Error)
}
if err != nil {
c.iamStatus = err.Error()
} else {
c.iamStatus = "ok"
}
logger.Println("#iam response received")
return
}
if task.Frame.Header.Function == "session_open" {
logger.Println("CloudConnection session_open data received", task.Frame.Data)
type SessionInfo struct {
SessionToken string `json:"session_token"`
Error string `json:"error"`
}
var sessionInfo SessionInfo
err = json.Unmarshal(task.Frame.Data, &sessionInfo)
if err == nil {
if sessionInfo.Error == "" {
logger.Println("CloudConnection session_open", sessionInfo.SessionToken)
c.sessionId = sessionInfo.SessionToken
c.regNode()
c.SaveSession()
c.loginStatus = "ok"
} else {
c.loginStatus = "error: " + sessionInfo.Error
}
} else {
c.loginStatus = "error: " + err.Error()
}
return
}
var allowed bool
{
var allowedValue bool
var allowedFound bool
if allowedValue, allowedFound = c.allowIncomingFunctions[task.Frame.Header.Function]; allowedFound {
if allowedValue {
allowed = true
}
}
allowed = true
}
var bs []byte
if allowed {
if c.requester == nil {
logger.Println("CloudConnection requester is nil")
return
}
// Frame for the node
bs, err = c.requester.RequestJson(task.Frame.Header.Function, task.Frame.Data, "web")
//logger.Println("CloudConnection REQUEST", task.Frame.Header.Function, "resLen:", len(bs))
} else {
logger.Println("NOT ALLOWED FUNCTION:", task.Frame.Header.Function)
err = errors.New("access denied")
}
if err != nil {
type ErrorStruct struct {
Error string `json:"error"`
}
var res ErrorStruct
res.Error = err.Error()
bs, _ = json.MarshalIndent(res, "", " ")
var frame BinFrame
frame.Header.Src = ""
frame.Header.Dest = ""
frame.Header.Function = task.Frame.Header.Function
frame.Header.TransactionId = task.Frame.Header.TransactionId
frame.Header.SessionId = ""
frame.Data = bs
c.sentBytes += task.Client.SendData(&frame)
return
}
var frame BinFrame
frame.Header.Src = ""
frame.Header.Dest = ""
frame.Header.Function = task.Frame.Header.Function
frame.Header.TransactionId = task.Frame.Header.TransactionId
frame.Header.SessionId = ""
frame.Data = bs
c.sentBytes += task.Client.SendData(&frame)
}
type ProxyTask struct {
TransactionId string
Function string
RequestText []byte
ResponseReceived bool
ErrorReceived bool
ResponseText []byte
}
func (c *Connection) Call(function string, requestText []byte) (response []byte, err error) {
// Unique Transaction Id
transactionId := strconv.FormatInt(rand.Int63(), 16) + strconv.FormatInt(time.Now().UnixNano(), 16)
// ProxyTask
var task ProxyTask
task.Function = function
task.RequestText = requestText
task.TransactionId = transactionId
task.ResponseReceived = false
c.mtx.Lock()
c.proxyTasks[transactionId] = &task
c.mtx.Unlock()
// Send frame to node
var frame BinFrame
frame.Header.Src = ""
frame.Header.Dest = ""
frame.Header.Function = function
frame.Header.TransactionId = transactionId
frame.Header.SessionId = ""
frame.Data = requestText
c.SendData(&frame)
// Waiting for response
tBegin := time.Now()
for time.Now().Sub(tBegin) < 3*time.Second && !task.ResponseReceived {
time.Sleep(10 * time.Millisecond)
}
// Remove task
c.mtx.Lock()
delete(c.proxyTasks, transactionId)
c.mtx.Unlock()
var resultBytes []byte
if task.ResponseReceived {
resultBytes = task.ResponseText
type ErrorStruct struct {
Error string `json:"error"`
}
var errStr ErrorStruct
err = json.Unmarshal(task.ResponseText, &errStr)
if errStr.Error != "" {
err = errors.New(string(resultBytes))
}
} else {
err = errors.New("node timeout")
}
return resultBytes, err
}
func (c *Connection) State() (nodeinterface.CloudStateResponse, error) {
var resp nodeinterface.CloudStateResponse
resp.Connected = c.conn != nil
resp.LoggedIn = c.sessionId != ""
resp.UserName = c.userName
resp.LoginStatus = c.loginStatus
resp.ConnectionStatus = c.connectionStatus
resp.IAmStatus = c.iamStatus
resp.CurrentRepeater = c.addr
resp.NodeId = c.nodeId
c.mtx.Lock()
resp.Counters = make([]nodeinterface.CloudStateResponseItem, 0)
for key, value := range c.calls {
resp.Counters = append(resp.Counters, nodeinterface.CloudStateResponseItem{
Name: key,
Value: value,
})
}
c.mtx.Unlock()
return resp, nil
}
func (c *Connection) Nodes() (resp nodeinterface.CloudNodesResponse, err error) {
resp.Nodes = make([]nodeinterface.CloudNodesResponseItem, 0)
cloudResp, err := c.Call("s-registered-nodes", nil)
if err != nil {
logger.Println("Connection::Nodes s-registered-nodes error", err)
return
}
type NodeResponseItem struct {
Id string `json:"id"`
Name string `json:"name"`
}
type NodesResponse struct {
Items []NodeResponseItem `json:"items"`
}
var nodesResp NodesResponse
err = json.Unmarshal(cloudResp, &nodesResp)
if err != nil {
return
}
for _, n := range nodesResp.Items {
resp.Nodes = append(resp.Nodes, nodeinterface.CloudNodesResponseItem{
NodeId: n.Id,
Name: n.Name,
})
}
return resp, nil
}
func (c *Connection) AddNode(name string) (resp nodeinterface.CloudAddNodeResponse, err error) {
type NodeAddRequest struct {
Name string `json:"name"`
}
var req NodeAddRequest
req.Name = name
var bs []byte
bs, err = json.Marshal(req)
if err != nil {
return
}
cloudResp, err := c.Call("s-node-add", bs)
if err != nil {
logger.Println("!!!!!! s-node-add", err)
return
}
logger.Println("!!!!!! s-node-add resp", string(cloudResp))
type NodeAddResponse struct {
NodeId string `json:"node_id"`
}
var r NodeAddResponse
err = json.Unmarshal(cloudResp, &r)
if err != nil {
return
}
resp.NodeId = r.NodeId
return
}
func (c *Connection) UpdateNode(nodeId string, name string) (resp nodeinterface.CloudUpdateNodeResponse, err error) {
type NodeUpdateRequest struct {
NodeId string `json:"node_id"`
Name string `json:"name"`
}
var req NodeUpdateRequest
req.NodeId = nodeId
req.Name = name
var bs []byte
bs, err = json.Marshal(req)
if err != nil {
return
}
logger.Println("!!!!!! s-node-update bin:", string(bs))
cloudResp, err := c.Call("s-node-update", bs)
if err != nil {
logger.Println("!!!!!! s-node-update error", err)
return
}
type NodeUpdateResponse struct {
}
var r NodeUpdateResponse
err = json.Unmarshal(cloudResp, &r)
if err != nil {
return
}
return
}
func (c *Connection) RemoveNode(nodeId string) (resp nodeinterface.CloudRemoveNodeResponse, err error) {
type NodeRemoveRequest struct {
NodeId string `json:"node_id"`
}
var req NodeRemoveRequest
req.NodeId = nodeId
var bs []byte
bs, err = json.Marshal(req)
if err != nil {
return
}
logger.Println("!!!!!! s-node-remove bin:", string(bs))
cloudResp, err := c.Call("s-node-remove", bs)
if err != nil {
logger.Println("!!!!!! s-node-remove error", err)
return
}
type NodeRemoveResponse struct {
}
var r NodeRemoveResponse
err = json.Unmarshal(cloudResp, &r)
if err != nil {
return
}
return
}
func (c *Connection) GetSettings(request nodeinterface.CloudGetSettingsRequest) (nodeinterface.CloudGetSettingsResponse, error) {
var resp nodeinterface.CloudGetSettingsResponse
return resp, nil
}
func (c *Connection) SetSettings(request nodeinterface.CloudSetSettingsRequest) (resp nodeinterface.CloudSetSettingsResponse, err error) {
return
}
func (c *Connection) AccountInfo(request nodeinterface.CloudAccountInfoRequest) (resp nodeinterface.CloudAccountInfoResponse, err error) {
cloudResp, err := c.Call("s-account-info", []byte("{}"))
if err != nil {
logger.Println("s-account-info error", err)
return
}
type AccountInfo struct {
Email string `json:"email"`
MaxNodesCount int64 `json:"max_nodes_count"`
}
var accountInfoResp AccountInfo
err = json.Unmarshal(cloudResp, &accountInfoResp)
if err != nil {
return
}
resp.Email = accountInfoResp.Email
resp.MaxNodesCount = accountInfoResp.MaxNodesCount
return resp, nil
}
func (c *Connection) SetCurrentNodeId(request nodeinterface.CloudSetCurrentNodeIdRequest) (resp nodeinterface.CloudSetCurrentNodeIdResponse, err error) {
if request.NodeId != c.nodeId {
c.nodeId = request.NodeId
c.CloseConnection()
err = c.SaveSession()
}
return
}
func (c *Connection) Stat() (res common_interfaces.StatGazerCloud) {
res.CallsPerSecond = c.callPerSecond
res.ReceiveSpeed = c.receivedBytesPerSecond
res.SendSpeed = c.sentBytesPerSecond
return
}