Example #1
0
func testConsensus(nodes messagePasser.Nodes) {
	localName := getLocalName()
	messagePasser.InitMessagePasser(nodes, localName)
	go outboundDispatcher()
	go inboundDispatcher()
	leader := nodes[0]
	consensus.InitConsensus(leader, nodes[1:], localName)
	go ConsensusReceiverRoutine()
	go ConsensusCheckReceiverRoutine()
	go ConsensusReachedRoutine()

	var proposalValue string
	for {
		if localName == leader.Name {
			fmt.Println("Hit enter to propose.")
			fmt.Scanf("%s", &proposalValue)
			consensus.Propose(proposalValue, "test")
		}
	}
}
Example #2
0
func testMessagePasser(localNode messagePasser.Node) {
	peers, err := bootstrapClient.GetNodes(localNode)
	if err != nil {
		fmt.Println("Couldn't get peers:", err)
		panic(err)
	}
	*peers = append(*peers, localNode)
	fmt.Print("--------------------------------\n")
	fmt.Println("Available Nodes:")
	for id, node := range *peers {
		fmt.Printf("  ID:%d – %+v\n", id, node)
	}
	fmt.Println("Initing with localName:", localNode.Name)
	messagePasser.InitMessagePasser(*peers, localNode.Name)

	/* start a receiveRoutine to be able to use nonBlockingReceive */
	go receiveRoutine()

	fmt.Println("Please select the operation you want to do:")
	for {
		fmt.Println("Getting operation")
		operation := getOperation()
		if operation == 0 {
			message := getMessage(*peers, localNode.Name)
			messagePasser.Send(message)
		} else if operation == 1 {
			var message messagePasser.Message = nonBlockingReceive()
			if (reflect.DeepEqual(message, messagePasser.Message{})) {
				fmt.Print("No messages received.\n\n")
			} else {
				fmt.Printf("Received: %+v\n\n", message)
			}
		} else if operation == 2 {
			message := getMessage(*peers, localNode.Name)
			messagePasser.Multicast(&message)
			fmt.Println("Did multicast")
		} else {
			fmt.Println("Operation not recognized. Please try again.")
		}
	}
}
Example #3
0
func runGame(gamePort int, uiPort int) {
	/**** THIS IS LIKE ACTUAL GAMEPLAY ***/
	// initialize communication with the UI
	fmt.Printf("Port is:%d\n", uiPort)
	bridges.InitPyBridge(uiPort)
	go PyBridgeReceiver()

	// get the localname
	localNodeName := <-localNameChannel
	fmt.Println("My name is:", localNodeName)

	// determine the game type (multi or single player)
	gameType := <-gameTypeChannel

	if gameType == defs.GAME_TYPE_MULTI {
		// get fellow players
		localNode := messagePasser.Node{Name: localNodeName, IP: "127.0.0.1", Port: gamePort}
		peers, err := bootstrapClient.GetNodes(localNode)
		if err != nil {
			fmt.Println("Couldn't get peers:", err)
			panic(err)
		}
		*peers = append(*peers, localNode)

		// set competitor location
		uiSetCompetitorLocation(localNode.Name, peers)

		// initialize message passer
		messagePasser.InitMessagePasser(*peers, localNodeName)
		fmt.Println(localNodeName, "made message passer.")

		// initialize elections
		go bullySelection.InitBullySelection(*peers, localNodeName)
		go UnicornReciever()

		/* start the routine waiting for messages coming from UI */
		go BullyReceiver()
		go inboundDispatcher()
		go outboundDispatcher()
	}
}