from device import Device
from packet import RoutingPacket
from event import Event
from blackwidow.network.rate_graph import Rate_Graph

ROUTING_PKT_ID = 'Routing Packet'


class Router(Device):
    """Class for routers.

    Routers are responsible for initializing and updating their
    routing table, and sending packets based on their routing table.

    Parameters
    ----------
    router_id : string
        A unique id for the router.


    Attributes
    ----------
    network_id : string
        A unique id of the device in the network.
    links : list
        A list of links that the router is connected to.
    routing_table : dict
        A dictionary representing the router's routing table.
    new_routing_table : dict
        A dictionary representing the router's new routing table.
    env : `Network`
        The network that the link belongs to.
    bw : `Blackwidow`
        BlackWidow simulation object containing simulation settings.
    send_rate : Rate_Graph object
        Send rate graphing object.
    receive_rate : Rate_Graph object
        Receive rate graphing object.

    Methods
    -------
    add_link(link)
        Adds a link to the router.
    send(packet)
        Sends a packet to a link.
    receive(packet)
        Receives a packet from a link.
    start_new_routing()
        Starts a new routing round.
    send_routing()
        Sends a routing packet to all neighbors.
    update_route()
        Update the new_routing_table based on routing packets.
    _distance(link)
        Gets the distance of a link.
    """
    def __init__(self, router_id, env, bw):
        """Constructor for Router class."""
        super(Router, self).__init__(router_id)
        self.env = env