Exemplo n.º 1
0
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")
}
Exemplo n.º 2
0
func closedCycle() {

	nm := nodemanager.NewNodeManager()

	id1, id2, id3 := nm.AddNewNode(), nm.AddNewNode(), nm.AddNewNode()
	node1, err := nm.GetNodeById(id1)
	if err != nil {
		panic(err)
	}
	node2, err := nm.GetNodeById(id2)
	if err != nil {
		panic(err)
	}
	node3, err := nm.GetNodeById(id3)
	if err != nil {
		panic(err)
	}

	tf12 := nm.ConnectNodeToNode(id1, id2) // transport pair between nodes 1 and 2
	t12, t21 := tf12.GetTransports()
	tid12, tid21 := t12.Id, t21.Id

	tf13 := nm.ConnectNodeToNode(id1, id3) // transport pair between nodes 1 and 3
	t13, t31 := tf13.GetTransports()
	tid13, tid31 := t13.Id, t31.Id

	tf23 := nm.ConnectNodeToNode(id2, id3) // transport pair between nodes 2 and 3
	t23, t32 := tf23.GetTransports()
	tid23, tid32 := t23.Id, t32.Id

	t13.MaxSimulatedDelay = 1100 // a possible point of failure, going through this transport can lead to packet drop

	routeId123, routeId231, routeId312 := messages.RandRouteId(), messages.RandRouteId(), messages.RandRouteId()

	route123 := node.RouteRule{tid21, tid23, routeId123, routeId231} // route from 1 to 3 through 2
	route231 := node.RouteRule{tid32, tid31, routeId231, routeId312} // route from 2 to 1 through 3
	route312 := node.RouteRule{tid13, tid12, routeId312, routeId123} // route from 3 to 2 through 1

	node1.RouteForwardingRules[routeId312] = &route312
	node2.RouteForwardingRules[routeId123] = &route123
	node3.RouteForwardingRules[routeId231] = &route231

	nm.Tick()

	inRouteMessage := messages.InRouteMessage{tid21, routeId123, []byte{'t', 'e', 's', 't'}}
	serialized := messages.Serialize(messages.MsgInRouteMessage, inRouteMessage)
	node2.IncomingChannel <- serialized
	time.Sleep(10 * time.Second)
}
Exemplo n.º 3
0
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")
}
Exemplo n.º 4
0
func (self *NodeManager) ConnectAll() {

	n := len(self.NodeIdList)

	// form transportFactories between the nodes sequentially
	for i := 0; i < n-1; i++ {
		id1, id2 := self.NodeIdList[i], self.NodeIdList[i+1]
		self.ConnectNodeToNode(id1, id2)
	}

	// create rules for building a route from the first node to the last one
	rules := []*node.RouteRule{}
	for i := 0; i < n; i++ {
		ruleId := messages.RandRouteId()
		rules = append(rules, &node.RouteRule{IncomingRoute: ruleId})
	}
	for i := 0; i < n; i++ {
		routeRule := rules[i]
		nodeId := self.NodeIdList[i]
		if i > 0 {
			prevNodeId := self.NodeIdList[i-1]
			incomingTransport, err := self.NodeList[prevNodeId].GetTransportToNode(nodeId)
			if err != nil {
				panic(err)
			}
			routeRule.IncomingTransport = incomingTransport.StubPair.Id
		} else {
			routeRule.IncomingTransport = (messages.TransportId)(0)
		}
		if i < n-1 {
			nextNodeId := self.NodeIdList[i+1]
			outgoingTransport, err := self.NodeList[nodeId].GetTransportToNode(nextNodeId)
			if err != nil {
				panic(err)
			}
			routeRule.OutgoingTransport = outgoingTransport.Id
			routeRule.OutgoingRoute = rules[i+1].IncomingRoute
		} else {
			routeRule.OutgoingTransport = (messages.TransportId)(0)
			routeRule.OutgoingRoute = (messages.RouteId)(0)
		}
		addRouteMessage := messages.AddRouteControlMessage{
			routeRule.IncomingTransport,
			routeRule.OutgoingTransport,
			routeRule.IncomingRoute,
			routeRule.OutgoingRoute,
		}
		serialized := messages.Serialize(messages.MsgAddRouteControlMessage, addRouteMessage)

		node0 := self.NodeList[nodeId]

		ccid := node0.AddControlChannel()
		controlMessage := messages.InControlMessage{ccid, serialized}
		node0.InjectControlMessage(controlMessage)
	}
}
Exemplo n.º 5
0
func TestAck(t *testing.T) {
	tf := NewTransportFactory()
	t1, _ := tf.CreateStubTransportPair()
	go tf.Tick()
	time.Sleep(1 * time.Second)
	for i := 0; i < 10; i++ {
		tdt := messages.OutRouteMessage{messages.RandRouteId(), []byte{'t', 'e', 's', 't'}}
		t1.sendTransportDatagramTransfer(&tdt)
	}
	time.Sleep(10 * time.Second)
	assert.Equal(t, t1.PacketsSent, t1.PacketsConfirmed)
}