Example #1
0
    def run(self):
        try:
            start_cmd = CommandStruct(ServerCommand.Com_Start)
            stop_cmd = CommandStruct(ServerCommand.Com_Stop)
            if self.is_connection_ok() and self.processor.check_response_ex(
                    start_cmd)[0]:
                if self.cmd == ServerCommand.Com_Status:
                    self.get_status()
                elif self.cmd == ServerCommand.Com_Data:
                    self.send_data()
                elif self.cmd == ServerCommand.Com_Get_Data:
                    self.get_data()
                elif self.cmd == ServerCommand.Com_Stop_Mining:
                    self.stop_slave()
                elif self.cmd == ServerCommand.Com_Setup:
                    self.setup_slave()
                elif self.cmd == ServerCommand.Com_Clear_Cache:
                    self.clear_cache()
                else:
                    pass
            try:  # the last call may have exception due to network connection problem
                self.processor.check_response_ex(stop_cmd)
            except:
                pass

        except Exception as ex:
            print(ex)
            pass
    def send_and_receive(self):
        in_buffer = self.rfile
        out_buffer = self.wfile
        command = CommandProcessor.receive_command(in_buffer)
        #print("process cmd: ", command.cmd)
        if command is not None:
            reply = CommandStruct(cmd=ServerCommand.Com_ReplyOK)
            if command.cmd == ServerCommand.Com_Start:
                #print("start conversation:")
                pass
            elif command.cmd == ServerCommand.Com_Stop:
                #print("end conversation:")
                return
            else:
                server_handle_result = self.handle_request(command)
                if isinstance(server_handle_result, Serializable):
                    reply.data = server_handle_result
                elif isinstance(server_handle_result,
                                bool) and server_handle_result:
                    pass
                else:
                    reply.cmd = ServerCommand.Com_ReplyError
                    reply.data = "command is not valid, please try again"
            CommandProcessor.send_command(out_buffer, reply)

            self.send_and_receive()
    def run(self):
        try:
            handler = self.handler_map.get(self.cmd)
            if handler is not None:
                start_cmd = CommandStruct(ServerCommand.Com_Start)
                stop_cmd = CommandStruct(ServerCommand.Com_Stop)
                if self.is_connection_ok() and self.processor.check_response_ex(start_cmd)[0]:
                    handler()
                try:  # the last call may have exception due to network connection problem
                    self.processor.check_response_ex(stop_cmd)
                except:
                    pass

        except Exception as ex:
            print(ex)
            pass
    def run(self):
        try:
            handler = self.prototype_one_way if not self._has_return_data else self.prototype_return_data
            if handler is not None:
                start_cmd = CommandStruct(ServerCommand.Com_Start)
                stop_cmd = CommandStruct(ServerCommand.Com_Stop)
                if self.is_connection_ok(
                ) and self.processor.check_response_ex(start_cmd)[0]:
                    handler(self.cmd)
                try:  # the last call may have exception due to network connection problem
                    self.processor.check_response_ex(stop_cmd)
                except:
                    pass

        except Exception as ex:
            print(ex)
            pass
 def prototype_return_data(self, cmd: str):
     get_data_cmd = CommandStruct(cmd=cmd, target=self.target, data=self.in_data)
     response = self.processor.check_response_ex(get_data_cmd)
     outdata = response[1].data
     if not response[0]:
         raise ValueError("prototype_return_data: get data failed")
     else:
         if isinstance(self.out_data, list):
             self.out_data.append(outdata)
Example #6
0
 def get_data(self):
     get_data_cmd = CommandStruct(cmd=ServerCommand.Com_Get_Data)
     response = self.processor.check_response_ex(get_data_cmd)
     outdata = response[1].data
     if not response[0]:
         raise ValueError("get data failed")
     else:
         if isinstance(self.out_data, type([])):
             self.out_data.append(outdata)
 def get_status(self):
     status_cmd = CommandStruct(cmd=ServerCommand.Com_Status)
     response = self.processor.check_response_ex(status_cmd)
     response_data = response[1].data
     if isinstance(response_data, dict):
         response_data = Serializable.get_deserialized(response_data)
     if not response[0]:
         raise ValueError("get status failed")
     elif response_data is not None and isinstance(response_data, ServerStatus):
         if isinstance(self.out_data, list):
             self.out_data.append(response_data)
         self.server.status = response_data
    def upload_seeds(ref: str,
                     target_host: Server,
                     seed_server: ServerRequestHandler,
                     niches=[],
                     init_seeds=[],
                     seed_source_addr="",
                     seeds_per_niche=5000):
        seeds = init_seeds
        if len(seeds) == 0:
            if len(seed_source_addr) > 0:
                raise NotImplementedError
            else:
                for niche in niches:
                    request = SeedDBRequest(niche=niche,
                                            random_read=True,
                                            reverse_read=True,
                                            data_len=seeds_per_niche)
                    cmd = CommandStruct(cmd=ServerCommand.Com_Get_DB_DATA,
                                        target=ServerType.ty_Seed_Database,
                                        data=request)

                    if isinstance(seed_server, ServerRequestHandler):
                        # case when it is a local seed db
                        temp = seed_server.handle_request(cmd)
                    elif len(seed_source_addr) > 0:
                        # TODO: case when it is a remote seed db
                        temp = None
                    else:
                        raise NotImplementedError

                    if isinstance(temp, MiningList):
                        seeds += temp.data

        seeds = [x for x in set(seeds)]
        in_data = MiningList(ref=ref, data=seeds)
        hostController = HostController(target_host,
                                        cmd=ServerCommand.Com_Add_Seed,
                                        in_data=in_data)
        hostController.start()
        hostController.join()
        return len(seeds)
 def prototype_one_way(self, cmd: str):
     data_cmd = CommandStruct(cmd=cmd, target=self.target,  data=self.in_data)
     response = self.processor.check_response_ex(data_cmd)
     if not response[0]:
         raise ValueError("prototype_one_way: command failed")
Example #10
0
 def clear_cache(self):
     stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Clear_Cache)
     response = self.processor.check_response_ex(stop_mining_cmd)
     if not response[0]:
         raise ValueError("clear cache failed")
Example #11
0
 def setup_slave(self):
     setup_cmd = CommandStruct(cmd=ServerCommand.Com_Setup,
                               data=self.in_data)
     response = self.processor.check_response_ex(setup_cmd)
     if not response[0]:
         raise ValueError("send data failed")
Example #12
0
 def stop_slave(self):
     stop_mining_cmd = CommandStruct(cmd=ServerCommand.Com_Stop_Mining)
     response = self.processor.check_response_ex(stop_mining_cmd)
     if not response[0]:
         raise ValueError("stop slave failed")
Example #13
0
 def send_data(self):
     data_cmd = CommandStruct(cmd=ServerCommand.Com_Data, data=self.in_data)
     response = self.processor.check_response_ex(data_cmd)
     if not response[0]:
         raise ValueError("send data failed")