Network.py 2.41 KB
Newer Older
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
1
from SystemEntity import SystemEntity
2 3 4
from Link import Link
import csv
from Node import Node
5
from OverlayNode import OverlayNode
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
6
import logging
7 8
import random
from numpy.random import randint
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
9
logger = logging.getLogger(__name__)
10
import numpy as np
11

12 13


14
class Network():
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
15
    def __init__(self):
16 17
        self.nodes = {}#{1 : Node(1),2:Node(2)}
        self.links = {} #{(1,2):Link(1-2),(2,1):Link(2,1),(3,2):Link(3*2)}      linkID = tuple(1,2)
18
        self.networkClockTime = 0
19
        self.pk_weight_map = {}
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
20 21

    def setupNetwork(self):
22 23 24 25 26 27 28 29 30 31 32
        '''
        Assumption : nodes are unique
                    links are unique and bidirectional
                    no errorneous nodes present in the file
                    no erroneous link present in the file
                    self loops are not there

        Adds links and nodes to the network .It is a one time activity.

        :return:
        '''
33 34 35
        w = randint(1, 51, 4) # 51 is excluded - range (1,50)
        self.badaW = np.sum(w)
        i = 0
36 37 38
        with open('Nodes.dat', 'r') as f:
            reader = csv.reader(f)
            for row in reader:
39 40 41 42
                node = row[0] # NOde id
                xnode = {node:OverlayNode(node,self,w[i])}
                self.pk_weight_map.update({xnode[node].pk:w[i]})
                self.nodes.update(xnode)#added node to network
43 44 45 46 47 48 49
                # print(self.nodes.get('1')) #keys are strings


        with open('Links.dat', 'r') as f:
            reader = csv.reader(f)
            for row in reader:
                link = tuple(row[0].split()) # tuple of a link
50 51
                linkDelay = 1
                self.links.update({link:Link(link,self,linkDelay)})
52 53 54 55 56 57 58

                # adding adjacent nodes list to each node
                self.nodes.get(link[0]).adjacentNodes.append(link[1])
                self.nodes.get(link[1]).adjacentNodes.append(link[0])
                # print(self.links.get(('1', '2'))) #keys are strings


THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
59 60
        pass

61

THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
62
    def simulate(self,time):
63
        # logger.info("simulating .. ")
64
        self.networkClockTime = time
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
65 66
        # Note links should be simulated before nodes
        #  in order to receive the messge which is supposed to be received in this tick
67
        for link in self.links.values():
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
68 69
            link.simulate(time)

70
        for node in self.nodes.values():
THAKARE AKSHAY HARIBHAU's avatar
THAKARE AKSHAY HARIBHAU committed
71 72 73
            node.simulate(time)

        pass
74 75 76 77 78 79

if __name__ == '__main__':
    network = Network()
    network.setupNetwork()

    pass