github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/renter.go (about)

     1  package renter
     2  
     3  import (
     4  	"github.com/NebulousLabs/Sia/modules"
     5  	"github.com/NebulousLabs/Sia/modules/renter/contractor"
     6  	"github.com/NebulousLabs/Sia/modules/renter/hostdb"
     7  	"github.com/NebulousLabs/Sia/persist"
     8  	"github.com/NebulousLabs/Sia/sync"
     9  	"github.com/NebulousLabs/Sia/types"
    10  )
    11  
    12  // A hostDB is a database of hosts that the renter can use for figuring out who
    13  // to upload to, and download from.
    14  type hostDB interface {
    15  	// ActiveHosts returns the list of hosts that are actively being selected
    16  	// from.
    17  	ActiveHosts() []modules.HostDBEntry
    18  
    19  	// AllHosts returns the full list of hosts known to the hostdb.
    20  	AllHosts() []modules.HostDBEntry
    21  
    22  	// AveragePrice returns the average price of a host.
    23  	AveragePrice() types.Currency
    24  
    25  	// Close closes the hostdb.
    26  	Close() error
    27  
    28  	// IsOffline reports whether a host is consider offline.
    29  	IsOffline(modules.NetAddress) bool
    30  }
    31  
    32  // A hostContractor negotiates, revises, renews, and provides access to file
    33  // contracts.
    34  type hostContractor interface {
    35  	// SetAllowance sets the amount of money the contractor is allowed to
    36  	// spend on contracts over a given time period, divided among the number
    37  	// of hosts specified. Note that contractor can start forming contracts as
    38  	// soon as SetAllowance is called; that is, it may block.
    39  	SetAllowance(modules.Allowance) error
    40  
    41  	// Allowance returns the current allowance
    42  	Allowance() modules.Allowance
    43  
    44  	// Contracts returns the contracts formed by the contractor.
    45  	Contracts() []modules.RenterContract
    46  
    47  	// Editor creates an Editor from the specified contract, allowing it to be
    48  	// modified.
    49  	Editor(modules.RenterContract) (contractor.Editor, error)
    50  
    51  	// FinancialMetrics returns the financial metrics of the contractor.
    52  	FinancialMetrics() modules.RenterFinancialMetrics
    53  
    54  	// Downloader creates a Downloader from the specified contract, allowing
    55  	// the retrieval of sectors.
    56  	Downloader(modules.RenterContract) (contractor.Downloader, error)
    57  }
    58  
    59  // A trackedFile contains metadata about files being tracked by the Renter.
    60  // Tracked files are actively repaired by the Renter. By default, files
    61  // uploaded by the user are tracked, and files that are added (via loading a
    62  // .sia file) are not.
    63  type trackedFile struct {
    64  	// location of original file on disk
    65  	RepairPath string
    66  }
    67  
    68  // A Renter is responsible for tracking all of the files that a user has
    69  // uploaded to Sia, as well as the locations and health of these files.
    70  type Renter struct {
    71  	// modules
    72  	cs     modules.ConsensusSet
    73  	wallet modules.Wallet
    74  
    75  	// resources
    76  	hostDB         hostDB
    77  	hostContractor hostContractor
    78  	log            *persist.Logger
    79  
    80  	// variables
    81  	files         map[string]*file
    82  	tracking      map[string]trackedFile // map from nickname to metadata
    83  	downloadQueue []*download
    84  
    85  	// constants
    86  	persistDir string
    87  
    88  	mu *sync.RWMutex
    89  }
    90  
    91  // New returns an empty renter.
    92  func New(cs modules.ConsensusSet, wallet modules.Wallet, tpool modules.TransactionPool, persistDir string) (*Renter, error) {
    93  	hdb, err := hostdb.New(cs, persistDir)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	hc, err := contractor.New(cs, wallet, tpool, hdb, persistDir)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	r := &Renter{
   103  		cs:             cs,
   104  		wallet:         wallet,
   105  		hostDB:         hdb,
   106  		hostContractor: hc,
   107  
   108  		files:    make(map[string]*file),
   109  		tracking: make(map[string]trackedFile),
   110  
   111  		persistDir: persistDir,
   112  		mu:         sync.New(modules.SafeMutexDelay, 1),
   113  	}
   114  	err = r.initPersist()
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	go r.threadedRepairLoop()
   120  
   121  	return r, nil
   122  }
   123  
   124  // Close closes the Renter and its dependencies
   125  func (r *Renter) Close() error {
   126  	return r.hostDB.Close()
   127  }
   128  
   129  // hostdb passthroughs
   130  func (r *Renter) ActiveHosts() []modules.HostDBEntry { return r.hostDB.ActiveHosts() }
   131  func (r *Renter) AllHosts() []modules.HostDBEntry    { return r.hostDB.AllHosts() }
   132  
   133  // contractor passthroughs
   134  func (r *Renter) Allowance() modules.Allowance           { return r.hostContractor.Allowance() }
   135  func (r *Renter) Contracts() []modules.RenterContract    { return r.hostContractor.Contracts() }
   136  func (r *Renter) SetAllowance(a modules.Allowance) error { return r.hostContractor.SetAllowance(a) }
   137  func (r *Renter) FinancialMetrics() modules.RenterFinancialMetrics {
   138  	return r.hostContractor.FinancialMetrics()
   139  }
   140  
   141  // enforce that Renter satisfies the modules.Renter interface
   142  var _ modules.Renter = (*Renter)(nil)