def __init__(self, endpoint): if endpoint: logger.debug("开始运行 REQ 服务器,连接地址为:{}...".format(endpoint)) self._req = ZmqREQConnection(ZmqFactory(), ZmqEndpoint('connect', endpoint)) else: self._req = None self.timeout = 0.95
def add_req(self, endpoint): ''' 设置 req 对象 :param endpoint: :return: ''' if endpoint: logger.debug("开始运行 REQ 服务器,连接地址为:{}...".format(endpoint)) self.req_ = ZmqREQConnection(ZmqFactory(), ZmqEndpoint('connect', endpoint))
def dealer_proxy_async(address, timeout=1): """ :param address: :returns: DealerProxyAsync """ socket = ZmqREQConnection(ZmqFactory(), ZmqEndpoint("connect", address)) socket.defaultRequestTimeout = timeout return DealerProxyAsync(socket)
def __init__(self): color_init() zf = ZmqFactory() e = ZmqEndpoint(ZmqEndpointType.connect, ENDPOINT) self._conn = ZmqREQConnection(zf, e) self.data = [] if self.service: self.data = [self.service]
def __init__(self, remote_host, port, zmq_factory, data_manager=None): self.data_manager = data_manager self.remote_host = remote_host endpoint = ZmqEndpoint(ZmqEndpointType.connect, "tcp://%s:%d" % (remote_host, port)) ZmqREQConnection.__init__(self, zmq_factory, endpoint)
def __init__(self, config, db_engine, cache): self.config = config self.db_engine = db_engine self.cache = cache self.running = False self.ping_errs = 0 self.syncer = ZmqREQConnection( ZmqFactory(), ZmqEndpoint('connect', self.config.ha.slave)) self.repops = {0: self.do_succ, 1: self.do_fail} reactor.callLater(1, self.ping) reactor.callLater(3, self.process)
def __init__(self, config, log=None): self.config = config self.log = log self.protocol = 'http' self.cache = mcache.Mcache() self.zauth_conn = ZmqREQConnection(ZmqFactory()) self.zacct_conn = ZmqREQConnection(ZmqFactory()) self.init_client().addCallback(self.log.info) reactor.callLater( 1.0, self.check_api, )
def __init__(self, cfg, print_json=False): self.cfg = cfg color_init() zf = ZmqFactory() e = ZmqEndpoint(ZmqEndpointType.connect, ENDPOINT) self._conn = ZmqREQConnection(zf, e) self.data = [] if self.service: self.data = [self.service] self.print_json = print_json
class reqModel(): ''' ''' def __init__(self, endpoint): if endpoint: logger.debug("开始运行 REQ 服务器,连接地址为:{}...".format(endpoint)) self._req = ZmqREQConnection(ZmqFactory(), ZmqEndpoint('connect', endpoint)) else: self._req = None self.timeout = 0.95 def sendReq(self, data): ''' 发送 req 信息 :param data: :param callBack: :param errBack: :return: 返回defer对象 ''' if self._req: d = self._req.sendMsg(data, timeout=self.timeout) return d else: logger.error("请创建请求者实例") return None
class ZMQfactory(): def __init__(self): self.pub_ = None self.sub_ = None self.req_ = None self.rep_ = None self.push_ = None self.pull_ = None self.timeout = 0.95 def set_timeout(self, timeout): self.timeout = timeout # pub sub 相关 def add_pub(self, endpoint): if endpoint: logger.debug("开始运行 PUB 服务器,服务地址为:{}...".format(endpoint)) self.pub_ = ZmqPubConnection(ZmqFactory(), ZmqEndpoint("bind", endpoint)) def add_sub(self, endpoint): if endpoint: logger.debug("开始运行 SUB 客户端 , 连接的服务器地址为:{}...".format(endpoint)) self.sub_ = ZmqSubConnection(ZmqFactory(), ZmqEndpoint("connect", endpoint)) self.set_callback() def set_filter(self, filter_=None): ''' 请输入需要接受的主题(必须调用该函数,否则无法收到任何数据) :param filter_: type str_list :return: ''' if self.sub_: if filter_: for item in filter_: self.sub_.subscribe(item.encode()) else: self.sub_.subscribe(b'') else: logger.error("请初始化SUB端口") def set_callback(self): if self.sub_: self.sub_.gotMessage = self.subscribeReceived else: logger.error("请初始化SUB端口") def subscribeReceived(self, *args): ''' 当接收到广播数据时,会触发该函数 :param data: :return: ''' def publishData(self, tag, data): ''' 从push服务器向外推送数据 :param tag: :param data: :return: ''' # req-rep 相关 def add_req(self, endpoint): ''' 设置 req 对象 :param endpoint: :return: ''' if endpoint: logger.debug("开始运行 REQ 服务器,连接地址为:{}...".format(endpoint)) self.req_ = ZmqREQConnection(ZmqFactory(), ZmqEndpoint('connect', endpoint)) def add_rep(self, endpoint, callBack=onPrint): ''' 设置 rep 对象 :param endpoint: :param callBack: :return: ''' if endpoint: logger.debug("开始运行 REP 服务器,监听地址为:{}...".format(endpoint)) self.rep_ = ZmqREPConnection(ZmqFactory(), ZmqEndpoint('bind', endpoint)) self.rep_.gotMessage = callBack def sendReq(self, data, callBack=onPrint, errBack=onTimeout): ''' 发送 req 信息 :param data: :param callBack: :param errBack: :return: ''' try: d = self.req_.sendMsg(data, timeout=self.timeout) d.addCallback(callBack).addErrback(errBack) except Exception as e: logger.error(e) # push pull 相关 def add_push(self, endpoint): ''' :param endpoint: :return: ''' if endpoint: logger.debug("开始运行 PUSH 服务器,监听地址为:{}...".format(endpoint)) self.push_ = ZmqPushConnection(ZmqFactory(), ZmqEndpoint('connect', endpoint)) def add_pull(self, endpoint, callBack=onPrint): ''' :param endpoint: :param callBack: :return: ''' if endpoint: logger.debug("开始运行 PULL 服务器,连接地址为:{}...".format(endpoint)) self.pull_ = ZmqPullConnection(ZmqFactory(), ZmqEndpoint('bind', endpoint)) self.pull_.onPull = callBack def pushData(self, data): ''' :param data: :return: ''' try: self.push_.push(data) except zmq.error.Again: logger.error("Skipping, no pull consumers...")
import os import sys from twisted.internet import reactor rootdir = os.path.realpath(os.path.join(os.path.dirname(sys.argv[0]), '..')) sys.path.insert(0, rootdir) os.chdir(rootdir) from txzmq import ZmqEndpoint, ZmqFactory, ZmqREQConnection, ZmqREPConnection zf = ZmqFactory() endpoint = "ipc:///tmp/txzmq-test" req = ZmqREQConnection(zf, ZmqEndpoint("connect", endpoint)) rep = ZmqREPConnection(zf, ZmqEndpoint("bind", endpoint)) def gotMessage(messageId, message): rep.reply(messageId, b"REP: " + message) rep.gotMessage = gotMessage exitCode = 0 def start(): def gotReply(reply): if reply != [b"REP: REQ1"]: print("Unexpected reply: %r" % (reply, ))
def get_zmq_connection(): zf = ZmqFactory() e = ZmqEndpoint(ZmqEndpointType.connect, ENDPOINT) return ZmqREQConnection(zf, e)
def __init__(self, app, ip_camera, role, ip_controller): if role == gs.CONTROLLER: self.images_folder = gs.CTRL_IMAGES_FOLDER self.crops_folder = gs.CTRL_CROPS_FOLDER self.flightdata_folder = gs.CTRL_FLIGHTDATA_FOLDER self.thumbs_folder = None else: self.images_folder = gs.IMAGES_FOLDER self.crops_folder = gs.CROPS_FOLDER self.flightdata_folder = gs.FLIGHTDATA_FOLDER self.thumbs_folder = gs.THUMBNAILS_FOLDER if not os.path.exists(self.images_folder): os.makedirs(self.images_folder) if not os.path.exists(self.crops_folder): os.makedirs(self.crops_folder) if not os.path.exists(self.flightdata_folder): os.makedirs(self.flightdata_folder) if self.thumbs_folder is not None and not os.path.exists( self.thumbs_folder): os.makedirs(self.thumbs_folder) self.role = role # # Setup the zmq socket used for receiving images. # self.zmq_factory = ZmqFactory() if self.role == gs.CONTROLLER: # # Socket for pulling notices about images from the camera. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_camera, zmq_port=gs.ZMQ_CAMERA_NOTICE_PORT)) self.pull_socket = ZmqPullConnection(self.zmq_factory, endpoint) self.pull_socket.onPull = self.handlePullMessage # # Socket for requesting images from the camera. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_camera, zmq_port=gs.ZMQ_CAMERA_FILES_PORT)) self.req_socket = ZmqREQConnection(self.zmq_factory, endpoint) # # Socket for publishing images to subscribers. # endpoint = ZmqEndpoint( 'bind', 'tcp://0.0.0.0:{zmq_port}'.format( zmq_port=gs.ZMQ_PRIMARY_GS_PORT)) self.pub_socket = MyZmqPubConnection(self.zmq_factory, endpoint) # # Socket for responding with queued images or sync data. # endpoint = ZmqEndpoint( 'bind', 'tcp://*:{zmq_port}'.format( zmq_port=gs.ZMQ_PRIMARY_GS_CONTROLLER_PORT)) self.sync_rep_socket = ControllerSyncConnection( self.zmq_factory, endpoint, self) else: # # Socket for subscribing to images. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_controller, zmq_port=gs.ZMQ_PRIMARY_GS_PORT)) self.sub_socket = MyZmqSubConnection(self.zmq_factory, endpoint) self.sub_socket.subscribe(gs.SUB_TAG) self.sub_socket.gotMessage = self.handleNewMessage # # Socket for requesting queued images or sync data from the controller. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_controller, zmq_port=gs.ZMQ_PRIMARY_GS_CONTROLLER_PORT)) self.sync_req_socket = ZmqREQConnection(self.zmq_factory, endpoint) log.msg('Sending sync request') self.requestSyncImageList() self.requestSyncCropList() self.app = app
def __init__(self, *args, **kwargs): ZmqREQConnection.__init__(self, *args, **kwargs)
def __init__(self, config, service = 'auth'): self.config = config self.service = service self.zmqreq = ZmqREQConnection(ZmqFactory(), ZmqEndpoint('bind', config.mqproxy[service + '_bind'])) logger.info('%s master zmqreq bind @ ' % self.zmqreq)
parser = OptionParser("") parser.add_option("-m", "--method", dest="method", help="0MQ socket connection: bind|connect") parser.add_option("-e", "--endpoint", dest="endpoint", help="0MQ Endpoint") parser.add_option("-M", "--mode", dest="mode", help="Mode: req|rep") parser.set_defaults(method="connect", endpoint="ipc:///tmp/txzmq-pc-demo") (options, args) = parser.parse_args() zf = ZmqFactory() e = ZmqEndpoint(options.method, options.endpoint) if options.mode == "req": s = ZmqREQConnection(zf, e) def produce(): # data = [str(time.time()), socket.gethostname()] data = str(time.time()) print "Requesting %r" % data try: d = s.sendMsg(data) def doPrint(reply): print("Got reply: %s" % (reply)) d.addCallback(doPrint) except zmq.error.Again:
class HaSyncTask(object): """ 同步任务,定时查询需要同步的数据并向同步服务器发送 """ def __init__(self, config, db_engine, cache): self.config = config self.db_engine = db_engine self.cache = cache self.running = False self.ping_errs = 0 self.syncer = ZmqREQConnection( ZmqFactory(), ZmqEndpoint('connect', self.config.ha.slave)) self.repops = {0: self.do_succ, 1: self.do_fail} reactor.callLater(1, self.ping) reactor.callLater(3, self.process) def do_succ(self, sync_id, **kwargs): try: with self.db_engine.begin() as db: table = models.TrRepliSyncStatus.__table__ db.execute(models.TrRepliSyncStatus.__table__.delete().where( table.c.id == sync_id)) except Exception as err: logger.error(traceback.format_exc()) def do_fail(self, sync_id, msg='', **kwargs): try: with self.db_engine.begin() as db: table = models.TrRepliSyncStatus.__table__ stmt = table.update().where(table.c.id == sync_id).values( last_sync=utils.get_currtime(), sync_times=table.c.sync_times + 1, sync_status=2, error=utils.safeunicode(msg)[:2000]) db.execute(stmt) except Exception as err: logger.error(traceback.format_exc()) def on_reply(self, result, sync_id): try: reply = msgpack.unpackb(result[0]) code, msg = reply['code'], reply['msg'] self.repops[code](sync_id, msg=msg) except Exception as err: logger.error(traceback.format_exc()) def on_fail(self, err, sync_id): self.do_fail(sync_id, msg=utils.safeunicode(err.getErrorMessage())[:2000]) def ping(self): def onsucc(r): self.running = True self.ping_errs = 0 self.cache.set(hadb_sync_status_cache_key, 0) logger.debug(u'同步链路检测成功:{}'.format(r[0])) def onfail(e): self.running = False self.ping_errs += 1 self.cache.set(hadb_sync_status_cache_key, 1) logger.error(u'同步链路检测失败:{}'.format(repr(e))) def on_timeout(d): if not d.called: self.running = False self.ping_errs += 1 self.cache.set(hadb_sync_status_cache_key, 2) logger.error(u'同步链路检测超时') try: d = self.syncer.sendMsg(msgpack.packb(dict(action='ping'))) d.addCallbacks(onsucc, onfail) reactor.callLater(3.0, on_timeout, d) except: self.running = False traceback.print_exc() reactor.callLater(self.config.ha.get('ping_interval', 60), self.ping) def process(self): next_interval = self.config.ha.get('interval', 5) if not self.running: reactor.callLater(next_interval, self.process) return try: table = models.TrRepliSyncStatus.__table__ with self.db_engine.begin() as conn: squery = conn.execute(table.select().where( table.c.sync_status.in_([0, 2 ])).where(table.c.sync_times < 5)) count = squery.rowcount self.cache.set(hadb_sync_count_cache_key, count) if count == 0: reactor.callLater(next_interval, self.process) return if count > 100: next_interval = 1.0 logger.info(u'等待同步数据记录数:{}'.format(count)) _total = 0 for _status in squery.fetchmany(size=100): try: statobj = Storage() statobj.id = _status[table.c.id] statobj.table_name = _status[table.c.table_name] statobj.action = _status.action statobj.pkeys = json.loads(_status[table.c.pkeys]) statobj.content = json.loads(_status[table.c.content]) statobj.sync_ver = _status[table.c.sync_ver] message = msgpack.packb(statobj) d = self.syncer.sendMsg(message) d.addCallback(self.on_reply, statobj.id) d.addErrback(self.on_fail, statobj.id) _total += 1 except Exception as err: logger.error(traceback.format_exc()) except Exception as err: logger.error(traceback.format_exc()) reactor.callLater(next_interval, self.process)
class Command(object): """A generic command dispatcher. Any command in the class attribute `commands` will be dispached and represented with a generic printer.""" service = '' usage = '''{name} <subcommand>'''.format(name=appname) epilog = ("Use bitmaskctl <subcommand> --help' to learn more " "about each command.") commands = [] def __init__(self): color_init() zf = ZmqFactory() e = ZmqEndpoint(ZmqEndpointType.connect, ENDPOINT) self._conn = ZmqREQConnection(zf, e) self.data = [] if self.service: self.data = [self.service] def execute(self, raw_args): self.parser = argparse.ArgumentParser(usage=self.usage, epilog=self.epilog) self.parser.add_argument('command', help='Subcommand to run') try: args = self.parser.parse_args(raw_args[0:1]) except SystemExit: return defer.succeed(None) # if command is in the default list, send the bare command # and use the default printer if args.command in self.commands: self.data += [args.command] return self._send(printer=default_dict_printer) elif (args.command == 'execute' or args.command.startswith('_') or not hasattr(self, args.command)): print 'Unrecognized command' return self.help([]) try: # use dispatch pattern to invoke method with same name return getattr(self, args.command)(raw_args[1:]) except SystemExit: return defer.succeed(None) def help(self, raw_args): self.parser.print_help() return defer.succeed(None) def _send(self, printer=_print_result): d = self._conn.sendMsg(*self.data, timeout=60) d.addCallback(self._check_err, printer) d.addErrback(self._timeout_handler) return d def _error(self, msg): print Fore.RED + "[!] %s" % msg + Fore.RESET sys.exit(1) def _check_err(self, stuff, printer): obj = json.loads(stuff[0]) if not obj['error']: return printer(obj['result']) else: print Fore.RED + 'ERROR:' + '%s' % obj['error'] + Fore.RESET def _timeout_handler(self, failure): # TODO ---- could try to launch the bitmask daemon here and retry if failure.trap(ZmqRequestTimeoutError) == ZmqRequestTimeoutError: print( Fore.RED + "[ERROR] Timeout contacting the bitmask daemon. " "Is it running?" + Fore.RESET)
from txzmq import ZmqEndpoint, ZmqFactory, ZmqREQConnection, ZmqREPConnection parser = OptionParser("") parser.add_option("-m", "--method", dest="method", help="0MQ socket connection: bind|connect") parser.add_option("-e", "--endpoint", dest="endpoint", help="0MQ Endpoint") parser.add_option("-M", "--mode", dest="mode", help="Mode: req|rep") parser.set_defaults(method="connect", endpoint="ipc:///tmp/txzmq-pc-demo") (options, args) = parser.parse_args() zf = ZmqFactory() e = ZmqEndpoint(options.method, options.endpoint) if options.mode == "req": s = ZmqREQConnection(zf, e) def produce(): # data = [str(time.time()), socket.gethostname()] data = str(time.time()) print "Requesting %r" % data try: d = s.sendMsg(data) def doPrint(reply): print("Got reply: %s" % (reply)) d.addCallback(doPrint) except zmq.error.Again:
class ApiClient(): """ RestFull Client :param config: :type config: """ def __init__(self, config, log=None): self.config = config self.log = log self.protocol = 'http' self.cache = mcache.Mcache() self.zauth_conn = ZmqREQConnection(ZmqFactory()) self.zacct_conn = ZmqREQConnection(ZmqFactory()) self.init_client().addCallback(self.log.info) reactor.callLater( 1.0, self.check_api, ) @defer.inlineCallbacks def init_client(self): try: reqdata = apiutils.make_request(self.config.api.api_secret, **{}) resp = yield self.send_http( "%s/radagent/fetch" % self.config.api.api_url, reqdata) auth_endpoints = [ ZmqEndpoint('connect', e) for e in resp.get('auth_endpoints', '').split(",") ] acct_endpoints = [ ZmqEndpoint('connect', e) for e in resp.get('acct_endpoints', '').split(",") ] self.api_auth_url = resp.get("api_auth_url") self.api_acct_url = resp.get("api_acct_url") self.log.info('auth_endpoints: %s' % auth_endpoints) self.log.info('acct_endpoints: %s' % acct_endpoints) self.log.info('api_auth_url: %s' % self.api_auth_url) self.log.info('api_acct_url: %s' % self.api_acct_url) self.protocol = resp.get('protocol') self.zauth_conn.addEndpoints(auth_endpoints) self.zacct_conn.addEndpoints(acct_endpoints) defer.returnValue("ApiClient init done") except Exception as err: import traceback traceback.print_exc() self.log.error(u"[ApiClient] :: fetch radagent failure, %s" % safeunicode(err.message)) defer.returnValue("ApiClient init error") def check_api(self): try: self.log.info("ping api service") retry = lambda err: self.init_client().addCallback(self.log.info) msg = apiutils.make_request(self.config.api.api_secret, action='ping') self.send_zmq('authorize', msg).addErrback(retry) self.send_zmq('accounting', msg).addErrback(retry) except Exception as err: self.log.error("ping api service error, %s" % apiutils.safeunicode(err.message)) reactor.callLater( 30.0, self.check_api, ) @defer.inlineCallbacks def send_http(self, apiurl, reqdata): """ send radius request :param apiurl: oss server api :param reqdata: json data """ try: if self.config.defaults.debug: self.log.debug( "[HttpClient] :: Send http request to {0}, {1}".format( safestr(apiurl), safeunicode(reqdata))) headers = {"Content-Type": ["application/json;charset=utf-8"]} resp = yield requests.post(safestr(apiurl), data=reqdata, headers=headers) if resp.code != 200: defer.returnValue( dict( code=1, msg=u'server return error http status code {0}'.format( resp.code))) else: result = yield resp.json() if self.config.defaults.debug: self.log.debug( "[HttpClient] :: Received http response from {0}, {1}". format(safestr(apiurl), safeunicode(result))) if result.get('code') == 0 and not apiutils.check_sign( self.config.api.api_secret, result): defer.returnValue(dict(code=1, msg=u"sign error")) else: defer.returnValue(result) except Exception as err: import traceback traceback.print_exc() defer.returnValue(dict(code=1, msg=u'server error')) @defer.inlineCallbacks def send_zmq(self, rtype, reqdata): """ send radius request :param rtype: radius msg type :param reqdata: json data """ try: if self.config.defaults.debug: self.log.debug("[ZmqClient] :: Send zmq request {0}".format( safestr(reqdata))) resp = None if rtype == 'authorize': resp = yield self.zauth_conn.sendMsg(reqdata, timeout=15.0) elif rtype == 'accounting': resp = yield self.zacct_conn.sendMsg(reqdata, timeout=15.0) else: defer.returnValue( dict(code=1, msg=u"not support radius msg type")) if self.config.defaults.debug: self.log.debug( "[ZmqClient] :: Received zmq response {0}".format( safeunicode(resp))) result = json.loads(resp[0]) if not apiutils.check_sign(self.config.api.api_secret, result): defer.returnValue(dict(code=1, msg=u"zmq msg sign error")) defer.returnValue(result) except Exception as err: import traceback traceback.print_exc() defer.returnValue(dict(code=1, msg=u'server error')) @defer.inlineCallbacks def get_nas(self, nasaddr, nas_identify=None): _start = time.clock() cache_key = "toughengine.get_nas.{0}".format(nasaddr) if cache_key in self.cache.cache: log.msg("[get_nas cast:%.6f second (hit cache)]" % (time.clock() - _start)) defer.returnValue(self.cache.get(cache_key)) api_url = self.config.api.api_url if not api_url: raise ValueError("api_url is None") try: _reqdata = dict( nasaddr=nasaddr, nas_identify=nas_identify, ) reqdata = apiutils.make_request(self.config.api.api_secret, _reqdata) resp = yield self.send_http("%s/nas/fetch" % api_url, reqdata) if resp['code'] == 0: self.cache.set(cache_key, resp, expire=120) self.log.info("[init_nas cast:%.6f second]" % (time.clock() - _start)) defer.returnValue(resp) except Exception as err: import traceback traceback.print_exc() self.log.error(u"[HttpClient] :: fetch nas failure,%s" % safestr(err.message)) defer.returnValue( dict(code=1, msg=u"fetch nas error, please see log detail")) @defer.inlineCallbacks def authorize(self, username, domain, macaddr, nasaddr, vlanid1, vlanid2, textinfo=None): """send radius auth request :param username: not contain @doamin :param domain: :param macaddr: :param nasaddr: :param vlanid1: :param vlanid2: :param textinfo: """ _start = time.clock() try: _reqdata = dict(username=username, domain=safestr(domain), macaddr=safestr(macaddr), nasaddr=nasaddr, vlanid1=vlanid1, vlanid2=vlanid2, textinfo=safestr(textinfo)) reqdata = apiutils.make_request(self.config.api.api_secret, _reqdata) if self.protocol == 'http': resp = yield self.send_http(self.api_auth_url, reqdata) self.log.info("[http authorize cast:%.6f second]" % (time.clock() - _start)) defer.returnValue(resp) elif self.protocol == 'zmq': resp = yield self.send_zmq('authorize', reqdata) self.log.info("[zmq authorize cast:%.6f second]" % (time.clock() - _start)) defer.returnValue(resp) else: defer.returnValue(dict(code=1, msg=u"not support")) except Exception as err: import traceback traceback.print_exc() self.log.error(u"[ApiClient] :: authorize failure,%s" % safestr(err.message)) defer.returnValue( dict(code=1, msg=u"authorize error, please see log detail")) @defer.inlineCallbacks def accounting(self, req_type, username, session_id, session_time, session_timeout, macaddr, nasaddr, ipaddr, input_octets, output_octets, input_pkts, output_pkts, nas_port, event_timestamp, nas_port_type, nas_port_id, textinfo=None): """send radius accounting request :param req_type: 1 Start 2 Stop 3 Alive :param username: :param session_id: :param session_time: :param session_timeout: :param macaddr: :param nasaddr: :param ipaddr: :param input_octets: :param output_octets: :param input_pkts: :param output_pkts: :param textinfo: """ _start = time.clock() try: _reqdata = dict(req_type=req_type, username=username, session_id=session_id, session_time=session_time, session_timeout=session_timeout, macaddr=macaddr, nasaddr=nasaddr, ipaddr=ipaddr, input_octets=input_octets, output_octets=output_octets, input_pkts=input_pkts, output_pkts=output_pkts, nas_port=nas_port, event_timestamp=event_timestamp, nas_port_type=nas_port_type, nas_port_id=nas_port_id, textinfo=safestr(textinfo)) reqdata = apiutils.make_request(self.config.api.api_secret, _reqdata) if self.protocol == 'http': resp = yield self.send_http(self.api_acct_url, reqdata) self.log.info("[http accounting cast:%.6f second]" % (time.clock() - _start)) defer.returnValue(resp) elif self.protocol == 'zmq': resp = yield self.send_zmq('accounting', reqdata) self.log.info("[zmq accounting cast:%.6f second]" % (time.clock() - _start)) defer.returnValue(resp) else: defer.returnValue(dict(code=1, msg=u"not support")) except Exception as err: self.log.error(u"[HttpClient] :: accounting failure,%s" % safestr(err.message)) defer.returnValue( dict(code=1, msg=u"accounting error, please see log detail"))
def sendMsg(self, *messageParts): d = ZmqREQConnection.sendMsg( self, *messageParts ) return d
class ImagesClient(object): """Handle the image/crops transfer from the airborne server.""" def __init__(self, app, ip_camera, role, ip_controller): if role == gs.CONTROLLER: self.images_folder = gs.CTRL_IMAGES_FOLDER self.crops_folder = gs.CTRL_CROPS_FOLDER self.flightdata_folder = gs.CTRL_FLIGHTDATA_FOLDER self.thumbs_folder = None else: self.images_folder = gs.IMAGES_FOLDER self.crops_folder = gs.CROPS_FOLDER self.flightdata_folder = gs.FLIGHTDATA_FOLDER self.thumbs_folder = gs.THUMBNAILS_FOLDER if not os.path.exists(self.images_folder): os.makedirs(self.images_folder) if not os.path.exists(self.crops_folder): os.makedirs(self.crops_folder) if not os.path.exists(self.flightdata_folder): os.makedirs(self.flightdata_folder) if self.thumbs_folder is not None and not os.path.exists( self.thumbs_folder): os.makedirs(self.thumbs_folder) self.role = role # # Setup the zmq socket used for receiving images. # self.zmq_factory = ZmqFactory() if self.role == gs.CONTROLLER: # # Socket for pulling notices about images from the camera. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_camera, zmq_port=gs.ZMQ_CAMERA_NOTICE_PORT)) self.pull_socket = ZmqPullConnection(self.zmq_factory, endpoint) self.pull_socket.onPull = self.handlePullMessage # # Socket for requesting images from the camera. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_camera, zmq_port=gs.ZMQ_CAMERA_FILES_PORT)) self.req_socket = ZmqREQConnection(self.zmq_factory, endpoint) # # Socket for publishing images to subscribers. # endpoint = ZmqEndpoint( 'bind', 'tcp://0.0.0.0:{zmq_port}'.format( zmq_port=gs.ZMQ_PRIMARY_GS_PORT)) self.pub_socket = MyZmqPubConnection(self.zmq_factory, endpoint) # # Socket for responding with queued images or sync data. # endpoint = ZmqEndpoint( 'bind', 'tcp://*:{zmq_port}'.format( zmq_port=gs.ZMQ_PRIMARY_GS_CONTROLLER_PORT)) self.sync_rep_socket = ControllerSyncConnection( self.zmq_factory, endpoint, self) else: # # Socket for subscribing to images. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_controller, zmq_port=gs.ZMQ_PRIMARY_GS_PORT)) self.sub_socket = MyZmqSubConnection(self.zmq_factory, endpoint) self.sub_socket.subscribe(gs.SUB_TAG) self.sub_socket.gotMessage = self.handleNewMessage # # Socket for requesting queued images or sync data from the controller. # endpoint = ZmqEndpoint( 'connect', 'tcp://{server_ip}:{zmq_port}'.format( server_ip=ip_controller, zmq_port=gs.ZMQ_PRIMARY_GS_CONTROLLER_PORT)) self.sync_req_socket = ZmqREQConnection(self.zmq_factory, endpoint) log.msg('Sending sync request') self.requestSyncImageList() self.requestSyncCropList() self.app = app def shutdown(self): """Shutdown the zmq connection.""" self.zmq_factory.shutdown() def handlePullMessage(self, new_pull_message, ignore_tag=None): """Handle a notice from the camera about a new image.""" try: if self.role != gs.CONTROLLER: return data_type = new_pull_message[0] if data_type == gs.RESIZED_IMAGE: image_name = new_pull_message[1] log.msg("Got new pull message: {id}".format(id=image_name)) # # Request the new image. # self.req_socket.sendMsg(image_name).addCallback( self.handleNewMessage) else: # # Handle other messages normally. # self.handleNewMessage(new_pull_message, ignore_tag) except Exception as e: log.err(e, 'handlePullMessage') def handleNewMessage(self, new_data_message, ignore_tag=None): """Analyze the data received from the airborne server.""" try: if self.role == gs.CONTROLLER: self.pub_socket.publish(new_data_message, tag=gs.SUB_TAG) data_type = new_data_message[0] if data_type == gs.RESIZED_IMAGE: self.handleNewImg(new_data_message[1:]) elif data_type in (gs.MANUAL_CROP, gs.AUTO_CROP): self.handleNewCrop(data_type, new_data_message[1:]) elif data_type == gs.FLIGHT_DATA: self.handleNewFlightData(new_data_message[1:]) #log.msg("Got updated flight data") elif data_type == gs.AUTO_ANALYSIS: self.handleNewADLCresults(new_data_message[1:]) elif data_type == gs.STATE: self.handleState(new_data_message[1:]) elif data_type == gs.TARGET_INFO: self.handleNewTarget(new_data_message[1:]) else: log.msg( "Unkown data type received from image server: {data_type}". format(data_type=data_type)) except Exception as e: log.err(traceback.format_exc(), 'handleNewMessage') def handleNewImg(self, new_img_message): """Should be implemented by a subclass""" pass def handleNewCrop(self, crop_type, new_crop_message): """Should be implemented by a subclass""" pass def handleNewFlightData(self, new_fd_message): """Should be implemented by a subclass""" pass def handleNewTarget(self, new_target_message): """Should be implemented by a subclass""" pass def handleNewADLCresults(self, new_target_message): """Should be implemented by a subclass""" pass def handleSyncImageListRequest(self, message): """Should be implemented by a controller subclass""" """Should return response message""" pass def handleSyncCropListRequest(self, message): """Should be implemented by a controller subclass""" """Should return response message""" pass def handleSyncQueueRequest(self, message): """Should be implemented by a controller subclass""" """Should return response message""" pass def handleSyncImageRequest(self, message): """Should be implemented by a controller subclass""" """Should return response message""" pass def handleSyncCropRequest(self, message): """Should be implemented by a controller subclass""" """Should return response message""" pass def handleSyncTargetRequest(self, message): self.pub_socket.publish([gs.TARGET_INFO] + list(message), tag=gs.SUB_TAG) self.handleNewTarget(message) return [] def handleSyncRequest(self, message): cmd = message[0] rep = None if cmd == gs.SYNC_LIST: sync_type = message[1] if sync_type == gs.SYNC_LIST_IMAGES: rep = self.handleSyncImageListRequest() elif sync_type == gs.SYNC_LIST_CROPS: rep = self.handleSyncCropListRequest() else: log.msg( 'Unknown sync list request: {type}'.format(type=sync_type)) if rep is not None: rep = rep[:] rep.insert(0, sync_type) elif cmd == gs.SYNC_QUEUE: rep = self.handleSyncQueueRequest() elif cmd == gs.SYNC_IMAGE: rep = self.handleSyncImageRequest(message[1:]) elif cmd == gs.SYNC_CROP: rep = self.handleSyncCropRequest(message[1:]) elif cmd == gs.SYNC_TARGET: rep = self.handleSyncTargetRequest(message[1:]) if rep is not None: rep = rep[:] rep.insert(0, cmd) return rep def handleSyncImageListResponse(self, message): """Should be implemented by a primary/secondary subclass""" pass def handleSyncCropListResponse(self, message): """Should be implemented by a primary/secondary subclass""" pass def handleSyncImageResponse(self, message): """Should be implemented by a primary/secondary subclass""" pass def handleSyncCropResponse(self, message): """Should be implemented by a primary/secondary subclass""" pass def handleSyncQueueResponse(self, message): """Should be implemented by a primary/secondary subclass""" pass def handleSyncTargetResponse(self, message): pass def handleSyncResponse(self, message): if self.role == gs.CONTROLLER: return cmd = message[0] if cmd == gs.SYNC_LIST: if message[1] == gs.SYNC_LIST_IMAGES: self.handleSyncImageListResponse(message[2:]) elif message[1] == gs.SYNC_LIST_CROPS: self.handleSyncCropListResponse(message[2:]) else: log.msg('Unknown sync list response: {type}'.format( type=message[1])) elif cmd == gs.SYNC_QUEUE: self.handleSyncQueueResponse(message[1:]) elif cmd == gs.SYNC_IMAGE: self.handleSyncImageResponse(message[1:]) elif cmd == gs.SYNC_CROP: self.handleSyncCropResponse(message[1:]) elif cmd == gs.SYNC_TARGET: self.handleSyncTargetResponse(message[1:]) else: log.msg('Unknown sync response: {cmd}'.format(cmd=cmd)) def requestSyncMsg(self, *msg): if self.role != gs.CONTROLLER: self.sync_req_socket.sendMsg(*msg).addCallback( self.handleSyncResponse) def requestSyncImageList(self): self.requestSyncMsg(gs.SYNC_LIST, gs.SYNC_LIST_IMAGES) def requestSyncCropList(self): self.requestSyncMsg(gs.SYNC_LIST, gs.SYNC_LIST_CROPS) def requestSyncImage(self, image_name): self.requestSyncMsg(gs.SYNC_IMAGE, image_name) def requestSyncCrop(self, crop_name): self.requestSyncMsg(gs.SYNC_CROP, crop_name) def requestSyncQueue(self): self.requestSyncMsg(gs.SYNC_QUEUE) def notifyTargetManual(self, crop_name, crop_yaw, lat, lon, target_type, shape, shape_color, text, text_color, orientation_text, desc): self.requestSyncMsg(gs.SYNC_TARGET, gs.TARGET_MANUAL, crop_name, str(crop_yaw), target_type, str(lat), str(lon), shape, shape_color, text, text_color, orientation_text) def notifyTargetAuto(self, target_id, crop_name, crop_yaw, lat, lon, target_type, shape, shape_color, text, text_color, orientation_text): self.requestSyncMsg(gs.SYNC_TARGET, gs.TARGET_AUTO, target_id, crop_name, str(crop_yaw), target_type, str(lat), str(lon), shape, shape_color, text, text_color, orientation_text) def handleState(self, message): type = message[0] if type == gs.STATE_DOWNLOAD: self.handleDownloadState(message[1:]) elif type == gs.STATE_SHOOT: self.handleCameraState(message[1:]) def handleDownloadState(self, message): pass def handleCameraState(self, message): pass