carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 1 | import ConfigParser, re |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 2 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 3 | class confNDNHost(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 4 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 5 | def __init__(self, name, app='', params='', cpu=None, cores=None, cache=None): |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 6 | self.name = name |
| 7 | self.app = app |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 8 | self.uri_tuples = params |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 9 | self.cpu = cpu |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 10 | self.cores = cores |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 11 | self.cache = cache |
| 12 | |
| 13 | # For now assume leftovers are NLSR configuration parameters |
| 14 | self.nlsrParameters = params |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 15 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 16 | def __repr__(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 17 | return 'Name: ' + self.name + \ |
| 18 | ' App: ' + self.app + \ |
| 19 | ' URIS: ' + str(self.uri_tuples) + \ |
| 20 | ' CPU: ' + str(self.cpu) + \ |
| 21 | ' Cores: ' + str(self.cores) + \ |
| 22 | ' Cache: ' + str(self.cache) + \ |
| 23 | ' Radius: ' + str(self.radius) + \ |
| 24 | ' Angle: ' + str(self.angle) + \ |
| 25 | ' NLSR Parameters: ' + self.nlsrParameters |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 26 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 27 | class confNDNLink(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 28 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 29 | def __init__(self,h1,h2,linkDict=None): |
| 30 | self.h1 = h1 |
| 31 | self.h2 = h2 |
| 32 | self.linkDict = linkDict |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 33 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 34 | def __repr__(self): |
| 35 | return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 36 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 37 | def parse_hosts(conf_arq): |
| 38 | 'Parse hosts section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 39 | config = ConfigParser.RawConfigParser() |
| 40 | config.read(conf_arq) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 41 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 42 | hosts = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 43 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 44 | items = config.items('nodes') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 45 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 46 | #makes a first-pass read to hosts section to find empty host sections |
| 47 | for item in items: |
| 48 | name = item[0] |
| 49 | rest = item[1].split() |
| 50 | if len(rest) == 0: |
| 51 | config.set('nodes', name, '_') |
| 52 | #updates 'items' list |
| 53 | items = config.items('nodes') |
| 54 | |
| 55 | #makes a second-pass read to hosts section to properly add hosts |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 56 | for item in items: |
| 57 | |
| 58 | name = item[0] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 59 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 60 | rest = item[1].split() |
| 61 | |
| 62 | app = rest.pop(0) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 63 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 64 | uris = rest |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 65 | params = {} |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 66 | cpu = None |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 67 | cores = None |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 68 | cache = None |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 69 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 70 | for uri in uris: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 71 | if re.match("cpu",uri): |
| 72 | cpu = float(uri.split('=')[1]) |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 73 | elif re.match("cores",uri): |
| 74 | cores = uri.split('=')[1] |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 75 | elif re.match("cache",uri): |
| 76 | cache = uri.split('=')[1] |
| 77 | elif re.match("mem",uri): |
| 78 | mem = uri.split('=')[1] |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 79 | else: |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 80 | params[uri.split('=')[0]] = uri.split('=')[1] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 81 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 82 | hosts.append(confNDNHost(name, app, params, cpu, cores, cache)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 83 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 84 | return hosts |
| 85 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 86 | def parse_links(conf_arq): |
| 87 | 'Parse links section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 88 | arq = open(conf_arq,'r') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 89 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 90 | links = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 91 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 92 | while True: |
| 93 | line = arq.readline() |
| 94 | if line == '[links]\n': |
| 95 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 96 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 97 | while True: |
| 98 | line = arq.readline() |
| 99 | if line == '': |
| 100 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 101 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 102 | args = line.split() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 103 | |
| 104 | #checks for non-empty line |
| 105 | if len(args) == 0: |
| 106 | continue |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 107 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 108 | h1, h2 = args.pop(0).split(':') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 109 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 110 | link_dict = {} |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 111 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 112 | for arg in args: |
| 113 | arg_name, arg_value = arg.split('=') |
| 114 | key = arg_name |
| 115 | value = arg_value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 116 | if key in ['bw','jitter','max_queue_size']: |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 117 | value = int(value) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 118 | if key in ['loss']: |
| 119 | value = float(value) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 120 | link_dict[key] = value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 121 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 122 | links.append(confNDNLink(h1,h2,link_dict)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 123 | |
| 124 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 125 | return links |