Пример #1
0
    def _parse_response():

        while True:
            msg = redis_sub.parse_response(block=False, timeout=60)
            if msg:
                try:
                    Message.read(*msg)
                except Exception as e:
                    print traceback.print_exc()
 def sendMsg(self):
     #Client data message to be sent to server
     msg = Message(Message.PI, socket.gethostbyname(str(RasPi.my_hostname)), \
                     RasPi.my_port, RasPi.num_organoids, RasPi.my_id, False)
         	#print(msg.who + msg.host_ip, msg.port)
     print(msg)
     #Pickle Message and send it to sever
     data_string = pickle.dumps(msg)
     self.s.send(data_string)
Пример #3
0
 def master_cmd(self):
     while True:
         if not self.cmd_queue.empty():
             while not self.cmd_queue.empty():
                 cmd = self.cmd_queue.get()
             logger.info('Master: Get new command - [%s]' % cmd['type'])
             if cmd['type'] == 'get_clients':
                 del_clients = []
                 client_list = []
                 for client in six.itervalues(self.clients):
                     if client.last_time + 61 > time.time():
                         client_list.append({
                             "id": client.id,
                             "state": client.state,
                             "slave_num": client.slave_num
                         })
                     else:
                         del_clients.append(client.id)
                 for i in del_clients:
                     print(i)
                     del self.clients[i]
                 if client_list:
                     self.result_queue.put(client_list)
                 else:
                     self.result_queue.put('0')
             elif cmd['type'] == 'sent_script':
                 script_data = {
                     "filename": cmd['filename'],
                     "script": cmd['script']
                 }
                 for client in six.itervalues(self.clients):
                     self.send(
                         Message("send_script", script_data,
                                 cmd['client_id']))
                 self.result_queue.put("OK")
             elif cmd['type'] == 'run':
                 data = {"filename": cmd['filename'], "num": cmd['num']}
                 for client in six.itervalues(self.clients):
                     self.send(Message("run", data, cmd['client_id']))
                     # if client.id == cmd['client_id']:
                     #     client.state = STATE_RUNNING
             elif cmd['type'] == 'stop':
                 for client in six.itervalues(self.clients):
                     self.send(Message("stop", None, cmd['client_id']))
                     # if client.id == cmd['client_id']:
                     #     client.state = STATE_INIT
             elif cmd['type'] == 'get_filelist':
                 for client in six.itervalues(self.clients):
                     self.send(
                         Message("get_filelist", None, cmd['client_id']))
             elif cmd['type'] == 'get_psinfo':
                 for client in six.itervalues(self.clients):
                     self.send(Message("get_psinfo", None,
                                       cmd['client_id']))
             elif cmd['type'] == 'clear_folder':
                 for client in six.itervalues(self.clients):
                     self.send(
                         Message("clear_folder", None, cmd['client_id']))
         gevent.sleep(1)
Пример #4
0
def xshell():
    while True:
        xstr = raw_input('>>')
        try:
            print "input: ", xstr
            if not xstr:
                continue
            elif xstr.strip() == "exit":
                exit()
            else:
                CLIENT.send(Message(tojid, xstr))
        except Exception as e:
            print e
Пример #5
0
def send_xmpp():
    client_xmpp = Client(server, port)
    connect = client_xmpp.connect()
    print "connect is ", connect
    print client_xmpp.auth(user, password, resource)
    client_xmpp.register_h()
    client_xmpp.register_message()
    # client_xmpp.send(Message(tojid, "today is a good day"))
    print "start to get response"
    global CLIENT
    CLIENT = client_xmpp
    threading.Thread(target=xshell).start()
    client_xmpp.get_response(client_xmpp.send,
                             Message(tojid, "today is a good day"))
Пример #6
0
 def run(self):
     command = ""
     while command != "exit":
         command = input("> ").lower().strip()
         if command:
             for packed_message in Message.packed_from_string(self.message_wrapper, command, secure=self.secure):
                 self._write_line(packed_message)
             if command != "exit":                    
                 response = self._get_message(self.message_wrapper, secure=self.secure)
                 try:
                     # Try to pretty print JSON responses
                     response = json.loads(response)                        
                     response = json.dumps(response, indent=4)
                 except (ValueError, json.JSONDecodeError):
                     pass
                 print(response)
