/// <summary> /// Adds a new neighbour to our network state. /// </summary> /// <param name="id">ID of the neighbouring process.</param> public void AddNeighbour(int remoteId, ConnectionWorker worker) { lock (neighbours) { neighbours[remoteId] = worker; } lock (Nb) { Nb[remoteId] = remoteId; } lock (D) { D[remoteId] = int.MaxValue; } lock (nD) { nD[Tuple.Create(remoteId, remoteId)] = 0; } Recompute(remoteId); UpdateAllRoutesToAllNeighbours(); }
/// <summary> /// Sends an update of all routes to a neighbour. /// </summary> /// <param name="neighbour"></param> private void UpdateAllRoutesToNeighbour(int neighbour) { ConnectionWorker worker = neighbours[neighbour]; lock (Nb) { foreach (KeyValuePair <int, int> route in Nb) { worker.sendMessage(string.Format("U {0} {1}", route.Key, D[route.Key])); } } }
/// <summary> /// Thread that listens for incomming connections. /// </summary> private void listenerThread() { listener.ExclusiveAddressUse = true; listener.Start(); while (true) { try { TcpClient client = listener.AcceptTcpClient(); ConnectionWorker worker = new ConnectionWorker(id, client, this); worker.Start(); } catch (Exception e) { Console.WriteLine("// {0}", e.Message); }; } }
/// <summary> /// Connects to the given port on the localhost. /// </summary> /// <param name="remoteId"></param> public void ConnectToPort(int remoteId) { TcpClient client = new TcpClient(); client.ExclusiveAddressUse = true; for (int tries = 0; tries < 20 && !client.Connected; tries++) { client.Connect("localhost", remoteId); if (!client.Connected) { Thread.Sleep(50); } } ConnectionWorker worker = new ConnectionWorker(id, client, this); worker.Start(); }