private Intersection InitialDestination(Intersection source) { // naïve solution: pick first destination at random Intersection east = source.getEast(); Intersection west = source.getWest(); Intersection north = source.getNorth(); Intersection south = source.getSouth(); List <Intersection> options = new List <Intersection>(); if (east != null) { options.Add(east); } if (west != null) { options.Add(west); } if (north != null) { options.Add(north); } if (south != null) { options.Add(south); } /** * Choose a "previous source" retroactively from the first available interface. * This is just to pouplate the previous_queue object in a sane way */ Intersection previous = options[0]; if (previous == east) { previous_queue = source.EQ; } if (previous == west) { previous_queue = source.WQ; } if (previous == north) { previous_queue = source.NQ; } if (previous == south) { previous_queue = source.SQ; } /** * Pick a first destination from the second available interface (there will always be at least 2) * E.g. if the destination is on the north interface, join the southern queue */ Intersection destination = options[1]; if (destination == east) { current_queue = destination.WQ; from = "west"; to = "east"; } if (destination == west) { current_queue = destination.EQ; from = "east"; to = "west"; } if (destination == north) { current_queue = destination.SQ; from = "south"; to = "north"; } if (destination == south) { current_queue = destination.NQ; from = "north"; to = "south"; } current_queue.AddLast(this); // join the current queue poller = source.getPoller(from, to); return(destination); }
private Intersection NextDestination(Intersection origin, Intersection excluding) { Intersection east = origin.getEast(); Intersection west = origin.getWest(); Intersection north = origin.getNorth(); Intersection south = origin.getSouth(); List <Intersection> options = new List <Intersection>(); if (east != null && east != excluding) { options.Add(east); } if (west != null && west != excluding) { options.Add(west); } if (north != null && north != excluding) { options.Add(north); } if (south != null && south != excluding) { options.Add(south); } /** Naïve solution: pick the next destination at random */ Intersection next_hop = options[Deterministic.random.Next(options.Count)]; if (next_hop == origin) { throw new InvalidOperationException("next_hop can't be same as origin"); } if (next_hop == origin.getEast()) { to = "east"; } else if (next_hop == origin.getWest()) { to = "west"; } else if (next_hop == origin.getNorth()) { to = "north"; } else if (next_hop == origin.getSouth()) { to = "south"; } if (source == origin.getEast()) { from = "east"; } else if (source == origin.getWest()) { from = "west"; } else if (source == origin.getNorth()) { from = "north"; } else if (source == origin.getSouth()) { from = "south"; } poller = origin.getPoller(from, to); return(next_hop); }