Skip to content

Instantly share code, notes, and snippets.

@anacrolix
Created January 15, 2016 17:54
Show Gist options
  • Save anacrolix/bff29c6bdb585551bc4f to your computer and use it in GitHub Desktop.
Save anacrolix/bff29c6bdb585551bc4f to your computer and use it in GitHub Desktop.
example using piece store
package main
import (
"bytes"
"crypto/tls"
"database/sql"
"encoding/hex"
"flag"
"html/template"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
_ "github.com/anacrolix/envpprof"
"github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/filecache"
"github.com/anacrolix/missinggo/httptoo"
"github.com/anacrolix/missinggo/itertools"
"github.com/anacrolix/missinggo/perf"
_ "github.com/anacrolix/sqlrpc"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/data/pieceStore"
"github.com/anacrolix/torrent/data/pieceStore/dataBackend"
"github.com/anacrolix/torrent/data/pieceStore/dataBackend/fileCache"
"github.com/anacrolix/torrent/data/pieceStore/dataBackend/http"
"github.com/anacrolix/torrent/dht"
"github.com/anacrolix/torrent/iplist"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/utp"
"github.com/dustin/go-humanize"
"github.com/garyburd/redigo/redis"
gctx "github.com/gorilla/context"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
func getBackend(storeURL string) dataBackend.I {
u, err := url.Parse(storeURL)
if err != nil {
missinggo.Fatal(err)
}
q := u.Query()
switch u.Scheme {
case "http", "https":
return httpDataBackend.New(*u)
case "file":
log.Printf("file blob-store rooted at: %q", u.Path)
c, err := filecache.NewCache(u.Path)
if err != nil {
log.Fatal(err)
}
if _cap_ := q.Get("capacity"); _cap_ != "" {
_cap, err := humanize.ParseBytes(_cap_)
if err != nil {
log.Fatal(err)
}
c.SetCapacity(int64(_cap))
}
return fileCacheDataBackend.New(c)
}
log.Fatalf("bad store scheme: %q", u.Scheme)
return nil
}
func getStore(storeURL string) torrent.TorrentDataOpener {
fileStore = getBackend(storeURL)
store := pieceStore.New(fileStore)
return func(mi *metainfo.Info) torrent.Data {
return store.OpenTorrentData(mi)
}
}
func main() {
blobStore := flag.String("blobStore", "http://127.0.0.1:2076", "url of http blob store")
flag.Parse()
storage := getStore(*blobStore)
torrentClient, err := torrent.NewClient(&torrent.Config{
DisableUTP: *disableUTP || *passiveClient,
DisableTCP: *disableTCP || *passiveClient,
DisablePEX: *disablePEX,
DataDir: "data",
DHTConfig: &dht.ServerConfig{
Passive: true,
PublicIP: func() (ret net.IP) {
if *dhtPublicIP == "" {
return
}
ret = net.ParseIP(*dhtPublicIP)
if ret == nil {
log.Fatal("bad -dhtPublicIP")
}
return
}(),
},
DisableTrackers: *disableTrackers || *passiveClient,
NoDHT: *passiveClient || *disableDHT,
DisableIPv6: *disableIPv6,
// The server is launched with everything in its working directory.
ConfigDir: ".",
ListenAddr: *torrentAddr,
TorrentDataOpener: storage,
})
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment