refactor(core): replace online bool with mode type
Brian Tiger Chow committed
Jan 10, 2015 at 22:52 UTC
ddf14bee78928d29a3cdf29e0d60bda7943777dd
1 file changed
+26
-7
core/core.go
+26
-7
@@ -17,7 +17,7 @@ import (
17
exchange "github.com/jbenet/go-ipfs/exchange"
18
bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
19
bsnet "github.com/jbenet/go-ipfs/exchange/bitswap/network"
20
- "github.com/jbenet/go-ipfs/exchange/offline"
20
+ offline "github.com/jbenet/go-ipfs/exchange/offline"
21
mount "github.com/jbenet/go-ipfs/fuse/mount"
22
importer "github.com/jbenet/go-ipfs/importer"
23
chunk "github.com/jbenet/go-ipfs/importer/chunk"
@@ -45,12 +45,20 @@ const kSizeBlockstoreWriteCache = 100
45
46
var log = eventlog.Logger("core")
47
48
+type mode int
49
+
50
+const (
51
+ // zero value is not a valid mode, must be explicitly set
52
+ invalidMode mode = iota
53
+ offlineMode
54
+ onlineMode
55
+)
56
+
57
// IpfsNode is IPFS Core module. It represents an IPFS instance.
58
type IpfsNode struct {
59
60
// Self
52
- Identity peer.ID // the local node's identity
53
- onlineMode bool // alternatively, offline
61
+ Identity peer.ID // the local node's identity
62
63
// TODO abstract as repo.Repo
64
Config *config.Config // the node's configuration
@@ -80,7 +88,8 @@ type IpfsNode struct {
88
// dht allows node to Bootstrap when dht is present
89
// TODO privatize before merging. This is here temporarily during the
90
// migration of the TestNet constructor
83
- DHT *dht.IpfsDHT
91
+ DHT *dht.IpfsDHT
92
+ mode mode
93
}
94
95
// Mounts defines what the node's mount state is. This should
@@ -142,8 +151,13 @@ func Standard(cfg *config.Config, online bool) ConfigOption {
151
return nil, debugerror.Errorf("configuration required")
152
}
153
n = &IpfsNode{
145
- onlineMode: online,
146
- Config: cfg,
154
+ mode: func() mode {
155
+ if online {
156
+ return onlineMode
157
+ }
158
+ return offlineMode
159
+ }(),
160
+ Config: cfg,
161
}
162
163
n.ContextGroup = ctxgroup.WithContextAndTeardown(ctx, n.teardown)
@@ -247,7 +261,12 @@ func (n *IpfsNode) teardown() error {
261
}
262
263
func (n *IpfsNode) OnlineMode() bool {
250
- return n.onlineMode
264
+ switch n.mode {
265
+ case onlineMode:
266
+ return true
267
+ default:
268
+ return false
269
+ }
270
}
271
272
func (n *IpfsNode) Bootstrap(ctx context.Context, peers []peer.PeerInfo) error {