# Path: vespa/node.py
# class Node(PThread):
#     # Overrides threading.Thread.run()
#     def run(self):
#         profiler = cProfile.Profile()
#         try:
#             return profiler.runcall(PThread.run, self)
#         finally:
#             profiler.dump_stats('myprofile-%s.profile' % (self.name))
# 
# Path: vespa/agent.py
# class Agent(Node):
#     def __init__(self, name, host, port, master, run=True):
#         super(Agent, self,).__init__(name, host, port, master, run)
# 
# Path: vespa/agent_controller.py
# class Agent_Controller(Agent):
#     """Create an Agent to send a mac address to an OpenFlow controller
# 
#     :return: The Agent instance to offer the OpenFlow alert_ip function
#     :rtype: Node
#     """
# 
#     def __init__(self, name, host, port, master, run=False):
#         self.controller_ip = "12.0.0.3"
#         self.controller_port = 80
#         super(Agent_Controller, self,).__init__(name, host, port, master, run)
#         self.backend = self.desc()
# 
#     def alert_ip(self, ip, mac):
#         """Block the mac address on the network
# 
#         :param str ip: The IP address or domain of the controller
#         :param str mac: The mac address to block on the network
#         :return: The "Ok" string
#         :rtype: str
#         """
#         url = 'http://%s:%s/' % (self.controller_ip, self.controller_port)
#         values = {'mac': mac}
#         data = urllib.urlencode(values)
#         print '!!!!!!!!!!!!!!!!!!!!! Sending mac %s to %s' % (mac, url)
#         req = urllib2.Request(url, data)
#         response = urllib2.urlopen(req)
#         the_page = response.read()
#         return "Ok"


import pytest
import unittest
import sys
import os
import SocketServer
import threading
import time
from vespa.node import Node
from vespa.agent import Agent
from vespa.agent_controller import Agent_Controller

DOSSIER_COURANT = os.path.dirname(os.path.abspath(__file__))
DOSSIER_PARENT = os.path.dirname(DOSSIER_COURANT)
sys.path.append(DOSSIER_PARENT)


class SimpleRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request.recv(102400) # token receive
        self.request.send("hello")
        time.sleep(0.1) # make sure it finishes receiving request before closing
        self.request.close()

def serve_data():
    SocketServer.TCPServer.allow_reuse_address = True
    server = SocketServer.TCPServer(('127.0.0.1', 18080), SimpleRequestHandler)
    http_server_thread = threading.Thread(target=server.handle_request)
    http_server_thread.setDaemon(True)
    http_server_thread.start()
    return server

@pytest.fixture(scope='module')
def agent_instance():

# Path: vespa/node.py
# class Node(PThread):
#     # Overrides threading.Thread.run()
#     def run(self):
#         profiler = cProfile.Profile()
#         try:
#             return profiler.runcall(PThread.run, self)
#         finally:
#             profiler.dump_stats('myprofile-%s.profile' % (self.name))
# 
# Path: vespa/agent.py
# class Agent(Node):
#     def __init__(self, name, host, port, master, run=True):
#         super(Agent, self,).__init__(name, host, port, master, run)
# 
# Path: vespa/agent_controller.py
# class Agent_Controller(Agent):
#     """Create an Agent to send a mac address to an OpenFlow controller
# 
#     :return: The Agent instance to offer the OpenFlow alert_ip function
#     :rtype: Node
#     """
# 
#     def __init__(self, name, host, port, master, run=False):
#         self.controller_ip = "12.0.0.3"
#         self.controller_port = 80
#         super(Agent_Controller, self,).__init__(name, host, port, master, run)
#         self.backend = self.desc()
# 
#     def alert_ip(self, ip, mac):
#         """Block the mac address on the network
# 
#         :param str ip: The IP address or domain of the controller
#         :param str mac: The mac address to block on the network
#         :return: The "Ok" string
#         :rtype: str
#         """
#         url = 'http://%s:%s/' % (self.controller_ip, self.controller_port)
#         values = {'mac': mac}
#         data = urllib.urlencode(values)
#         print '!!!!!!!!!!!!!!!!!!!!! Sending mac %s to %s' % (mac, url)
#         req = urllib2.Request(url, data)
#         response = urllib2.urlopen(req)
#         the_page = response.read()
#         return "Ok"


# Path: tests/test_agent_controller.py
import pytest
import unittest
import sys
import os
import SocketServer
import threading
import time
from vespa.node import Node
from vespa.agent import Agent
from vespa.agent_controller import Agent_Controller


DOSSIER_COURANT = os.path.dirname(os.path.abspath(__file__))
DOSSIER_PARENT = os.path.dirname(DOSSIER_COURANT)
sys.path.append(DOSSIER_PARENT)


class SimpleRequestHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        data = self.request.recv(102400) # token receive
        self.request.send("hello")
        time.sleep(0.1) # make sure it finishes receiving request before closing
        self.request.close()

def serve_data():
    SocketServer.TCPServer.allow_reuse_address = True
    server = SocketServer.TCPServer(('127.0.0.1', 18080), SimpleRequestHandler)
    http_server_thread = threading.Thread(target=server.handle_request)
    http_server_thread.setDaemon(True)
    http_server_thread.start()
    return server

@pytest.fixture(scope='module')
def agent_instance():

    a = Agent_Controller('testnode', "127.0.0.1", 1340, None, run=False)