# Path: pif_ir/bir/objects/processor.py
# class ThreadedProcessor(Thread, Processor):
#     def __init__(self, name, next_processor=None):
#         Thread.__init__(self)
#         Processor.__init__(self, name, next_processor)
#         self.running = True
# 
#     def run(self):
#         msg = "Processor {}: does not implement run()".format(self.name)
#         raise BIRError(msg)
# 
#     def kill(self):
#         self.running = False


import logging
from threading import Condition
from pif_ir.bir.objects.processor import ThreadedProcessor


class TrafficManager(ThreadedProcessor):
    job_lock = Condition()

    def __init__(self):
        super(TrafficManager, self).__init__("TrafficManager")

        self.queues = []
        for port in range(10): # 10 queues
            self.queues.append([]) 

    def process(self, packet, bit_offset=0):
        job_queue = packet.idx % len(self.queues)

        TrafficManager.job_lock.acquire()
        self.queues[job_queue].append((packet, bit_offset))
        TrafficManager.job_lock.release()

    def run(self):
        active = 0
        while self.running:
            active = (active + 1) % len(self.queues)
            if len(self.queues[active]) < 1:
                continue

            TrafficManager.job_lock.acquire()
            packet, bit_offset = self.queues[job_queue].pop(0)

# Path: pif_ir/bir/objects/processor.py
# class ThreadedProcessor(Thread, Processor):
#     def __init__(self, name, next_processor=None):
#         Thread.__init__(self)
#         Processor.__init__(self, name, next_processor)
#         self.running = True
# 
#     def run(self):
#         msg = "Processor {}: does not implement run()".format(self.name)
#         raise BIRError(msg)
# 
#     def kill(self):
#         self.running = False


# Path: pif_ir/bir/objects/other_processor/traffic_manager.py
import logging
from threading import Condition
from pif_ir.bir.objects.processor import ThreadedProcessor



class TrafficManager(ThreadedProcessor):
    job_lock = Condition()

    def __init__(self):
        super(TrafficManager, self).__init__("TrafficManager")

        self.queues = []
        for port in range(10): # 10 queues
            self.queues.append([]) 

    def process(self, packet, bit_offset=0):
        job_queue = packet.idx % len(self.queues)

        TrafficManager.job_lock.acquire()
        self.queues[job_queue].append((packet, bit_offset))
        TrafficManager.job_lock.release()

    def run(self):
        active = 0
        while self.running:
            active = (active + 1) % len(self.queues)
            if len(self.queues[active]) < 1:
                continue

            TrafficManager.job_lock.acquire()
            packet, bit_offset = self.queues[job_queue].pop(0)

            TrafficManager.job_lock.release()