def __init__(self): self.pathstorage = Pathstorage() self.obstacle_map = [[]] self.fifo = Fifo(self) self.islands = Islands() #TODO: searching Islands! self.jobs = [] # contains all pathfindingjobs
class TestFifo(unittest.TestCase): def setUp(self): self.policity = Fifo() self.aPCB = PCB("Program1") self.bPCB = PCB("Program2", 20) self.cPCB = PCB("Program2", 50) self.dPCB = PCB("Program2", 15) def tearDown(self): pass def testBuilder(self): self.assertEquals(len(self.policity.qReady), 0) self.assertFalse(self.policity.isRR()) def testAddAndNext(self): self.policity.add(self.aPCB) self.policity.add(self.bPCB) self.policity.add(self.cPCB) self.policity.add(self.dPCB) self.assertEquals(self.policity.qReady[0], self.aPCB) self.assertEquals(self.policity.qReady[1], self.bPCB) self.assertEquals(self.policity.qReady[2], self.cPCB) self.assertEquals(self.policity.qReady[3], self.dPCB) self.assertEquals(self.policity.next(), self.aPCB) self.assertEquals(self.policity.next(), self.bPCB) def testRetryAdd(self): self.policity.retryAdd(self.aPCB) self.policity.retryAdd(self.bPCB) self.assertEquals(self.policity.qReady[0].priority, 10) self.assertEquals(self.policity.qReady[1].priority, 20)
class TestFifo(unittest.TestCase): def setUp(self): self.policity = Fifo() self.aPCB = PCB("Program1") self.bPCB = PCB("Program2", 20) self.cPCB = PCB("Program2", 50) self.dPCB = PCB("Program2", 15) def tearDown(self): pass def testBuilder(self): self.assertEquals(len(self.policity.qReady),0) self.assertFalse(self.policity.isRR()) def testAddAndNext(self): self.policity.add(self.aPCB) self.policity.add(self.bPCB) self.policity.add(self.cPCB) self.policity.add(self.dPCB) self.assertEquals(self.policity.qReady[0], self.aPCB) self.assertEquals(self.policity.qReady[1], self.bPCB) self.assertEquals(self.policity.qReady[2], self.cPCB) self.assertEquals(self.policity.qReady[3], self.dPCB) self.assertEquals(self.policity.next(), self.aPCB) self.assertEquals(self.policity.next(), self.bPCB) def testRetryAdd(self): self.policity.retryAdd(self.aPCB) self.policity.retryAdd(self.bPCB) self.assertEquals(self.policity.qReady[0].priority, 10) self.assertEquals(self.policity.qReady[1].priority, 20)
def __init__(self): self.pathstorage = Pathstorage() self.obstacle_map = [[]] self.fifo = Fifo(self) self.islands = Islands() self.jobs = {} # contains all pathfindingjobs self.job_ID = 0 # the ID for a pathfindingjob - must be unique
def graphConstructor(wikiInput, stop): fifo = Fifo.Fifo() hashT = hashingTable.UrlDistinctTester() depthC = depthController.DepthController(stop) fifo.addElement(wikiInput) # initialization of the fifo G = nx.DiGraph() # creating direct graph while not (fifo.isFifoEmpty()): # handling fifo current_url = fifo.removeFirstIn() # extracting elt to use depthC.elementsTreated() links_current_url = WikiPagesReader.wikiPagesFinder(current_url) # computing all mentionned pages if depthC.depthControl() == 1: # removing all url already in fifo links_current_distinct_url = [] for url in links_current_url: if hashT.add(url): # return True if url not in hashing table links_current_distinct_url.append(url) # feeding the fifo fifo.addList(links_current_distinct_url) depthC.addElementsToTreat(links_current_distinct_url) # feeding the graph G.add_node(current_url) G.add_nodes_from(links_current_distinct_url) # adding edge from current_url to elt (because elt is mentionned in current_url) for elt in links_current_url: G.add_edge(current_url, elt) # when going through last layer's nodes elif depthC.depthControl() == 2: edges_to_add = [elt for elt in links_current_url if hashT.checkInTable(elt)] for elt in edges_to_add: G.add_edge(current_url, elt) # adding edge from current_url to elt (because elt is mentionned in current_url depthC.cursorUpdate() nx.write_gml(G, "network_depth_2.gml") # saving the graph into a .gml file
def setUp(self): self.policity = Fifo() self.aPCB = PCB("Program1") self.bPCB = PCB("Program2", 20) self.cPCB = PCB("Program2", 50) self.dPCB = PCB("Program2", 15)
##Testing Fifo class import sys sys.path.append('..') import Fifo fifo = Fifo.Fifo() fifo.addList([1, 2, 3]) print(fifo.getList()) fifo.removeFirstIn() print(fifo.getList()) fifo.addElement(4) print(fifo.getList()) while not (fifo.isFifoEmpty()): x = fifo.removeFirstIn() if x == 4: fifo.addElement(5) print(x) #return: # [1, 2, 3] # [2, 3] # [2, 3, 4] # 2 # 3 # 4 # 5
import audioop import threading import time import Fifo import config _received_queue = Fifo.Fifo() _sending_queue = Fifo.Fifo() silence = chr(0) * 1024 * 4 _MAX_BUFFER_SIZE = int(config.RATE / 10) _DELAY_RATE = 1 / config.RATE def receive_audio(audio_stream, socket_connection, chunk=config.CHUNK): t1 = threading.Thread(target=play_audio, args=(audio_stream, )) t1.start() while True: _received_queue.put((socket_connection.recv(chunk))) if len(_received_queue) > _MAX_BUFFER_SIZE: _received_queue.get(len(_received_queue) - _MAX_BUFFER_SIZE) def send_audio( audio_stream, socket_connection, node, chunk=config.CHUNK, ): t1 = threading.Thread(target=record_audio, args=(audio_stream, )) t1.start()
class Pathmanager: """ Organizes the pathfinding. Can start serveral threads, realizes the two-tiered pathfinding, cares about the obstaclemap ... There is one Pathmanager per match. He cares about all pathrequest from all players. """ # TODO: ObstacleMap def __init__(self): self.pathstorage = Pathstorage() self.obstacle_map = [[]] self.fifo = Fifo(self) self.islands = Islands() #TODO: searching Islands! self.jobs = [] # contains all pathfindingjobs def addJob(self, reference, start, end): """ @param reference: the reference of the instance of the settler who needs the path @param start: the startnode @param end: the endnode """ self.jobs.append(threading.Thread(target = self.findPath, args=(reference,start,end))) self.jobs[len(self.jobs)-1].start() def returnJob(self): """ Returns a path to the unit. Called by the FIFO. """ while not self.fifo.isEmpty(): job = self.fifo.pop() #print job reference = job.keys() reference.callbackPath(job[reference]) #pass def findPath(self, reference, start, end): """ Start computing the Path. @param reference: the reference of the settler who needs the path @param start: the startnode @param end: the endnode """ if self.islands.sameIsland(start, end): pf = Pathfinder(self.pathstorage) lock = threading.Lock() # computing the (macro)path: path = pf.aStar(start, end, None) # obstaclemap is None for testing # TODO: if both nodes are at the same island, there should be a path every time # if a macropath was computed: if abs(path[0][0] - path[1][0]) > 1 or abs(path[0][1] - path[1][1]) > 1: macro_path = path last_index = len(path) - 1 #path = [] i = 0 # only every second node of the macro path is used for node in macro_path: if i%2 == 0 and i+2 < last_index: temp_path = pf.aStar(node, macro_path[i+2], None) lock.acquire() # because only one thread should access the one FIFO self.fifo.add(reference, temp_path) lock.release() #print temp_path #for item in temp_path: # path.append(item) time.sleep(2) elif i%2 == 0: # endnode is closer than one macrostep temp_path = pf.aStar(node, end, None) lock.acquire() self.fifo.add(reference, temp_path) lock.release() #print temp_path #for item in temp_path: # path.append(item) break i = i + 1 else: # its a short path (no macropath needed) lock.acquire() self.fifo.add(reference, path) lock.release() else: # start- and endpoint not at the same island - no path can be computed lock.acquire() self.fifo.add(reference, None) lock.release() #print path def findIslands(self): """ Let the class Islands find and store islands. """ #self.updateObstaclemap() self.islands.searchIslands(self.obstacle_map) def updateObstaclemap(self): """ Be up to date. Checks every node wheather it is blocked (permanently, not by units) or not. """ pass
import Arbiter from Arbiter import * import Requestor from Requestor import * import Request from Request import * import json requestor_cfg_fname = 'requestorConfig.txt' requestor_0 = Requestor(requestor_cfg_fname) requestor_1 = Requestor(requestor_cfg_fname) requestor_2 = Requestor(requestor_cfg_fname) requestor_3 = Requestor(requestor_cfg_fname) fifo_0 = Fifo(depth=8) fifo_1 = Fifo(depth=8) fifo_2 = Fifo(depth=8) fifo_3 = Fifo(depth=8) pipe_0 = Pipe(num_stages=8) pipe_1 = Pipe(num_stages=4) pipe_2 = Pipe(num_stages=2) pipe_3 = Pipe(num_stages=12) arb_0 = Arbiter(num_ports=4) all_requestors_done = False clk_cnt = 0 def inc_clk():
class Pathmanager: """ Organizes the pathfinding. Can start serveral threads, realizes the two-tiered pathfinding, cares about the obstaclemap ... There is one Pathmanager per match. He cares about all pathrequest from all players. """ # TODO: ObstacleMap def __init__(self): self.pathstorage = Pathstorage() self.obstacle_map = [[]] self.fifo = Fifo(self) self.islands = Islands() #TODO: searching Islands! self.jobs = [] # contains all pathfindingjobs def addJob(self, reference, start, end): """ @param reference: the reference of the instance of the settler who needs the path @param start: the startnode @param end: the endnode """ self.jobs.append( threading.Thread(target=self.findPath, args=(reference, start, end))) self.jobs[len(self.jobs) - 1].start() def returnJob(self): """ Returns a path to the unit. Called by the FIFO. """ while not self.fifo.isEmpty(): job = self.fifo.pop() #print job reference = job.keys() reference.callbackPath(job[reference]) #pass def findPath(self, reference, start, end): """ Start computing the Path. @param reference: the reference of the settler who needs the path @param start: the startnode @param end: the endnode """ if self.islands.sameIsland(start, end): pf = Pathfinder(self.pathstorage) lock = threading.Lock() # computing the (macro)path: path = pf.aStar(start, end, None) # obstaclemap is None for testing # TODO: if both nodes are at the same island, there should be a path every time # if a macropath was computed: if abs(path[0][0] - path[1][0]) > 1 or abs(path[0][1] - path[1][1]) > 1: macro_path = path last_index = len(path) - 1 #path = [] i = 0 # only every second node of the macro path is used for node in macro_path: if i % 2 == 0 and i + 2 < last_index: temp_path = pf.aStar(node, macro_path[i + 2], None) lock.acquire( ) # because only one thread should access the one FIFO self.fifo.add(reference, temp_path) lock.release() #print temp_path #for item in temp_path: # path.append(item) time.sleep(2) elif i % 2 == 0: # endnode is closer than one macrostep temp_path = pf.aStar(node, end, None) lock.acquire() self.fifo.add(reference, temp_path) lock.release() #print temp_path #for item in temp_path: # path.append(item) break i = i + 1 else: # its a short path (no macropath needed) lock.acquire() self.fifo.add(reference, path) lock.release() else: # start- and endpoint not at the same island - no path can be computed lock.acquire() self.fifo.add(reference, None) lock.release() #print path def findIslands(self): """ Let the class Islands find and store islands. """ #self.updateObstaclemap() self.islands.searchIslands(self.obstacle_map) def updateObstaclemap(self): """ Be up to date. Checks every node wheather it is blocked (permanently, not by units) or not. """ pass