-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnect_view_controller.go
More file actions
852 lines (724 loc) · 21.8 KB
/
connect_view_controller.go
File metadata and controls
852 lines (724 loc) · 21.8 KB
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
package sdk
import (
"context"
"errors"
// "fmt"
"math"
mathrand "math/rand"
"sync"
"time"
"github.com/urnetwork/glog"
"github.com/urnetwork/connect"
)
type ConnectionStatus = string
const (
Disconnected ConnectionStatus = "DISCONNECTED"
Connecting ConnectionStatus = "CONNECTING"
DestinationSet ConnectionStatus = "DESTINATION_SET"
Connected ConnectionStatus = "CONNECTED"
)
type SelectedLocationListener interface {
SelectedLocationChanged(location *ConnectLocation)
}
type ConnectionStatusListener interface {
ConnectionStatusChanged()
}
// type ConnectGridListener interface {
// ConnectGridPointChanged(index int32)
// }
type GridListener interface {
GridChanged()
}
type ConnectViewController struct {
ctx context.Context
cancel context.CancelFunc
device Device
stateLock sync.Mutex
connectLocationChangedSub Sub
// this is set when the client is connected
connected bool
connectionStatus ConnectionStatus
selectedLocation *ConnectLocation
grid *ConnectGrid
// providerGridPointList *ProviderGridPointList
selectedLocationListeners *connect.CallbackList[SelectedLocationListener]
connectionStatusListeners *connect.CallbackList[ConnectionStatusListener]
// connectGridListeners *connect.CallbackList[ConnectGridListener]
gridListeners *connect.CallbackList[GridListener]
}
func newConnectViewController(ctx context.Context, device Device) *ConnectViewController {
cancelCtx, cancel := context.WithCancel(ctx)
vc := &ConnectViewController{
ctx: cancelCtx,
cancel: cancel,
device: device,
connected: false,
connectionStatus: Disconnected,
selectedLocation: nil,
grid: nil,
selectedLocationListeners: connect.NewCallbackList[SelectedLocationListener](),
connectionStatusListeners: connect.NewCallbackList[ConnectionStatusListener](),
// connectGridListeners: connect.NewCallbackList[ConnectGridListener](),
// windowSizeListeners: connect.NewCallbackList[WindowSizeListener](),
gridListeners: connect.NewCallbackList[GridListener](),
}
// if location := device.GetConnectLocation(); location != nil {
// vc.setConnected(true)
// vc.setSelectedLocation(location)
// vc.setConnectionStatus(DestinationSet)
// vc.setGrid()
// }
vc.connectLocationChangedSub = device.AddConnectLocationChangeListener(vc)
connectedLocation := device.GetConnectLocation()
vc.ConnectLocationChanged(connectedLocation)
if connectedLocation == nil {
vc.setSelectedLocation(device.GetDefaultLocation())
}
return vc
}
// ConnectLocationChangeListener
func (self *ConnectViewController) ConnectLocationChanged(location *ConnectLocation) {
if location == nil {
self.setConnected(false)
// keep the previous selected location
} else {
self.setConnected(true)
self.setSelectedLocation(location)
}
self.setGrid()
}
func (self *ConnectViewController) Start() {}
func (self *ConnectViewController) Stop() {}
func (self *ConnectViewController) Close() {
glog.Info("[cvc]close")
self.cancel()
self.connectLocationChangedSub.Close()
}
func (self *ConnectViewController) GetConnected() bool {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.connected
}
func (self *ConnectViewController) GetConnectionStatus() ConnectionStatus {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.connectionStatus
}
func (self *ConnectViewController) setConnectionStatus(status ConnectionStatus) {
changed := false
func() {
self.stateLock.Lock()
defer self.stateLock.Unlock()
if self.connectionStatus != status {
self.connectionStatus = status
changed = true
}
}()
if changed {
self.connectionStatusChanged()
}
}
func (self *ConnectViewController) connectionStatusChanged() {
for _, listener := range self.connectionStatusListeners.Get() {
connect.HandleError(func() {
listener.ConnectionStatusChanged()
})
}
}
func (self *ConnectViewController) AddConnectionStatusListener(listener ConnectionStatusListener) Sub {
callbackId := self.connectionStatusListeners.Add(listener)
return newSub(func() {
self.connectionStatusListeners.Remove(callbackId)
})
}
func (self *ConnectViewController) AddSelectedLocationListener(listener SelectedLocationListener) Sub {
callbackId := self.selectedLocationListeners.Add(listener)
return newSub(func() {
self.selectedLocationListeners.Remove(callbackId)
})
}
func (self *ConnectViewController) setSelectedLocation(location *ConnectLocation) {
func() {
self.stateLock.Lock()
defer self.stateLock.Unlock()
self.selectedLocation = location
}()
self.selectedLocationChanged(location)
}
func (self *ConnectViewController) GetSelectedLocation() *ConnectLocation {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.selectedLocation
}
func (self *ConnectViewController) selectedLocationChanged(location *ConnectLocation) {
for _, listener := range self.selectedLocationListeners.Get() {
connect.HandleError(func() {
listener.SelectedLocationChanged(location)
})
}
}
func (self *ConnectViewController) AddGridListener(listener GridListener) Sub {
callbackId := self.gridListeners.Add(listener)
return newSub(func() {
self.gridListeners.Remove(callbackId)
})
}
func (self *ConnectViewController) gridChanged() {
for _, listener := range self.gridListeners.Get() {
connect.HandleError(func() {
listener.GridChanged()
})
}
}
// func (self *ConnectViewController) AddConnectGridListener(listener ConnectGridListener) Sub {
// callbackId := self.connectGridListeners.Add(listener)
// return newSub(func() {
// self.connectGridListeners.Remove(callbackId)
// })
// }
// func (self *ConnectViewController) connectGridPointChanged(index int32) {
// for _, listener := range self.connectGridListeners.Get() {
// connect.HandleError(func() {
// listener.ConnectGridPointChanged(index)
// })
// }
// }
// func (self *ConnectViewController) AddWindowSizeListener(listener WindowSizeListener) Sub {
// callbackId := self.windowSizeListeners.Add(listener)
// return newSub(func() {
// self.windowSizeListeners.Remove(callbackId)
// })
// }
// func (self *ConnectViewController) windowSizeChanged() {
// for _, listener := range self.windowSizeListeners.Get() {
// connect.HandleError(func() {
// listener.WindowSizeChanged()
// })
// }
// }
func (self *ConnectViewController) setConnected(connected bool) {
self.stateLock.Lock()
defer self.stateLock.Unlock()
self.connected = connected
}
func (self *ConnectViewController) Connect(location *ConnectLocation) {
// self.setConnected(true)
if self.device.GetProvideControlMode() == ProvideControlModeAuto {
// enable provider
provideMode := ProvideModePublic
self.device.GetNetworkSpace().GetAsyncLocalState().GetLocalState().SetProvideMode(provideMode)
self.device.SetProvideMode(provideMode)
}
// persist the connection location for automatic reconnect
self.device.GetNetworkSpace().GetAsyncLocalState().GetLocalState().SetConnectLocation(location)
self.device.SetConnectLocation(location)
// set location as default location
// if user disconnects, we still want that location populated when they reload the app
self.device.GetNetworkSpace().GetAsyncLocalState().GetLocalState().SetDefaultLocation(location)
// todo - is this needed?
self.device.SetDefaultLocation(location)
}
func (self *ConnectViewController) ConnectBestAvailable() {
self.Connect(&ConnectLocation{
ConnectLocationId: &ConnectLocationId{
BestAvailable: true,
},
})
}
func (self *ConnectViewController) Disconnect() {
if self.device.GetProvideControlMode() == ProvideControlModeAuto {
// disable provider
provideMode := ProvideModeNone
self.device.GetNetworkSpace().GetAsyncLocalState().GetLocalState().SetProvideMode(provideMode)
self.device.SetProvideMode(provideMode)
}
self.device.GetNetworkSpace().GetAsyncLocalState().GetLocalState().SetConnectLocation(nil)
self.device.SetConnectLocation(nil)
}
func (self *ConnectViewController) setGrid() {
var grid *ConnectGrid
changed := false
func() {
self.stateLock.Lock()
defer self.stateLock.Unlock()
if self.connected {
if self.grid != nil {
self.grid.close()
}
grid = newConnectGridWithDefaults(self.ctx, self)
self.grid = grid
changed = true
} else if self.grid != nil {
self.grid.close()
self.grid = nil
changed = true
}
}()
if grid != nil {
self.setConnectionStatus(DestinationSet)
} else {
self.setConnectionStatus(Disconnected)
}
if changed {
if grid != nil {
if windowMonitor := self.device.(device).windowMonitor(); windowMonitor != nil {
grid.listenToWindow(windowMonitor)
}
}
self.gridChanged()
}
}
func (self *ConnectViewController) GetGrid() *ConnectGrid {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.grid
}
type ProviderState = string
const (
ProviderStateInEvaluation ProviderState = "InEvaluation"
ProviderStateEvaluationFailed ProviderState = "EvaluationFailed"
ProviderStateNotAdded ProviderState = "NotAdded"
ProviderStateAdded ProviderState = "Added"
ProviderStateRemoved ProviderState = "Removed"
)
func parseProviderState(state connect.ProviderState) (ProviderState, error) {
switch state {
case connect.ProviderStateInEvaluation:
return ProviderStateInEvaluation, nil
case connect.ProviderStateEvaluationFailed:
return ProviderStateEvaluationFailed, nil
case connect.ProviderStateNotAdded:
return ProviderStateNotAdded, nil
case connect.ProviderStateAdded:
return ProviderStateAdded, nil
case connect.ProviderStateRemoved:
return ProviderStateRemoved, nil
default:
return "", errors.New("invalid ProviderState")
}
}
func providerStateIsTerminal(state ProviderState) bool {
switch state {
case ProviderStateEvaluationFailed, ProviderStateNotAdded, ProviderStateRemoved:
return true
default:
return false
}
}
func providerStateIsActive(state ProviderState) bool {
switch state {
case ProviderStateAdded:
return true
default:
return false
}
}
type ProviderGridPoint struct {
// note gomobile does not support struct composition
X int32
Y int32
ClientId *Id
// EventTime *Time
State ProviderState
// the time when this point will be removed
// the ui can transition out based on this value
EndTime *Time
// wether the point is active for routing
Active bool
}
type gridPointCoord struct {
X int
Y int
}
type gridPoint struct {
gridPointCoord
Plottable bool
Occupied bool
}
func defaultConnectGridSettings() *connectGridSettings {
return &connectGridSettings{
MinSideLength: 16,
ExpandFraction: 1.5,
RemoveTimeout: 8 * time.Second,
}
}
type connectGridSettings struct {
MinSideLength int
ExpandFraction float32
RemoveTimeout time.Duration
}
type ConnectGrid struct {
ctx context.Context
cancel context.CancelFunc
connectViewController *ConnectViewController
settings *connectGridSettings
providerGridPointsMonitor *connect.Monitor
stateLock sync.Mutex
windowTargetSize int
windowCurrentSize int
windowSub func()
sideLength int
gridPoints map[gridPointCoord]*gridPoint
providerGridPoints map[connect.Id]*ProviderGridPoint
}
func newConnectGridWithDefaults(ctx context.Context, connectViewController *ConnectViewController) *ConnectGrid {
return newConnectGrid(ctx, connectViewController, defaultConnectGridSettings())
}
func newConnectGrid(ctx context.Context, connectViewController *ConnectViewController, settings *connectGridSettings) *ConnectGrid {
cancelCtx, cancel := context.WithCancel(ctx)
providerGrid := &ConnectGrid{
ctx: cancelCtx,
cancel: cancel,
connectViewController: connectViewController,
settings: settings,
providerGridPointsMonitor: connect.NewMonitor(),
windowTargetSize: 0,
windowCurrentSize: 0,
windowSub: nil,
sideLength: 0,
gridPoints: map[gridPointCoord]*gridPoint{},
providerGridPoints: map[connect.Id]*ProviderGridPoint{},
}
go connect.HandleError(providerGrid.run, cancel)
return providerGrid
}
func (self *ConnectGrid) GetWidth() int32 {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return int32(self.sideLength)
}
func (self *ConnectGrid) GetHeight() int32 {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return int32(self.sideLength)
}
func (self *ConnectGrid) GetWindowTargetSize() int32 {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return int32(self.windowTargetSize)
}
func (self *ConnectGrid) GetWindowCurrentSize() int32 {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return int32(self.windowCurrentSize)
}
func (self *ConnectGrid) GetProviderGridPointByClientId(clientId *Id) *ProviderGridPoint {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.providerGridPoints[clientId.toConnectId()]
}
func (self *ConnectGrid) GetProviderGridPointList() *ProviderGridPointList {
self.stateLock.Lock()
defer self.stateLock.Unlock()
providerGridPointList := NewProviderGridPointList()
for _, providerGridPoint := range self.providerGridPoints {
// make a copy
copyProviderGridPoint := *providerGridPoint
providerGridPointList.Add(©ProviderGridPoint)
}
return providerGridPointList
}
// *important* do not call this while holding the view controller state lock
// because this intialized with the current state, it will call back into the view controller
func (self *ConnectGrid) listenToWindow(windowMonitor windowMonitor) {
done := false
func() {
self.stateLock.Lock()
defer self.stateLock.Unlock()
select {
case <-self.ctx.Done():
done = true
return
default:
}
if self.windowSub != nil {
self.windowSub()
}
self.windowSub = windowMonitor.AddMonitorEventCallback(self.windowMonitorEventCallback)
}()
if done {
return
}
// initialize with the current values
windowExpandEvent, providerEvents := windowMonitor.Events()
self.windowMonitorEventCallback(windowExpandEvent, providerEvents, true)
}
func (self *ConnectGrid) close() {
self.cancel()
func() {
self.stateLock.Lock()
defer self.stateLock.Unlock()
if self.windowSub != nil {
self.windowSub()
self.windowSub = nil
}
}()
}
func (self *ConnectGrid) run() {
defer self.cancel()
for {
providerGridPointChanged := false
var notify chan struct{}
var minEndPoint *ProviderGridPoint
var timeout time.Duration
func() {
self.stateLock.Lock()
defer self.stateLock.Unlock()
notify = self.providerGridPointsMonitor.NotifyChannel()
now := time.Now()
removedCount := 0
for clientId, point := range self.providerGridPoints {
if point.EndTime != nil {
if !now.Before(point.EndTime.toTime()) {
// remove
delete(self.providerGridPoints, clientId)
gridPointCoord := gridPointCoord{X: int(point.X), Y: int(point.Y)}
self.gridPoints[gridPointCoord].Occupied = false
providerGridPointChanged = true
removedCount += 1
} else if minEndPoint == nil || point.EndTime.toTime().Before(minEndPoint.EndTime.toTime()) {
minEndPoint = point
}
}
}
if minEndPoint != nil {
timeout = minEndPoint.EndTime.toTime().Sub(now)
}
if 0 < removedCount {
glog.Infof(
"[grid]%d->%d points=%d(-%d)\n",
self.windowCurrentSize,
self.windowTargetSize,
len(self.providerGridPoints),
removedCount,
)
}
}()
if providerGridPointChanged {
self.connectViewController.gridChanged()
}
if minEndPoint != nil {
// there is a next point to be removed after `timeout`
select {
case <-self.ctx.Done():
return
case <-notify:
case <-time.After(timeout):
}
} else {
select {
case <-self.ctx.Done():
return
case <-notify:
}
}
}
}
// must be called with the state lock
// create a new grid based on targetClientSize sets specific points as plottable or unplottable
func (self *ConnectGrid) resize() {
targetClientSize := math.Ceil(math.Pow(float64(len(self.providerGridPoints)), float64(self.settings.ExpandFraction)))
sideLength := max(
self.settings.MinSideLength,
// currently the size never contracts
self.sideLength,
int(math.Ceil(math.Sqrt(targetClientSize))),
)
if self.sideLength == sideLength {
return
}
gridPoints := map[gridPointCoord]*gridPoint{}
// merge the existing state
for x := range sideLength {
for y := range sideLength {
c := gridPointCoord{X: x, Y: y}
// shiftC := gridPointCoord{
// X: x - (sideLength - self.sideLength) / 2,
// Y: y - (sideLength - self.sideLength) / 2,
// }
point, ok := self.gridPoints[c]
if ok {
// reset the plottable state, which will be set below to the new shape
point.Plottable = true
// point = &gridPoint{
// gridPointCoord: c,
// Plottable: true,
// Occupied: point.Occupied,
// }
gridPoints[c] = point
} else {
point = &gridPoint{
gridPointCoord: c,
Plottable: true,
Occupied: false,
}
gridPoints[c] = point
}
}
}
// in a equal corner triangle, x+y<s
// cut out 1/3 corner triangles
for x := range sideLength / 3 {
for y := range sideLength / 3 {
// triangle is w*y+h*x<=w*h
if x+y < sideLength/3 {
gridPoints[gridPointCoord{X: x, Y: y}].Plottable = false
// flip x
gridPoints[gridPointCoord{X: sideLength - 1 - x, Y: y}].Plottable = false
// flip y
gridPoints[gridPointCoord{X: x, Y: sideLength - 1 - y}].Plottable = false
// flip x and y
gridPoints[gridPointCoord{X: sideLength - 1 - x, Y: sideLength - 1 - y}].Plottable = false
}
}
}
self.sideLength = sideLength
self.gridPoints = gridPoints
}
// connect.MonitorEventFunction
func (self *ConnectGrid) windowMonitorEventCallback(windowExpandEvent *connect.WindowExpandEvent, providerEvents map[connect.Id]*connect.ProviderEvent, reset bool) {
done := false
windowSizeChanged := false
providerGridPointChanged := false
var connectionStatus ConnectionStatus
func() {
self.stateLock.Lock()
defer self.stateLock.Unlock()
select {
case <-self.ctx.Done():
done = true
return
default:
}
// eventTime := time.Now()
if reset {
for _, point := range self.providerGridPoints {
c := gridPointCoord{
X: int(point.X),
Y: int(point.Y),
}
if gridPoint, ok := self.gridPoints[c]; ok {
gridPoint.Occupied = false
}
}
clear(self.providerGridPoints)
providerGridPointChanged = true
}
for clientId, providerEvent := range providerEvents {
providerState, err := parseProviderState(providerEvent.State)
if err != nil {
glog.Errorf("[grid]could not parse provider event state: %s", string(providerEvent.State))
continue
}
point, ok := self.providerGridPoints[clientId]
if ok {
if point.State != providerState {
point.State = providerState
var endTime *Time
if providerEvent.State.IsTerminal() {
// schedule the point to be removed
// note this resets the end time if already set
endTime = newTime(time.Now().Add(self.settings.RemoveTimeout))
}
point.EndTime = endTime
point.Active = providerEvent.State.IsActive()
providerGridPointChanged = true
}
// point.EventTime = newTime(eventTime)
} else {
// insert a new provider point
findUnuccupiedGridPoint := func() *gridPoint {
// try random sampling, then use an exhaustive search
if self.sideLength == 0 {
return nil
}
for range 8 {
c := gridPointCoord{
X: mathrand.Intn(self.sideLength),
Y: mathrand.Intn(self.sideLength),
}
if gridPoint, ok := self.gridPoints[c]; ok && gridPoint.Plottable && !gridPoint.Occupied {
return gridPoint
}
}
unoccupiedGridPoints := []*gridPoint{}
for _, gridPoint := range self.gridPoints {
if gridPoint.Plottable && !gridPoint.Occupied {
unoccupiedGridPoints = append(unoccupiedGridPoints, gridPoint)
}
}
if len(unoccupiedGridPoints) == 0 {
return nil
}
return unoccupiedGridPoints[mathrand.Intn(len(unoccupiedGridPoints))]
}
self.resize()
unoccupiedGridPoint := findUnuccupiedGridPoint()
if unoccupiedGridPoint == nil {
self.resize()
unoccupiedGridPoint = findUnuccupiedGridPoint()
}
if unoccupiedGridPoint == nil {
// resize is broken
// TODO log
continue
}
unoccupiedGridPoint.Occupied = true
var endTime *Time
if providerEvent.State.IsTerminal() {
// schedule the point to be removed
endTime = newTime(time.Now().Add(self.settings.RemoveTimeout))
}
point = &ProviderGridPoint{
X: int32(unoccupiedGridPoint.X),
Y: int32(unoccupiedGridPoint.Y),
ClientId: newId(clientId),
State: providerState,
// EventTime: newTime(eventTime),
EndTime: endTime,
Active: providerEvent.State.IsActive(),
}
self.providerGridPoints[clientId] = point
providerGridPointChanged = true
}
}
// compute the window size here from the number of added providers
windowCurrentSize := 0
for _, point := range self.providerGridPoints {
if point.Active {
windowCurrentSize += 1
}
}
if self.windowCurrentSize != windowCurrentSize || self.windowTargetSize != windowExpandEvent.TargetSize {
self.windowCurrentSize = windowCurrentSize
self.windowTargetSize = windowExpandEvent.TargetSize
windowSizeChanged = true
}
// note the callback is only active while the device is connected
if windowExpandEvent.MinSatisfied {
connectionStatus = Connected
} else {
connectionStatus = Connecting
}
glog.Infof(
"[grid]%d->%d(%t) points=%d %s (w=%t, p=%t)\n",
self.windowCurrentSize,
self.windowTargetSize,
windowExpandEvent.MinSatisfied,
len(self.providerGridPoints),
connectionStatus,
windowSizeChanged,
providerGridPointChanged,
)
}()
if done {
return
}
if providerGridPointChanged {
self.providerGridPointsMonitor.NotifyAll()
}
if windowSizeChanged || providerGridPointChanged {
self.connectViewController.gridChanged()
}
self.connectViewController.setConnectionStatus(connectionStatus)
}