Ejemplo n.º 1
0
    def _broadcastCommand(self,nodesIPs:list,command,params:dict={},recv_file=False):
        # get kdfs config
        finalResponse = {}
        config = self._getConfig()
        port = config.getInteger('queen_port',4040)
        chunk_size = config.getInteger('chunk_size',1024)
        # iterate all selected nodes
        for IP in nodesIPs:
            # get node name by IP
            nodeName = ServerUtils.findNodeByIP(IP)
            KDFSProtocol.echo("Sending \"{}\" command by \"{}\" param(s) to \"{}\" node ...".format(command,','.join(params),nodeName),'command')
            # connect to socket server
            socketi = ServerUtils.socketConnect(IP,port,config.getInteger('max_timeout',60))
            try: 
                # send command with params
                KDFSProtocol.sendMessage(socketi,chunk_size,KDFSProtocol.sendCommandFormatter(command,params,send_queen=True,recv_file=recv_file))
                # get response of command, if exist!
                response = KDFSProtocol.receiveMessage(socketi,chunk_size,is_file=recv_file)
                # print('(debug) node list response:',response)
                # append response to final by node name
                finalResponse[nodeName] = response


            except Exception as e:
                KDFSProtocol.echo("Raise an exception when broadcasting command",'command',err=e,is_err=True)
                # raise
            finally:
                # close socket 
                if socketi is not None:
                    socketi.close()

        return finalResponse
    # ------------------------------
    # ------------------------------
    # ------------------------------
Ejemplo n.º 2
0
    def _broadcastFile(self,nodeIPs:list,command,params:dict={},file_content=b''):
        # get kdfs config
        finalResponse = {}
        config = self._getConfig()
        port = config.getInteger('queen_port',4040)
        chunk_size = config.getInteger('chunk_size',1024)
        socket_list = {}
        # create socket connections by IP nodes
        for IP in nodeIPs:
            # get node name by IP
            nodeName = ServerUtils.findNodeByIP(IP)
            # connect to socket server
            socketi = ServerUtils.socketConnect(IP,port,config.getInteger('max_timeout',60))
            try:   
                # send command with params
                KDFSProtocol.echo("Sending \"{}\" command by \"{}\" param(s) to \"{}\" node ...".format(command,','.join(params),nodeName),'command')

                KDFSProtocol.sendMessage(socketi,chunk_size,KDFSProtocol.sendCommandFormatter(command,params,send_queen=True,send_file=True,max_send_packets=2))
                # get response of command, if exist!
                response = KDFSProtocol.receiveMessage(socketi,chunk_size)
                # KDFSProtocol.echo(f'response data(accept):{response}','debug')
                # get error of response,if exist
                if response['data'] is None or (response['data'] != 'success' and response['data'] != 'yes'):
                    return ('','failed to retrive accept response for send file')
                
                # if file to send, send file
                KDFSProtocol.echo("Sending file for \"{}\" command to \"{}\" node ...".format(command,nodeName),'command')

                # KDFSProtocol.echo(f'data file:{params}','debug')
                KDFSProtocol.sendMessage(socketi,chunk_size,file_content,send_file=True) 
                # get response of file, if exist!
                response = KDFSProtocol.receiveMessage(socketi,chunk_size)
                # get error of response,if exist
                if response['errors'] is not None and len(response['errors'])>1:
                    return ('',response['errors'][0])
                

            except Exception as e:
                KDFSProtocol.echo("Raise an exception when broadcasting file",'command',err=e,is_err=True)
                # raise
            finally:
                # close socket 
                if socketi is not None:
                    socketi.close()
        # return success response to send file successfully
        return ('success','')
Ejemplo n.º 3
0
 def sendAndReceiveServerIdentify(config: Config, ip: str):
     socketi = ServerUtils.socketConnect(
         ip, config.getInteger('queen_port', 4040), 2)
     try:
         # send identify command
         chunk_size = config.getInteger('chunk_size', 1024)
         KDFSProtocol.sendMessage(
             socketi, chunk_size,
             KDFSProtocol.sendCommandFormatter('identify', {},
                                               send_queen=True))
         # get response of command, if exist!
         response = KDFSProtocol.receiveMessage(socketi, chunk_size)['data']
         # print('(debug) node identify:',response)
         return response
     except Exception as e:
         return None
         # raise
     finally:
         # close socket
         if socketi is not None:
             socketi.close()
     return None
Ejemplo n.º 4
0
 params = sys.argv[2:]
 # print('argvs:',command,params)
 # check for arguments
 if command == 'help':
     help()
 else:
     # start time of process command
     startTime = currentMilliseconds()
     # set loading
     print('Please Wait...',end="\r")
     try:
         # create a socket object
         socketi = ServerUtils.socketConnect(KDFSConfig.get('client_ip','127.0.0.1'),KDFSConfig.getInteger('client_port',4041),KDFSConfig.getInteger('max_timeout',60))
         # send command to server
         chunk_size = KDFSConfig.getInteger('chunk_size',1024)
         KDFSProtocol.sendMessage(socketi,chunk_size,KDFSProtocol.sendCommandFormatter(command,params))
         # print("(debug):",socketi,chunk_size,command,params)
         # get response of command
         response = KDFSProtocol.receiveMessage(socketi,chunk_size)
         # print("(debug) receive response : ",response)
         # end time of process command
         endTime = currentMilliseconds()
         # show command with params on output
         print("\r({}) >> [{} sec] {} {}\n\n".format(platform.node(),(endTime-startTime)/1000, command,' '.join(params)))
         # check for errors
         if response is None or response['data'] is None:
             raiseError("Can not retrive response from server!",True)
             exit(1)
         if len(response['errors']) > 0:
             for err in response['errors']:
                 raiseError(err,True)