コード例 #1
0
func attemptConnectionToPeer(address string, cm *network.ConnectionManager) {
	c, err := tls.Dial(address)
	if err != nil {
		log.Printf("Error trying to connect to %v : %v", address, err)
	} else {
		cm.Handle(c)
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: sdgoij/TheDistributedBay
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()
}