def replyProcess(codi, data): global state, socudp, rndnum # tractem la resposta rebuda per part del servidor en funcio del tipus # de paquet que haguem rebut if codi == "": print time.strftime("%X") + " No s'ha pogut contactar amb el servidor" state = utilities.closeConnection(socudp, state) elif codi == cons.SUBS_ACK: # en cas de que el registre sigui correcte ens disposem a distribuir el # treball de mantenir la comunicacio i la recepcio de comandes time.sleep(1) sendSubsInfo() elif codi == cons.SUBS_NACK: print time.strftime("%X") + " Denegacio de Registre" state = utilities.actState("NOT_SUBSCRIBED") state = utilities.closeConnection(socudp, state) elif codi == cons.SUBS_REJ: print time.strftime("%X") + " " + data state = utilities.actState("NOT_SUBSCRIBED") state = utilities.closeConnection(socudp, state) time.sleep(2) register() elif codi == cons.INFO_ACK: print time.strftime("%X") + " Rebut INFO_ACK" distributeWork(codi) else: print time.strftime("%X") + " Estat Desconegut" state = utilities.closeConnection(socudp, state)
def sendSubsInfo(): global state elements = (localTCP, ';'.join(elemntslst)) data = ','.join(elements) comPDU = utilities.definePDU(options.verbose, cons.PDU_FORM, cons.SUBS_INFO, mac, rndnum, data) socudp.sendto(comPDU, (server, int(srvUDP))) state = utilities.actState("WAIT_ACK_INFO") reply = struct.unpack(cons.PDU_FORM, socudp.recvfrom(recPort)[0]) replyProcess(reply)
def regTry(regPDU, t): global socudp, state, options, server, srvUDP, recPort # s'intenta fer un registre recPort = socudp.getsockname()[1] # enviem la nostra PDU i establim un temps per esperar la resposta utilities.debugMode("Enviant packet de registre", options.verbose) socudp.sendto(regPDU, (server, int(srvUDP))) socudp.settimeout(t) state = utilities.actState("WAIT_ACK_SUBS") # retornem la resposta si l'hem rebut, en cas contrari es llanca una # exepcio que sera agafada per la funcio "registerloop" unpacked = struct.unpack(cons.PDU_FORM, socudp.recvfrom(recPort)[0]) print unpacked return unpacked
def main(): global state state = utilities.actState("NOT_SUBSCRIBED") register()