@@ -25,9 +25,13 @@ func (e edge) Flip() edge {
2525 }
2626}
2727
28+ // This test network uses the same set of options for all
29+ // participants. The rpc.Options instance can be cloned
30+ // without issue.
2831type network struct {
29- myID PeerID
30- global * Joiner
32+ myID PeerID
33+ options rpc.Options
34+ global * Joiner
3135}
3236
3337// A Joiner is a global view of a test network, which can be joined by a
@@ -51,12 +55,13 @@ func NewJoiner() *Joiner {
5155 }
5256}
5357
54- func (j * Joiner ) Join () rpc.Network {
58+ func (j * Joiner ) Join (opts * rpc. Options ) rpc.Network {
5559 j .mu .Lock ()
5660 defer j .mu .Unlock ()
5761 ret := network {
58- myID : j .nextID ,
59- global : j ,
62+ myID : j .nextID ,
63+ global : j ,
64+ options : * opts ,
6065 }
6166 j .nextID ++
6267 return ret
@@ -72,13 +77,11 @@ func (j *Joiner) getAcceptQueue(id PeerID) spsc.Queue[PeerID] {
7277}
7378
7479func (n network ) LocalID () rpc.PeerID {
75- return rpc.PeerID {n .myID }
80+ return rpc.PeerID {Value : n .myID }
7681}
7782
78- func (n network ) Dial (dst rpc.PeerID , opts * rpc.Options ) (* rpc.Conn , error ) {
79- if opts == nil {
80- opts = & rpc.Options {}
81- }
83+ func (n network ) Dial (dst rpc.PeerID ) (* rpc.Conn , error ) {
84+ opts := n .options
8285 opts .Network = n
8386 opts .RemotePeerID = dst
8487 dstID := dst .Value .(PeerID )
@@ -101,7 +104,7 @@ func (n network) Dial(dst rpc.PeerID, opts *rpc.Options) (*rpc.Conn, error) {
101104
102105 }
103106 if ent .Conn == nil {
104- ent .Conn = rpc .NewConn (ent .Transport , opts )
107+ ent .Conn = rpc .NewConn (ent .Transport , & opts )
105108 } else {
106109 // There's already a connection, so we're not going to use this, but
107110 // we own it. So drop it:
@@ -110,30 +113,32 @@ func (n network) Dial(dst rpc.PeerID, opts *rpc.Options) (*rpc.Conn, error) {
110113 return ent .Conn , nil
111114}
112115
113- func (n network ) Accept (ctx context.Context , opts * rpc. Options ) ( * rpc. Conn , error ) {
116+ func (n network ) Serve (ctx context.Context ) error {
114117 n .global .mu .Lock ()
115118 q := n .global .getAcceptQueue (n .myID )
116119 n .global .mu .Unlock ()
117120
118- incoming , err := q .Recv (ctx )
119- if err != nil {
120- return nil , err
121- }
122- opts .Network = n
123- opts .RemotePeerID = rpc.PeerID {incoming }
124- n .global .mu .Lock ()
125- defer n .global .mu .Unlock ()
126- edge := edge {
127- From : n .myID ,
128- To : incoming ,
121+ for {
122+ incoming , err := q .Recv (ctx )
123+ if err != nil {
124+ return err
125+ }
126+ opts := n .options
127+ opts .Network = n
128+ opts .RemotePeerID = rpc.PeerID {incoming }
129+ n .global .mu .Lock ()
130+ defer n .global .mu .Unlock ()
131+ edge := edge {
132+ From : n .myID ,
133+ To : incoming ,
134+ }
135+ ent := n .global .connections [edge ]
136+ if ent .Conn == nil {
137+ ent .Conn = rpc .NewConn (ent .Transport , & opts )
138+ } else {
139+ opts .BootstrapClient .Release ()
140+ }
129141 }
130- ent := n .global .connections [edge ]
131- if ent .Conn == nil {
132- ent .Conn = rpc .NewConn (ent .Transport , opts )
133- } else {
134- opts .BootstrapClient .Release ()
135- }
136- return ent .Conn , nil
137142}
138143
139144func (n network ) Introduce (provider , recipient * rpc.Conn ) (rpc.IntroductionInfo , error ) {
@@ -157,24 +162,7 @@ func (n network) Introduce(provider, recipient *rpc.Conn) (rpc.IntroductionInfo,
157162 sendToRecipient .SetNonce (nonce )
158163 sendToProvider .SetPeerId (uint64 (recipientPeer .Value .(PeerID )))
159164 sendToProvider .SetNonce (nonce )
160- ret .SendToRecipient = rpc .ThirdPartyCapID (sendToRecipient .ToPtr ())
161- ret .SendToProvider = rpc .RecipientID (sendToProvider .ToPtr ())
165+ ret .SendToRecipient = rpc .ThirdPartyToContact (sendToRecipient .ToPtr ())
166+ ret .SendToProvider = rpc .ThirdPartyToAwait (sendToProvider .ToPtr ())
162167 return ret , nil
163168}
164- func (n network ) DialIntroduced (capID rpc.ThirdPartyCapID , introducedBy * rpc.Conn ) (* rpc.Conn , rpc.ProvisionID , error ) {
165- cid := PeerAndNonce (capnp .Ptr (capID ).Struct ())
166-
167- _ , seg := capnp .NewSingleSegmentMessage (nil )
168- pid , err := NewPeerAndNonce (seg )
169- if err != nil {
170- return nil , rpc.ProvisionID {}, err
171- }
172- pid .SetPeerId (uint64 (introducedBy .RemotePeerID ().Value .(PeerID )))
173- pid .SetNonce (cid .Nonce ())
174-
175- conn , err := n .Dial (rpc.PeerID {PeerID (cid .PeerId ())}, nil )
176- return conn , rpc .ProvisionID (pid .ToPtr ()), err
177- }
178- func (n network ) AcceptIntroduced (recipientID rpc.RecipientID , introducedBy * rpc.Conn ) (* rpc.Conn , error ) {
179- panic ("TODO" )
180- }
0 commit comments