def sync(self): println(bp.getProtocolString(bp.SYNC)) # this should block now until we get a response line = readLine() if line != (bp.getProtocolString(bp.SYNC) + "_SUCCESS"): raise RuntimeError( "Barrier sync failed!")
def initialize(self): """ INIT protocol works as follows: START OP_CODE PROTOCOL_NUMBER SET_BSPJOB_CONF OP_CODE NUMBER OF CONF ITEMS (#KEY + #VALUES) N-LINES, where line is key and the following the value """ # parse our initial values line = readLine() # start code is the first if line == bp.getProtocolString(bp.START): # check the protocol compatibility protocolNumber = int(readLine()) if protocolNumber != self.PROTOCOL_VERSION: raise RuntimeError( "Protocol version mismatch: Expected: " + str(self.PROTOCOL_VERSION) + " but got: " + str(protocolNumber)) line = readLine() # parse the configurations if line == bp.getProtocolString(bp.SET_BSPJOB_CONF): numberOfItems = readLine() key = None value = None for i in range(0, int(numberOfItems), 2): key = readLine() value = readLine() self.config.put(key, value) self.ack(bp.START)
def initialize(self): """ INIT protocol works as follows: START OP_CODE PROTOCOL_NUMBER SET_BSPJOB_CONF OP_CODE NUMBER OF CONF ITEMS (#KEY + #VALUES) N-LINES, where line is key and the following the value """ # parse our initial values line = readLine() # start code is the first if line == bp.getProtocolString(bp.START): # check the protocol compatibility protocolNumber = int(readLine()) if protocolNumber != self.PROTOCOL_VERSION: raise RuntimeError("Protocol version mismatch: Expected: " + str(self.PROTOCOL_VERSION) + " but got: " + str(protocolNumber)) line = readLine() # parse the configurations if line == bp.getProtocolString(bp.SET_BSPJOB_CONF): numberOfItems = readLine() key = None value = None for i in range(0, int(numberOfItems), 2): key = readLine() value = readLine() self.config.put(key, value) self.ack(bp.START)
def getCurrentMessage(self): println(bp.getProtocolString(bp.GET_MSG)) line = readLine() # if no message is send it will send %%-1%% if line == "%%-1%%": return False return line;
def readNext(self): println(bp.getProtocolString(bp.READ_KEYVALUE)) line = readLine() secondLine = readLine() # if no message is send it will send %%-1%% if line == "%%-1%%" and secondLine == "%%-1%%": return False return [line, secondLine]
def getAllPeerNames(self): println(bp.getProtocolString(bp.GET_ALL_PEERNAME)) ln = readLine() names = [] for i in range(int(ln)): peerName = readLine() names.append(peerName) return names
def getCurrentMessage(self): println(bp.getProtocolString(bp.GET_MSG)) line = readLine() # if no message is send it will send %%-1%% if line == "%%-1%%": return False return line
def getCurrentMessage(self): println(bp.getProtocolString(bp.GET_MSG)) line = readLine() # if no message is send it will send %%-1%% if line == "%%-1%%": return False # TODO # Problem reported by Roman: # If I send any message of the length L, I receive the message with # additional (L-1)/2 '^@' symbols after it. # return line; return line[:len(line)-len(line)//3]
def threaded_client(conn): try: while True: data = conn.recv() data = data[0] * 256 + data[1] data = BP.decode(data) print('Received data:', data) send_command(conn, data) except RuntimeError as e: print(e) #except Exception as e: #print('Exception caught') #print(e) finally: conn.close()
def clear(self): println(bp.getProtocolString(bp.CLEAR))
def getPeerIndex(self): println(bp.getProtocolString(bp.GET_PEER_INDEX)) return readLine()
def getPeerNameForIndex(self, index): println(bp.getProtocolString(bp.GET_PEERNAME)) println(str(index)); return readLine()
def reopenInput(self): println(bp.getProtocolString(bp.REOPEN_INPUT))
def done(self): println(bp.getProtocolString(bp.TASK_DONE)) println(bp.getProtocolString(bp.DONE))
def runBSP(self): line = readLine() # start code is the first if line.startswith(bp.getProtocolString(bp.RUN_BSP)): self.bspClass.bsp(self) self.ack(bp.RUN_BSP)
def write(self, key, value): println(bp.getProtocolString(bp.WRITE_KEYVALUE)) println(key) println(value)
def runCleanup(self): line = readLine() # start code is the first if line.startswith(bp.getProtocolString(bp.RUN_CLEANUP)): self.bspClass.cleanup(self); self.ack(bp.RUN_CLEANUP)
def send(self, peer, msg): println(bp.getProtocolString(bp.SEND_MSG)) println(peer) println(msg)
def runCleanup(self): line = readLine() # start code is the first if line.startswith(bp.getProtocolString(bp.RUN_CLEANUP)): self.bspClass.cleanup(self) self.ack(bp.RUN_CLEANUP)
def log(self, msg): println(bp.getProtocolString(bp.LOG) + msg)
def getNumPeers(self): println(bp.getProtocolString(bp.GET_PEER_COUNT)) return readLine()
def runBSP(self): line = readLine() # start code is the first if line.startswith(bp.getProtocolString(bp.RUN_BSP)): self.bspClass.bsp(self); self.ack(bp.RUN_BSP)
def ack(self, code): println(bp.getAckProtocolString(code))
def getNumCurrentMessages(self): println(bp.getProtocolString(bp.GET_MSG_COUNT)) return readLine()
def sync(self): println(bp.getProtocolString(bp.SYNC)) # this should block now until we get a response line = readLine() if line != (bp.getProtocolString(bp.SYNC) + "_SUCCESS"): raise RuntimeError("Barrier sync failed!")
def getSuperstepCount(self): println(bp.getProtocolString(bp.GET_SUPERSTEP_COUNT)) return readLine()
def send_passer_name(conn, message): message = message.encode('utf-8') for i in range(len(message)): conn.send(bytes(divmod(BP.passing(message[i]), 256))) conn.send(bytes(divmod(BP.passing(0), 256)))
def getPeerNameForIndex(self, index): println(bp.getProtocolString(bp.GET_PEERNAME)) println(str(index)) return readLine()