# 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_bandwidth.py
# class Agent_Bandwidth(Agent):
#     """Provide a wrapper around Linux interfaces /proc files. The Agent can
#     extract information of specific interfaces, i.e. eth0 or lo.
# 
#     :return: The agent to grab informations
#     :rtype: Node
#     """
# 
#     def __init__(self, name, host, port, master, run=True):
#         # self.proc = None
#         self.devfile = "/proc/net/dev"
#         self.iface = "eth0"
#         super(Agent_Bandwidth, self,).__init__(name, host, port, master, run)
#         self.backend = self.desc()
# 
#     def launch(self):
#         """Send _recv_bytes_ and _trans_bytes_ back to the master every
#         second
#         """
# 
#         import time
# 
#         while not self.quitting:
#             infos = self._get_ifaces()
#             tm = time.time()
# 
#             r = infos[self.iface]['recv_bytes']
#             # self.sendRemote(self.master, "alert|%s>recv_bytes#%s#%s" %
#             # (self.name, tm, r), needack=False)
#             self.sendAlert("recv_bytes#%s#%s" % (tm, r))
# 
#             t = infos[self.iface]['trans_bytes']
#             # self.sendRemote(self.master, "alert|%s>trans_bytes#%s#%s" %
#             # (self.name, tm, t), needack=False)
#             self.sendAlert("trans_bytes#%s#%s" % (tm, t))
# 
#             time.sleep(1)
# 
#     def _get_ifaces(self):
#         """Function parsing the /proc/net/dev file and feeding a table
# 
#         :return: The table mapping the device file
#         :rtype: list
#         """
# 
#         lines = open(self.devfile, "r").readlines()
# 
#         columnLine = lines[1]
#         _, receiveCols, transmitCols = columnLine.split("|")
#         receiveCols = map(lambda a: "recv_" + a, receiveCols.split())
#         transmitCols = map(lambda a: "trans_" + a, transmitCols.split())
# 
#         cols = receiveCols + transmitCols
# 
#         faces = {}
#         for line in lines[2:]:
#             if line.find(":") < 0:
#                 continue
#             face, data = line.split(":")
#             faceData = dict(zip(cols, data.split()))
#             faces[face.strip()] = faceData
# 
#         return faces
# 
#     def get_mac(self):
#         """Grab the mac address of the class defined _self.iface_
# 
#         :return: The string containing the mac address, colon separated
#         :rtype: str
#         """
# 
#         return self._get_mac(self.iface)
# 
#     def _get_mac(self, ifname):
#         """Send an ioctl to recover the mac address of a specific interface
# 
#         :return: The string containing the mac address, colon separated
#         :rtype: str
#         """
# 
#         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#         info = fcntl.ioctl(
#             s.fileno(),
#             0x8927,
#             struct.pack(
#                 '256s',
#                 ifname[
#                     :15]))
#         return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]


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_bandwidth import Agent_Bandwidth

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


TEST_STRING = "hello"


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

# 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_bandwidth.py
# class Agent_Bandwidth(Agent):
#     """Provide a wrapper around Linux interfaces /proc files. The Agent can
#     extract information of specific interfaces, i.e. eth0 or lo.
# 
#     :return: The agent to grab informations
#     :rtype: Node
#     """
# 
#     def __init__(self, name, host, port, master, run=True):
#         # self.proc = None
#         self.devfile = "/proc/net/dev"
#         self.iface = "eth0"
#         super(Agent_Bandwidth, self,).__init__(name, host, port, master, run)
#         self.backend = self.desc()
# 
#     def launch(self):
#         """Send _recv_bytes_ and _trans_bytes_ back to the master every
#         second
#         """
# 
#         import time
# 
#         while not self.quitting:
#             infos = self._get_ifaces()
#             tm = time.time()
# 
#             r = infos[self.iface]['recv_bytes']
#             # self.sendRemote(self.master, "alert|%s>recv_bytes#%s#%s" %
#             # (self.name, tm, r), needack=False)
#             self.sendAlert("recv_bytes#%s#%s" % (tm, r))
# 
#             t = infos[self.iface]['trans_bytes']
#             # self.sendRemote(self.master, "alert|%s>trans_bytes#%s#%s" %
#             # (self.name, tm, t), needack=False)
#             self.sendAlert("trans_bytes#%s#%s" % (tm, t))
# 
#             time.sleep(1)
# 
#     def _get_ifaces(self):
#         """Function parsing the /proc/net/dev file and feeding a table
# 
#         :return: The table mapping the device file
#         :rtype: list
#         """
# 
#         lines = open(self.devfile, "r").readlines()
# 
#         columnLine = lines[1]
#         _, receiveCols, transmitCols = columnLine.split("|")
#         receiveCols = map(lambda a: "recv_" + a, receiveCols.split())
#         transmitCols = map(lambda a: "trans_" + a, transmitCols.split())
# 
#         cols = receiveCols + transmitCols
# 
#         faces = {}
#         for line in lines[2:]:
#             if line.find(":") < 0:
#                 continue
#             face, data = line.split(":")
#             faceData = dict(zip(cols, data.split()))
#             faces[face.strip()] = faceData
# 
#         return faces
# 
#     def get_mac(self):
#         """Grab the mac address of the class defined _self.iface_
# 
#         :return: The string containing the mac address, colon separated
#         :rtype: str
#         """
# 
#         return self._get_mac(self.iface)
# 
#     def _get_mac(self, ifname):
#         """Send an ioctl to recover the mac address of a specific interface
# 
#         :return: The string containing the mac address, colon separated
#         :rtype: str
#         """
# 
#         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#         info = fcntl.ioctl(
#             s.fileno(),
#             0x8927,
#             struct.pack(
#                 '256s',
#                 ifname[
#                     :15]))
#         return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]


# Path: tests/test_agent_bandwidth.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_bandwidth import Agent_Bandwidth


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


TEST_STRING = "hello"


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

        self.request.close()