offline.go 1.23 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3
// package offline implements an object that implements the exchange
// interface but returns nil values to every request.
package offline
4 5 6 7 8 9 10 11 12 13 14

import (
	"errors"

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

	blocks "github.com/jbenet/go-ipfs/blocks"
	exchange "github.com/jbenet/go-ipfs/exchange"
	u "github.com/jbenet/go-ipfs/util"
)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
15 16
var OfflineMode = errors.New("Block unavailable. Operating in offline mode")

Brian Tiger Chow's avatar
Brian Tiger Chow committed
17
func Exchange() exchange.Interface {
18 19 20 21 22
	return &offlineExchange{}
}

// offlineExchange implements the Exchange interface but doesn't return blocks.
// For use in offline mode.
23
type offlineExchange struct{}
24

Jeromy's avatar
Jeromy committed
25
// GetBlock returns nil to signal that a block could not be retrieved for the
26 27
// given key.
// NB: This function may return before the timeout expires.
Jeromy's avatar
Jeromy committed
28
func (_ *offlineExchange) GetBlock(context.Context, u.Key) (*blocks.Block, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
29
	return nil, OfflineMode
30 31 32
}

// HasBlock always returns nil.
Jeromy's avatar
Jeromy committed
33
func (_ *offlineExchange) HasBlock(context.Context, *blocks.Block) error {
34 35
	return nil
}
36 37 38 39 40

// Close always returns nil.
func (_ *offlineExchange) Close() error {
	return nil
}
Jeromy's avatar
Jeromy committed
41 42 43 44

func (_ *offlineExchange) GetBlocks(context.Context, []u.Key) (<-chan *blocks.Block, error) {
	return nil, OfflineMode
}