Esempio n. 1
0
// this function only generate a task and add it into `tasks`
func addTask(taskdata icarus.TaskData) (t *task.Task, err error) {
	c, err := client.GetHandle(taskdata.Handle)
	if err != nil {
		return
	}
	var user icarus.User
	user, err = client.MakeUserByData(c, taskdata.User)
	if err != nil {
		return
	}
	courses := make([]icarus.Course, 0)
	for _, csData := range taskdata.Courses {
		var cs icarus.Course
		cs, err = client.MakeCourceByData(c, csData)
		if err != nil {
			return
		}
		courses = append(courses, cs)
	}

	t = task.NewTask(user, courses)
	tasks[taskdata.ID] = TaskEntry{
		Header:   taskdata,
		Instance: t,
	}

	return
}
Esempio n. 2
0
func main() {
	flag.Parse()

	fmt.Println("Icarus Personal")
	fmt.Println("---------------")

	localListen := fmt.Sprintf(":%d", *flagPort)
	localUrl := fmt.Sprintf("http://127.0.0.1:%d", *flagPort)
	delay := 200 * time.Millisecond

	regList := client.RegisteredList()
	if len(regList) == 0 {
		log.Errorf("This personal icarus is running with no actual logic.\n")
		os.Exit(-1)
	}

	server.InitDefaultDispatcher()
	go func() {
		log.Infof("Launching local server on %s...", localListen)
		mux := http.NewServeMux()
		mux.Handle("/", server.DefaultDispatcher)
		panic(http.ListenAndServe(localListen, mux))
	}()

	log.Infof("Launching local satellite on %s...", localUrl)
	satellite.SilentSatellite = true
	for i := 0; i < 8; i++ {
		go satellite.StandardSatellite(localUrl, delay)
	}

	var handler string
	if len(regList) > 1 {
		fmt.Printf("Select a handler in %v: ", regList)
		_, err := fmt.Scanf("%s", &handler)
		if err != nil {
			panic(err)
		}
	} else {
		handler = regList[0]
	}

	cli, err := client.GetHandle(handler)
	if err != nil {
		log.Errorf("Fatal error occured when getting handler %s: %s\n", handler, err.Error())
		os.Exit(1)
	}

	time.Sleep(time.Second)

	var username, password string
	fmt.Printf("Username: "******"Password: "******"Fatal error occured when making user: %s\n", err.Error())
		os.Exit(1)
	}
	courses, err := user.ListCourse()
	if err != nil {
		log.Errorf("Fatal error occured when retrieving courses: %s\n", err.Error())
		os.Exit(1)
	}

	if len(courses) == 0 {
		log.Errorf("You have no courses to be supplemented.\n")
		os.Exit(1)
	}
	fmt.Printf("Select a course: \n")
	for k, v := range courses {
		fmt.Printf("%d) %s: %s\n", k, v.Name, v.Desc)
	}
	var idx int
	for {
		fmt.Printf("Choice (%d - %d): ", 0, len(courses)-1)
		_, err := fmt.Scanf("%d", &idx)
		if err != nil {
			fmt.Printf("Failed to read your choice: %s\n", err.Error())
		} else if idx < 0 || idx >= len(courses) {
			fmt.Printf("Out of range.\n")
		} else {
			break
		}
	}

	course, err := client.MakeCourceByData(cli, courses[idx])
	if err != nil {
		log.Errorf("Fatal error occured when making course: %s\n", err.Error())
		os.Exit(1)
	}

	// Make task
	task := task.NewTask(user, []icarus.Course{course})
	task.Start()

	// Wait for elected
	for {
		time.Sleep(2 * time.Second)
		stat := task.Statistics()
		if stat.Elected {
			os.Exit(0)
		}
	}
}