# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/utils/Network.py
# def convert_ip(ip):
#     """Receives the IP as a list of ints:
#     [192, 168, 0, 0] representing "192.168.0.0"
#     
#     Or, receives as a string, and converts it for internal use:
#     "192.168.0.0" to [192, 168, 0, 0] and then uses it
#     """
#     ip = sanitize_ip(ip)
#     int_ip = 0
#     int_ip |= (ip[0] << 24)
#     int_ip |= (ip[1] << 16)
#     int_ip |= (ip[2] << 8)
#     int_ip |= ip[3]
#     
#     return int_ip


import os
import time
import base64
import cPickle
import sqlite3
from types import StringTypes
from umit.icm.agent.logger import g_logger
from umit.icm.agent.utils.Network import convert_ip
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_ALL,TASK_SUCCESSED,TASK_FAILED
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_SUCCESSED, TASK_FAILED,TASK_ALL,TASK
    from umit.icm.agent.BasePaths import *

    def get_peer(self, agent_id):
        return self.select("SELECT * FROM peers WHERE id = '%s'" % agent_id)

    def remove_peer(self, agent_id):
        return self.execute("DELETE FROM peers WHERE id = '%s'" % agent_id)

    def insert_banned_network(self, start_ip, end_ip, nodes_count, flag):
        bannet = self.get_banned_network(start_ip, end_ip)
        if bannet:
            # Update node count and flags
            self.execute("UPDATE bannets SET start_number=%d, "
                         "end_number=%d, nodes_count=%d, "
                         "flags=%d, updated_at=%d WHERE "
                         "id=%d" % (start_ip, end_ip, nodes_count,
                                    flag, int(time.time()), bannet[0][0]))
        else:
            # Insert into db
            self.execute("INSERT INTO bannets (start_number, end_number, "
                         "nodes_count, flags, created_at, updated_at) VALUES "
                         "('%s', '%s', '%s', %d, %d, %d)" % \
                            (start_ip, end_ip, nodes_count, flag,
                             int(time.time()), int(time.time())))

        self.db_conn.commit()

    def get_banned_network(self, start_ip, end_ip):
        if type(start_ip) in StringTypes:
            start_ip = convert_ip(start_ip)

        if type(end_ip) in StringTypes:

# Path: umit/icm/agent/logger.py
# LOGLEVEL = 'DEBUG'
# 
# Path: umit/icm/agent/utils/Network.py
# def convert_ip(ip):
#     """Receives the IP as a list of ints:
#     [192, 168, 0, 0] representing "192.168.0.0"
#     
#     Or, receives as a string, and converts it for internal use:
#     "192.168.0.0" to [192, 168, 0, 0] and then uses it
#     """
#     ip = sanitize_ip(ip)
#     int_ip = 0
#     int_ip |= (ip[0] << 24)
#     int_ip |= (ip[1] << 16)
#     int_ip |= (ip[2] << 8)
#     int_ip |= ip[3]
#     
#     return int_ip


# Path: umit/icm/agent/db.py
import os
import time
import base64
import cPickle
import sqlite3
from types import StringTypes
from umit.icm.agent.logger import g_logger
from umit.icm.agent.utils.Network import convert_ip
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_ALL,TASK_SUCCESSED,TASK_FAILED
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.test import TASK_STATUS_DONE,TASK_STATUS_FAILED
        from umit.icm.agent.gui.dashboard.DashboardListBase import CAPA_THROTTLED,CAPACITY,CAPA_SERVICE
        from umit.icm.agent.gui.dashboard.DashboardListBase import REPORT,REPORT_SENT,REPORT_UNSENT,REPORT_RECEIVED
        from umit.icm.agent.gui.dashboard.DashboardListBase import TASK_SUCCESSED, TASK_FAILED,TASK_ALL,TASK
    from umit.icm.agent.BasePaths import *

    def get_peer(self, agent_id):
        return self.select("SELECT * FROM peers WHERE id = '%s'" % agent_id)

    def remove_peer(self, agent_id):
        return self.execute("DELETE FROM peers WHERE id = '%s'" % agent_id)

    def insert_banned_network(self, start_ip, end_ip, nodes_count, flag):
        bannet = self.get_banned_network(start_ip, end_ip)
        if bannet:
            # Update node count and flags
            self.execute("UPDATE bannets SET start_number=%d, "
                         "end_number=%d, nodes_count=%d, "
                         "flags=%d, updated_at=%d WHERE "
                         "id=%d" % (start_ip, end_ip, nodes_count,
                                    flag, int(time.time()), bannet[0][0]))
        else:
            # Insert into db
            self.execute("INSERT INTO bannets (start_number, end_number, "
                         "nodes_count, flags, created_at, updated_at) VALUES "
                         "('%s', '%s', '%s', %d, %d, %d)" % \
                            (start_ip, end_ip, nodes_count, flag,
                             int(time.time()), int(time.time())))

        self.db_conn.commit()

    def get_banned_network(self, start_ip, end_ip):
        if type(start_ip) in StringTypes:
            start_ip = convert_ip(start_ip)

        if type(end_ip) in StringTypes:

            end_ip = convert_ip(end_ip)