Пример #7
0
 def command_loop(self, message_wrapper):
     command_handler = CommandHandlerFactory.get_instance(configuration)
     print(f"[+] Loaded commands: [{', '.join(command_handler.supported_commands)}]")
     while True:
         message = self._get_message(message_wrapper, secure=configuration.secure)
         if message:
             split_message = message.split()            
             command = split_message[:1][0].lower()
             args = split_message[1:]
             if command == "exit":
                 return
             elif command_handler.can_handle_command(command):
                 response = command_handler.handle_command(command, *args)
             else:
                 response = "Invalid command."
             print(f"[+] Responding to '{message}' with {response[160:]}")
             for packed_message in Message.packed_from_string(message_wrapper, response, secure=configuration.secure):
                 self._write_line(packed_message)
Пример #8
0
    def discover(addr=None):
        """Scan for devices in the network."""
        timeout = 5
        is_broadcast = addr is None
        seen_addrs = []
        if is_broadcast:
            addr = '<broadcast>'
            is_broadcast = True
            _LOGGER.info("Sending discovery to %s with timeout of %ss..", addr,
                         timeout)
        # magic, length 32
        helobytes = bytearray.fromhex(
            '21310020ffffffffffffffffffffffffffffffffffffffffffffffffffffffff')

        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        s.settimeout(timeout)
        for i in range(3):
            s.sendto(helobytes, (addr, 54321))
        while True:
            try:
                data, addr = s.recvfrom(1024)
                m = Message.parse(data)
                # _LOGGER.debug("Got a response: %s" % m)
                if not is_broadcast:
                    return m

                if addr[0] not in seen_addrs:
                    _LOGGER.info("  IP %s: %s - token: %s" %
                                 (addr[0], m.header.value.devtype,
                                  codecs.encode(m.checksum, 'hex')))
                    seen_addrs.append(addr[0])
            except socket.timeout:
                if is_broadcast:
                    _LOGGER.info("Discovery done")
                return  # ignore timeouts on discover
            except Exception as ex:
                _LOGGER.warning("error while reading discover results: %s", ex)
                break
Пример #9
0
 def worker(self):
     while True:
         msg = self.recv()
         if msg.node_id == self.client_id:
             logger.info('Slave: Get new msg from master - [%s]' % msg.type)
             # 接收压测脚本保存到./script文件夹中
             if msg.type == "send_script":
                 logger.info("Save script to file...")
                 if not os.path.exists("./script/"):
                     os.mkdir("./script/")
                 self.file_name = os.path.join("./script/", msg.data["filename"])
                 with codecs.open(self.file_name, 'w', encoding='utf-8') as f:
                     f.write(msg.data["script"])
                 logger.info("Script saved into file:%s" % self.file_name)
             # 运行locust压测进程,完成后返回成功启动的进程数给master
             elif msg.type == "run":
                 if self.state != STATE_RUNNING:
                     self.run_locusts(master_host=self.host, nums=msg.data["num"], file_name=msg.data["filename"])
                 if self.slave_num > 0:
                     self.state = STATE_RUNNING
                     logger.info("Client %s run OK!" % self.client_id)
                 else:
                     self.state = STATE_INIT
                 self.send(Message("slave_num", self.slave_num, self.client_id))
             # 停止locust压测进程并更新状态给master
             elif msg.type == "stop":
                 logger.info("Client %s stopped!" % self.client_id)
                 self.stop()
                 #self.send(Message("client_ready", self.slave_num, self.client_id))
                 self.send(Message("slave_num", self.slave_num, self.client_id))
             # 退出slave,当master退出时收到此消息
             elif msg.type == "quit":
                 logger.info("Got quit message from master, shutting down...")
                 self.stop()
                 self.greenlet.kill(block=True)
             # 获取当前客户端的压测文件列表
             elif msg.type == "get_filelist":
                 if os.path.exists("./script/"):
                     file_list = []
                     for root, dirs, files in os.walk("./script/"):
                         for f in files:
                             if os.path.splitext(f)[1] == '.py':
                                 file_list.append(f)
                         self.send(Message("file_list", file_list, self.client_id))
                 else:
                     self.send(Message("file_list", None, self.client_id))
             # 获取当前客户端的资源状态:包括IP, CPU,内存,网络
             elif msg.type == "get_psinfo":
                 ip = socket.gethostbyname(socket.gethostname())
                 nets = ps.net_io_counters()
                 sleep(1)
                 nets1 = ps.net_io_counters()
                 net = {'sent': nets1.bytes_sent / 1024, 'recv': nets1.bytes_recv / 1024,
                        'per_sec_sent': (nets1.bytes_sent - nets.bytes_sent) / 1024,
                        'per_sec_recv': (nets1.bytes_recv - nets.bytes_recv) / 1024}
                 cpu_times = ps.cpu_percent(interval=0.1)
                 cpu_logical_nums = ps.cpu_count()
                 cpu_nums = ps.cpu_count(logical=False)
                 cpu_freq = ps.cpu_freq()
                 if cpu_freq is not None:
                     cpu = {'num': cpu_nums, 'logical_num': cpu_logical_nums, 'percent': cpu_times,
                            'freq': {'current': cpu_freq.current, 'min': cpu_freq.min, 'max': cpu_freq.max}}
                 else:
                     cpu = {'num': cpu_nums, 'logical_num': cpu_logical_nums, 'percent': cpu_times,
                            'freq': {'current': 0, 'min': 0, 'max': 0}}
                 mems = ps.virtual_memory()
                 mem = {'total': mems.total / 1024 / 1024, 'available': mems.available / 1024 / 1024,
                        'percent': mems.percent}
                 psinfo = {"cpu": cpu, "mem": mem, "net": net, "IP": ip}
                 self.send(Message("psinfo", psinfo, self.client_id))
             # 清除压测脚本文件夹
             elif msg.type == "clear_folder":
                 if os.path.exists("./script/"):
                     shutil.rmtree("./script")
                 self.send(Message("clear_folder", None, self.client_id))
