本文整理匯總了Python中router.Router.add_route方法的典型用法代碼示例。如果您正苦於以下問題:Python Router.add_route方法的具體用法?Python Router.add_route怎麽用?Python Router.add_route使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類router.Router
的用法示例。
在下文中一共展示了Router.add_route方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: PortAgent
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import add_route [as 別名]
class PortAgent(object):
_agent = 'http://localhost:8500/v1/agent/'
def __init__(self, config):
self.config = config
self.data_port = config['port']
self.command_port = config['commandport']
self.sniff_port = config['sniffport']
self.name = config.get('name', str(self.command_port))
self.refdes = config.get('refdes', config['type'])
self.ttl = config['ttl']
self.data_name = 'port-agent'
self.command_name = 'command-port-agent'
self.sniffer_name = 'sniff-port-agent'
self.data_port_id = '%s-%s' % (self.data_name, self.refdes)
self.command_port_id = '%s-%s' % (self.command_name, self.refdes)
self.sniffer_port_id = '%s-%s' % (self.sniffer_name, self.refdes)
self.router = Router()
self.connections = set()
self.clients = set()
self._register_loggers()
self._create_routes()
self._start_servers()
self._heartbeat()
self.num_connections = 0
log.msg('Base PortAgent initialization complete')
def _register_loggers(self):
self.data_logger = DailyLogFile('%s.datalog' % self.name, '.')
self.ascii_logger = DailyLogFile('%s.log' % self.name, '.')
self.router.register(EndpointType.DATALOGGER, self.data_logger)
self.router.register(EndpointType.LOGGER, self.ascii_logger)
def _create_routes(self):
# Register the logger and datalogger to receive all messages
self.router.add_route(PacketType.ALL, EndpointType.LOGGER, data_format=Format.ASCII)
self.router.add_route(PacketType.ALL, EndpointType.DATALOGGER, data_format=Format.PACKET)
# from DRIVER
self.router.add_route(PacketType.FROM_DRIVER, EndpointType.INSTRUMENT, data_format=Format.RAW)
# from INSTRUMENT
self.router.add_route(PacketType.FROM_INSTRUMENT, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PICKLED_FROM_INSTRUMENT, EndpointType.CLIENT, data_format=Format.PACKET)
# from COMMAND SERVER
self.router.add_route(PacketType.PA_COMMAND, EndpointType.COMMAND_HANDLER, data_format=Format.PACKET)
# from PORT_AGENT
self.router.add_route(PacketType.PA_CONFIG, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_CONFIG, EndpointType.COMMAND, data_format=Format.RAW)
self.router.add_route(PacketType.PA_FAULT, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_HEARTBEAT, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_STATUS, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_STATUS, EndpointType.COMMAND, data_format=Format.RAW)
# from COMMAND HANDLER
self.router.add_route(PacketType.DIGI_CMD, EndpointType.DIGI, data_format=Format.RAW)
# from DIGI
self.router.add_route(PacketType.DIGI_RSP, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.DIGI_RSP, EndpointType.COMMAND, data_format=Format.RAW)
@staticmethod
def done(response, caller=''):
log.msg(caller + 'http response: %s' % response.code)
def data_port_cb(self, port):
self.data_port = port.getHost().port
values = {
'ID': self.data_port_id,
'Name': self.data_name,
'Port': self.data_port,
'Check': {'TTL': '%ss' % self.ttl},
'Tags': [self.refdes]
}
put(self._agent + 'service/register', json.dumps(values)).addCallback(self.done, caller='data_port_cb: ')
log.msg('data_port_cb: port is', self.data_port)
def command_port_cb(self, port):
self.command_port = port.getHost().port
values = {
'ID': self.command_port_id,
'Name': self.command_name,
'Port': self.command_port,
'Check': {'TTL': '%ss' % self.ttl},
'Tags': [self.refdes]
}
put(self._agent + 'service/register', json.dumps(values)).addCallback(self.done, caller='command_port_cb: ')
log.msg('command_port_cb: port is', self.command_port)
def sniff_port_cb(self, port):
#.........這裏部分代碼省略.........
示例2: PortAgent
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import add_route [as 別名]
class PortAgent(object):
_agent = 'http://localhost:8500/v1/agent/'
def __init__(self, config):
self.config = config
self.refdes = config.get('refdes', config['type'])
self.ttl = config['ttl']
self.num_connections = 0
self.disconnect_notification_id = None
self.router = Router(self)
self.stats_publisher = StatisticsPublisher()
self.stats_publisher.connect()
self.connections = set()
self.clients = set()
self._register_loggers()
self._create_routes()
self._start_servers()
self.get_consul_host()
heartbeat_task = task.LoopingCall(self._heartbeat)
heartbeat_task.start(interval=HEARTBEAT_INTERVAL)
log.msg('Base PortAgent initialization complete')
def _register_loggers(self):
"""
Create and register the binary and ascii loggers
:return:
"""
if not os.path.exists(CURRENTDIR):
os.makedirs(CURRENTDIR)
binary_name = '%s.datalog' % self.refdes
ascii_name = '%s.log' % self.refdes
self.data_logger = ArchivingDailyLogFile(binary_name, CURRENTDIR, os.path.join(ARCHIVEDIR, self.refdes))
self.ascii_logger = ArchivingDailyLogFile(ascii_name, CURRENTDIR, os.path.join(DISCARDDIR, self.refdes))
self.router.register(EndpointType.DATALOGGER, self.data_logger)
self.router.register(EndpointType.LOGGER, self.ascii_logger)
def _create_routes(self):
"""
Default routes for all port agent types
This provides a mapping from packet type to endpoint type
:return:
"""
# Register the logger and datalogger to receive all messages
self.router.add_route(PacketType.ALL, EndpointType.LOGGER, data_format=Format.ASCII)
self.router.add_route(PacketType.ALL, EndpointType.DATALOGGER, data_format=Format.PACKET)
# from DRIVER
self.router.add_route(PacketType.FROM_DRIVER, EndpointType.INSTRUMENT, data_format=Format.RAW)
# from INSTRUMENT
self.router.add_route(PacketType.FROM_INSTRUMENT, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.FROM_INSTRUMENT, EndpointType.RAW, data_format=Format.RAW)
self.router.add_route(PacketType.PICKLED_FROM_INSTRUMENT, EndpointType.CLIENT, data_format=Format.PACKET)
# from COMMAND SERVER
self.router.add_route(PacketType.PA_COMMAND, EndpointType.COMMAND_HANDLER, data_format=Format.PACKET)
# from PORT_AGENT
self.router.add_route(PacketType.PA_CONFIG, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_CONFIG, EndpointType.COMMAND, data_format=Format.RAW)
self.router.add_route(PacketType.PA_FAULT, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_HEARTBEAT, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_STATUS, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.PA_STATUS, EndpointType.COMMAND, data_format=Format.RAW)
# from COMMAND HANDLER
self.router.add_route(PacketType.DIGI_CMD, EndpointType.DIGI, data_format=Format.RAW)
# from DIGI
self.router.add_route(PacketType.DIGI_RSP, EndpointType.CLIENT, data_format=Format.PACKET)
self.router.add_route(PacketType.DIGI_RSP, EndpointType.COMMAND, data_format=Format.RAW)
def get_service_name_id(self, service):
"""
Given a service name, compute the fully qualified service name and id for this agent
:param service: service name
:return:
"""
base_id = 'port-agent'
if service != 'data':
name = service + '-' + base_id
else:
name = base_id
service_id = name + '-' + self.refdes
return name, service_id
@inlineCallbacks
def got_port_cb(self, service, port_obj):
"""
Called after a port is successfully bound by the server.
Register the service with Consul.
:param service: service to be registered
:param port_obj: port object containing the IP address and port
#.........這裏部分代碼省略.........