Esempio n. 1
0
func NewAgent(dataDir, port, dockerEndpoint, btClient string) (agent *Agent, err error) {
	if err = initWorkspace(dataDir); err != nil {
		return
	}

	dockerClient, err := docker.NewClient(dockerEndpoint)
	if err != nil {
		return
	}

	err = dockerClient.Ping()
	if err != nil {
		return
	}

	var bt bittorrent.BitTorrent

	if btClient == "builtin" {
		bt, err = bittorrent.NewBuiltin(filepath.Join(dataDir, "package"))
		if err != nil {
			return
		}
	} else if btClient == "ctorrent" {
		//TODO check if ctorrent is installed
		bt, err = bittorrent.NewCtorrent()
		if err != nil {
			return
		}
	} else {
		err = fmt.Errorf("unsupported bt client: %s", btClient)
		return
	}

	agent = &Agent{
		DataDir:      dataDir,
		Port:         port,
		DockerClient: dockerClient,
		BTClient:     bt,
		TaskPool:     make(map[string]chan struct{}),
	}
	return
}
Esempio n. 2
0
func NewManager(dataDir, port, dockerEndpoint, btClient, scheduler string, trackers []string) (manager *Manager, err error) {
	if err = initWorkspace(dataDir); err != nil {
		return
	}

	dockerClient, err := docker.NewClient(dockerEndpoint)
	if err != nil {
		return
	}

	err = dockerClient.Ping()
	if err != nil {
		return
	}

	var bt bittorrent.BitTorrent

	if btClient == "builtin" {
		bt, err = bittorrent.NewBuiltin(filepath.Join(dataDir, "package"))
		if err != nil {
			return
		}
	} else if btClient == "ctorrent" {
		//TODO check if ctorrent is installed
		bt, err = bittorrent.NewCtorrent()
		if err != nil {
			return
		}
	} else {
		err = fmt.Errorf("unsupported bt client: %s", btClient)
		return
	}

	var s sch.Scheduler

	if scheduler == "batch" {
		s = sch.NewBatch()
	} else {
		err = fmt.Errorf("unsupported scheduler: %s", scheduler)
		return
	}

	if len(trackers) == 0 {
		err = errors.New("tracker is nil")
		return
	}

	ip, err := getIP()
	if err != nil {
		return
	}

	manager = &Manager{
		DataDir:          dataDir,
		Port:             port,
		DockerClient:     dockerClient,
		BTClient:         bt,
		Scheduler:        s,
		Trackers:         trackers,
		FileServerPrefix: "http://" + ip + ":" + port + "/torrent/",
		TaskPool:         make(map[string]chan struct{}),
	}

	return
}