def __init__(self, ix, task_queue, pid): self.serverName = "%s_%d" % (config.get_mac_address(), pid) self.conn = pymongo.Connection(config.MONGO_CONN) self.pageurls = self.conn.pongo.pageurls self.task_queue = task_queue self.ix = ix self.p = re.compile(u"(http://blog.csdn.net/[^\\/]+/article/details/\\d+)")
def __init__(self, ix, task_queue, pid): self.serverName = "%s_%d" % (config.get_mac_address(), pid) self.conn = pymongo.Connection(config.MONGO_CONN) self.pageurls = self.conn.pongo.pageurls self.task_queue = task_queue self.ix = ix self.p = re.compile( u'(http://blog.csdn.net/[^\\/]+/article/details/\\d+)')
def deal_pkg(self, header, body): res = None if header.cmd == pkg.SET_CHANNEL_CTRL_REQ: res = pkg.Header(cmd=pkg.SET_CHANNEL_CTRL_RSP) for k in ('mode', 'enable', 'mu_factor'): Config.set(header.channel, k, getattr(body, k)) wsets = [(w.begin, w.noise, w.end) for w in body.data] Config.set(header.channel, 'win_settings', wsets) elif header.cmd == pkg.GET_BASE_INFO_REQ: res = pkg.Header(cmd=pkg.GET_BASE_INFO_RSP) body = pkg.BaseInfo( mac=get_mac_address(self.ifname), hwid='Emultr', hwcode='EM', hw_version=chr(2) + chr(0), sw_version=chr(2) + chr(0), sw_revision=0, proto_version=2, channel_num=2, machine_id=0, ip_num=1, slot_id=0 ) res = pkg.combine(res, body) elif header.cmd == pkg.SET_SWITCH_CTRL_REQ: res = pkg.Header(cmd=pkg.SET_SWITCH_CTRL_RSP) if body.led == 1: # light on if self.test: algorithm_test(self.ip + ":" + str(header.channel)) led = 'LED RED' elif body.led == 0xff: # blink led = 'LED BLINK' elif body.led == 0: # light off led = 'LED BLACK' logger.critical((self.ip, led)) elif header.cmd == pkg.GET_CHANNEL_CTRL_REQ: res = pkg.Header(cmd=pkg.GET_CHANNEL_CTRL_RSP) ch_ctl = pkg.ChannelCtrl() for k in ('enable', 'mu_factor', 'mode'): setattr(ch_ctl, k, Config.get(header.channel, k)) win_sts = Config.get(header.channel, 'win_settings') ch_ctl.win_size = len(win_sts) for ws in win_sts: wp = pkg.ChannelCtrl.WinSetting( begin=ws[0], end=ws[1], noise=ws[2] ) ch_ctl.data += wp.pack() res.data = ch_ctl.pack() else: logger.info(repr((header, body))) if res: res.seq = header.seq return res.pack()
HEARTBEAT_LIVENESS = config.HEARTBEAT_LIVENESS # 3..5 is reasonable HEARTBEAT_INTERVAL = config.HEARTBEAT_INTERVAL # Seconds INTERVAL_INIT = config.INTERVAL_INIT INTERVAL_MAX = config.INTERVAL_MAX # Paranoid Pirate Protocol constants PPP_READY = config.PPP_READY # Signals worker is ready PPP_HEARTBEAT = config.PPP_HEARTBEAT # Signals worker heartbeat INDEX_PATH = "indexdir" HOST = 'localhost' WORKER_HOST = "tcp://localhost:5556" SUBSCRIBER_HOST = "tcp://localhost:5557" mac_address = config.get_mac_address() def worker_socket(context, poller, pid): """Helper function that returns a new configured socket connected to the Paranoid Pirate queue""" worker = context.socket(zmq.DEALER) # DEALER identity = "work-%s-%d:%04X-%04X" % (mac_address, pid, randint( 0, 0x10000), randint(0, 0x10000)) worker.setsockopt(zmq.IDENTITY, identity) poller.register(worker, zmq.POLLIN) worker.connect(WORKER_HOST) worker.send(PPP_READY) return worker