# 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
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 umit.icm.agent.core.ReportManager import ReportStatus

    def process(self):
        # Upload Report
        if theApp.aggregator.available:
            g_logger.info("Sending %d reports to the aggregator." % \
                          len(self.report_manager.cached_reports))
            for report_key in self.report_manager.cached_reports.keys():    
                self.send_report(report_key)
                #theApp.aggregator.send_report(report_entry.Report)
        else:
            # Later, We should implement other upload path: super peer, normal peer 
            # Choose a random super peer to upload
            g_logger.info("Cannot connect to aggregator, so send report to super peer first!")
            speer_id = theApp.peer_manager.get_random_speer_connected()
            if speer_id is not None:
                g_logger.info("Sending %s reports to the super agent %s." % \
                              (len(self.report_manager.cached_reports),
                               str(speer_id)))
                for report_entry in self.report_manager.cached_reports.values():
                    theApp.peer_manager.sessions[speer_id].\
                          send_report(report_entry.Report)
            elif theApp.peer_info.Type == 2:
                g_logger.info("Cannot connect to aggregator and super peer ,so send report to normal peers!")
                cnt = 0
                sessions = []
                for peer_id in theApp.peer_manager.normal_peers:
                    if theApp.peer_manager.normal_peers[peer_id].status == \
                       'Connected':
                        cnt = cnt + 1
                        sessions.append(theApp.peer_manager.sessions[peer_id])
                if cnt != 0:

# 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/ReportUploader.py
import time
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 umit.icm.agent.core.ReportManager import ReportStatus

    def process(self):
        # Upload Report
        if theApp.aggregator.available:
            g_logger.info("Sending %d reports to the aggregator." % \
                          len(self.report_manager.cached_reports))
            for report_key in self.report_manager.cached_reports.keys():    
                self.send_report(report_key)
                #theApp.aggregator.send_report(report_entry.Report)
        else:
            # Later, We should implement other upload path: super peer, normal peer 
            # Choose a random super peer to upload
            g_logger.info("Cannot connect to aggregator, so send report to super peer first!")
            speer_id = theApp.peer_manager.get_random_speer_connected()
            if speer_id is not None:
                g_logger.info("Sending %s reports to the super agent %s." % \
                              (len(self.report_manager.cached_reports),
                               str(speer_id)))
                for report_entry in self.report_manager.cached_reports.values():
                    theApp.peer_manager.sessions[speer_id].\
                          send_report(report_entry.Report)
            elif theApp.peer_info.Type == 2:
                g_logger.info("Cannot connect to aggregator and super peer ,so send report to normal peers!")
                cnt = 0
                sessions = []
                for peer_id in theApp.peer_manager.normal_peers:
                    if theApp.peer_manager.normal_peers[peer_id].status == \
                       'Connected':
                        cnt = cnt + 1
                        sessions.append(theApp.peer_manager.sessions[peer_id])
                if cnt != 0:

                    g_logger.info("Sending %d reports to %d normal agents." % \
