示例#1
0
/* Handles all outbound messages  */
func outboundDispatcher() {
	for {
		// get message from the send channel
		message := <-sendChannel
		// based on it's destination, determine which messagePasser
		//	routine is appropriate
		if message.Destination == defs.MULTICAST_DEST {
			messagePasser.Multicast(&message)
		} else {
			messagePasser.Send(message)
		}
	}
}
示例#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.")
		}
	}
}