# 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/rpc/MessageFactory.py
# class MessageFactory(object):
#     """"""
#     _reader = BinaryReader()
#     _writer = BinaryWriter()
# 
#     #----------------------------------------------------------------------
#     #def __init__(self):
#         #"""Constructor"""
# 
#     @classmethod
#     def create(cls, msg_type):
#         return message_creator[msg_type]()
# 
#     @classmethod
#     def encode(cls, message):
#         cls._writer.reset()
#         msg_type = message.DESCRIPTOR.name
#         msg_type_id = message_type_to_id[msg_type]
#         msg_str = message.SerializeToString()
#         cls._writer.writeInt32(msg_type_id)
#         cls._writer.writeFixedLengthString(msg_str, len(msg_str))
#         return cls._writer.getString()
# 
#     @classmethod
#     def decode(cls, str_):
#         cls._reader.setString(str_)
#         msg_type_id = cls._reader.readInt32()
#         msg_type = message_id_to_type[msg_type_id]
#         message = cls.create(msg_type)
#         msg_str = cls._reader.readFixedLengthString(len(str_)-4)
#         message.ParseFromString(msg_str)
#         return message
# 
#     @classmethod
#     def encrypt(cls, str_):
#         pass
# 
#     @classmethod
#     def decrypt(cls, str_):
#         pass


import base64
import time,datetime
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.rpc.MessageFactory import MessageFactory

            theApp.statistics.reports_in_queue = \
                  theApp.statistics.reports_in_queue - 1

    def load_unsent_reports(self):
        rs = g_db_helper.select("select * from unsent_reports")
        for record in rs:
            report_entry = ReportEntry()
            report_entry.ID = record[0]
            report_entry.TestID = record[1]
            report_entry.TimeGen = int(time.mktime(time.strptime(record[2], "%Y-%m-%d %H:%M:%S")))
            report_entry.Report = MessageFactory.decode(base64.b64decode(record[3]))
            report_entry.SourceID = record[4]
            report_entry.SourceIP = record[5]
            report_entry.Status = record[6]
            self.cached_reports[report_entry.ID] = report_entry
        theApp.statistics.reports_in_queue = len(rs)
        g_logger.info("Loaded %d unsent reports from DB." % len(rs))

    def load_reports_from_db(self, table_name):
        rs = g_db_helper.select("select * from %s" % table_name)
        return rs

    def save_report_to_db(self, table_name, report_entry):
        sql_stmt = "insert into %s (report_id, test_id, time_gen, content, "\
                   "source_id, source_ip, status) values "\
                   "('%s', '%s', '%s', '%s', '%s', '%s', '%s')" % \
                   (table_name,
                    report_entry.ID,
                    report_entry.TestID,
                    datetime.datetime.fromtimestamp(report_entry.TimeGen),

# 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/rpc/MessageFactory.py
# class MessageFactory(object):
#     """"""
#     _reader = BinaryReader()
#     _writer = BinaryWriter()
# 
#     #----------------------------------------------------------------------
#     #def __init__(self):
#         #"""Constructor"""
# 
#     @classmethod
#     def create(cls, msg_type):
#         return message_creator[msg_type]()
# 
#     @classmethod
#     def encode(cls, message):
#         cls._writer.reset()
#         msg_type = message.DESCRIPTOR.name
#         msg_type_id = message_type_to_id[msg_type]
#         msg_str = message.SerializeToString()
#         cls._writer.writeInt32(msg_type_id)
#         cls._writer.writeFixedLengthString(msg_str, len(msg_str))
#         return cls._writer.getString()
# 
#     @classmethod
#     def decode(cls, str_):
#         cls._reader.setString(str_)
#         msg_type_id = cls._reader.readInt32()
#         msg_type = message_id_to_type[msg_type_id]
#         message = cls.create(msg_type)
#         msg_str = cls._reader.readFixedLengthString(len(str_)-4)
#         message.ParseFromString(msg_str)
#         return message
# 
#     @classmethod
#     def encrypt(cls, str_):
#         pass
# 
#     @classmethod
#     def decrypt(cls, str_):
#         pass


# Path: umit/icm/agent/core/ReportManager.py
import base64
import time,datetime
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.rpc.MessageFactory import MessageFactory

            theApp.statistics.reports_in_queue = \
                  theApp.statistics.reports_in_queue - 1

    def load_unsent_reports(self):
        rs = g_db_helper.select("select * from unsent_reports")
        for record in rs:
            report_entry = ReportEntry()
            report_entry.ID = record[0]
            report_entry.TestID = record[1]
            report_entry.TimeGen = int(time.mktime(time.strptime(record[2], "%Y-%m-%d %H:%M:%S")))
            report_entry.Report = MessageFactory.decode(base64.b64decode(record[3]))
            report_entry.SourceID = record[4]
            report_entry.SourceIP = record[5]
            report_entry.Status = record[6]
            self.cached_reports[report_entry.ID] = report_entry
        theApp.statistics.reports_in_queue = len(rs)
        g_logger.info("Loaded %d unsent reports from DB." % len(rs))

    def load_reports_from_db(self, table_name):
        rs = g_db_helper.select("select * from %s" % table_name)
        return rs

    def save_report_to_db(self, table_name, report_entry):
        sql_stmt = "insert into %s (report_id, test_id, time_gen, content, "\
                   "source_id, source_ip, status) values "\
                   "('%s', '%s', '%s', '%s', '%s', '%s', '%s')" % \
                   (table_name,
                    report_entry.ID,
                    report_entry.TestID,
                    datetime.datetime.fromtimestamp(report_entry.TimeGen),

                    base64.b64encode(MessageFactory.encode(report_entry.Report)),
