# Path: service/server.py
# class Server():
#     def __init__(self, name, port):
#     def add_component_post(self, route, component):
#         def binder():
#             def binded():
#     def add_component_get(self, route, component):
#         def binder():
#             def binded():
#     def run(self):
# 
# Path: database/database_service.py
# class DBClient(LocalClient):
#     """
#     The Database Client is a local client as we will only do
#     request from the populator or from the CLI/front-end.
#     """
#     def db_request(self, resource, product, version):
#         product = str(product)
#         version = str(version)
# 
#         db_json = {
#             "product" : product,
#             "version" : version
#         }
# 
#         output = self.post(resource, json=db_json)
#         return output
# 
# Path: topology/discovery/discovery.py
# def discovery_ip(ip):
#     """
#     A wrapper over Nmap that does OS discovery and port mapping. The wrapper is
#     written in C++ and interfaced in Python, thus allowing parallel scanning.
# 
#     :param ip: given IP to scan as a string
#     :return: JSON of the scan or {} when a scan fails
#     """
#     default = {}
# 
#     # Getting host information for ip
#     try:
#         logger.info("Running port scan...")
#         subprocess.getoutput("nmap -oX host.xml -O -sV -p 1-1023 " + ip)
#         logger.info("Examining port scan results...")
# 
#         # Need to do make before...
#         binary_location = PROJECT_ROOT + "/build_topology"
#         host_json = subprocess.getoutput(binary_location + " host.xml")
#         if "Aborted" in host_json:
#             return default
#         else:
#             return json.loads(host_json)
#     except Exception as e:
#         return default
# 
# Path: topology/graph/graph.py
# class Node():
#     """
#     The class used to encapsulate a node.
# 
#     Ip contains the mutable filds `ip` and `running`.
#     """
#     def __init__(self, ip):
#         self.ip = ip
#         self.running = {"scanned": "false"}
# 
#     def __eq__(self, other):
#         """Override the default Equals behavior"""
#         if isinstance(other, self.__class__):
#             return self.ip == other.ip
#         return False
# 
#     def __ne__(self, other):
#         """Define a non-equality test"""
#         return not self.__eq__(other)
# 
#     def __str__(self):
#         if self.running == {"scanned": "false"}:
#             return str(self.ip)
#         return str((self.ip, self.running))
# 
#     def __hash__(self):
#         numbers = [int(x) for x in self.ip.split(".")]
#         return numbers[0] * 256 ** 3 + numbers[1] * 256 ** 2 + numbers[2] * 256 + numbers[3]


import time
import random
import logging
from service.server import config
from database.database_service import DBClient
from topology.discovery.discovery import discovery_ip
from topology.graph.graph import Node
from threading import Lock, Thread
from clint.textui import colored
            threads[idx] = Thread(target=process, args=(str(idx)))
            threads[idx].start()
        for idx in range(len(batch)):
            threads[idx].join()
        return results

    def update_graph(self, graph, results, batch):
        # Putting the results on the graph
        graph.lock.acquire()
        for i in range(0, len(batch)):
            ip = batch[i]
            if Node(ip) not in graph.unpopulated:
                # It might be that a merge happended since we got the batch
                logger.info("Node {} already populated".format(ip))
                continue

            # updating unpopulated
            graph.unpopulated.remove(Node(ip))

            # updating populated
            logger.info(colored.green("Node {} scanned.".format(ip)))
            node = Node(ip)
            node.running = results[i]
            node.running["scanned"] = "true"

            if node not in graph.populated:
                self.updated = True
            graph.populated.add(node)

            #update nodes

# Path: service/server.py
# class Server():
#     def __init__(self, name, port):
#     def add_component_post(self, route, component):
#         def binder():
#             def binded():
#     def add_component_get(self, route, component):
#         def binder():
#             def binded():
#     def run(self):
# 
# Path: database/database_service.py
# class DBClient(LocalClient):
#     """
#     The Database Client is a local client as we will only do
#     request from the populator or from the CLI/front-end.
#     """
#     def db_request(self, resource, product, version):
#         product = str(product)
#         version = str(version)
# 
#         db_json = {
#             "product" : product,
#             "version" : version
#         }
# 
#         output = self.post(resource, json=db_json)
#         return output
# 
# Path: topology/discovery/discovery.py
# def discovery_ip(ip):
#     """
#     A wrapper over Nmap that does OS discovery and port mapping. The wrapper is
#     written in C++ and interfaced in Python, thus allowing parallel scanning.
# 
#     :param ip: given IP to scan as a string
#     :return: JSON of the scan or {} when a scan fails
#     """
#     default = {}
# 
#     # Getting host information for ip
#     try:
#         logger.info("Running port scan...")
#         subprocess.getoutput("nmap -oX host.xml -O -sV -p 1-1023 " + ip)
#         logger.info("Examining port scan results...")
# 
#         # Need to do make before...
#         binary_location = PROJECT_ROOT + "/build_topology"
#         host_json = subprocess.getoutput(binary_location + " host.xml")
#         if "Aborted" in host_json:
#             return default
#         else:
#             return json.loads(host_json)
#     except Exception as e:
#         return default
# 
# Path: topology/graph/graph.py
# class Node():
#     """
#     The class used to encapsulate a node.
# 
#     Ip contains the mutable filds `ip` and `running`.
#     """
#     def __init__(self, ip):
#         self.ip = ip
#         self.running = {"scanned": "false"}
# 
#     def __eq__(self, other):
#         """Override the default Equals behavior"""
#         if isinstance(other, self.__class__):
#             return self.ip == other.ip
#         return False
# 
#     def __ne__(self, other):
#         """Define a non-equality test"""
#         return not self.__eq__(other)
# 
#     def __str__(self):
#         if self.running == {"scanned": "false"}:
#             return str(self.ip)
#         return str((self.ip, self.running))
# 
#     def __hash__(self):
#         numbers = [int(x) for x in self.ip.split(".")]
#         return numbers[0] * 256 ** 3 + numbers[1] * 256 ** 2 + numbers[2] * 256 + numbers[3]


# Path: topology/graph/populator.py
import time
import random
import logging
from service.server import config
from database.database_service import DBClient
from topology.discovery.discovery import discovery_ip
from topology.graph.graph import Node
from threading import Lock, Thread
from clint.textui import colored

            threads[idx] = Thread(target=process, args=(str(idx)))
            threads[idx].start()
        for idx in range(len(batch)):
            threads[idx].join()
        return results

    def update_graph(self, graph, results, batch):
        # Putting the results on the graph
        graph.lock.acquire()
        for i in range(0, len(batch)):
            ip = batch[i]
            if Node(ip) not in graph.unpopulated:
                # It might be that a merge happended since we got the batch
                logger.info("Node {} already populated".format(ip))
                continue

            # updating unpopulated
            graph.unpopulated.remove(Node(ip))

            # updating populated
            logger.info(colored.green("Node {} scanned.".format(ip)))
            node = Node(ip)
            node.running = results[i]
            node.running["scanned"] = "true"

            if node not in graph.populated:
                self.updated = True
            graph.populated.add(node)

            #update nodes

            graph.nodes.remove(Node(ip))