Ejemplo n.º 1
0
// Add transport to Node
func AddPeersToNode(node *mesh.Node, config TestConfig) {

	emptyPK := cipher.PubKey{}

	// Connect
	for info, peerToPeer := range config.PeerToPeers {
		if peerToPeer.Peer == emptyPK {
			continue
		}
		addr, port := infoToAddr(info)
		udpConfig := physical.CreateUdp(port, addr)
		udpTransport := physical.CreateNewUDPTransport(udpConfig)
		connectError := udpTransport.ConnectToPeer(peerToPeer.Peer, peerToPeer.Info)
		if connectError != nil {
			panic(connectError)
		}
		transportToPeer := transport.NewTransport(udpTransport, config.TransportConfig)
		node.AddTransport(transportToPeer)
	}
}
Ejemplo n.º 2
0
// Initialize the Nodes for communication and sending messages
func InitializeNode(idConfig int, config TestConfig, wg *sync.WaitGroup, statusChannel chan bool) {
	fmt.Fprintf(os.Stderr, "Starting Config: %v\n", idConfig)
	defer wg.Done()

	udpTransport := createNewUDPTransport(config.UDPConfig)

	// Connect
	for _, connectTo := range config.PeersToConnect {
		connectError := udpTransport.ConnectToPeer(connectTo.Peer, connectTo.Info)
		if connectError != nil {
			panic(connectError)
		}
	}

	// Transport closes UDPTransport
	transportToPeer := transport.NewTransport(udpTransport, config.TransportConfig)
	//	defer transportToPeer.Close()

	node, createNodeError := mesh.NewNode(config.NodeConfig)
	if createNodeError != nil {
		panic(createNodeError)
	}
	defer node.Close()
	node.AddTransport(transportToPeer)

	fmt.Fprintf(os.Stdout, "UDP connect info: %v\n", udpTransport.GetTransportConnectInfo())

	// Setup route
	for _, routeConfig := range config.RoutesToEstablish {
		if len(routeConfig.Peers) == 0 {
			continue
		}
		addRouteErr := node.AddRoute((domain.RouteID)(routeConfig.ID), routeConfig.Peers[0])
		if addRouteErr != nil {
			panic(addRouteErr)
		}
		for peer := 1; peer < len(routeConfig.Peers); peer++ {
			extendErr := node.ExtendRoute((domain.RouteID)(routeConfig.ID), routeConfig.Peers[peer], 5*time.Second)
			if extendErr != nil {
				panic(extendErr)
			}
		}
	}

	// Send messages
	for _, messageToSend := range config.MessagesToSend {
		sendMsgErr := node.SendMessageThruRoute((domain.RouteID)(messageToSend.ThruRoute), messageToSend.Contents)
		if sendMsgErr != nil {
			panic(sendMsgErr)
		}
		fmt.Fprintf(os.Stdout, "Send message %v from Node: %v to Node: %v\n", messageToSend.Contents, idConfig, node.GetConnectedPeers()[0].Hex())
	}

	// Receive messages
	received := make(chan domain.MeshMessage, 2*len(config.MessagesToReceive))
	node.SetReceiveChannel(received)

	// Wait for messages to pass thru
	recvMap := make(map[string]domain.ReplyTo)
	for timeEnd := time.Now().Add(5 * time.Second); time.Now().Before(timeEnd); {

		if len(received) > 0 {
			fmt.Fprintf(os.Stdout, "Len Receive Channel %v in Node: %v \n", len(received), idConfig)
			msgRecvd := <-received
			recvMap[fmt.Sprintf("%v", msgRecvd.Contents)] = msgRecvd.ReplyTo

			for _, messageToReceive := range config.MessagesToReceive {
				if fmt.Sprintf("%v", messageToReceive.Contents) == fmt.Sprintf("%v", msgRecvd.Contents) {
					if len(messageToReceive.Reply) > 0 {
						sendBackErr := node.SendMessageBackThruRoute(msgRecvd.ReplyTo, messageToReceive.Reply)
						if sendBackErr != nil {
							panic(sendBackErr)
						}
						fmt.Fprintf(os.Stdout, "=== Send back %v\n", time.Now())
					}
				}
			}
		}
		runtime.Gosched()
	}

	success := true

	for _, messageToReceive := range config.MessagesToReceive {
		_, received := recvMap[fmt.Sprintf("%v", messageToReceive.Contents)]
		if !received {
			success = false
			fmt.Fprintf(os.Stdout, "Didn't receive message contents: %v - Node: %v\n", messageToReceive.Contents, idConfig)
		}
	}
	// Wait for messages to pass back
	time.Sleep(5 * time.Second)

	fmt.Fprintf(os.Stdout, "-- Finished test -- %v\n", time.Now())
	if success {
		fmt.Fprint(os.Stdout, "\t Success!\n")
	} else {
		fmt.Fprint(os.Stderr, "\t Failure. \n")
	}

	statusChannel <- success
}