Computer Network 5 | Distance Vector Implementation
1. Project Documentation
The current project is similar to the last one we ran.
run_topo.py
The simple driver that triggers the simulation and write the logs.
open_log: function defined inhelperspackage used to load a file for logging purposeTopology: define a class ofTopologywhich contains a collection ofNodestopo.run_topo(): triggers the simulationfinish_log: function defined inhelpersto end the logging
SimpleTopo.txt, SimpleNegativeCycleTopo.txt, BadTopo.txt, SingleLoopTopo.txt, ComplexTopo.txt
The topology files containing the map information. Note that BadTopo.txt is a topology that defined with node A link to an nonexistent node z. It should give an error message.
Neighbor
A simple class defined in Node.py with a neighbor node and a path cost.
name: neighbor node nameweight: the path cost
Node
Node is a class defined in Node.py used to store the node information.
name: node/router nameincoming_links: list ofNeighborobjects of incoming node name and costoutgoing_links: list ofNeighborobjects of outgoing node name and costneighbor_names: list of incoming link node namestopology: a backlink to theTopologyobject and it should not be used in the implementationmessages: list of messages defined by userA.send_msg(msg, B): send the messagemsgto the nodeBfromAB.queue_msg(msg): called when other nodes sends messagemsgtoB. It contains a simple attend function that will add the message to a list.
Note that DistanceVector class is based on this class.
Topology
A class defined by Topology.py used to store the topology map.
topodict: a dictionary with each node as the field mapping to itsincoming_linksandoutgoing_links. It containsDistanceVectorobjects.nodes: a list version oftopodict
helpers.py
A helper package that contains several useful functions.
open_log(filename): function called inrun_topo.pyto open a log filefilename. Variableslogfileandcurrent_logsare set to zeros. It also initialize thecurrent_logsas an empty dictionary.finish_log(): close the globallogfilevariableadd_entry(switch, logstring): addswitch: logstringentry tocurrent_logsfinish_round(): This is the function called inTopology.pyand it is used to write all the entries in thecurrent_logsto thelogfilewith a alphabet order of field names. It will seperate each round with string"-----\n"and clear thecurrent_logs
2. Assistant File
Makefile
This file can be used to simplify the testing process.
#Makefile
clean:
	rm -rf ./*.log
run: clean
	./run.sh SimpleTopo 2>/dev/null | grep -i 'invalid\|complete'
	./run.sh SimpleNegativeCycleTopo 2>/dev/null | grep -i 'invalid\|complete'
	./run.sh BadTopo 2>/dev/null | grep -i 'invalid\|complete'
	./run.sh SingleLoopTopo 2>/dev/null | grep -i 'invalid\|complete'
	./run.sh ComplexTopo 2>/dev/null | grep -i 'invalid\|complete'
	rm -rf __pycache__
3. Message Class
We should define a Message class or other data types but it should have the following information.
name: source node to forward fromdistVector: the distance vector of the source node
4. DistanceVector Class
DistanceVector is a given class based on class Node and it should add a mapping variable called distVector which keeps track of the distance vector information. it should be initialized with itself as the field and 0 cost as a value.
5. send_initial_messages Function
send_initial_messages is relatively simple. It is used to send the initial message to all the incoming links (say, neighboring routers) at the beginning. Node.send_msg(Message, str) function should be called and the implementation of this message queue is a FIFO data structure.
Note that when passing the distVector in Message, make sure it is copyed with its current value so a future change won’t effect its value.
6. log_distances Function
log_distances is used to output the current distVector in a easy-reading way. For example, it should have the pattern like A:A0,B1,C2 based on {A: 0, B: 1, C: 2} and its implementation should be very easy.
7. process_BF Function
process_BF is actually a impletation of Bellman-Ford algorithm and it must accomplish the two tasks,
Process queued messages
Send neighbors sendMessage distance
The second task is like send_initial_messages with the new message so let’s consider the first task. This is a pseudocode for implementing this task,
Loop through all messages:
    Loop through all nodes in message.distVector:
    
        Skip current node
        
        Calculate the cost from x -> s
        - Cost(x, v)
        - Dv(s)
        - dist* = Cost(x, v) + Dv(s)
        
        1 For new nodes in distVector
        1-1 if node is a neighbor
            update with Cost(x, s)
        1-2 if node is not a neighbor
            update with dist*
        
        2 For existing nodes in distVector
        2-1 if Cost(x, v) or Dv(s) is smaller than -99, then dist* will be -99 no matter the other value
        2-2 if Cost(x, v) + Dv(s) < -99, then dist* will be -99
        2-3 if Cost(x, v) + Dv(s) > -99 but smaller than Dv(s), should also update