@cryptotaxi247 / kubo / commits / 4584bc8d1

clean up core initialization

- less floating variables

Juan Batiz-Benet committed Oct 25, 2014 at 06:42 UTC 4584bc8d182eb711988517db231b6170a23b8ef4
1 file changed +39 -57
core/core.go
+39 -57
@@ -77,14 +77,11 @@ type IpfsNode struct {
77 }
78
79 // NewIpfsNode constructs a new IpfsNode based on the given config.
80 -func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
81 - // derive this from a higher context.
82 - // cancel if we need to fail early.
83 - ctx, cancel := context.WithCancel(context.TODO())
80 +func NewIpfsNode(cfg *config.Config, online bool) (n *IpfsNode, err error) {
81 success := false // flip to true after all sub-system inits succeed
82 defer func() {
86 - if !success {
87 - cancel()
83 + if !success && n != nil {
84 + n.Close()
85 }
86 }()
87
@@ -92,94 +89,79 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
89 return nil, fmt.Errorf("configuration required")
90 }
91
95 - d, err := makeDatastore(cfg.Datastore)
96 - if err != nil {
92 + // derive this from a higher context.
93 + ctx := context.TODO()
94 + n = &IpfsNode{
95 + Config: cfg,
96 + ContextCloser: ctxc.NewContextCloser(ctx, nil),
97 + }
98 +
99 + // setup datastore.
100 + if n.Datastore, err = makeDatastore(cfg.Datastore); err != nil {
101 return nil, err
102 }
103
100 - peerstore := peer.NewPeerstore()
101 - local, err := initIdentity(cfg, peerstore, online)
104 + // setup peerstore + local peer identity
105 + n.Peerstore = peer.NewPeerstore()
106 + n.Identity, err = initIdentity(n.Config, n.Peerstore, online)
107 if err != nil {
108 return nil, err
109 }
110
106 - // FIXME(brian): This is a bit dangerous. If any of the vars declared in
107 - // this block are assigned inside of the "if online" block using the ":="
108 - // declaration syntax, the compiler permits re-declaration. This is rather
109 - // undesirable
110 - var (
111 - net inet.Network
112 - // TODO: refactor so we can use IpfsRouting interface instead of being DHT-specific
113 - route *dht.IpfsDHT
114 - exchangeSession exchange.Interface
115 - diagnostics *diag.Diagnostics
116 - network inet.Network
117 - )
118 -
111 + // setup online services
112 if online {
113
114 dhtService := netservice.NewService(ctx, nil) // nil handler for now, need to patch it
115 exchangeService := netservice.NewService(ctx, nil) // nil handler for now, need to patch it
123 - diagService := netservice.NewService(ctx, nil)
116 + diagService := netservice.NewService(ctx, nil) // nil handler for now, need to patch it
117
125 - net, err = inet.NewIpfsNetwork(ctx, local, peerstore, &mux.ProtocolMap{
118 + muxMap := &mux.ProtocolMap{
119 mux.ProtocolID_Routing: dhtService,
120 mux.ProtocolID_Exchange: exchangeService,
121 mux.ProtocolID_Diagnostic: diagService,
122 // add protocol services here.
130 - })
123 + }
124 +
125 + // setup the network
126 + n.Network, err = inet.NewIpfsNetwork(ctx, n.Identity, n.Peerstore, muxMap)
127 if err != nil {
128 return nil, err
129 }
134 - network = net
130 + n.AddCloserChild(n.Network)
131
136 - diagnostics = diag.NewDiagnostics(local, net, diagService)
137 - diagService.SetHandler(diagnostics)
132 + // setup diagnostics service
133 + n.Diagnostics = diag.NewDiagnostics(n.Identity, n.Network, diagService)
134 + diagService.SetHandler(n.Diagnostics)
135
139 - route = dht.NewDHT(ctx, local, peerstore, net, dhtService, d)
136 + // setup routing service
137 + dhtRouting := dht.NewDHT(ctx, n.Identity, n.Peerstore, n.Network, dhtService, n.Datastore)
138 // TODO(brian): perform this inside NewDHT factory method
141 - dhtService.SetHandler(route) // wire the handler to the service.
139 + dhtService.SetHandler(dhtRouting) // wire the handler to the service.
140 + n.Routing = dhtRouting
141
142 + // setup exchange service
143 const alwaysSendToPeer = true // use YesManStrategy
144 - exchangeSession = bitswap.NetMessageSession(ctx, local, net, exchangeService, route, d, alwaysSendToPeer)
144 + n.Exchange = bitswap.NetMessageSession(ctx, n.Identity, n.Network, exchangeService, n.Routing, n.Datastore, alwaysSendToPeer)
145 + // ok, this function call is ridiculous o/ consider making it simpler.
146
146 - // TODO(brian): pass a context to initConnections
147 - go initConnections(ctx, cfg, peerstore, route)
147 + go initConnections(ctx, n.Config, n.Peerstore, dhtRouting)
148 }
149
150 // TODO(brian): when offline instantiate the BlockService with a bitswap
151 // session that simply doesn't return blocks
152 - bs, err := bserv.NewBlockService(d, exchangeSession)
152 + n.Blocks, err = bserv.NewBlockService(n.Datastore, n.Exchange)
153 if err != nil {
154 return nil, err
155 }
156
157 - dag := merkledag.NewDAGService(bs)
158 - ns := namesys.NewNameSystem(route)
159 - p, err := pin.LoadPinner(d, dag)
157 + n.DAG = merkledag.NewDAGService(n.Blocks)
158 + n.Namesys = namesys.NewNameSystem(n.Routing)
159 + n.Pinning, err = pin.LoadPinner(n.Datastore, n.DAG)
160 if err != nil {
161 - p = pin.NewPinner(d, dag)
161 + n.Pinning = pin.NewPinner(n.Datastore, n.DAG)
162 }
163
164 success = true
165 - n := &IpfsNode{
166 - Config: cfg,
167 - Peerstore: peerstore,
168 - Datastore: d,
169 - Blocks: bs,
170 - DAG: dag,
171 - Resolver: &path.Resolver{DAG: dag},
172 - Exchange: exchangeSession,
173 - Identity: local,
174 - Routing: route,
175 - Namesys: ns,
176 - Diagnostics: diagnostics,
177 - Network: network,
178 - Pinning: p,
179 - ContextCloser: ctxc.NewContextCloser(ctx, nil),
180 - }
181 -
182 - n.AddCloserChild(n.Network)
165 return n, nil
166 }
167