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 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 (self *Transport) sendAck(msg *[]byte, m2 *messages.TransportDatagramTransfer) { routeId := m2.RouteId sequence := m2.Sequence datagram := m2.Datagram msgToNode := messages.InRouteMessage{self.Id, routeId, datagram} serialized := messages.Serialize(messages.MsgInRouteMessage, msgToNode) self.InjectNodeMessage(serialized) time.Sleep(time.Duration(rand.Intn(self.MaxSimulatedDelay)) * time.Millisecond) // simulating delay, testing purposes! ackMsg := messages.TransportDatagramACK{sequence, 0} ackSerialized := messages.Serialize(messages.MsgTransportDatagramACK, ackMsg) ackChannel := self.StubPair.ackChannels[string(*msg)] ackChannel <- ackSerialized }
func (self *Node) HandleInRouteMessage(m1 messages.InRouteMessage) { //look in route table routeId := m1.RouteId transportId := m1.TransportId //who is is from //check that transport exists if _, ok := self.Transports[transportId]; !ok { log.Printf("Node: Received message From Transport that does not exist\n") } //check if route exists routeRule, ok := self.RouteForwardingRules[routeId] if !ok { log.Printf("Node: Received route message for route that does not exist\n") } //check that incoming transport is correct if transportId != routeRule.IncomingTransport { log.Panic("Node: incoming route does not match the transport id it should be received from") } if routeId != routeRule.IncomingRoute { log.Panic("Node: impossible, incoming route id does not match route rule id") } //construct new packet var out messages.OutRouteMessage out.RouteId = routeRule.OutgoingRoute //replace inRoute, with outRoute, using rule outgoingTransportId := routeRule.OutgoingTransport //find transport to resend datagram out.Datagram = m1.Datagram //serialize message, with prefix b1 := messages.Serialize(messages.MsgOutRouteMessage, out) //self.Transports[transportId].InjectNodeMessage(b1) //inject message to transport if outgoingTransport, found := self.Transports[outgoingTransportId]; found { outgoingTransport.IncomingChannel <- b1 //inject message to transport } }
func TestNetwork(t *testing.T) { n := 20 nm := NewNodeManager() nm.CreateNodeList(n) assert.Len(t, nm.NodeIdList, n, fmt.Sprintf("Should be %d nodes", n)) nm.Tick() nm.ConnectAll() time.Sleep(1 * time.Second) node0, err := nm.GetNodeById(nm.NodeIdList[0]) if err != nil { panic(err) } initRoute := &node.RouteRule{} for _, initRoute = range node0.RouteForwardingRules { break } inRouteMessage := messages.InRouteMessage{(messages.TransportId)(0), initRoute.IncomingRoute, []byte{'t', 'e', 's', 't'}} serialized := messages.Serialize(messages.MsgInRouteMessage, inRouteMessage) node0.IncomingChannel <- serialized time.Sleep(10 * time.Second) for _, tf := range nm.TransportFactoryList { t0 := tf.TransportList[0] t1 := tf.TransportList[1] assert.Equal(t, (uint32)(1), t0.PacketsSent) assert.Equal(t, (uint32)(1), t0.PacketsConfirmed) assert.Equal(t, (uint32)(0), t1.PacketsSent) assert.Equal(t, (uint32)(0), t1.PacketsConfirmed) } }
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) } }
func (self *Transport) sendTransportDatagramTransfer(msg *messages.OutRouteMessage) { //get message and put into the queue to be sent out //prime message for transit between the two transport ends self.PacketsSent++ var m1b messages.TransportDatagramTransfer m1b.Datagram = msg.Datagram m1b.Sequence = self.PacketsSent m1b.RouteId = msg.RouteId b1 := messages.Serialize(messages.MsgTransportDatagramTransfer, m1b) self.PendingOut <- b1 //push to queue, to be transferred }
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) }
func testNetwork(nodes int) { nm := nodemanager.NewNodeManager() nm.CreateNodeList(nodes) nm.Tick() nm.ConnectAll() time.Sleep(1 * time.Second) node0, err := nm.GetNodeById(nm.NodeIdList[0]) if err != nil { panic(err) } initRoute := &node.RouteRule{} for _, initRoute = range node0.RouteForwardingRules { break } inRouteMessage := messages.InRouteMessage{(messages.TransportId)(0), initRoute.IncomingRoute, []byte{'t', 'e', 's', 't'}} serialized := messages.Serialize(messages.MsgInRouteMessage, inRouteMessage) node0.IncomingChannel <- serialized time.Sleep(10 * time.Second) ok := true for _, tf := range nm.TransportFactoryList { t0 := tf.TransportList[0] t1 := tf.TransportList[1] if t0.PacketsSent != (uint32)(1) || t0.PacketsConfirmed != (uint32)(1) || t1.PacketsSent != (uint32)(0) || t1.PacketsConfirmed != (uint32)(0) { ok = false break } } if ok { fmt.Println("PASSED") } else { fmt.Println("FAILED") } }