storage

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 30 Imported by: 2

Documentation

Index

Constants

View Source
const (
	EventErr             = "ERR"
	EventBagResolved     = "BAG_RESOLVED"
	EventFileDownloaded  = "FILE_DOWNLOADED"
	EventDone            = "DONE"
	EventPieceDownloaded = "PIECE_DOWNLOADED"
	EventProgress        = "PROGRESS"
)

Variables

View Source
var DownloadMaxInflightScore = int32(400)
View Source
var DownloadPrefetch = 200
View Source
var DownloadThreads = 24
View Source
var ErrFileNotExist = errors.New("file is not exists in torrent")
View Source
var Logger = func(...any) {}

Functions

func ToSpeed

func ToSpeed(speed uint64) string

func ToSz

func ToSz(sz uint64) string

Types

type ADNLProofScheme added in v0.5.0

type ADNLProofScheme struct {
	Key []byte `tl:"int256"`
}

type AddUpdate

type AddUpdate struct {
	SessionID int64 `tl:"long"`
	Seqno     int64 `tl:"int"`
	Update    any   `tl:"struct boxed [storage.updateInit,storage.updateHavePieces,storage.updateState]"`
}

type Connector

type Connector struct {
	TorrentServer
	// contains filtered or unexported fields
}

func NewConnector

func NewConnector(srv TorrentServer) *Connector

func (*Connector) CreateDownloader

func (c *Connector) CreateDownloader(ctx context.Context, t *Torrent) (_ TorrentDownloader, err error)

func (*Connector) GetADNLPrivateKey added in v0.5.0

func (c *Connector) GetADNLPrivateKey() ed25519.PrivateKey

func (*Connector) GetDownloadLimit

func (c *Connector) GetDownloadLimit() uint64

func (*Connector) GetUploadLimit

func (c *Connector) GetUploadLimit() uint64

func (*Connector) SetDownloadLimit

func (c *Connector) SetDownloadLimit(bytesPerSec uint64)

func (*Connector) SetUploadLimit

func (c *Connector) SetUploadLimit(bytesPerSec uint64)

func (*Connector) ThrottleDownload

func (c *Connector) ThrottleDownload(ctx context.Context, sz uint64) error

func (*Connector) ThrottleUpload

func (c *Connector) ThrottleUpload(ctx context.Context, sz uint64) error

type DHT

type DHT interface {
	StoreAddress(ctx context.Context, addresses address.List, ttl time.Duration, ownerKey ed25519.PrivateKey, copies int) (int, []byte, error)
	FindAddresses(ctx context.Context, key []byte) (*address.List, ed25519.PublicKey, error)
	FindOverlayNodes(ctx context.Context, overlayId []byte, continuation ...*dht.Continuation) (*overlay.NodesList, *dht.Continuation, error)
	Close()
}

type DownloadResult

type DownloadResult struct {
	Path        string
	Dir         string
	Description string
}

type Event

type Event struct {
	Name  string
	Value any
}

type FDesc

type FDesc struct {
	// contains filtered or unexported fields
}

func (*FDesc) Free

func (f *FDesc) Free()

func (*FDesc) Get

func (f *FDesc) Get() io.ReaderAt

type FECInfoNone

type FECInfoNone struct{}

type FS added in v0.2.0

type FS interface {
	Open(name string, mode OpenMode) (FSFile, error)
	Delete(name string) error
	Exists(name string) bool
}

type FSController

type FSController struct {
	// contains filtered or unexported fields
}

FSController caches files descriptors to avoid unnecessary open/close of most used files

func NewFSController

func NewFSController() *FSController

func (*FSController) Acquire

func (f *FSController) Acquire(path string) (*FDesc, error)

type FSFile added in v0.2.0

type FSFile interface {
	io.ReaderAt
	io.WriterAt
	io.Closer
	Sync() error
}

type FileIndex

type FileIndex struct {
	BlockFrom       uint32
	BlockTo         uint32
	BlockFromOffset uint32
	BlockToOffset   uint32
	Name            string
	// contains filtered or unexported fields
}

type FileInfo

type FileInfo struct {
	Size            uint64
	FromPiece       uint32
	ToPiece         uint32
	FromPieceOffset uint32
	ToPieceOffset   uint32
	Index           uint32
	Name            string
}

type FileRef added in v0.2.0

type FileRef interface {
	GetName() string
	GetSize() uint64
	CreateReader() (io.ReadCloser, error)
}

type GetPiece

type GetPiece struct {
	PieceID int32 `tl:"int"`
}

type GetTorrentInfo

type GetTorrentInfo struct{}

type NetConnector

type NetConnector interface {
	GetADNLPrivateKey() ed25519.PrivateKey
	SetDownloadLimit(bytesPerSec uint64)
	SetUploadLimit(bytesPerSec uint64)
	GetUploadLimit() uint64
	GetDownloadLimit() uint64
	ThrottleDownload(ctx context.Context, sz uint64) error
	ThrottleUpload(ctx context.Context, sz uint64) error
	CreateDownloader(ctx context.Context, t *Torrent) (_ TorrentDownloader, err error)
	TorrentServer
}

