예제 #1
0
func testBootstrap(nodes messagePasser.Nodes) {
	localName := getLocalName()
	_, localNode, _ := messagePasser.FindNodeByName(nodes, localName)
	fmt.Println("Contacting the bootstrap server...")
	peers, err := bootstrapClient.GetNodes(localNode)
	if err != nil {
		fmt.Println("Got error:", err)
	} else {
		fmt.Printf("Got peers: %+v\n", peers)
	}
}
예제 #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.")
		}
	}
}
예제 #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()
	}
}