//process received LSP public void ReceivePacket(LinkStatePacket lsp) { if (_bStatus) //if router is not shut down { foreach (Neighbor _neighbour in _lstNeighbors) { if (_neighbour.Id.Equals(lsp.Sender)) //update for which tick LSP was received { _neighbour.TickForLSP = _nTick; } } lsp.TimeToLive--; if (lsp.TimeToLive <= 0 || lsp.OriginId.Equals(_strId)) //LSP is lost or source and destination are same { return; } if (_dicProcessedLSPs.ContainsKey(lsp.OriginId)) { int seq = _dicProcessedLSPs[lsp.OriginId]; if (seq >= lsp.Sequence) //already processed LSP for source { return; } } _dicProcessedLSPs[lsp.OriginId] = lsp.Sequence; //add/update to LSP list _hsEdges.AddRange(lsp.links); //update set of edges with latest info ComputeShortestPaths(); //run djikstra algo to compute routing table SendPacket(lsp); //send to downstream neighbors } }
private void SendPacket(LinkStatePacket lsp) { string _srSender = lsp.Sender; lsp.Sender = _strId; //update sender ID to this router foreach (Neighbor _neighbour in _lstNeighbors) { if (!_neighbour.Cost.Equals(LSPConstants.Infinity)) { //all connected routers for finite cost if (!_neighbour.Id.Equals(_srSender)) //except the sending one { Program._dicRouters[_neighbour.Id].ReceivePacket(lsp); } } } }
/// <summary> /// create Link State Packet from router /// </summary> public void OriginatePacket() { if (_bStatus) //if router is not shut down { _nTick++; _nLspSeq++; LinkStatePacket _lsp = new LinkStatePacket(_strId, _nLspSeq); //generate LSP foreach (Neighbor _neighbour in _lstNeighbors) { if (_nTick > _neighbour.TickForLSP + 2) //if LSP is heard in two clock ticks then cost is infinity assuming that neighbour is shut down. Remove the edge from graph { _neighbour.Cost = LSPConstants.Infinity; _hsEdges.Remove(new Edge(_strId, _neighbour.Id, 0, "")); } else //add LSP to this network details { Edge l = new Edge(_strId, _neighbour.Id, _neighbour.Cost, Program._dicRouters[_neighbour.Id]._strNetworkName); _lsp.addLink(l); } } ComputeShortestPaths(); //run djikstra algo to compute routing table SendPacket(_lsp); //send to all neighbors } }