type Ok

type Ok struct{}

type OpenMode added in v0.2.0

type OpenMode int
const (
	OpenModeRead OpenMode = iota
	OpenModeWrite
)

type PeerConnection

type PeerConnection struct {
	// contains filtered or unexported fields
}

func (*PeerConnection) CloseFor

func (c *PeerConnection) CloseFor(peer *storagePeer)

func (*PeerConnection) GetFor

func (c *PeerConnection) GetFor(id []byte) *storagePeer

func (*PeerConnection) UseFor

func (c *PeerConnection) UseFor(peer *storagePeer)

type PeerInfo

type PeerInfo struct {
	Addr       string
	LastSeenAt time.Time
	Uploaded   uint64
	Downloaded uint64
	// contains filtered or unexported fields
}

func (*PeerInfo) GetDownloadSpeed

func (p *PeerInfo) GetDownloadSpeed() uint64

func (*PeerInfo) GetUploadSpeed

func (p *PeerInfo) GetUploadSpeed() uint64

type Piece

type Piece struct {
	Proof []byte `tl:"bytes"`
	Data  []byte `tl:"bytes"`
}

type PieceInfo

type PieceInfo struct {
	StartFileIndex uint32
	Proof          []byte
}

type PiecesInfo

type PiecesInfo struct {
	OverallPieces    int
	PiecesToDownload int
}

type Ping

type Ping struct {
	SessionID int64 `tl:"long"`
}

type Pong

type Pong struct{}

type PreFetcher

type PreFetcher struct {
	// contains filtered or unexported fields
}

func NewPreFetcher

func NewPreFetcher(ctx context.Context, torrent *Torrent, downloader TorrentDownloader, report func(Event), downloaded uint64, threads, prefetch int, pieces []uint32) *PreFetcher

func (*PreFetcher) Free

func (f *PreFetcher) Free(piece uint32)

func (*PreFetcher) Get

func (f *PreFetcher) Get(ctx context.Context, piece uint32) ([]byte, []byte, error)

func (*PreFetcher) Stop

func (f *PreFetcher) Stop()

type Progress

