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 {
linkDefaults LinkOptions
proc goprocess.Process // for Context closing
ctx context.Context
sync.RWMutex
}
......@@ -42,6 +43,7 @@ func New(ctx context.Context) Mocknet {
hosts: map[peer.ID]*bhost.BasicHost{},
links: map[peer.ID]map[peer.ID]map[*link]struct{}{},
proc: goprocessctx.WithContext(ctx),
ctx: ctx,
}
}
......@@ -62,7 +64,7 @@ func (mn *mocknet) GenPeer() (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 {
return nil, err
}
......@@ -70,7 +72,7 @@ func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) {
h := bhost.New(n)
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.nets[n.peer] = n
......@@ -298,11 +300,11 @@ func (mn *mocknet) ConnectAll() 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) {
return a.DialPeer(mn.cg.Context(), b.LocalPeer())
return a.DialPeer(mn.ctx, b.LocalPeer())
}
func (mn *mocknet) DisconnectPeers(p1, p2 peer.ID) error {
......
......@@ -66,7 +66,7 @@ func newPeernet(ctx context.Context, m *mocknet, k ic.PrivKey,
notifs: make(map[inet.Notifiee]struct{}),
}
n.cg.SetTeardown(n.teardown)
n.proc.SetTeardown(n.teardown)
return n, nil
}
......@@ -94,7 +94,7 @@ func (pn *peernet) allConns() []*conn {
// Close calls the ContextCloser func
func (pn *peernet) Close() error {
return pn.cg.Close()
return pn.proc.Close()
}
func (pn *peernet) Peerstore() peer.Peerstore {
......
......@@ -65,6 +65,7 @@ type Swarm struct {
Filters *filter.Filters
proc goprocess.Process
ctx context.Context
bwc metrics.Reporter
}
......@@ -82,6 +83,7 @@ func NewSwarm(ctx context.Context, listenAddrs []ma.Multiaddr,
local: local,
peers: peers,
proc: goprocessctx.WithContext(ctx),
ctx: ctx,
dialT: DialTimeout,
notifs: make(map[inet.Notifiee]ps.Notifiee),
bwc: bwc,
......@@ -137,6 +139,11 @@ func (s *Swarm) Process() goprocess.Process {
return s.proc
}
// Context returns the context of the swarm
func (s *Swarm) Context() context.Context {
return s.ctx
}
// Close stops the Swarm.
func (s *Swarm) Close() error {
return s.proc.Close()
......
......@@ -64,7 +64,7 @@ func (s *Swarm) setupListener(maddr ma.Multiaddr) error {
log.Warning("Listener not given PrivateKey, so WILL NOT SECURE conns.")
}
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 {
return err
}
......@@ -112,7 +112,7 @@ func (s *Swarm) setupListener(maddr ma.Multiaddr) error {
return
}
}
}(s.cg.Context(), sl)
}(s.Context(), sl)
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