示例#1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	log.SetFlags(log.Lshortfile | log.LstdFlags)

	listen := flag.String("listen", ":7654", "Address to listen on")
	connect := flag.String("connect", "", "Address to connect to")
	httpAddress := flag.String("http", ":8080", "Address to listen on for HTTP")
	dumpPath := flag.String("databasedump", "", "The path to a database dump that can be loaded. This should end in .csv.gz")
	devAssets := flag.Bool("devassets", false, "Tells the front end to serve development assests instead of the precompiled production ones.")
	flag.Parse()

	db, err := database.NewTorrentDB("torrent.db")
	if err != nil {
		log.Fatal("Error opening torrent database", err)
	}
	cm := network.NewConnectionManager(db)

	if *connect != "" {
		c, err := tls.Dial(*connect)
		if err != nil {
			log.Fatalf("Error trying to connect to %v : %v", *connect, err)
		}
		cm.Handle(c)
	} else {
		l, err := tls.Listen(*listen)
		if err != nil {
			log.Fatalf("Error trying to listen to %v : %v", *listen, err)
		}
		go cm.Listen(l)
	}

	if *dumpPath != "" {
		go importer.Import(*dumpPath, db)
	}

	go stats.ReportStats(db, cm)
	go torrent.DiscoverPeers(cm, *listen)

	s, err := search.NewSearcher(db, "thedistributedbay")
	if err != nil {
		log.Fatal("Error opening index", err)
	}

	statsUpdater := torrent.NewStatsUpdater(s, db)

	log.Println("Running...")
	frontend.Serve(httpAddress, db, s, *devAssets, statsUpdater)
	cm.Close()
}
示例#2
0
func TestSingleHop(t *testing.T) {
	log.SetFlags(log.Lshortfile | log.LstdFlags)
	t1 := createDefaultTorrent("test1")

	db1, err := database.NewTorrentDB("db1")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll("db1")
	db1.Add(t1)
	cm1 := NewConnectionManager(db1)

	db2, err := database.NewTorrentDB("db2")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll("db2")
	cm2 := NewConnectionManager(db2)

	l, r := testSewer()
	go cm1.Handle(l)
	go cm2.Handle(r)

	c := make(chan *core.Torrent, 2)
	db2.AddTorrentClient(c)

	select {
	case <-c:
	case <-time.After(time.Second):
	}

	recv, err := db2.Get(t1.Hash)
	if err != nil {
		t.Fatalf("Expected torrent %v, error: %v", t1, err)
	}

	if recv.Hash != t1.Hash {
		t.Errorf("Expected torrent %v, got %v", t1, recv)
	}

	t2 := createDefaultTorrent("test2")
	if t2.Hash == t1.Hash {
		t.Fatalf("identical hashes... wtf")
	}

	db1.Add(t2)

	select {
	case <-c:
	case <-time.After(time.Second):
	}

	recv, err = db2.Get(t2.Hash)
	if err != nil {
		t.Fatalf("Expected torrent with hash in %v, error: %v", db2, err)
	}

	if recv.Hash != t2.Hash {
		t.Errorf("Expected torrent with %v, got %v", t1, recv)
	}

	if cm1.NumPeers() != 1 {
		t.Errorf("Expected 1 peer not %d", cm1.NumPeers())
	}

	cm1.Close()
	cm2.Close()
}