예제 #1
0
파일: profile.go 프로젝트: reuteran/2013
func RunProfile(name string) {
	file, ok := profiles[name]
	if !ok {
		fmt.Println("There is no such profile loaded.")
		return
	}

	// Remove kill
	select {
	case <-kill:
	default:
	}

	r := bufio.NewReader(file)
	fmt.Println("Profile started.")

outer:
	for {
		// Check if we have been killed
		select {
		case <-kill:
			break outer
		default:
		}

		// Read a line from the file
		line, err := r.ReadString('\n')
		if err != nil {
			if err.Error() == "EOF" {
				break
			}

			fmt.Println("There has been an error while reading the profile.")
			return
		}

		// Get rid of the white spaces and comments
		line = strings.TrimSpace(line)
		if line == "" || line[0] == '#' {
			continue
		}

		// Figure out the command
		cmd := strings.Split(line, " ")
		switch cmd[0] {
		case "print": // We want to print some text
			fmt.Printf("(p %s):", name)
			for i := 1; i < len(cmd); i++ {
				fmt.Printf(" %s", cmd[i])
			}
			fmt.Println()

		case "sleep": // We want to sleep a couple of seconds
			if len(cmd) < 2 {
				fmt.Println("You must specify the number of seconds to sleep.")
				break
			}
			sec, err := strconv.ParseInt(cmd[1], 10, 64)
			if err != nil {
				fmt.Println("The sleep time must be a number.")
				break
			}
			time.Sleep(time.Duration(sec) * time.Second)

		case "next": // Initiate the next round
			rounds.NewRound()

		case "send": // Send a transaction
			rounds.NewTransaction()

		case "mode": // Set a sending mode
			if len(cmd) < 2 {
				fmt.Println("You must specify the mode.")
				break
			}
			tpermin, err := strconv.ParseInt(cmd[1], 10, 64)
			if err != nil {
				fmt.Println("Please specify the number of transactions per minute.")
				break
			}
			rounds.AutogenTransactions(tpermin)

		case "ceil":
			if len(cmd) < 2 {
				fmt.Println("You must specify the ceil.")
				break
			}
			ceil, err := strconv.ParseInt(cmd[1], 10, 64)
			if err != nil {
				fmt.Println("Please specify the ceil.")
				break
			}
			rounds.SetCeil(int(ceil))

		case "fine":
			if len(cmd) < 2 {
				fmt.Println("You must specify the fine.")
				break
			}
			ceil, err := strconv.ParseInt(cmd[1], 10, 64)
			if err != nil {
				fmt.Println("Please specify the fine.")
				break
			}
			rounds.SetFine(int(ceil))

		case "hops":
			if len(cmd) < 2 {
				fmt.Println("You must specify the hops.")
				break
			}
			ceil, err := strconv.ParseInt(cmd[1], 10, 64)
			if err != nil {
				fmt.Println("Please specify the hops.")
				break
			}
			rounds.SetHops(int(ceil))

		default: // Woops, we don't know this one
			fmt.Println("Unrecogized command:", cmd[0])
		}
	}

	fmt.Println("Profile stopped.")
	file.Seek(0, os.SEEK_SET)
}
예제 #2
0
파일: main.go 프로젝트: reuteran/2013
func main() {
	defer func() {
		log.Close()             // Close log
		server.Close()          // Close server
		betternode.Close()      // Close nodes
		profile.StopProfile()   // Close profile
		time.Sleep(time.Second) // Wait for everything to close
	}()

	// Make Go run parallel
	runtime.GOMAXPROCS(runtime.NumCPU())

	log.Message("ManiacMaster starting.")

	// Initialize database
	log.Message("Connecting to database.")
	err := database.Connect("localhost:27018", "maniac", "", "")
	if err != nil {
		log.Error(err)
		fmt.Println("Error:", err.Error())
	}

	// Create TCP server
	log.Message("Starting TCP server.")
	err = server.Start(":6789", betternode.NewNode)
	if err != nil {
		log.Error(err)
		fmt.Println("Error:", err.Error())
	}

	// Take commands
	log.Message("Ready for command line input.")

	fmt.Println("MANIAC Master ready.")
	var command string

	s := bufio.NewScanner(os.Stdin)

quit:
	for {
		// Get next command
		fmt.Print("M> ")
		if !s.Scan() {
			fmt.Println("Input ended!")
			break
		}
		command = s.Text()

		// to trimmed lower case
		command = strings.ToLower(strings.TrimSpace(command))

		switch command {
		case "help": // show helpful text
			printHelp()

		case "quit", "exit": // quits the master
			break quit

		case "next": // starts new round
			rounds.NewRound()

		case "send": // sends new message
			rounds.NewTransaction()

		case "info": // show info about the master
			printInfo()

		case "load":
			fmt.Print("Profile file: ")
			fmt.Scanln(&command)
			profile.OpenProfile(command)

		case "run":
			fmt.Print("Profile name: ")
			fmt.Scanln(&command)
			go profile.RunProfile(command)

		case "kill":
			profile.StopProfile()

		case "list":
			profile.ListProfiles()

		case "close":
			fmt.Print("Profile name: ")
			fmt.Scanln(&command)
			profile.CloseProfile(command)

		default:
			fmt.Println("Invalid command: Type \"help\" to get help.")

		}
	}
}