# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/Application.py
# def main_is_frozen():
#     def __init__(self):
#     def _init_components(self, aggregator):
#     def _load_from_db(self):
#     def init_after_running(self, port=None, username=None, password=None,
#                            server_enabled=True, skip_server_check=False):
#     def check_aggregator_success(self,response):
#     def login_window_show(self):       
#     def check_aggregator_failed(self,message):
#     def login_without_gui(self):
#     def check_software_auto(self):
#     def register_agent(self, username, password):
#     def _handle_register(self, result):
#     def _handle_errback(self, failure):
#     def login(self, username, password, save_login=False, login_only=False):
#     def check_username(self,username="",password=""):
#     def _login_after_register_callback(self, message, username,
#                                        password, save_login, login_only):
#     def _handle_login_errback(self,failure):
#     def _handle_login(self, result, username, password, save_login,login_only=False):
#     def login_simulate(self):
#     def task_loop_manager(self):
#     def logout(self):
#     def _handle_logout(self, result):
#     def start(self, run_reactor=True, managed_mode=False, aggregator=None):
#     def quit_window_in_wrong(self,primary_text = "",secondary_text = ""):
#     def terminate(self):
#     def on_quit(self):
# class Application(object):


import time
import random
from zope.interface import implements
from twisted.internet.interfaces import IConsumer
from umit.icm.agent.logger import g_logger
from umit.icm.agent.Application import theApp
from umit.icm.agent.Global import *
        from google.protobuf.text_format import MessageToString
        from umit.icm.agent.rpc.message import *
        from umit.icm.agent.rpc.MessageFactory import MessageFactory
        from umit.proto import messages_pb2
        from umit.icm.agent.test import *
        
        if message.testVersionNo >= self.current_test_version:
            g_logger.info("[Higher Version]Start to execute the tests(%d > %d)"%(message.testVersionNo,self.current_test_version))
            self.set_test_version(message.testVersionNo)
            g_logger.debug("Test size: " + str(len(message.tests)))
            for test in message.tests:  #tests is a list 
                self.parse_test_message(test)
                g_logger.debug("Tests " + str(test))
        else:
            g_logger.info("[Lower Version]Avoid these tests(%d <= %d)"%(message.testVersionNo,self.current_test_version))
        
    
    def fetch_tests(self):
        """
        Fetch New Test Sets from Aggregator(Keep polling the aggregator)
        Agent must constantly execute it.
        """
        if theApp.aggregator.available:
            g_logger.info("Fetching new test sets from aggregator, "+ str(self.current_test_version) )
            
            #The real check_tests by using check_tests Aggregator API
            defer_ = theApp.aggregator.check_tests(self.current_test_version)
            defer_.addCallback(self.execute_test)
            defer_.addErrback(self._handler_error)
            
            #Test by manual
            #self.execute_test(self.test_by_manually())
           
        else:
            g_logger.info("Cannot Fetching new test sets from aggregator, will try to connect super peers")

# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/Application.py
# def main_is_frozen():
#     def __init__(self):
#     def _init_components(self, aggregator):
#     def _load_from_db(self):
#     def init_after_running(self, port=None, username=None, password=None,
#                            server_enabled=True, skip_server_check=False):
#     def check_aggregator_success(self,response):
#     def login_window_show(self):       
#     def check_aggregator_failed(self,message):
#     def login_without_gui(self):
#     def check_software_auto(self):
#     def register_agent(self, username, password):
#     def _handle_register(self, result):
#     def _handle_errback(self, failure):
#     def login(self, username, password, save_login=False, login_only=False):
#     def check_username(self,username="",password=""):
#     def _login_after_register_callback(self, message, username,
#                                        password, save_login, login_only):
#     def _handle_login_errback(self,failure):
#     def _handle_login(self, result, username, password, save_login,login_only=False):
#     def login_simulate(self):
#     def task_loop_manager(self):
#     def logout(self):
#     def _handle_logout(self, result):
#     def start(self, run_reactor=True, managed_mode=False, aggregator=None):
#     def quit_window_in_wrong(self,primary_text = "",secondary_text = ""):
#     def terminate(self):
#     def on_quit(self):
# class Application(object):


# Path: umit/icm/agent/core/TestSetsFetcher.py
import time
import random
from zope.interface import implements
from twisted.internet.interfaces import IConsumer
from umit.icm.agent.logger import g_logger
from umit.icm.agent.Application import theApp
from umit.icm.agent.Global import *
        from google.protobuf.text_format import MessageToString
        from umit.icm.agent.rpc.message import *
        from umit.icm.agent.rpc.MessageFactory import MessageFactory
        from umit.proto import messages_pb2
        from umit.icm.agent.test import *

        
        if message.testVersionNo >= self.current_test_version:
            g_logger.info("[Higher Version]Start to execute the tests(%d > %d)"%(message.testVersionNo,self.current_test_version))
            self.set_test_version(message.testVersionNo)
            g_logger.debug("Test size: " + str(len(message.tests)))
            for test in message.tests:  #tests is a list 
                self.parse_test_message(test)
                g_logger.debug("Tests " + str(test))
        else:
            g_logger.info("[Lower Version]Avoid these tests(%d <= %d)"%(message.testVersionNo,self.current_test_version))
        
    
    def fetch_tests(self):
        """
        Fetch New Test Sets from Aggregator(Keep polling the aggregator)
        Agent must constantly execute it.
        """
        if theApp.aggregator.available:
            g_logger.info("Fetching new test sets from aggregator, "+ str(self.current_test_version) )
            
            #The real check_tests by using check_tests Aggregator API
            defer_ = theApp.aggregator.check_tests(self.current_test_version)
            defer_.addCallback(self.execute_test)
            defer_.addErrback(self._handler_error)
            
            #Test by manual
            #self.execute_test(self.test_by_manually())
           
        else:
            g_logger.info("Cannot Fetching new test sets from aggregator, will try to connect super peers")

            super_id = theApp.peer_manager.get_random_speer_connected()