func TestAddRoute(t *testing.T) { node1, node2 := NewNode(), NewNode() assert.Len(t, node1.RouteForwardingRules, 0, "Should be 0 routes") ccid := node1.AddControlChannel() node1.Tick() // run channels consuming tf := transport.NewTransportFactory() tf.ConnectNodeToNode(node1, node2) tr1, _ := tf.GetTransports() tid1 := tr1.Id incomingRouteId := messages.RandRouteId() outgoingRouteId := messages.RandRouteId() msg := messages.AddRouteControlMessage{ (messages.TransportId)(0), tr1.Id, incomingRouteId, outgoingRouteId, } msgS := messages.Serialize(messages.MsgAddRouteControlMessage, msg) controlMessage := messages.InControlMessage{ccid, msgS} node1.InjectControlMessage(controlMessage) time.Sleep(1 * time.Millisecond) assert.Len(t, node1.RouteForwardingRules, 1, "Should be 1 routes") assert.Equal(t, node1.RouteForwardingRules[incomingRouteId].IncomingRoute, incomingRouteId) assert.Equal(t, node1.RouteForwardingRules[incomingRouteId].OutgoingRoute, outgoingRouteId) assert.Equal(t, node1.RouteForwardingRules[incomingRouteId].OutgoingTransport, tid1) fmt.Println("--------------------\n") }
func TestRemoveRoute(t *testing.T) { node1, node2 := NewNode(), NewNode() ccid := node1.AddControlChannel() node1.Tick() // run channels consuming tf := transport.NewTransportFactory() tf.ConnectNodeToNode(node1, node2) routeId := messages.RandRouteId() msg := messages.AddRouteControlMessage{} msg.IncomingRouteId = routeId msgS := messages.Serialize(messages.MsgAddRouteControlMessage, msg) controlMessage := messages.InControlMessage{ccid, msgS} node1.InjectControlMessage(controlMessage) msg2 := messages.RemoveRouteControlMessage{routeId} msgS2 := messages.Serialize(messages.MsgRemoveRouteControlMessage, msg2) controlMessage = messages.InControlMessage{ccid, msgS2} node1.InjectControlMessage(controlMessage) time.Sleep(1 * time.Millisecond) assert.Len(t, node1.RouteForwardingRules, 0, "Should be 0 routes") fmt.Println("--------------------\n") }
func (self *NodeManager) ConnectNodeToNode(idA, idB cipher.PubKey) *transport.TransportFactory { if idA == idB { fmt.Println("Cannot connect node to itself") return &transport.TransportFactory{} } nodes := self.NodeList nodeA, found := nodes[idA] if !found { fmt.Println("Cannot find node with ID", idA) return &transport.TransportFactory{} } nodeB, found := nodes[idB] if !found { fmt.Println("Cannot find node with ID", idB) return &transport.TransportFactory{} } tf := transport.NewTransportFactory() tf.ConnectNodeToNode(nodeA, nodeB) self.TransportFactoryList = append(self.TransportFactoryList, tf) go tf.Tick() return tf }