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 | |
| 3 | class confCCNHost(): |
| 4 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 5 | def __init__(self, name, app='', uri_tuples='', cpu=None): |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 6 | self.name = name |
| 7 | self.app = app |
| 8 | self.uri_tuples = uri_tuples |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 9 | self.cpu = cpu |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 10 | |
| 11 | def __repr__(self): |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 12 | return 'Name: ' + self.name + ' App: ' + self.app + ' URIS: ' + str(self.uri_tuples) + ' CPU:' + str(self.cpu) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 13 | |
| 14 | class confCCNLink(): |
| 15 | |
| 16 | def __init__(self,h1,h2,linkDict=None): |
| 17 | self.h1 = h1 |
| 18 | self.h2 = h2 |
| 19 | self.linkDict = linkDict |
| 20 | |
| 21 | def __repr__(self): |
| 22 | return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict) |
| 23 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 24 | def parse_hosts(conf_arq): |
| 25 | 'Parse hosts section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 26 | config = ConfigParser.RawConfigParser() |
| 27 | config.read(conf_arq) |
| 28 | |
| 29 | hosts = [] |
| 30 | |
| 31 | items = config.items('hosts') |
| 32 | |
| 33 | for item in items: |
| 34 | |
| 35 | name = item[0] |
| 36 | |
| 37 | rest = item[1].split() |
| 38 | |
| 39 | app = rest.pop(0) |
| 40 | |
| 41 | uris = rest |
| 42 | uri_list=[] |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 43 | cpu = None |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 44 | for uri in uris: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 45 | if re.match("cpu",uri): |
| 46 | cpu = float(uri.split('=')[1]) |
| 47 | else: |
| 48 | uri_list.append((uri.split(',')[0],uri.split(',')[1])) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 49 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 50 | hosts.append(confCCNHost(name , app, uri_list,cpu)) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 51 | |
| 52 | return hosts |
| 53 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 54 | def parse_routers(conf_arq): |
| 55 | 'Parse routers section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 56 | config = ConfigParser.RawConfigParser() |
| 57 | config.read(conf_arq) |
| 58 | |
| 59 | routers = [] |
| 60 | |
| 61 | items = config.items('routers') |
| 62 | |
| 63 | for item in items: |
| 64 | name = item[0] |
| 65 | |
| 66 | rest = item[1].split() |
| 67 | |
| 68 | uris = rest |
| 69 | uri_list=[] |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 70 | cpu = None |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 71 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 72 | for uri in uris: |
| 73 | if re.match("cpu",uri): |
| 74 | cpu = float(uri.split('=')[1]) |
| 75 | else: |
| 76 | uri_list.append((uri.split(',')[0],uri.split(',')[1])) |
| 77 | |
| 78 | routers.append(confCCNHost(name=name , uri_tuples=uri_list, cpu=cpu)) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 79 | |
| 80 | return routers |
| 81 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 82 | def parse_links(conf_arq): |
| 83 | 'Parse links section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 84 | arq = open(conf_arq,'r') |
| 85 | |
| 86 | links = [] |
| 87 | |
| 88 | while True: |
| 89 | line = arq.readline() |
| 90 | if line == '[links]\n': |
| 91 | break |
| 92 | |
| 93 | while True: |
| 94 | line = arq.readline() |
| 95 | if line == '': |
| 96 | break |
| 97 | |
| 98 | args = line.split() |
| 99 | h1, h2 = args.pop(0).split(':') |
| 100 | |
| 101 | link_dict = {} |
| 102 | |
| 103 | for arg in args: |
| 104 | arg_name, arg_value = arg.split('=') |
| 105 | key = arg_name |
| 106 | value = arg_value |
| 107 | if key in ['loss','bw','jitter']: |
| 108 | value = int(value) |
| 109 | |
| 110 | link_dict[key] = value |
| 111 | |
| 112 | links.append(confCCNLink(h1,h2,link_dict)) |
| 113 | |
| 114 | |
| 115 | return links |
| 116 | |
| 117 | |
| 118 | |