Exemplo n.º 1
0
def processWork(i, bufferHead, sleepQueue, lock):

    startStr = "Process {} :".format(os.getpid())
    sys.stdout = open("file" + str(i), 'w')
    print(startStr, "is running...")

    blockNo = random.randint(1, 32)
    print(startStr, "Wants blockNo", blockNo)

    lockedBuffer = BufferManagement.getBlk(blockNo, bufferHead, sleepQueue,
                                           lock)
    lockedBuffer.setValidStatus(True)

    print(startStr, "Acquired blockNo", lockedBuffer.getBlockNum())
    performOperation(blockNo, bufferHead, lock)

    lock.acquire()
    bufferHead.printHashQ()
    bufferHead.printFreeList()
    sleepQueue.printSQ()
    lock.release()

    time.sleep(5)

    BufferRelease.brelse(lockedBuffer, bufferHead, lock, sleepQueue)

    print(startStr, 'Finished execution...')
    sys.stdout.close()
    os.system(
        "gnome-terminal -e 'bash -c \"cat file{}; exec bash\"'".format(i))
    os.remove("file" + str(i))
Exemplo n.º 2
0
def processWork(bufferHead, sleepQueue, lock):
    '''
    Target function for each spawned process.\n
    Calls getBlk for a random Block Number.\n
    Performs some work on the acquired block.\n
    Then releases the block using brelse.
    '''

    startStr = "Process {} :".format(os.getpid())
    print(startStr, "is running...")

    blockNo = random.randint(1, 32)
    print(startStr, "Wants blockNo", blockNo)

    lockedBuffer = BufferManagement.getBlk(blockNo, bufferHead, sleepQueue,
                                           lock)
    lockedBuffer.setValidStatus(True)

    print(startStr, "Acquired blockNo", lockedBuffer.getBlockNum())
    performOperation(blockNo, bufferHead, lock)

    lock.acquire()
    bufferHead.printHashQ()
    bufferHead.printFreeList()
    sleepQueue.printSQ()
    lock.release()

    time.sleep(5)

    BufferRelease.brelse(lockedBuffer, bufferHead, lock, sleepQueue)

    print(startStr, 'Finished execution...')
Exemplo n.º 3
0
def listen(livingPlayers):
    """Either:
    - Repeatedly check for change in DB (If API affects DB)
    - Listen to the Sockets """
    for player in livingPlayers:
        messages = BufferManagement.empty_buffer(player['UserID'],
                                                 player['GameID'], "IN")
        if messages != []:
            return messages, player
Exemplo n.º 4
0
 def post(self,UserID,GameID):
     incoming = json.loads(request.data)
     PlayerDetails = incoming
     if not os.path.isfile("IN"+GameID+'/'+ UserID +".pkl"):
         BufferManagement.create_buffer(UserID,GameID,"IN")
     if not os.path.isfile("OUT"+GameID+'/'+ UserID +".pkl"):
         BufferManagement.create_buffer(UserID,GameID,"OUT")
     BufferManagement.append_to_buffer(PlayerDetails,UserID,GameID,"IN")
Exemplo n.º 5
0
def SetupGame(
        gameID, timeout, amount_players,
        setuptimeout):  #Getting FREAKY with yo double variable names brah
    """Gathers all mah lovely players and return a beautifull list
    That list contains a bunch of dictionaries containg all the data you could ever want as a whopping DICT
    Needs to:
    - Create Buffer File"""
    GameInfo = {'StartTime': time.time(), 'timeout': timeout, 'ID': gameID}
    livingPlayers = []
    try:
        os.makedirs("IN" + str(gameID))
    except:
        print("Directory", "IN" + str(gameID), "already exists")

    try:
        os.makedirs("OUT" + str(gameID))
    except:
        print("Directory", "OUT" + str(gameID), "already exists")

    joins = []
    SUtimeout = False
    SUstartTime = time.time()
    while len(joins) != amount_players or not SUtimeout:
        joins = os.listdir("IN" + str(gameID) + "/")
        if len(joins) == amount_players:
            time.sleep(0.5)
            for path in joins:
                message = BufferManagement.empty_buffer(
                    str(os.path.splitext(path)[0]), str(gameID), "IN")
                print("message:", message)
                livingPlayers.append(message[0])
            random.shuffle(livingPlayers)
            print("libingplayers:", livingPlayers)
            for i in range(len(livingPlayers)):
                livingPlayers[i]['target'] = livingPlayers[
                    (i + 1) % len(livingPlayers)]['ID']
                print(livingPlayers[i]['target'])
            return True, livingPlayers, GameInfo
        if time.time() - SUstartTime > setuptimeout:
            SUtimeout = False
    return False [0]
def process(sleepQueue, bufferDataStructure, lock, maxNoOfBlocks):

    i = 0
    while (i < noOfBufferRequestsByEachProcess):
        time.sleep(
            2)  #process will request a random block after every 2 second
        requestedBlock = random.randint(0, maxNoOfBlocks - 1)
        print(
            "\n---------------------------------------------------------\nProcess ",
            os.getpid(), " has requested block number ", requestedBlock,
            "\n---------------------------------------------------------\n")
        recievedBuffer = BufferManagement.getBlock(sleepQueue, requestedBlock,
                                                   lock, bufferDataStructure)
        print("\nProcess ", os.getpid(), ": RECIEVED BUFFER ", recievedBuffer)

        print("\n", os.getpid(), " HashQ : ")
        bufferDataStructure.printHashQ()
        print("\n", os.getpid(), " FreeList :")
        bufferDataStructure.printFreeList()

        pseudoOperation(bufferDataStructure, recievedBuffer)
        pseudoBRelease(sleepQueue, bufferDataStructure, lock, recievedBuffer)
        i += 1
Exemplo n.º 7
0
def send(User, data):
    """Sends the given data to the specified user... pretty simple"""
    BufferManagement.append_to_buffer(data, User['ID'], User['GameID'], "OUT")
Exemplo n.º 8
0
 def get(self,UserID,GameID):
     commands = BufferManagement.empty_buffer(GameID,UserID,"OUT")
     return(jsonify(commands))
Exemplo n.º 9
0
 def post(self,UserID,GameID):
     incoming = json.loads(request.data)
     BufferManagement.append_to_buffer(incoming, UserID, GameID, "IN")
     pass