blob: 07eb8c9b9c60d8132645bf29b31af5aa705fd934 [file] [log] [blame]
import ConfigParser, re
class confCCNHost():
def __init__(self, name, app='', uri_tuples='', cpu=None, cores=None, cache=None):
self.name = name
self.app = app
self.uri_tuples = uri_tuples
self.cpu = cpu
self.cores = cores
self.cache = cache
def __repr__(self):
return 'Name: ' + self.name + ' App: ' + self.app + ' URIS: ' + str(self.uri_tuples) + ' CPU:' + str(self.cpu) + ' Cores:' +str(self.cores) + ' Cache: ' + str(self.cache)
class confCCNLink():
def __init__(self,h1,h2,linkDict=None):
self.h1 = h1
self.h2 = h2
self.linkDict = linkDict
def __repr__(self):
return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict)
def parse_hosts(conf_arq):
'Parse hosts section from the conf file.'
config = ConfigParser.RawConfigParser()
config.read(conf_arq)
hosts = []
items = config.items('nodes')
#makes a first-pass read to nodes section to find empty nodes sections
for item in items:
name = item[0]
rest = item[1].split()
if len(rest) == 0:
config.set('nodes', name, '_')
#updates 'items' list
items = config.items('nodes')
#makes a second-pass read to nodes section to properly add hosts
for item in items:
name = item[0]
rest = item[1].split()
app = rest.pop(0)
uris = rest
uri_list=[]
cpu = None
cores = None
cache = None
for uri in uris:
if re.match("cpu",uri):
cpu = float(uri.split('=')[1])
elif re.match("cores",uri):
cores = uri.split('=')[1]
elif re.match("cache",uri):
cache = uri.split('=')[1]
elif re.match("mem",uri):
mem = uri.split('=')[1]
else:
uri_list.append((uri.split(',')[0],uri.split(',')[1]))
hosts.append(confCCNHost(name , app, uri_list,cpu,cores,cache))
return hosts
def parse_routers(conf_arq):
'Parse routers section from the conf file.'
config = ConfigParser.RawConfigParser()
config.read(conf_arq)
routers = []
items = config.items('routers')
for item in items:
name = item[0]
rest = item[1].split()
uris = rest
uri_list=[]
cpu = None
cores = None
for uri in uris:
if re.match("cpu",uri):
cpu = float(uri.split('=')[1])
elif re.match("cores",uri):
cores = uri.split('=')[1]
else:
uri_list.append((uri.split(',')[0],uri.split(',')[1]))
routers.append(confCCNHost(name=name , uri_tuples=uri_list, cpu=cpu, cores=cores))
return routers
def parse_links(conf_arq):
'Parse links section from the conf file.'
arq = open(conf_arq,'r')
links = []
while True:
line = arq.readline()
if line == '[links]\n':
break
while True:
line = arq.readline()
if line == '':
break
args = line.split()
#checks for non-empty line
if len(args) == 0:
continue
h1, h2 = args.pop(0).split(':')
link_dict = {}
for arg in args:
arg_name, arg_value = arg.split('=')
key = arg_name
value = arg_value
if key in ['bw','jitter','max_queue_size']:
value = int(value)
if key in ['loss']:
value = float(value)
link_dict[key] = value
links.append(confCCNLink(h1,h2,link_dict))
return links