Beispiel #1
0
func initStockClient(stockServerHostPort string) error {
	cli, err := stockclient.NewStockClient(stockServerHostPort)
	if err != nil {
		return err
	}

	client = cli
	/*	status, err := client.CreateUser("user", "pass")
		if err != nil {
			LOGE.Println("FAIL: Could not create user. Received status: ", statusMap[status])
			return err
		}

		status, key, err := client.LoginUser("user", "pass")
		if err != nil {
			LOGE.Println("FAIL: Could not login. Received status: ", statusMap[status])
			return err
		}

		sessionKey = key*/

	return nil
}
Beispiel #2
0
func main() {
	flag.Parse()
	if flag.NFlag() != 1 {
		flag.Usage()
		os.Exit(1)
	}

	client, err := stockclient.NewStockClient(net.JoinHostPort("localhost", strconv.Itoa(*port)))
	if err != nil {
		log.Fatalln("Failed to create StockClient:", err)
	}

	cmdlist := []cmdInfo{
		{"login", "StockServer.LoginUser", 2},
		{"cu", "StockServer.CreateUser", 2},
		{"ct", "StockServer.CreateTeam", 2},
		{"jt", "StockServer.JoinTeam", 2},
		{"lt", "StockServer.LeaveTeam", 1},
		{"tx", "StockServer.MakeTransaction", 1},
		{"gp", "StockServer.GetPortfolio", 1},
		{"pr", "StockServer.GetPrice", 1},
	}

	cmdmap := make(map[string]cmdInfo)
	for _, j := range cmdlist {
		cmdmap[j.cmdline] = j
	}

	flag.Usage()
	input := bufio.NewReader(os.Stdin)

	for {
		fmt.Println("Please enter a command: ")
		line, err := input.ReadString('\n')
		/*		_, err := fmt.Fscanf(os.Stdin, "%s %s %s \n", &args[0], &args[1], &args[2])*/
		if err != nil {
			fmt.Println("Error Parsing Args: ", err)
			flag.Usage()
			os.Exit(1)
		}

		args := strings.Split(line, " ")
		cmd := args[0]
		lastBytes := []byte(args[len(args)-1])
		args[len(args)-1] = string(lastBytes[0 : len(lastBytes)-1])

		ci, found := cmdmap[cmd]
		if !found {
			fmt.Printf("Command not found: %s\n", cmd)
			flag.Usage()
			//os.Exit(1)
		}

		if len(args) < (ci.nargs + 1) {
			fmt.Printf("Expected args: %d, but got: %d\n", ci.nargs, len(args)-1)
			flag.Usage()
			continue
		}

		switch cmd {
		case "login": // login
			status, key, err := client.LoginUser(args[1], args[2])
			sessionKey = key
			printStatus(ci.funcname, status, err)
		case "cu": // create user
			log.Println("Attempting to create user")
			status, err := client.CreateUser(args[1], args[2])
			printStatus(ci.funcname, status, err)
		case "ct": // create team
			checkSession()
			if len(args) < 3 {
				fmt.Println("Error parsing requests")
				break
			}
			status, err := client.CreateTeam(sessionKey, args[1], args[2])
			printStatus(ci.funcname, status, err)
		case "jt": // join team

			checkSession()
			if len(args) < 3 {
				fmt.Println("Error parsing requests")
				break
			}
			status, err := client.JoinTeam(sessionKey, args[1], args[2]) // error handling for
			printStatus(ci.funcname, status, err)
		case "lt": // leave team
			checkSession()
			status, err := client.LeaveTeam(sessionKey, args[1])
			printStatus(ci.funcname, status, err)
		case "tx": // make transaction
			checkSession()
			reqs, err := parseRequests(args[1:])
			if err != nil {
				fmt.Println("Error parsing requests")
				break
			}
			status, err := client.MakeTransaction(sessionKey, reqs)
			printStatus(ci.funcname, status, err)
		case "gp":
			holdings, status, err := client.GetPortfolio(args[1])
			if status == datatypes.OK {
				printHoldings(holdings)
			} else {
				printStatus(ci.funcname, status, err)
			}
		case "pr":
			price, status, err := client.GetPrice(args[1])
			if status == datatypes.OK {
				printTicker(args[1], price)
			} else {
				printStatus(ci.funcname, status, err)
			}

		}

	} // infinite for loop reading input
}