def set_client_id(self):
     """ Sets client_id to the return packet from server if present. """
     if len(self.server_message):
         self.client_id = self.server_message[0]
         test_log.debug('id received: {}'.format(self.client_id))
     else:
         test_log.debug('Id request returned no information')
         self.handle_close()
 def set_run_test(self):
     """ Sets run_test to the return packet from server if present """
     if len(self.server_message):
         test_log.debug('running test from server: {} {}'.format(self.server_message[0], self.server_message[1]))
         function_args = [int(arg) for arg in self.server_message[1].split(',')]
         self.run_test = self.test_handler[self.server_message[0]](*function_args)
         self.run()
     else:
         test_log.debug('no test found, ending session')
         self.handle_close()
    def run(self):
        """ Sends test information for run_test to server. Then forks to run test in child.
            Continues to log any information from run_test.message_queue until
            run_test.end_of_test is set by the test. Ends the connection gracefully.
        """
        if self.run_test:
            test_log.debug('Starting test')
            self.send(Config.API_RUNNING_TEST + Config.API_DELIMITER + self.run_test.get_test_name() +
                      Config.API_DELIMITER + self.run_test.get_test_args() + Config.TERMINATOR)
            test_process = multiprocessing.Process(target=self.run_test.run)
            test_process.start()

            while not self.run_test.end_of_test.is_set():
                if not self.run_test.message_queue.empty():
                    self.send(self.run_test.message_queue.get())

            test_log.debug('Test ended')
            self.end()
        else:
            test_log.debug('No running test')
            self.handle_close()
 def handle_error(self):
     typ, val, traceback = sys.exc_info()
     test_log.debug('Connection closed: ' + str(val))
     self.close()
 def end(self):
     test_log.debug('Ending session')
     self.send(Config.API_CLIENT_END + Config.TERMINATOR)
     self.handle_close()
 def log_unknown_server_command(self):
     test_log.debug('Unknown command, ending session' + self.server_header)
     self.handle_close()
 def handle_close(self):
     test_log.debug('Socket closed')
     self.shutdown(socket.SHUT_RDWR)
     self.close()
        test_log.debug('Ending session')
        self.send(Config.API_CLIENT_END + Config.TERMINATOR)
        self.handle_close()


if __name__ == '__main__':
    # Spin up a client to connect to the test server. Input arguments are for the file write test,
    # more can be added to support additional tests.
    parser = argparse.ArgumentParser()
    parser.add_argument('-t', '--timeout', dest='timeout', default=10, type=int,
                        help='runtime for client')
    parser.add_argument('-f', '--filesize', dest='file_size', default=10, type=int,
                        help='chunk size of test files')
    cmd_input = parser.parse_args()

    if len(sys.argv) > 1:
        test_log.debug('custom test {}'.format(cmd_input))
        client = TestClient(Config.HOST, Config.PORT, FileWriteTest(cmd_input.timeout, cmd_input.file_size))
    else:
        client = TestClient(Config.HOST, Config.PORT)

    try:
        client.connect_to_host()
    except KeyboardInterrupt:
        test_log.debug('Ended via keyboard interrupt')
    except Exception as e:
        test_log.debug('Faulted during execution.')
        raise e
    finally:
        client.close()