refac(core): expose core.OnlineWithRouting constructor option
fix FIXUP maybeRouter in core
Brian Tiger Chow committed
Jan 26, 2015 at 15:57 UTC
1beef957daf9da557970271f753635e1e7cd8054
1 file changed
+24
-8
core/core.go
+24
-8
@@ -144,12 +144,24 @@ func Offline(r repo.Repo) ConfigOption {
144
return Standard(r, false)
145
}
146
147
+func OnlineWithRouting(r repo.Repo, router routing.IpfsRouting) ConfigOption {
148
+ if router == nil {
149
+ panic("router required")
150
+ }
151
+ return standardWithRouting(r, true, router)
152
+}
153
+
154
func Online(r repo.Repo) ConfigOption {
155
return Standard(r, true)
156
}
157
158
// DEPRECATED: use Online, Offline functions
159
func Standard(r repo.Repo, online bool) ConfigOption {
160
+ return standardWithRouting(r, online, nil)
161
+}
162
+
163
+// TODO refactor so maybeRouter isn't special-cased in this way
164
+func standardWithRouting(r repo.Repo, online bool, maybeRouter routing.IpfsRouting) ConfigOption {
165
return func(ctx context.Context) (n *IpfsNode, err error) {
166
// FIXME perform node construction in the main constructor so it isn't
167
// necessary to perform this teardown in this scope.
@@ -191,7 +203,7 @@ func Standard(r repo.Repo, online bool) ConfigOption {
203
}
204
205
if online {
194
- if err := n.StartOnlineServices(ctx); err != nil {
206
+ if err := n.StartOnlineServices(ctx, maybeRouter); err != nil {
207
return nil, err
208
}
209
} else {
@@ -203,7 +215,7 @@ func Standard(r repo.Repo, online bool) ConfigOption {
215
}
216
}
217
206
-func (n *IpfsNode) StartOnlineServices(ctx context.Context) error {
218
+func (n *IpfsNode) StartOnlineServices(ctx context.Context, maybeRouter routing.IpfsRouting) error {
219
220
if n.PeerHost != nil { // already online.
221
return debugerror.New("node already online")
@@ -220,7 +232,7 @@ func (n *IpfsNode) StartOnlineServices(ctx context.Context) error {
232
}
233
n.PeerHost = peerhost
234
223
- if err := n.startOnlineServicesWithHost(ctx); err != nil {
235
+ if err := n.startOnlineServicesWithHost(ctx, maybeRouter); err != nil {
236
return err
237
}
238
@@ -237,16 +249,20 @@ func (n *IpfsNode) StartOnlineServices(ctx context.Context) error {
249
250
// startOnlineServicesWithHost is the set of services which need to be
251
// initialized with the host and _before_ we start listening.
240
-func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context) error {
252
+func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, maybeRouter routing.IpfsRouting) error {
253
// setup diagnostics service
254
n.Diagnostics = diag.NewDiagnostics(n.Identity, n.PeerHost)
255
256
// setup routing service
245
- dhtRouting, err := constructDHTRouting(ctx, n.PeerHost, n.Repo.Datastore())
246
- if err != nil {
247
- return debugerror.Wrap(err)
257
+ if maybeRouter != nil {
258
+ n.Routing = maybeRouter
259
+ } else {
260
+ dhtRouting, err := constructDHTRouting(ctx, n.PeerHost, n.Repo.Datastore())
261
+ if err != nil {
262
+ return debugerror.Wrap(err)
263
+ }
264
+ n.Routing = dhtRouting
265
}
249
- n.Routing = dhtRouting
266
267
// setup exchange service
268
const alwaysSendToPeer = true // use YesManStrategy