type Progress struct {
	Downloaded string
	Speed      string
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(dht *dht.Client, gate *adnl.Gateway, key ed25519.PrivateKey, serverMode bool) *Server

func (*Server) GetADNLPrivateKey added in v0.5.0

func (s *Server) GetADNLPrivateKey() ed25519.PrivateKey

func (*Server) GetPeerIfActive

func (s *Server) GetPeerIfActive(id []byte) *PeerConnection

func (*Server) SetStorage

func (s *Server) SetStorage(store Storage)

func (*Server) StartPeerSearcher

func (s *Server) StartPeerSearcher(t *Torrent)

func (*Server) Stop

func (s *Server) Stop()

type State

type State struct {
	WillUpload   bool `tl:"bool"`
	WantDownload bool `tl:"bool"`
}

type Storage

type Storage interface {
	GetFS() FS
	GetAll() []*Torrent
	GetTorrentByOverlay(overlay []byte) *Torrent
	SetTorrent(torrent *Torrent) error
	SetActiveFiles(bagId []byte, ids []uint32) error
	GetActiveFiles(bagId []byte) ([]uint32, error)
	GetPiece(bagId []byte, id uint32) (*PieceInfo, error)
	RemovePiece(bagId []byte, id uint32) error
	SetPiece(bagId []byte, id uint32, p *PieceInfo) error
	PiecesMask(bagId []byte, num uint32) []byte
	UpdateUploadStats(bagId []byte, val uint64) error
}

type Torrent

type Torrent struct {
	BagID     []byte
	Path      string
	Info      *TorrentInfo
	Header    *TorrentHeader
	CreatedAt time.Time
	// contains filtered or unexported fields
}

func CreateTorrent

func CreateTorrent(ctx context.Context, filesRootPath, dirName, description string, db Storage, connector NetConnector, files []FileRef, progressCallback func(done uint64, max uint64)) (*Torrent, error)

func NewTorrent

func NewTorrent(path string, db Storage, connector NetConnector) *Torrent

func (*Torrent) DownloadedPiecesNum added in v0.3.0

func (t *Torrent) DownloadedPiecesNum() int

func (*Torrent) GetActiveFilesIDs

func (t *Torrent) GetActiveFilesIDs() []uint32

func (*Torrent) GetConnector

func (t *Torrent) GetConnector() NetConnector

func (*Torrent) GetFileOffsets

func (t *Torrent) GetFileOffsets(name string) (*FileInfo, error)

func (*Torrent) GetFileOffsetsByID

func (t *Torrent) GetFileOffsetsByID(i uint32) (*FileInfo, error)

func (*Torrent) GetFilesInPiece

func (t *Torrent) GetFilesInPiece(piece uint32) ([]*FileInfo, error)

func (*Torrent) GetLastVerifiedAt added in v0.6.0

func (t *Torrent) GetLastVerifiedAt() (bool, time.Time)

func (*Torrent) GetPeer

func (t *Torrent) GetPeer(id []byte) *PeerInfo

func (*Torrent) GetPeers

func (t *Torrent) GetPeers() map[string]PeerInfo

func (*Torrent) GetPiece

func (t *Torrent) GetPiece(id uint32) (*Piece, error)

func (*Torrent) GetPieceProof added in v0.2.0

func (t *Torrent) GetPieceProof(id uint32) ([]byte, error)

func (*Torrent) GetUploadStats added in v0.3.0

func (t *Torrent) GetUploadStats() uint64

func (*Torrent) InitMask

func (t *Torrent) InitMask()

func (*Torrent) IsActive

func (t *Torrent) IsActive() (activeDownload, activeUpload bool)

func (*Torrent) IsActiveRaw added in v0.4.0

func (t *Torrent) IsActiveRaw() (activeDownload, activeUpload bool)

func (*Torrent) IsCompleted added in v0.3.0

func (t *Torrent) IsCompleted() bool

func (*Torrent) IsDownloadAll

func (t *Torrent) IsDownloadAll() bool

func (*Torrent) IsDownloadOrdered added in v0.2.0

func (t *Torrent) IsDownloadOrdered() bool

func (*Torrent) ListFiles

func (t *Torrent) ListFiles() ([]string, error)

func (*Torrent) LoadActiveFilesIDs

func (t *Torrent) LoadActiveFilesIDs() error

func (*Torrent) PiecesMask

func (t *Torrent) PiecesMask() []byte

func (*Torrent) PiecesNum

func (t *Torrent) PiecesNum() uint32

func (*Torrent) RemovePeer

func (t *Torrent) RemovePeer(id []byte)

func (*Torrent) ResetDownloadPeer

func (t *Torrent) ResetDownloadPeer(id []byte)

func (*Torrent) SetActiveFiles

func (t *Torrent) SetActiveFiles(names []string) error

func (*Torrent) SetActiveFilesIDs

func (t *Torrent) SetActiveFilesIDs(ids []uint32) error

func (*Torrent) SetUploadStats added in v0.3.0

func (t *Torrent) SetUploadStats(val uint64)

func (*Torrent) Start

func (t *Torrent) Start(withUpload, downloadAll, downloadOrdered bool) (err error)

func (*Torrent) Stop

func (t *Torrent) Stop()

func (*Torrent) TouchPeer

func (t *Torrent) TouchPeer(peer *storagePeer) *PeerInfo

func (*Torrent) UpdateDownloadedPeer

func (t *Torrent) UpdateDownloadedPeer(peer *storagePeer, bytes uint64)

func (*Torrent) UpdateUploadedPeer

func (t *Torrent) UpdateUploadedPeer(peer *storagePeer, bytes uint64)

type TorrentDownloader

type TorrentDownloader interface {
	DownloadPiece(ctx context.Context, pieceIndex uint32) (_ []byte, err error)
	DownloadPieceDetailed(ctx context.Context, pieceIndex uint32) (data []byte, proof []byte, peer []byte, peerAddr string, err error)
	Close()
	IsActive() bool
}

type TorrentHeader

type TorrentHeader struct {
	FilesCount    uint32
	TotalNameSize uint64
	TotalDataSize uint64
	FEC           FECInfoNone
	DirNameSize   uint32
	DirName       []byte
	NameIndex     []uint64
	DataIndex     []uint64
	Names         []byte
	Data          []byte
}

func (*TorrentHeader) Parse

func (t *TorrentHeader) Parse(data []byte) (_ []byte, err error)

func (*TorrentHeader) Serialize

func (t *TorrentHeader) Serialize() ([]byte, error)

type TorrentInfo

type TorrentInfo struct {
	PieceSize   uint32   `tlb:"## 32"`
	FileSize    uint64   `tlb:"## 64"`
	RootHash    []byte   `tlb:"bits 256"`
	HeaderSize  uint64   `tlb:"## 64"`
	HeaderHash  []byte   `tlb:"bits 256"`
	Description tlb.Text `tlb:"."`
}

type TorrentInfoContainer

type TorrentInfoContainer struct {
	Data []byte `tl:"bytes"`
}

type TorrentServer

type TorrentServer interface {
	GetADNLPrivateKey() ed25519.PrivateKey
	StartPeerSearcher(t *Torrent)
}

type TorrentStats added in v0.3.0

type TorrentStats struct {
	Uploaded uint64
}

type UpdateHavePieces

type UpdateHavePieces struct {
	PieceIDs []int32 `tl:"vector int"`
}

type UpdateInit

type UpdateInit struct {
	HavePieces       []byte `tl:"bytes"`
	HavePiecesOffset int32  `tl:"int"`
	State            State  `tl:"struct boxed"`
}

type UpdateState

type UpdateState struct {
	State State `tl:"struct boxed"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL