本文整理匯總了Python中router.Router.got_data方法的典型用法代碼示例。如果您正苦於以下問題:Python Router.got_data方法的具體用法?Python Router.got_data怎麽用?Python Router.got_data使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類router.Router
的用法示例。
在下文中一共展示了Router.got_data方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: PortAgent
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import got_data [as 別名]
#.........這裏部分代碼省略.........
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):
self.sniff_port = port.getHost().port
values = {
'ID': self.sniffer_port_id,
'Name': self.sniffer_name,
'Port': self.sniff_port,
'Check': {'TTL': '%ss' % self.ttl},
'Tags': [self.refdes]
}
put(self._agent + 'service/register', json.dumps(values)).addCallback(self.done, caller='sniff_port_cb: ')
log.msg('sniff_port_cb: port is', self.sniff_port)
def _start_servers(self):
self.data_endpoint = TCP4ServerEndpoint(reactor, self.data_port)
data_deferred = self.data_endpoint.listen(DataFactory(self, PacketType.FROM_DRIVER, EndpointType.CLIENT))
data_deferred.addCallback(self.data_port_cb)
self.command_endpoint = TCP4ServerEndpoint(reactor, self.command_port)
command_deferred = self.command_endpoint.listen(
CommandFactory(self, PacketType.PA_COMMAND, EndpointType.COMMAND))
command_deferred.addCallback(self.command_port_cb)
self.sniff_port = int(self.sniff_port)
self.sniff_endpoint = TCP4ServerEndpoint(reactor, self.sniff_port)
sniff_deferred = self.sniff_endpoint.listen(DataFactory(self, PacketType.UNKNOWN, EndpointType.LOGGER))
sniff_deferred.addCallback(self.sniff_port_cb)
def _heartbeat(self):
packets = Packet.create('HB', PacketType.PA_HEARTBEAT)
self.router.got_data(packets)
# Set TTL Check Status
check_string = self._agent + 'check/pass/service:'
get(check_string + self.data_port_id).addCallback(
self.done, caller='%s TTL check status: ' % self.data_port_id)
get(check_string + self.command_port_id).addCallback(
self.done, caller='%s TTL check status: ' % self.command_port_id)
get(check_string + self.sniffer_port_id).addCallback(
self.done, caller='%s TTL check status: ' % self.sniffer_port_id)
reactor.callLater(HEARTBEAT_INTERVAL, self._heartbeat)
def client_connected(self, connection):
log.msg('CLIENT CONNECTED FROM ', connection)
self.clients.add(connection)
def client_disconnected(self, connection):
self.clients.remove(connection)
log.msg('CLIENT DISCONNECTED FROM ', connection)
def instrument_connected(self, connection):
log.msg('CONNECTED TO ', connection)
self.connections.add(connection)
if len(self.connections) == self.num_connections:
self.router.got_data(Packet.create('CONNECTED', PacketType.PA_STATUS))
def instrument_disconnected(self, connection):
self.connections.remove(connection)
log.msg('DISCONNECTED FROM ', connection)
self.router.got_data(Packet.create('DISCONNECTED', PacketType.PA_STATUS))
def register_commands(self, command_protocol):
log.msg('PortAgent register commands for protocol: %s' % command_protocol)
command_protocol.register_command('get_state', self.get_state)
command_protocol.register_command('get_config', self.get_config)
command_protocol.register_command('get_version', self.get_version)
def get_state(self, *args):
log.msg('get_state: %r %d' % (self.connections, self.num_connections))
if len(self.connections) == self.num_connections:
return Packet.create('CONNECTED', PacketType.PA_STATUS)
return Packet.create('DISCONNECTED', PacketType.PA_STATUS)
def get_config(self, *args):
return Packet.create(json.dumps(self.config), PacketType.PA_CONFIG)
def get_version(self, *args):
return Packet.create(ooi_port_agent.__version__, PacketType.PA_CONFIG)
示例2: PortAgent
# 需要導入模塊: from router import Router [as 別名]
# 或者: from router.Router import got_data [as 別名]
#.........這裏部分代碼省略.........
if response.code != httplib.OK:
log.msg(service + 'http response: %s' % response.code)
def _start_servers(self):
"""
Start the base port agent servers:
DATA
COMMAND
SNIFF
:return:
"""
self.data_endpoint = TCP4ServerEndpoint(reactor, 0)
self.data_endpoint.listen(
DataFactory(self, PacketType.FROM_DRIVER, EndpointType.CLIENT)
).addCallback(partial(self.got_port_cb, 'data'))
self.command_endpoint = TCP4ServerEndpoint(reactor, 0)
self.command_endpoint.listen(
CommandFactory(self, PacketType.PA_COMMAND, EndpointType.COMMAND)
).addCallback(partial(self.got_port_cb, 'command'))
self.sniff_endpoint = TCP4ServerEndpoint(reactor, 0)
self.sniff_endpoint.listen(
DataFactory(self, PacketType.UNKNOWN, EndpointType.LOGGER)
).addCallback(partial(self.got_port_cb, 'sniff'))
@inlineCallbacks
def _heartbeat(self):
"""
Send a heartbeat packet and set our Consul status to PASS
:return:
"""
packets = Packet.create('HB', PacketType.PA_HEARTBEAT)
self.router.got_data(packets)
# Set TTL Check Status
check_string = self._agent + 'check/pass/service:'
for service in self.config['ports']:
name, service_id = self.get_service_name_id(service)
yield get(check_string + service_id)
@inlineCallbacks
def get_consul_host(self):
"""
Fetch our host information from Consul
:return:
"""
url = self._agent + 'self'
try:
response = yield get(url)
body = yield readBody(response)
data = json.loads(body)
host = data['Member']['Addr']
self.config['host'] = host
except (ConnectionRefusedError, KeyError) as e:
log.msg('Unable to retrieve host address from consul: ', e)
reactor.callLater(CONSUL_RETRY_INTERVAL, self.get_consul_host)
def client_connected(self, connection):
"""
Log an incoming client connection
:param connection: connection object
:return:
"""
log.msg('CLIENT CONNECTED FROM ', connection)