Пример #10
0
 def on_quitting():
     self.send(Message("quit", None, self.client_id))
     self.greenlet.kill(block=True)
Пример #11
0
 def recv(self):
     data = self.receiver.recv()
     return Message.unserialize(data)
Пример #12
0
 def ready_loop(self):
     while True:
         # 发送ready状态至master
         logger.info('Send ready to server!')
         self.send(Message("slave_ready", self.slave_num, self.client_id))
         gevent.sleep(60)
Пример #13
0
 def quit(self):
     for client in six.itervalues(self.clients):
         logger.info("Master: send quit message to client - %s" % client.id)
         self.send(Message("quit", None, client.id))
     self.greenlet.kill(block=True)
Пример #14
0
def message_test():
    print Message(tojid, "today is a good day")
Пример #15
0
    def send(self, command, parameters=None):
        """Build and send the given command."""
        delta_ts = int(time.mktime(
            datetime.datetime.utcnow().timetuple())) - self._ts_server
        if self._devtype is None or self._serial is None or (delta_ts > 120):
            self.__enter__()  # when called outside of cm, initialize.
            delta_ts = int(time.mktime(
                datetime.datetime.utcnow().timetuple())) - self._ts_server

        cmd = {
            "id": self._id,
            "method": command,
        }

        cmd["params"] = parameters

        self._ts += datetime.timedelta(seconds=delta_ts)
        header = {
            'length': 0,
            'unknown': 0x00000000,
            'devtype': self._devtype,
            'serial': self._serial,
            'ts': self._ts
        }

        msg = {
            'data': {
                'value': cmd
            },
            'header': {
                'value': header
            },
            'checksum': 0
        }
        ctx = {'token': self.token}
        m = Message.build(msg, ctx)
        _LOGGER.debug("%s:%s >>: %s" % (self.ip, self.port, cmd))
        if self.debug > 1:
            _LOGGER.debug("send (timeout %s): %s", self._timeout,
                          Message.parse(m, ctx))

        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.settimeout(self._timeout)

        try:
            s.sendto(m, (self.ip, self.port))
        except Exception as ex:
            _LOGGER.error("failed to send msg: %s" % ex)

        try:
            data, addr = s.recvfrom(1024)
            m = Message.parse(data, ctx)
            if self.debug > 1:
                _LOGGER.debug("recv: %s" % m)
            _LOGGER.debug(
                "%s:%s (ts: %s) << %s" %
                (self.ip, self.port, m.header.value.ts, m.data.value))
            self._ts = m.header.value.ts
            self._ts_server = int(
                time.mktime(datetime.datetime.utcnow().timetuple()))
            return m.data.value["result"]
        except Exception as ex:
            _LOGGER.error("got error when receiving: %s" % ex)
            self.__enter__()
            raise