本文整理汇总了Python中interface.Interface.transmit方法的典型用法代码示例。如果您正苦于以下问题:Python Interface.transmit方法的具体用法?Python Interface.transmit怎么用?Python Interface.transmit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类interface.Interface
的用法示例。
在下文中一共展示了Interface.transmit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Router
# 需要导入模块: from interface import Interface [as 别名]
# 或者: from interface.Interface import transmit [as 别名]
class Router(object):
def __init__(self, hostname=None, demand=None):
if hostname is not None:
self._hostname = hostname
self._demand = demand
self._lsdb = ospf.Database()
self._links = {}
self._neighbors = {}
self._seen = {}
self._energy_flow = {}
self._init_timers()
self._interface = Interface(self)
def __del__(self):
self.stop()
def init_router(self, name, demand=0):
if not name.endswith(".cfg"):
self.__init__(name, demand)
return
cfg = ConfigParser.SafeConfigParser()
try:
cfg.read(str(name))
except ConfigParser.MissingSectionHeaderError:
print('MissingSectionHeaderError')
sys.exit(-1)
hostname = cfg.get('Router', 'hostname')
demand = int(cfg.get('Router', 'demand'))
self.__init__(hostname, demand)
links = [i for i in cfg.sections() if i.startswith('Link')]
for link in links:
link_id = cfg.get(link, 'link')
cost = int(cfg.get(link, 'cost'))
capacity = int(cfg.get(link, 'capacity'))
self.add_link(link_id, cost, capacity)
def _init_timers(self):
log('Init timers.')
self._dead_timer = None
self._timers = {'lsdb': Timer(ospf.AGE_INTERVAL, self._update_lsdb),
'refresh_lsa': Timer(ospf.LS_REFRESH_TIME, self._refresh_lsa),
'hello': Timer(ospf.HELLO_INTERVAL, self._hello)}
def _update_lsdb(self):
log('LSDB update.')
flushed = self._lsdb.update()
if flushed:
log('LSA(s) of %s reached MaxAge and was/were flushed from the LSDB' % (', '.join(flushed),))
def _refresh_lsa(self):
if self._hostname in self._lsdb:
log('Refreshing own LSA')
self._advertise()
def _hello(self):
"""Establish adjacency"""
log('Sending HelloPacket.')
for link, attr in self._links.iteritems():
packet = ospf.HelloPacket(self._hostname, (link, attr[0], attr[1]))
self.transmit(packet, link)
for neighbor_id in self._seen:
if neighbor_id not in self._neighbors:
self._sync_lsdb(neighbor_id)
def _update_energy_flow(self):
log('Recalculating flow.')
flow_cost, flow_dict = FlowManager.calculate_flow(self._lsdb)
self._energy_flow = flow_dict
log(flow_dict[self._hostname])
def get_energy_flow(self):
return self._energy_flow
def get_lsdb(self):
return self._lsdb
def _break_adjacency(self, neighbor_id):
log('Break adjacency.')
# Save reference QObject errors
self._dead_timer = self._timers[neighbor_id]
del self._timers[neighbor_id]
del self._neighbors[neighbor_id]
del self._seen[neighbor_id]
log(' '.join([neighbor_id, 'is down']))
self._advertise()
def _flood(self, packet, source_link=None):
"""Flood received packet to other interfaces"""
if packet.adv_router == self._hostname:
log('Flooding own LSA')
else:
log('Flooding LSA of %s' % (packet.adv_router,))
for data in self._neighbors.values():
if data[0] != source_link:
self.transmit(packet, data[0])
#.........这里部分代码省略.........