/
proxy.go
executable file
·1090 lines (1032 loc) · 29.1 KB
/
proxy.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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2010 - 2011 Yigong Liu
//
// Distributed under New BSD License
//
package router
import (
"reflect"
"os"
"fmt"
"sync"
"io"
)
/*
Proxy is the primary interface to connect router to its peer router.
At both ends of a connection, there is a proxy object for its router.
Simple router connection can be set up thru calling Router.Connect().
Proxy.Connect() can be used to set up more complicated connections,
such as setting IdFilter to allow only a subset of messages pass thru
the connection, or setting IdTranslator which can "relocate" remote message
ids into a subspace in local router's namespace.
Proxy.Close() is called to disconnect router from its peer.
*/
type Proxy interface {
Connect(Proxy) os.Error
ConnectRemote(io.ReadWriteCloser, MarshalingPolicy, ...bool) os.Error
Close()
//query messaging interface with peer
LocalPubInfo() []*IdChanInfo
LocalSubInfo() []*IdChanInfo
PeerPubInfo() []*IdChanInfo
PeerSubInfo() []*IdChanInfo
}
/*
Peers are to be connected thru forwarding channels
Proxy and Stream are peers
*/
type peerIntf interface {
appMsgChanForId(Id) (Channel, int)
sendCtrlMsg(*genericMsg) os.Error
}
type proxyImpl struct {
//chan for ctrl msgs from peers
ctrlChan chan *genericMsg
//use peerIntf to forward app/ctrl msgs to peer (proxy or stream)
peer peerIntf
streamPeer bool //is peer a stream
flowControlled bool
//my/local router
router *routerImpl
//chans connecting to local router on behalf of peer
sysChans *sysChans
appSendChans *sendChanBundle //send to local router
appRecvChans *recvChanBundle //recv from local router
//filter & translator
filter IdFilter
translator IdTranslator
//cache of export/import ids at proxy
exportSendIds map[interface{}]*IdChanInfo //exported send ids, global publish
exportRecvIds map[interface{}]*IdChanInfo //exported recv ids, global subscribe
importSendIds map[interface{}]*IdChanInfo //imported send ids from peer
importRecvIds map[interface{}]*IdChanInfo //imported recv ids from peer
//mutex to protect cache
cacheLock sync.Mutex
//for log/debug
name string
Logger
FaultRaiser
//others
connReady bool
Closed bool
errChan chan os.Error
}
/*
Proxy constructor. It accepts the following arguments:
1. r: the router which will be bound with this proxy and be owner of this proxy
2. name: proxy's name, used in log messages if owner router's log is turned on
3. f: IdFilter to be installed at this proxy
4. t: IdTranslator to be installed at this proxy
*/
func NewProxy(r Router, name string, f IdFilter, t IdTranslator) Proxy {
p := new(proxyImpl)
p.router = r.(*routerImpl)
p.name = name
p.filter = f
p.translator = t
//create chan for incoming ctrl msgs during connSetup
p.ctrlChan = make(chan *genericMsg, DefCmdChanBufSize)
//chans to local router
p.sysChans = newSysChans(p)
p.appSendChans = newSendChanBundle(p, ScopeLocal, MemberRemote)
p.appRecvChans = newRecvChanBundle(p, ScopeLocal, MemberRemote)
//cache: only need to create import cache, since export cache are queried/returned from router
p.importSendIds = make(map[interface{}]*IdChanInfo)
p.importRecvIds = make(map[interface{}]*IdChanInfo)
p.router.addProxy(p)
ln := ""
if len(p.router.name) > 0 {
if len(p.name) > 0 {
ln = p.router.name + p.name
} else {
ln = p.router.name + "_proxy"
}
}
p.Logger.Init(p.router.SysID(RouterLogId), p.router, ln)
p.FaultRaiser.Init(p.router.SysID(RouterFaultId), p.router, ln)
return p
}
func (p1 *proxyImpl) Connect(pp Proxy) os.Error {
p2 := pp.(*proxyImpl)
p2.peer = p1
p1.peer = p2
p1.errChan = make(chan os.Error)
p1.start()
p2.start()
return <-p1.errChan
}
func (p *proxyImpl) ConnectRemote(rwc io.ReadWriteCloser, mar MarshalingPolicy, flags ...bool) os.Error {
s := newStream(rwc, mar, p)
s.peer = p
p.peer = s
if len(flags) > 0 {
p.flowControlled = flags[0]
}
p.streamPeer = true
p.errChan = make(chan os.Error)
p.start()
s.start()
return <-p.errChan
}
func (p *proxyImpl) start() { go p.connInitLoop() }
func (p *proxyImpl) Close() {
p.Log(LOG_INFO, "proxy.Close() is called")
//close(p.ctrlChan)
p.peer.sendCtrlMsg(&genericMsg{p.router.SysID(DisconnId), &ConnInfoMsg{}})
p.shutdown()
}
func (p *proxyImpl) shutdown() {
//close sysChans pubSubInfo chan so that connInitLoop
//will exit and call closeImpl()
p.sysChans.Shutdown()
}
func (p *proxyImpl) closeImpl() {
pubInfo := p.PeerPubInfo()
subInfo := p.PeerSubInfo()
p.cacheLock.Lock()
defer p.cacheLock.Unlock()
if !p.Closed {
p.Closed = true
//notify local chan that remote peer is leaving (unpub, unsub)
p.Log(LOG_INFO, fmt.Sprintf("unpub [%d], unsub [%d]", len(pubInfo), len(subInfo)))
p.sysChans.SendPubSubInfo(UnPubId, &IdChanInfoMsg{pubInfo})
p.sysChans.SendPubSubInfo(UnSubId, &IdChanInfoMsg{subInfo})
//start closing
p.Log(LOG_INFO, "proxy closing")
p.sysChans.Close()
//p.Closed = true
p.router.delProxy(p)
//close my chans
p.appRecvChans.Close()
p.appSendChans.Close()
//close logger
p.FaultRaiser.Close()
p.Logger.Close()
}
p.Log(LOG_INFO, "proxy closed")
}
const (
raw = iota
async
flowControlled
)
func (p *proxyImpl) connType() int {
switch {
case p.router.async:
return async
case p.flowControlled:
return flowControlled
}
return raw
}
func (p *proxyImpl) connSetup() os.Error {
r := p.router
//1. to initiate conn setup handshaking, send my conn info to peer
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ConnId), &ConnInfoMsg{Id: r.seedId, Type: p.connType()}})
//2. recv connInfo from peer
switch m := <-p.ctrlChan; m.Id.SysIdIndex() {
case ConnId:
//save peer conninfo & forward it
ci := m.Data.(*ConnInfoMsg)
p.sysChans.SendConnInfo(ConnId, ci)
//check type info
if reflect.Typeof(ci.Id) != reflect.Typeof(r.seedId) || ci.Type != p.connType() {
err := os.ErrorString(errRouterTypeMismatch)
ci.Error = err
//tell local listeners about the fail
p.sysChans.SendConnInfo(ErrorId, ci)
//tell peer about fail
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ErrorId), &ConnInfoMsg{Error: err}})
p.LogError(err)
return err
}
default:
err := os.ErrorString(errConnInvalidMsg)
//tell peer about fail
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ErrorId), &ConnInfoMsg{Error: err}})
p.LogError(err)
return err
}
//3. send initial pub/sub info to peer
p.peer.sendCtrlMsg(&genericMsg{r.SysID(PubId), p.initPubInfoMsg()})
p.peer.sendCtrlMsg(&genericMsg{r.SysID(SubId), p.initSubInfoMsg()})
//4. handle init_pub/sub msgs, send connReady to peer and wait for peer's connReady
peerReady := false
myReady := false
for !(peerReady && myReady) {
switch m := <-p.ctrlChan; m.Id.SysIdIndex() {
case ErrorId:
ci := m.Data.(*ConnInfoMsg)
p.sysChans.SendConnInfo(ErrorId, ci)
p.LogError(ci.Error)
return ci.Error
case ReadyId:
crm := m.Data.(*ConnReadyMsg)
if crm.Info != nil {
_, err := p.handlePeerReadyMsg(m)
p.sysChans.SendReadyInfo(ReadyId, m.Data.(*ConnReadyMsg))
if err != nil {
//tell peer about fail
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ErrorId), &ConnInfoMsg{Error: err}})
p.LogError(err)
return err
}
} else {
peerReady = true
}
case PubId:
_, err := p.handlePeerPubMsg(m)
p.sysChans.SendPubSubInfo(PubId, m.Data.(*IdChanInfoMsg))
if err != nil {
//tell peer about fail
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ErrorId), &ConnInfoMsg{Error: err}})
p.LogError(err)
return err
}
case SubId:
_, err := p.handlePeerSubMsg(m)
p.sysChans.SendPubSubInfo(SubId, m.Data.(*IdChanInfoMsg))
if err != nil {
//tell peer about fail
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ErrorId), &ConnInfoMsg{Error: err}})
p.LogError(err)
return err
}
myReady = true
//tell peer i am ready by sending an empty ConnReadyMsg
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ReadyId), &ConnReadyMsg{}})
default:
err := os.ErrorString(errConnInvalidMsg)
//tell peer about fail
p.peer.sendCtrlMsg(&genericMsg{r.SysID(ErrorId), &ConnInfoMsg{Error: err}})
p.LogError(err)
return err
}
}
return nil
}
func (p *proxyImpl) connInitLoop() {
//query router main goroutine to retrieve exported ids
p.exportSendIds = p.router.IdsForSend(ExportedId)
p.exportRecvIds = p.router.IdsForRecv(ExportedId)
//start conn handshaking
err := p.connSetup()
if p.errChan != nil {
p.errChan <- err
}
if err != nil {
p.Log(LOG_INFO, "-- connection error")
p.closeImpl()
return
}
p.Log(LOG_INFO, "-- connection ready")
p.cacheLock.Lock()
p.connReady = true
p.cacheLock.Unlock()
//drain the remaing peer ctrl msgs
close(p.ctrlChan)
for m := range p.ctrlChan {
p.handlePeerCtrlMsg(m)
}
//turn into loop handling only local ctrl msgs
cont := true
for cont {
m := <-p.sysChans.pubSubInfo
if closed(p.sysChans.pubSubInfo) {
cont = false
break
}
if err = p.handleLocalCtrlMsg(m); err != nil {
cont = false
}
}
p.closeImpl()
}
func (p *proxyImpl) handleLocalCtrlMsg(m *genericMsg) (err os.Error) {
p.Log(LOG_INFO, "enter handleLocalCtrlMsg")
switch m.Id.SysIdIndex() {
case PubId:
_, err = p.handleLocalPubMsg(m)
case UnPubId:
_, err = p.handleLocalUnPubMsg(m)
case SubId:
_, err = p.handleLocalSubMsg(m)
case UnSubId:
_, err = p.handleLocalUnSubMsg(m)
}
if err != nil {
//tell peer about fail
ci := &ConnInfoMsg{Error: err}
p.peer.sendCtrlMsg(&genericMsg{p.router.SysID(ErrorId), ci})
p.sysChans.SendConnInfo(ErrorId, ci)
p.LogError(err)
}
p.Log(LOG_INFO, "exit handleLocalCtrlMsg")
return
}
func (p *proxyImpl) handlePeerCtrlMsg(m *genericMsg) (err os.Error) {
switch m.Id.SysIdIndex() {
case DisconnId:
p.sysChans.SendConnInfo(DisconnId, m.Data.(*ConnInfoMsg))
p.Log(LOG_INFO, "recv Disconn msg, proxy shutdown")
p.shutdown()
return
case ErrorId:
ci := m.Data.(*ConnInfoMsg)
p.sysChans.SendConnInfo(ErrorId, ci)
p.LogError(ci.Error)
p.shutdown()
return
case ReadyId:
_, err = p.handlePeerReadyMsg(m)
p.sysChans.SendReadyInfo(ReadyId, m.Data.(*ConnReadyMsg))
case PubId:
_, err = p.handlePeerPubMsg(m)
p.sysChans.SendPubSubInfo(PubId, m.Data.(*IdChanInfoMsg))
case UnPubId:
_, err = p.handlePeerUnPubMsg(m)
p.sysChans.SendPubSubInfo(UnPubId, m.Data.(*IdChanInfoMsg))
case SubId:
_, err = p.handlePeerSubMsg(m)
p.sysChans.SendPubSubInfo(SubId, m.Data.(*IdChanInfoMsg))
case UnSubId:
_, err = p.handlePeerUnSubMsg(m)
p.sysChans.SendPubSubInfo(UnSubId, m.Data.(*IdChanInfoMsg))
}
if err != nil {
//tell peer about fail
ci := &ConnInfoMsg{Error: err}
p.peer.sendCtrlMsg(&genericMsg{p.router.SysID(ErrorId), ci})
p.sysChans.SendConnInfo(ErrorId, ci)
p.LogError(err)
p.shutdown()
}
return
}
//the following 2 functions are external interface exposed to peers
func (p *proxyImpl) sendCtrlMsg(m *genericMsg) (err os.Error) {
p.cacheLock.Lock()
if !p.connReady {
p.cacheLock.Unlock()
p.ctrlChan <- m
} else {
p.cacheLock.Unlock()
err = p.handlePeerCtrlMsg(m)
}
return
}
func (p *proxyImpl) appMsgChanForId(id Id) (Channel, int) {
id1 := id
if p.translator != nil {
id1 = p.translator.TranslateInward(id)
}
return p.appSendChans.findSender(id1)
}
func (p *proxyImpl) chanTypeMatch(info1, info2 *IdChanInfo) bool {
if info1.ChanType != nil {
if info2.ChanType != nil {
return info1.ChanType == info2.ChanType
}
//at here, info2 should be marshaled data from remote
if info2.ElemType == nil {
p.Log(LOG_ERROR, fmt.Sprintf("IdChanInfo miss both ChanType & ElemType info for %v", info2.Id))
return false
}
//1. marshal data for info1
if info1.ElemType == nil {
info1.ElemType = new(chanElemTypeData)
elemType := info1.ChanType.Elem()
info1.ElemType.FullName = elemType.PkgPath() + "." + elemType.Name()
//do the real type encoding
}
//2. compare marshaled data
if info1.ElemType.FullName == info2.ElemType.FullName {
//3. since type match, use info1's ChanType for info2
info2.ChanType = info1.ChanType
return true
} else {
return false
}
} else {
if info2.ChanType == nil {
p.Log(LOG_ERROR, fmt.Sprintf("both pub/sub miss ChanType for %v", info2.Id))
return false
}
//at here, info1 should be marshaled data from remote
if info1.ElemType == nil {
p.Log(LOG_ERROR, fmt.Sprintf("IdChanInfo miss both ChanType & ElemType info for %v", info1.Id))
return false
}
//1. marshal data for info2
if info2.ElemType == nil {
info2.ElemType = new(chanElemTypeData)
elemType := info2.ChanType.Elem()
info2.ElemType.FullName = elemType.PkgPath() + "." + elemType.Name()
//do the real type encoding
}
//2. compare marshaled data
if info1.ElemType.FullName == info2.ElemType.FullName {
//3. since type match, use info1's ChanType for info2
info1.ChanType = info2.ChanType
return true
} else {
return false
}
}
return false
}
//for the following 8 Local/Peer Pub/Sub mehtods, the general rule is that we set up
//senders (to recv routers) before we set up recvers (from send routers), so msg will not be lost
func (p *proxyImpl) handleLocalSubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
sInfo := msg.Info
if len(sInfo) == 0 {
return
}
numReady := 0
readyInfo := make([]*ChanReadyInfo, len(sInfo))
sInfo2 := make([]*IdChanInfo, len(sInfo))
p.cacheLock.Lock()
for _, sub := range sInfo {
p.Log(LOG_INFO, fmt.Sprintf("handleLocalSubMsg: %v", sub.Id))
if p.filter != nil && p.filter.BlockInward(sub.Id) {
continue
}
_, ok := p.exportRecvIds[sub.Id.Key()]
if ok {
continue
}
//here we have a new global sub id
//update exported id cache
p.exportRecvIds[sub.Id.Key()] = sub
sInfo2[num] = sub
num++ //one more to be exported
//check if peer already pubed it
for _, pub := range p.importSendIds {
if sub.Id.Match(pub.Id) {
if p.chanTypeMatch(sub, pub) {
readyInfo[numReady] = &ChanReadyInfo{pub.Id, p.router.recvChanBufSize(sub.Id)}
numReady++
p.Log(LOG_INFO, fmt.Sprintf("send ConnReady for: %v", pub.Id))
p.appSendChans.AddSender(pub.Id, pub.ChanType)
} else {
err = os.ErrorString(errRmtChanTypeMismatch)
p.sysChans.SendConnInfo(ErrorId, &ConnInfoMsg{Id: sub.Id, Error: err})
p.LogError(err)
p.cacheLock.Unlock()
p.Raise(err)
}
}
}
}
p.cacheLock.Unlock()
//send subscriptions to peer
if num > 0 {
sInfo2 = sInfo2[0:num]
if p.translator == nil {
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{Info: sInfo2}})
} else {
for i, sub := range sInfo2 {
sInfo2[i] = new(IdChanInfo)
sInfo2[i].Id = p.translator.TranslateOutward(sub.Id)
sInfo2[i].ChanType = sub.ChanType
sInfo2[i].ElemType = sub.ElemType
}
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{Info: sInfo2}})
}
}
//send connReadyInfo to peer
if numReady > 0 {
readyInfo = readyInfo[0:numReady]
if p.translator == nil {
p.peer.sendCtrlMsg(&genericMsg{p.router.SysID(ReadyId), &ConnReadyMsg{Info: readyInfo}})
} else {
for i, ready := range readyInfo {
readyInfo[i].Id = p.translator.TranslateOutward(ready.Id)
}
p.peer.sendCtrlMsg(&genericMsg{p.router.SysID(ReadyId), &ConnReadyMsg{Info: readyInfo}})
}
}
return
}
func (p *proxyImpl) handleLocalUnSubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
sInfo := msg.Info
if len(sInfo) == 0 {
return
}
sInfo2 := make([]*IdChanInfo, len(sInfo))
p.cacheLock.Lock()
for _, sub := range sInfo {
p.Log(LOG_INFO, fmt.Sprintf("handleLocalUnSubMsg: %v", sub.Id))
_, ok := p.exportRecvIds[sub.Id.Key()]
if !ok {
continue
}
if p.filter != nil && p.filter.BlockInward(sub.Id) {
continue
}
delCache := false
delSender := false
switch no := p.appSendChans.BindingCount(sub.Id); {
case no < 0: //not in
delCache = true
case no == 0:
delCache = true
delSender = true
case no > 0:
continue
}
//update exported id cache
if delCache {
p.exportRecvIds[sub.Id.Key()] = sub, false
sInfo2[num] = sub
num++
}
//check if peer already pubed it
if delSender {
pub, ok := p.importSendIds[sub.Id.Key()]
if ok {
p.appSendChans.DelSender(pub.Id)
}
}
}
p.cacheLock.Unlock()
if num > 0 {
sInfo2 = sInfo2[0:num]
if p.translator == nil {
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{Info: sInfo2}})
} else {
for i, sub := range sInfo2 {
sInfo2[i] = new(IdChanInfo)
sInfo2[i].Id = p.translator.TranslateOutward(sub.Id)
sInfo2[i].ChanType = sub.ChanType
sInfo2[i].ElemType = sub.ElemType
}
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{Info: sInfo2}})
}
}
return
}
func (p *proxyImpl) handleLocalPubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
pInfo := msg.Info
if len(pInfo) == 0 {
return
}
pInfo2 := make([]*IdChanInfo, len(pInfo))
p.cacheLock.Lock()
for _, pub := range pInfo {
p.Log(LOG_INFO, fmt.Sprintf("handleLocalPubMsg: %v", pub.Id))
if p.filter != nil && p.filter.BlockOutward(pub.Id) {
continue
}
_, ok := p.exportSendIds[pub.Id.Key()]
if ok {
continue
}
//update exported id cache
p.exportSendIds[pub.Id.Key()] = pub
pInfo2[num] = pub
num++ //one more export
//check if peer already subed it
for _, sub := range p.importRecvIds {
if pub.Id.Match(sub.Id) {
if !p.chanTypeMatch(sub, pub) {
err = os.ErrorString(errRmtChanTypeMismatch)
p.sysChans.SendConnInfo(ErrorId, &ConnInfoMsg{Id: pub.Id, Error: err})
p.LogError(err)
p.cacheLock.Unlock()
return
}
}
}
}
p.cacheLock.Unlock()
if num > 0 {
pInfo2 = pInfo2[0:num]
if p.translator == nil {
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{pInfo2}})
} else {
for cnt, pub := range pInfo2 {
pInfo2[cnt] = new(IdChanInfo)
pInfo2[cnt].Id = p.translator.TranslateOutward(pub.Id)
pInfo2[cnt].ChanType = pub.ChanType
pInfo2[cnt].ElemType = pub.ElemType
}
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{pInfo2}})
}
}
return
}
func (p *proxyImpl) handleLocalUnPubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
pInfo := msg.Info
if len(pInfo) == 0 {
return
}
pInfo2 := make([]*IdChanInfo, len(pInfo))
p.cacheLock.Lock()
for _, pub := range pInfo {
p.Log(LOG_INFO, fmt.Sprintf("handleLocalUnPubMsg: %v", pub.Id))
_, ok := p.exportSendIds[pub.Id.Key()]
if !ok {
continue
}
if p.filter != nil && p.filter.BlockOutward(pub.Id) {
continue
}
delCache := false
delRecver := false
switch no := p.appRecvChans.BindingCount(pub.Id); {
case no < 0: //not in
delCache = true
case no == 0:
delCache = true
delRecver = true
case no > 0:
continue
}
//update exported id cache
if delCache {
p.exportSendIds[pub.Id.Key()] = pub, false
pInfo2[num] = pub
num++
}
//check if peer already pubed it
if delRecver {
sub, ok := p.importRecvIds[pub.Id.Key()]
if ok {
p.appRecvChans.DelRecver(sub.Id)
}
}
}
p.cacheLock.Unlock()
if num > 0 {
pInfo2 = pInfo2[0:num]
if p.translator == nil {
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{pInfo2}})
} else {
for cnt, pub := range pInfo2 {
pInfo2[cnt] = new(IdChanInfo)
pInfo2[cnt].Id = p.translator.TranslateOutward(pub.Id)
pInfo2[cnt].ChanType = pub.ChanType
pInfo2[cnt].ElemType = pub.ElemType
}
p.peer.sendCtrlMsg(&genericMsg{m.Id, &IdChanInfoMsg{pInfo2}})
}
}
return
}
func (p *proxyImpl) handlePeerReadyMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*ConnReadyMsg)
rInfo := msg.Info
if len(rInfo) == 0 {
return
}
for _, ready := range rInfo {
if p.translator != nil {
ready.Id = p.translator.TranslateInward(ready.Id)
}
//p.Log(LOG_INFO, fmt.Sprintf("handlePeerReadyMsg: %v, %v", ready.Id, ready.Credit))
if p.filter != nil && p.filter.BlockOutward(ready.Id) {
continue
}
//check
if r, _ := p.appRecvChans.findRecver(ready.Id); r != nil {
fs, ok := r.(*flowChanSender)
if ok {
fs.ack(ready.Credit)
//p.Log(LOG_INFO, fmt.Sprintf("handlePeerReadyMsg: add credit %v for recver %v", ready.Credit, ready.Id))
} else {
p.Log(LOG_INFO, fmt.Sprintf("handlePeerReadyMsg: recver %v is not flow sender", ready.Id))
}
continue
}
//check if local already pubed it
p.cacheLock.Lock()
for _, pub := range p.exportSendIds {
if pub.Id.Match(ready.Id) {
//ready.Id, _ = ready.Id.Clone(ScopeLocal, MemberRemote)
peerChan, _ := p.peer.appMsgChanForId(ready.Id)
if peerChan != nil {
p.appRecvChans.AddRecver(ready.Id, peerChan, ready.Credit)
p.Log(LOG_INFO, fmt.Sprintf("handlePeerReadyMsg, add recver for: %v, %v", ready.Id, ready.Credit))
}
num++
break
}
}
p.cacheLock.Unlock()
}
return
}
func (p *proxyImpl) handlePeerSubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
sInfo := msg.Info
if len(sInfo) == 0 {
return
}
p.cacheLock.Lock()
for _, sub := range sInfo {
if p.translator != nil {
sub.Id = p.translator.TranslateInward(sub.Id)
}
p.Log(LOG_INFO, fmt.Sprintf("handlePeerSubMsg: %v", sub.Id))
if p.filter != nil && p.filter.BlockOutward(sub.Id) {
continue
}
_, ok := p.importRecvIds[sub.Id.Key()]
if ok {
p.cacheLock.Unlock()
return
}
p.importRecvIds[sub.Id.Key()] = sub
p.Log(LOG_INFO, fmt.Sprintf("handlePeerSubMsg1: %v", sub.Id))
//check if local already pubed it
for _, pub := range p.exportSendIds {
if pub.Id.Match(sub.Id) {
if !p.chanTypeMatch(pub, sub) {
err = os.ErrorString(errRmtChanTypeMismatch)
p.sysChans.SendConnInfo(ErrorId, &ConnInfoMsg{Id: sub.Id, Error: err})
p.LogError(err)
p.cacheLock.Unlock()
return
}
break
}
}
num++
}
p.cacheLock.Unlock()
return
}
func (p *proxyImpl) handlePeerUnSubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
sInfo := msg.Info
if len(sInfo) == 0 {
return
}
p.cacheLock.Lock()
defer p.cacheLock.Unlock()
for _, sub := range sInfo {
if p.translator != nil {
sub.Id = p.translator.TranslateInward(sub.Id)
}
p.Log(LOG_INFO, fmt.Sprintf("handlePeerUnSubMsg: %v", sub.Id))
//update import cache
p.importRecvIds[sub.Id.Key()] = sub, false
//check if local already pubed it
p.appRecvChans.DelRecver(sub.Id)
num++
}
return
}
func (p *proxyImpl) handlePeerPubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
pInfo := msg.Info
if len(pInfo) == 0 {
return
}
readyInfo := make([]*ChanReadyInfo, len(pInfo))
p.cacheLock.Lock()
for _, pub := range pInfo {
if p.translator != nil {
pub.Id = p.translator.TranslateInward(pub.Id)
}
p.Log(LOG_INFO, fmt.Sprintf("handlePeerPubMsg: %v", pub.Id))
if p.filter != nil && p.filter.BlockInward(pub.Id) {
continue
}
_, ok := p.importSendIds[pub.Id.Key()]
if ok {
p.cacheLock.Unlock()
return
}
p.importSendIds[pub.Id.Key()] = pub
for _, sub := range p.exportRecvIds {
if pub.Id.Match(sub.Id) {
if !p.chanTypeMatch(pub, sub) {
err = os.ErrorString(errRmtChanTypeMismatch)
p.sysChans.SendConnInfo(ErrorId, &ConnInfoMsg{Id: sub.Id, Error: err})
p.LogError(err)
p.cacheLock.Unlock()
return
}
id := pub.Id
if p.translator != nil {
id = p.translator.TranslateOutward(id)
}
p.Log(LOG_INFO, fmt.Sprintf("send ConnReady for: %v", pub.Id))
readyInfo[num] = &ChanReadyInfo{id, p.router.recvChanBufSize(sub.Id)}
num++
p.appSendChans.AddSender(pub.Id, pub.ChanType)
}
}
}
p.cacheLock.Unlock()
//send ConnReadyMsg
if num > 0 {
p.peer.sendCtrlMsg(&genericMsg{p.router.SysID(ReadyId), &ConnReadyMsg{Info: readyInfo[0:num]}})
p.Log(LOG_INFO, fmt.Sprintf("handlePeerPubMsg sends ConnReadyMsg for : %v", readyInfo[0].Id))
}
return
}
func (p *proxyImpl) handlePeerUnPubMsg(m *genericMsg) (num int, err os.Error) {
msg := m.Data.(*IdChanInfoMsg)
pInfo := msg.Info
if len(pInfo) == 0 {
return
}
p.cacheLock.Lock()
defer p.cacheLock.Unlock()
for _, pub := range pInfo {
if p.translator != nil {
pub.Id = p.translator.TranslateInward(pub.Id)
}
p.Log(LOG_INFO, fmt.Sprintf("handlePeerUnPubMsg: %v", pub.Id))
//update import cache
p.importSendIds[pub.Id.Key()] = pub, false
p.appSendChans.DelSender(pub.Id)
num++
}
return
}
//called from stream, so need lock protection
func (p *proxyImpl) getExportRecvChanType(id Id) *reflect.ChanType {
p.cacheLock.Lock()
sub, ok := p.exportRecvIds[id.Key()]
p.cacheLock.Unlock()
if ok {
return sub.ChanType
}
return nil
}
func (p *proxyImpl) initSubInfoMsg() *IdChanInfoMsg {
num := len(p.exportRecvIds)
info := make([]*IdChanInfo, num)
idx := 0
for _, v := range p.exportRecvIds {
if p.filter != nil && p.filter.BlockInward(v.Id) {
continue
}
info[idx] = new(IdChanInfo)
if p.translator != nil {
info[idx].Id = p.translator.TranslateOutward(v.Id)
} else {
info[idx].Id = v.Id
}
info[idx].ChanType = v.ChanType
idx++
}
return &IdChanInfoMsg{Info: info[0:idx]}
}
func (p *proxyImpl) initPubInfoMsg() *IdChanInfoMsg {
num := len(p.exportSendIds)
info := make([]*IdChanInfo, num)
idx := 0
for _, v := range p.exportSendIds {
if p.filter != nil && p.filter.BlockOutward(v.Id) {
continue
}
info[idx] = new(IdChanInfo)
if p.translator != nil {
info[idx].Id = p.translator.TranslateOutward(v.Id)
} else {
info[idx].Id = v.Id
}
info[idx].Id = v.Id
info[idx].ChanType = v.ChanType
idx++
}
return &IdChanInfoMsg{Info: info[0:idx]}
}
func (p *proxyImpl) PeerPubInfo() []*IdChanInfo {
p.cacheLock.Lock()
defer p.cacheLock.Unlock()
num := len(p.importSendIds)
info := make([]*IdChanInfo, num)
idx := 0
for _, v := range p.importSendIds {
info[idx] = new(IdChanInfo)
info[idx].Id = v.Id
info[idx].ChanType = v.ChanType
idx++
}
return info
}
func (p *proxyImpl) PeerSubInfo() []*IdChanInfo {
p.cacheLock.Lock()
defer p.cacheLock.Unlock()
num := len(p.importRecvIds)
info := make([]*IdChanInfo, num)
idx := 0
for _, v := range p.importRecvIds {
info[idx] = new(IdChanInfo)
info[idx].Id = v.Id
info[idx].ChanType = v.ChanType
idx++
}
return info
}
func (p *proxyImpl) LocalPubInfo() []*IdChanInfo {
p.cacheLock.Lock()
defer p.cacheLock.Unlock()
num := len(p.exportSendIds)
info := make([]*IdChanInfo, num)
idx := 0
for _, v := range p.exportSendIds {
info[idx] = new(IdChanInfo)
info[idx].Id = v.Id
info[idx].ChanType = v.ChanType
idx++
}
return info
}
func (p *proxyImpl) LocalSubInfo() []*IdChanInfo {
p.cacheLock.Lock()
defer p.cacheLock.Unlock()
num := len(p.exportRecvIds)
info := make([]*IdChanInfo, num)
idx := 0