# SVN changeset patch # User imanay # Date 2012-11-28 22:30:55.228044 # Revision 47 Index: trunk/absroot/source/abspy/abscontrol/client.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/client.py b/trunk/absroot/source/abspy/abscontrol/client.py --- a/trunk/absroot/source/abspy/abscontrol/client.py (revision 46) +++ b/trunk/absroot/source/abspy/abscontrol/client.py (revision 47) @@ -31,15 +31,18 @@ FileStr = "".join(FileList) data = FileStr - self.commObj.sendTxRxCommand(cmd="SNDF", data=data) + self.commObj.sendData(cmd="SNDF", data=data) + self.commObj.waitData() def changeBeam(self, newBeam): - self.commObj.sendTxRxCommand(cmd="CHGB", data=newBeam) + self.commObj.sendData(cmd="CHGB", data=newBeam) + self.commObj.waitData() def getStatus(self, data): - self.commObj.sendTxRxCommand(cmd="ANST", data = data) + self.commObj.sendData(cmd="ANST", data = data) + self.commObj.waitData2() ########## Index: trunk/absroot/source/abspy/abscontrol/experimento1.abs =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/experimento1.abs b/trunk/absroot/source/abspy/abscontrol/experimento1.abs --- a/trunk/absroot/source/abspy/abscontrol/experimento1.abs (revision 46) +++ b/trunk/absroot/source/abspy/abscontrol/experimento1.abs (revision 47) @@ -7,29 +7,29 @@ [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0],$ [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5],$ [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5],$ + [1.0,1.0,1.0,1.0,1.0,2.0,3.0,0.0],$ + [0.5,0.5,0.5,1.0,2.0,3.0,0.0,1.0],$ + [1.0,1.0,1.0,1.0,3.0,0.0,1.0,2.0],$ + [0.5,0.5,0.5,0.5,0.0,1.0,2.0,3.0]] + + [[0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5],$ + [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0],$ + [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5],$ + [0.5,0.5,0.5,0.5,0.5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5,0.5,0.5,0.5,0.5,0.5]] + [1.0,1.0,1.0,1.0,0.5,1.5,2.5,3.5],$ + [0.5,0.5,0.5,1.0,1.5,2.5,3.5,0.5],$ + [1.0,1.0,1.0,1.0,2.5,3.5,0.5,1.5],$ + [0.5,0.5,0.5,0.5,3.5,0.5,1.5,2.5]] [[0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5],$ [1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0],$ Index: trunk/absroot/source/abspy/abscontrol/library.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/library.py b/trunk/absroot/source/abspy/abscontrol/library.py --- a/trunk/absroot/source/abspy/abscontrol/library.py (revision 46) +++ b/trunk/absroot/source/abspy/abscontrol/library.py (revision 47) @@ -1,84 +1,5 @@ #import tftpy import socket - -class UDPServer: - - buf = 16384 - #buf = 1024 - - def __init__(self, port): - # Set the socket parameters - host = "192.168.1.255" - #host = "localhost" - self.addr = (host,port) - self.createObjects() - - def createObjects(self): - - # Create socket and bind to address - self.sock_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) - self.sock_s.bind(self.addr) - - def listen(self,mode): - - # Receive messages - print "Central Control initialized ...with UDP" - print - if mode == 0: - while 1: - data_rx, add = self.sock_s.recvfrom(self.buf) - if not data_rx: - print "Client has exited!" - break - - print "\nCentral Control:" - print "Received message '", data_rx,"'" - #print "Sendinf ack ..." - #self.sock_s.sendto('ack', addr_rx) - else: - data_rx, add = self.sock_s.recvfrom(self.buf) - print "\nCentral Control:" - print "Received message '", data_rx,"'" - - # Close socket - self.sock_s.close() - print "\nsocket closed" - return data_rx - -class UDPClient: - - buf = 1024 - - def __init__(self, port): - # Set the socket parameters - host = "192.168.1.255" - self.multicast_addr = (host,port) - self.createObjects() - - def createObjects(self): - - # Create socket - self.socket_c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,0) - #if self.socket_c == -1: -# print "No se pudo establecer conexion con el socket" - - #self.socket_c.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20) - self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) - - - - def sendData(self,TxFrame): - - # Send messages - if(self.socket_c.sendto(TxFrame, self.multicast_addr)): - print "Sending message:[" + TxFrame + "]" - - data_rx, server_rx = self.socket_c.recvfrom(16) - - print "Data received ", data_rx, server_rx - - # Close socket - self.socket_c.close() class UDPComm: @@ -90,6 +11,7 @@ self.ipDestino = ipDestino self.portDestino = portDestino self.addr = (ipDestino,portDestino) + self.answer = ipDestino #test self.openSocket(asServer) @@ -113,55 +35,47 @@ self.socket_c.bind(self.addr) print "\nServer initialized" - def waitRequest(self, nbytes): + def waitData(self, nbytes = 16384): - trama_rx, add = self.socket_c.recvfrom(nbytes) - print "\nServer has received a data" + print "\nWaiting some data" + trama_rx, self.answer = self.socket_c.recvfrom(nbytes) + print "\nThis socket has received some data" ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) return ipSource, ipDestino, cmd, data - - def sendData(self,TxFrame): + + def waitData2(self, nbytes = 16384): - # Send messages - if(self.socket_c.sendto(TxFrame, self.addr)): - print "Sending message:[" + TxFrame + "]" - -# data_rx, server_rx = self.socket_c.recvfrom(16) -# -# print "Data received ", data_rx, server_rx + print "\nWaiting some data" + trama_rx, self.answer = self.socket_c.recvfrom(nbytes) + print "\nThis socket has received some data" + ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) + filename = "report.txt" + fobj = open(filename,"w") + fobj.writelines(data) + fobj.close() - # Close socket - #self.socket_c.close() + return ipSource, ipDestino, cmd, data - def sendTxRxCommand(self, cmd, data, nbytes = 16384): - - self.sendRequest(cmd, data) - - #time.sleep(1) - - # ipSource_rx, ipDestino_rx, cmd_rx, data_rx = self.getRpta(nbytes) - - # if not(self.ipSource == ipDestino_rx): - # print "Error" - - # return data_rx - - def sendRequest(self, cmd, data): + def sendData(self, cmd, data): trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" - #self.socket_c.send(trama_tx) # Send messages if(self.socket_c.sendto(trama_tx, self.addr)): print "Sending message:[" + trama_tx + "]" + + def sendData2(self, cmd, data): + trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" + # Send messages + if(self.socket_c.sendto(trama_tx, self.answer)): + print "Sending message:[" + trama_tx + "]" + + def sendData2ControlModules(self, trama_tx): - def getRpta(self, nbytes): - - trama_rx = self.socket_c.recv(nbytes) - ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) - - return ipSource, ipDestino, cmd, data + # Send messages + if(self.socket_c.sendto(trama_tx, self.addr)): + print "Sending message:[" + trama_tx + "]" def __getTrama(self, trama): Index: trunk/absroot/source/abspy/abscontrol/server.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/server.py b/trunk/absroot/source/abspy/abscontrol/server.py --- a/trunk/absroot/source/abspy/abscontrol/server.py (revision 46) +++ b/trunk/absroot/source/abspy/abscontrol/server.py (revision 47) @@ -14,6 +14,8 @@ self.portDestino2 = portDestino2 self.ftpPortDestino = ftpPortDestino + self.experiment_name = "default" + self.tx_buffer = "default" self.createObjects() @@ -23,74 +25,16 @@ self.commServerObj = library.UDPComm(self.ipSource, self.ipDestino, self.portDestino, asServer) self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2) #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino) - - def waitRequest(self,nbytes = 16384): - - ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitRequest(nbytes) - - if cmd == "SNDF": - self.sendFile(10) - - if cmd == "CHGB": - self.changeBeam() - - if cmd == "ANST": - self.getStatus(10) - - def sendAnswer(self,nbytes = 16384): - -# ipSource, ipDestino, cmd, self.datarx = self.commServerObj.sendData(nbytes) - - pass - - def EliminaSaltoDeLinea(cadena): - i = 0 - for elemento in cadena: - if elemento =='\n' or elemento =='\r': - pass - else: - i=i+1 - return cadena [:i] - - def connection_status(self,base): - #File creation is neccesary? - number_of_modules = 16 - #base = 10 - # Variables for the report - status_array = ["Status of modules\n"] - status_array.append("----------------\n") - # Variables for the program - module_list = [] - - for i in range(number_of_modules): - cmd = "ping -c 1 -w 1 192.168.1."+ str(base + i + 1) + " >> /dev/null" - status = os.system(cmd) - if status == 256: - status_array.append("192.168.1." + str(base + i + 1) + " [0 0]\n") #Un-connected - module_list.append("0") - else: - status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n") - module_list.append("1") - # Generating a file report - if base == 10: - fobj = open("module_status_S.txt","w") - else: - fobj = open("module_status_N.txt","w") - fobj.writelines(status_array) - fobj.close() - # For the program - return module_list - + def sendFile(self, base): - #Needed for the file creation -# file_str = self.datarx #Needed for the loop rx_frame_list = self.datarx.split('\n',2) experiment_name = rx_frame_list[0] experiment_number = rx_frame_list[1] str_control_modules = rx_frame_list[2] + lst_control_modules = str_control_modules.split("------\n") #Setting variables of the loop i =3 @@ -117,23 +61,148 @@ i = i + number_of_beams + 2 module += 1 #Working with the UDP socket - #self.commClientObj.sendData("CARGA:experimento1.ab1:") - self.commClientObj.sendData("CARGA:" + experiment_name + ":") - self.commClientObj.sendData("CAMBIA:0:") -# +# self.commClientObj.sendData2ControlModules("CARGA:" + experiment_name + ":") +# self.commClientObj.sendData2ControlModules("CAMBIA:0:") + #Another tftp way # self.ftpCommObj.sendFile(filename) -# rpta = self.commClientObj.sendTxRxCommand(cmd='CARGA', data=filename) - - +# rpta = self.commClientObj.sendTxRxCommand(cmd# filename2 = "report.txt" + #fobj2 = open(filename2,"w") + #fobj2.write("Verification_file\n") + #fobj2.write("-----------------\n") + #fobj2.close()='CARGA', data=filename) + self.experiment_name = experiment_name + + + def __sendAnswer(self, cmd): + + data = "OK" + + if cmd == "SNDF": + self.commServerObj.sendData2(cmd="SNDF", data=data) + if cmd == "CHGB": + self.commServerObj.sendData2(cmd="CHGB", data=data) + if cmd == "ANST": + data = self.tx_buffer + self.commServerObj.sendData2(cmd="ANST", data=data) + + def waitRequest(self): + + ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData() + + datarpta = "OK" + + if cmd == "SNDF": + self.sendFile2Modules() + + if cmd == "CHGB": + self.changeBeam() + + if cmd == "ANST": + self.getStatus(10, mode=2) + datarpta = self.tx_buffer + + self.commServerObj.sendData2(cmd=cmd, data=datarpta) + + return cmd + + def __writeReport(self, enaModules): + + status_array = ["Status of modules\n"] + status_array.append("----------------\n") + + for address in range(1,65): + if address in enaModules: +# status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n") + status_array.append("192.168.1." + str(address) + " [1 1]\n") + else: + status_array.append("192.168.1." + str(address) + " [0 0]\n") + + f = open("module_status.txt","w") + f.writelines(status_array) + f.close() + + def checkModule(self, address): + + cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null" + status = os.system(cmd) + + if status == 256: + return False + + return True + + def checkAntenna(self): + + """ + Direccion de los modulos de las antenas: + + Norte : 01-16 + Este : 17-32 + Oeste: : 33-48 + Sur : 49-64 + + """ + + enaModules = [] + + for address in range(1,65): + if self.checkModule(address): + enaModules.append(address) + + self.__writeReport(enaModules) + return enaModules + + def __loadFile(self): + + #Working with the UDP socket + self.commClientObj.sendData2ControlModules("CARGA:" + self.experiment_name + ":") + self.commClientObj.sendData2ControlModules("CAMBIA:0:") + + def __writeModuleFile(self, filename, str): + + fobj = open(filename,"w") + fobj.write(filename + "\n") + fobj.write("------\n") + fobj.write(str) + fobj.write("------\n") + fobj.close() + + def sendFile2Modules(self): + + #Needed for the loop + rx_frame_list = self.datarx.split('\n',2) + + experiment_name = rx_frame_list[0] + experiment_number = rx_frame_list[1] + str_control_modules = rx_frame_list[2] + + lst_control_modules = str_control_modules.split("------\n") + + enaModules = self.checkAntenna() + + for address in range(1,65): + + if address not in enaModules: + continue + + self.__writeModuleFile(experiment_name, lst_control_modules[address-1]) + + cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + experiment_name + print cmd + os.system(cmd) + + self.experiment_name = experiment_name + + self.__loadFile() + def changeBeam(self): #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0") - self.commClientObj.sendData("CAMBIA:" + self.datarx + ":") - - def getStatus(self, base): - + self.commClientObj.sendData2ControlModules("CAMBIA:" + self.datarx + ":") + + def __getStsMode1(self): #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") - self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":") + self.commClientObj.sendData2ControlModules("CHEQUEO:" + self.datarx + ":") seconds = int (self.datarx) # Give 5 seconds to the control modules time.sleep(seconds) @@ -169,15 +238,99 @@ fobj2.write("------\n") fobj2.close() module = module + 1 -# print "\nFinalizado" + + def __getStsMode2(self): + + #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") + self.commClientObj.sendData2ControlModules("CHEQUEO:" + self.datarx + ":") + seconds = int (self.datarx) + # Give 5 seconds to the control modules + time.sleep(seconds) + # Checking the module connection + module_list = self.connection_status(10) + #Generating the complete report + module = 1 + number_of_modules = 16 + filename1 = "Verificacion" + line1 = "Verification_file\n" + line2 = "-----------------\n" + report_list = [line1, line2] + while module <= number_of_modules: + if module_list[module -1] == "1": + #Preparing and doing the tftp command + cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1 + print cmd + os.system(cmd) + #Sub_header + if base == 10: + report_list.append("S" + str(module) + "\n") + else: + report_list.append("N" + str(module) + "\n") + # Content + fobj1 = open(filename1,"r") + file_list_1 = fobj1.readlines() + fobj1.close() + content = ''.join(file_list_1[2:-1]) +# content = "010101\n" + report_list.append(content) + #Ending + report_list.append("------\n") + module = module + 1 + #print "\nFinalizado" + self.tx_buffer = ''.join(report_list) + + def __getStsMode3(self): + + #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") + self.commClientObj.sendData2ControlModules("CHEQUEO:" + self.datarx + ":") + seconds = int (self.datarx) + # Give 5 seconds to the control modules + time.sleep(seconds) + # Checking the module connection + module_list = self.connection_status(10) + #Generating the complete report + module = 1 + number_of_modules = 16 + filename1 = "Verificacion" + line1 = "Verification_file\n" + line2 = "-----------------\n" + report_list = [line1, line2] + while module <= number_of_modules: + if module_list[module -1] == "1": + #Preparing and doing the tftp command + cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1 + print cmd + os.system(cmd) + #Sub_header + if base == 10: + report_list.append("S" + str(module) + "\n") + else: + report_list.append("N" + str(module) + "\n") + # Content + fobj1 = open(filename1,"r") + file_list_1 = fobj1.readlines() + fobj1.close() + content = ''.join(file_list_1[2:-1]) +# content = "010101\n" + report_list.append(content) + #Ending + report_list.append("------\n") + module = module + 1 + #print "\nFinalizado" + self.tx_buffer = ''.join(report_list) + + def getStatus(self, base, mode): + + if mode == 1: + self.__getStsMode1() + else: + self.__getStsMode2() - - if __name__ == '__main__': absObj = ABSServer() while 1: - absObj.waitRequest() -# absObj.sendAnswer(nbytes) \ No newline at end of file + cmd = absObj.waitRequest() + absObj.__sendAnswer(cmd) \ No newline at end of file Index: trunk/absroot/source/abspy/scripts/sendFile.py =================================================================== diff --git a/trunk/absroot/source/abspy/scripts/sendFile.py b/trunk/absroot/source/abspy/scripts/sendFile.py --- a/trunk/absroot/source/abspy/scripts/sendFile.py (revision 46) +++ b/trunk/absroot/source/abspy/scripts/sendFile.py (revision 47) @@ -0,0 +1,6 @@ +import abscontrol/client + +filename = "experimento1.abs" + +absObj = ABSClient() +absObj.sendFile(filename) \ No newline at end of file