Commit 316ba886 authored by rht's avatar rht
Browse files

Change Process interface into object variable



License: MIT
Signed-off-by: default avatarrht <rhtbot@gmail.com>
parent e2481a24
...@@ -33,6 +33,7 @@ type mocknet struct { ...@@ -33,6 +33,7 @@ type mocknet struct {
linkDefaults LinkOptions linkDefaults LinkOptions
proc goprocess.Process // for Context closing proc goprocess.Process // for Context closing
ctx context.Context
sync.RWMutex sync.RWMutex
} }
...@@ -42,6 +43,7 @@ func New(ctx context.Context) Mocknet { ...@@ -42,6 +43,7 @@ func New(ctx context.Context) Mocknet {
hosts: map[peer.ID]*bhost.BasicHost{}, hosts: map[peer.ID]*bhost.BasicHost{},
links: map[peer.ID]map[peer.ID]map[*link]struct{}{}, links: map[peer.ID]map[peer.ID]map[*link]struct{}{},
proc: goprocessctx.WithContext(ctx), proc: goprocessctx.WithContext(ctx),
ctx: ctx,
} }
} }
...@@ -62,7 +64,7 @@ func (mn *mocknet) GenPeer() (host.Host, error) { ...@@ -62,7 +64,7 @@ func (mn *mocknet) GenPeer() (host.Host, error) {
} }
func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) { func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) {
n, err := newPeernet(mn.cg.Context(), mn, k, a) n, err := newPeernet(mn.ctx, mn, k, a)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -70,7 +72,7 @@ func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) { ...@@ -70,7 +72,7 @@ func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) {
h := bhost.New(n) h := bhost.New(n)
log.Debugf("mocknet added listen addr for peer: %s -- %s", n.LocalPeer(), a) log.Debugf("mocknet added listen addr for peer: %s -- %s", n.LocalPeer(), a)
mn.cg.AddChild(n.cg) mn.proc.AddChild(n.proc)
mn.Lock() mn.Lock()
mn.nets[n.peer] = n mn.nets[n.peer] = n
...@@ -298,11 +300,11 @@ func (mn *mocknet) ConnectAll() error { ...@@ -298,11 +300,11 @@ func (mn *mocknet) ConnectAll() error {
} }
func (mn *mocknet) ConnectPeers(a, b peer.ID) (inet.Conn, error) { func (mn *mocknet) ConnectPeers(a, b peer.ID) (inet.Conn, error) {
return mn.Net(a).DialPeer(mn.cg.Context(), b) return mn.Net(a).DialPeer(mn.ctx, b)
} }
func (mn *mocknet) ConnectNets(a, b inet.Network) (inet.Conn, error) { func (mn *mocknet) ConnectNets(a, b inet.Network) (inet.Conn, error) {
return a.DialPeer(mn.cg.Context(), b.LocalPeer()) return a.DialPeer(mn.ctx, b.LocalPeer())
} }
func (mn *mocknet) DisconnectPeers(p1, p2 peer.ID) error { func (mn *mocknet) DisconnectPeers(p1, p2 peer.ID) error {
......
...@@ -66,7 +66,7 @@ func newPeernet(ctx context.Context, m *mocknet, k ic.PrivKey, ...@@ -66,7 +66,7 @@ func newPeernet(ctx context.Context, m *mocknet, k ic.PrivKey,
notifs: make(map[inet.Notifiee]struct{}), notifs: make(map[inet.Notifiee]struct{}),
} }
n.cg.SetTeardown(n.teardown) n.proc.SetTeardown(n.teardown)
return n, nil return n, nil
} }
...@@ -94,7 +94,7 @@ func (pn *peernet) allConns() []*conn { ...@@ -94,7 +94,7 @@ func (pn *peernet) allConns() []*conn {
// Close calls the ContextCloser func // Close calls the ContextCloser func
func (pn *peernet) Close() error { func (pn *peernet) Close() error {
return pn.cg.Close() return pn.proc.Close()
} }
func (pn *peernet) Peerstore() peer.Peerstore { func (pn *peernet) Peerstore() peer.Peerstore {
......
...@@ -65,6 +65,7 @@ type Swarm struct { ...@@ -65,6 +65,7 @@ type Swarm struct {
Filters *filter.Filters Filters *filter.Filters
proc goprocess.Process proc goprocess.Process
ctx context.Context
bwc metrics.Reporter bwc metrics.Reporter
} }
...@@ -82,6 +83,7 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr, ...@@ -82,6 +83,7 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
local: local, local: local,
peers: peers, peers: peers,
proc: goprocessctx.WithContext(ctx), proc: goprocessctx.WithContext(ctx),
ctx: ctx,
dialT: DialTimeout, dialT: DialTimeout,
notifs: make(map[inet.Notifiee]ps.Notifiee), notifs: make(map[inet.Notifiee]ps.Notifiee),
bwc: bwc, bwc: bwc,
...@@ -137,6 +139,11 @@ func (s *Swarm) Process() goprocess.Process { ...@@ -137,6 +139,11 @@ func (s *Swarm) Process() goprocess.Process {
return s.proc return s.proc
} }
// Context returns the context of the swarm
func (s *Swarm) Context() context.Context {
return s.ctx
}
// Close stops the Swarm. // Close stops the Swarm.
func (s *Swarm) Close() error { func (s *Swarm) Close() error {
return s.proc.Close() return s.proc.Close()
......
...@@ -64,7 +64,7 @@ func (s *Swarm) setupListener(maddr ma.Multiaddr) error { ...@@ -64,7 +64,7 @@ func (s *Swarm) setupListener(maddr ma.Multiaddr) error {
log.Warning("Listener not given PrivateKey, so WILL NOT SECURE conns.") log.Warning("Listener not given PrivateKey, so WILL NOT SECURE conns.")
} }
log.Debugf("Swarm Listening at %s", maddr) log.Debugf("Swarm Listening at %s", maddr)
list, err := conn.Listen(s.cg.Context(), maddr, s.local, sk) list, err := conn.Listen(s.Context(), maddr, s.local, sk)
if err != nil { if err != nil {
return err return err
} }
...@@ -112,7 +112,7 @@ func (s *Swarm) setupListener(maddr ma.Multiaddr) error { ...@@ -112,7 +112,7 @@ func (s *Swarm) setupListener(maddr ma.Multiaddr) error {
return return
} }
} }
}(s.cg.Context(), sl) }(s.Context(), sl)
return nil return nil
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment