def _publisher_thread(self, pub_addrs, pull_address, push_addr): #FIXME aaaaahhh pub_addresses are set here, not in the main thread # (which reads them in _register method) pub_sock, self.pub_addresses = self._init_socket( pub_addrs, zmq.PUB) pull_sock = self.ctx.socket(zmq.PULL) pull_sock.bind(pull_address) push_sock = self.ctx.socket(zmq.PUSH) push_sock.connect(push_addr) send_msg(push_sock, u'1') po = PollingObject() while not self._stop_publishing: try: to_publish, det = po.poll_recv(pull_sock, 500) if to_publish: send_msg(pub_sock, to_publish) except: #print self.name, '.Publisher -- STOP.' break # self.logger.info( "close sock %s %s", pub_addrs, pub_sock) pub_sock.close() pull_sock.close() push_sock.close()
def handle(self): print "SERVER REQUEST", self.__class__, "ME: ", self.request.getsockname( ) print "FROM :", self.request.getpeername() message = recv_unicode_netstring(self.rfile) print message pl = PollingObject() parsed = self.server.mtool.unpack_msg(message) if parsed.type == 'find_eeg_experiments' or parsed.type == 'find_eeg_amplifiers': pull_addr = 'tcp://' + socket.gethostname() + ':' + str( self.server.pull_port) parsed.client_push_address = pull_addr srv_sock = self.make_srv_sock() try: send_msg(srv_sock, parsed.SerializeToString()) response, det = pl.poll_recv(srv_sock, timeout=5000) finally: srv_sock.close() print "passed msg and got result: ", response if not response: self.bad_response(self.wfile, det) return if parsed.type == 'find_eeg_experiments' or parsed.type == 'find_eeg_amplifiers': response, det = pl.poll_recv(self.server.pull_sock, timeout=20000) if not response: self.bad_response(self.wfile, det) return data = make_unicode_netstring(response) self.wfile.write(data)
def __init__(self, proc_description, reg_timeout_desc=None, monitoring_optflags=PING, logger=None): self.desc = proc_description self.must_register = reg_timeout_desc is not None self._status_lock = threading.RLock() self._status = UNKNOWN if self.must_register else RUNNING self._status_details = None self.ping_it = monitoring_optflags & PING self.check_returncode = monitoring_optflags & RETURNCODE if \ self.desc.pid is not None else False self.logger = logger or get_logger( 'subprocess_monitor' + '-' + self.desc.name + '-' + str(self.desc.pid), stream_level='info') self.set_registration_timeout_handler(reg_timeout_desc) self.registration_data = None self._stop_monitoring = False self._ping_thread = None self._ping_retries = 8 self._returncode_thread = None self._mtool = OBCIMessageTool(message_templates) self._ctx = None self.rq_sock = None self._poller = PollingObject() self.delete = False
def __init__(self, srv_addrs, ctx, client_push_address, nearby_servers): self.ctx = ctx self.server_req_socket = self.ctx.socket(zmq.REQ) for addr in srv_addrs: self.server_req_socket.connect(addr) self.poller = PollingObject() self.mtool = OBCIMessageTool(message_templates) self.nearby_servers = nearby_servers self._amplified_cache = {}
def __init__(self, zmq_ctx, uuid, logger=None, obci_dns=None): self._processes = {} self._ctx = zmq_ctx self.uuid = uuid self.logger = logger or get_logger('subprocess_monitor', stream_level='warning') self.obci_dns = obci_dns self._mtool = OBCIMessageTool(message_templates) self.poller = PollingObject() self._proc_lock = threading.RLock()
def handle(self): message = recv_unicode_netstring(self.rfile) srv_sock = self.make_srv_sock() try: send_msg(srv_sock, message) pl = PollingObject() response, det = pl.poll_recv(srv_sock, timeout=5000) finally: srv_sock.close() if not response: self.bad_response(self.wfile, det) self.rfile.write(make_unicode_netstring(response))
def __init__(self, server_addresses, zmq_context=None): self.ctx = zmq_context if zmq_context else zmq.Context() self.server_addresses = server_addresses self.server_req_socket = None self.init_server_socket(server_addresses) #self = zmq.Poller() #self.register(self.server_req_socket, zmq.POLLIN) self.poller = PollingObject() self.mtool = OBCIMessageTool(message_templates) self.dns = net.DNS()
def __init__(self, server_address, handler_class, bind_and_activate=True, zmq_ctx=None, zmq_rep_addr=None): daemon_threads = True server_timeout = 45 SocketServer.TCPServer.__init__(self, server_address, handler_class, bind_and_activate) self.mtool = OBCIMessageTool(message_templates) self.pl = PollingObject() self.ctx = zmq_ctx self.rep_addr = zmq_rep_addr
def find_eeg_experiments_and_push_results(ctx, srv_addrs, rq_message, nearby_servers): LOGGER = logger.get_logger("eeg_experiment_finder", "info") finder = EEGExperimentFinder(srv_addrs, ctx, rq_message.client_push_address, nearby_servers) exps = finder.find_amplified_experiments() mpoller = PollingObject() checked = rq_message.checked_srvs if not isinstance(checked, list): checked = [] nrb = {} for uid, srv in nearby_servers.snapshot().iteritems(): if srv.ip not in checked: nrb[uid] = srv if not checked and nearby_servers.dict_snapshot(): my_addr = nearby_servers.ip(hostname=socket.gethostname()) LOGGER.info("checking other servers") print[(srv.hostname, srv.ip) for srv in nrb.values()] ip_list = [srv.ip for srv in nrb.values() if \ srv.ip != my_addr] LOGGER.info("number of servers to query: " + str(len(ip_list))) exps += _gather_other_server_results(ctx, my_addr, ip_list) else: LOGGER.info("not checking other servers") LOGGER.info("return to: " + rq_message.client_push_address) to_client = ctx.socket(zmq.PUSH) to_client.connect(rq_message.client_push_address) send_msg( to_client, finder.mtool.fill_msg('eeg_experiments', sender_ip=socket.gethostname(), experiment_list=exps)) LOGGER.info("sent exp data... " + str(exps)[:500] + ' [...]') time.sleep(0.1)
def kill(self): # send "kill" to the process or kill request to its supervisor? self.stop_monitoring() if not self._ctx: self._ctx = zmq.Context() rq_sock = self._ctx.socket(zmq.REQ) try: rq_sock.connect(self.rq_address) mtool = OBCIMessageTool(message_templates) poller = PollingObject() send_msg(rq_sock, mtool.fill_msg("kill_process", pid=self.pid, machine=self.machine_ip)) res, _ = poller.poll_recv(rq_sock, timeout=5000) finally: rq_sock.close() if res: res = mtool.unpack_msg(res) print "Response to kill request: ", res with self._status_lock: self._status = TERMINATED
def stringReceived(self, string): req_sock = self.factory.ctx.socket(zmq.REQ) req_sock.connect(self.factory.zmq_rep_addr) try: req = unicode(string, encoding='utf-8') print "twisted got:", req bad = False try: parsed = self.factory.mtool.unpack_msg(req) except ValueError: bad = True if not bad: if parsed.type in self.factory.long_rqs: sock, port = self.factory.long_rqs[parsed.type] pull_addr = 'tcp://' + socket.gethostname() + ':' + str( port) parsed.client_push_address = pull_addr send_msg(req_sock, parsed.SerializeToString()) else: send_msg(req_sock, req) pl = PollingObject() msg, det = pl.poll_recv(req_sock, timeout=5000) finally: req_sock.close() if not msg: msg = self.factory.mtool.fill_msg("rq_error", details=det) if not bad: if parsed.type in self.factory.long_rqs: sock, port = self.factory.long_rqs[parsed.type] msg, det = pl.poll_recv(sock, timeout=20000) if not msg: msg = self.factory.mtool.fill_msg("rq_error", details=det) return encmsg = msg.encode('utf-8') self.sendString(encmsg) reactor.callFromThread(self.sendString, encmsg)
def __init__(self, preset_data=None, launcher_data=None, ctx=None): self.preset_data = preset_data self.launch_file = None self.launcher_data = launcher_data self.name = None self.info = "" self.public_params = [] self.origin_machine = '' self.unsupervised_peers = {} self.old_uid = None self.ctx = ctx self.exp_req = None self.mtool = OBCIMessageTool(message_templates) self.poller = PollingObject() self.category = DEFAULT_CATEGORY if preset_data is not None: self.setup_from_preset(preset_data) elif launcher_data is not None: self.setup_from_launcher(launcher_data) super(ExperimentEngineInfo, self).__init__()
# -*- coding: utf-8 -*- import json import zmq import sys import socket from obci.control.common.message import OBCIMessageTool, send_msg, recv_msg, PollingObject from obci.control.launcher.launcher_messages import message_templates, error_codes from obci.control.common.obci_control_settings import PORT_RANGE import obci.control.common.net_tools as net if __name__ == '__main__': mtool = OBCIMessageTool(message_templates) pl = PollingObject() # ifname = net.server_ifname() my_addr = 'tcp://' + 'localhost' ctx = zmq.Context() server_req = ctx.socket(zmq.REQ) server_req.connect(my_addr + ':' + net.server_rep_port()) exp_info_pull = ctx.socket(zmq.PULL) port = exp_info_pull.bind_to_random_port('tcp://*', min_port=PORT_RANGE[0], max_port=PORT_RANGE[1], max_tries=500)