Index: trunk/absroot/source/abspy/abscontrol/client2.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/client2.py b/trunk/absroot/source/abspy/abscontrol/client2.py --- a/trunk/absroot/source/abspy/abscontrol/client2.py (revision 92) +++ b/trunk/absroot/source/abspy/abscontrol/client2.py (revision 93) @@ -4,11 +4,11 @@ import time import numpy as np -import library +import library2 class ABSClient: - def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000): + def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000): self.ipSource = ipSource self.ipDestino = ipDestino @@ -18,7 +18,7 @@ def createObjects(self): - self.commObj = library.TCPComm(self.ipSource, self.ipDestino, self.portDestino) + self.commObj = library2.TCPComm(self.ipSource, self.ipDestino, self.portDestino) def sendFile(self, filename): @@ -30,15 +30,21 @@ F_Obj.close() FileStr = "".join(FileList) data = FileStr - - self.commObj.sendData(cmd="SNDF", data=data) - self.commObj.waitData() + + self.commObj.open_socket() +# self.commObj.sendData(cmd="SNDF", data=data, id = 117) + self.commObj.sendData2(cmd="SNDF", data=data, ipDestino = self.ipDestino) +# self.commObj.waitData() + self.commObj.waitClient() self.commObj.close_socket() def changeBeam(self, newBeam): - - self.commObj.sendData(cmd="CHGB", data=newBeam) - self.commObj.waitData() + + self.commObj.open_socket() +# self.commObj.sendData(cmd="CHGB", data=newBeam, id = 117) + self.commObj.sendData2(cmd="CHGB", data=newBeam, ipDestino = self.ipDestino) +# self.commObj.waitData() + self.commObj.waitClient() self.commObj.close_socket() def __writeFile(self, filename, data): @@ -48,9 +54,12 @@ fobj.close() def getStatus(self, data): - - self.commObj.sendData(cmd="ANST", data = data) - ipSource, ipDestino, cmd, data = self.commObj.waitData() + + self.commObj.open_socket() +# self.commObj.sendData(cmd="ANST", data = data, id = 117) + self.commObj.sendData2(cmd="ANST", data = data, ipDestino = self.ipDestino) +# ipSource, ipDestino, cmd, data = self.commObj.waitData() + ipSource, ipDestino, cmd, data = self.commObj.waitClient() self.commObj.close_socket() self.__writeFile("report.txt", data) @@ -479,9 +488,9 @@ filename = "experimento1.abs" absObj = ABSClient() - absObj.sendFile(filename) +# absObj.sendFile(filename) # absObj.changeBeam("0") -# absObj.changeBeam("1") + absObj.changeBeam("1") # absObj.changeBeam("2") # absObj.changeBeam("3") # absObj.changeBeam("4") 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 92) +++ b/trunk/absroot/source/abspy/abscontrol/library.py (revision 93) @@ -13,6 +13,7 @@ self.addr = (ipDestino,portDestino) self.answer = "none" #test self.mode = "none" + self.openSocket(asServer) @@ -78,7 +79,8 @@ class TCPComm: - __HEADER = "ABS" + __HEADER = "JRO" + __TYPE = "ABS" def __init__(self, ipSource, ipDestino, portDestino, asServer=False): @@ -90,30 +92,35 @@ self.sc = "none" self.answer = "none" #test self.mode = "none" + self.len = 0 + self.crc = 0 self.openSocket(asServer) def openSocket(self, asServer): #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM) - self.socket_c = socket.socket() +# self.socket_c = socket.socket() # self.socket_c.connect((self.ipDestino, self.portDestino)) if asServer: - self.configAsServer() + self.socket_c = socket.socket() +# self.configAsServer() + self.socket_c.bind(self.addr) self.mode = "server" else: - self.configAsClient() - self.mode = "client" - - def configAsClient(self): +# self.configAsClient() + self.mode = "client" #Socket is opened at the sendData function + +# def configAsClient(self): #Buscar broadcast TCP # self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) - self.socket_c.connect(self.addr) - - def configAsServer(self): - - self.socket_c.bind(self.addr) +# self.socket_c.connect(self.addr) +# pass + +# def configAsServer(self): +# +# self.socket_c.bind(self.addr) def waitData(self, nbytes = 1024): @@ -135,21 +142,22 @@ if trama_rx[-4:] == "quit": break - print "\nThis socket has received some data from:" - print self.answer + print "\nThis socket has received some data." ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) return ipSource, ipDestino, cmd, data - def sendData(self, cmd, data): - + def sendData(self, cmd, data, id): + + trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + if self.portDestino == 7000: - trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" + "quit" - else: - trama_tx = data + trama_tx = trama_tx + ":" + "quit" # Send messages if self.mode == "client": + host = "192.168.1." + str(id) + self.socket_c.connect((host, self.portDestino)) self.socket_c.send(trama_tx) else: self.sc.send(trama_tx) @@ -160,16 +168,22 @@ FrameList = trama.split(':') header = FrameList[0] - ipSource = FrameList[1] - ipDestino = FrameList[2] - cmd = FrameList[3] - data = FrameList[4] - trash = FrameList[5] + TypeOfInstrument = FrameList[1] + ipSource = FrameList[2] + ipDestino = FrameList[3] + len = FrameList[4] + cmd = FrameList[5] + data = FrameList[6] + crc = FrameList[7] + trash = FrameList[8] return ipSource, ipDestino, cmd, data def close_socket(self): self.socket_c.close() + + def open_socket(self): + self.socket_c = socket.socket() #class FTPComm: # Index: trunk/absroot/source/abspy/abscontrol/library2.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/library2.py b/trunk/absroot/source/abspy/abscontrol/library2.py new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/absroot/source/abspy/abscontrol/library2.py (revision 93) @@ -0,0 +1,165 @@ +import socket + +class TCPComm: + + __HEADER = "JRO" + __TYPE = "ABS" + + def __init__(self, ipSource, ipDestino, portDestino, asServer=False): + + self.ipSource = ipSource + self.ipDestino = ipDestino + self.portDestino = portDestino + self.addr = (ipDestino,portDestino) + + self.sc = "none" + self.answer = "none" #test + self.asServer = False + self.len = 0 + self.crc = 0 + + self.openSocket(asServer) + + def openSocket(self, asServer): + + #self.socket_c = socket.socket(AF_INET,SOCK_DGRAM) +# self.socket_c = socket.socket() +# self.socket_c.connect((self.ipDestino, self.portDestino)) + + if asServer: + self.socket_c = socket.socket() +# self.configAsServer() + self.socket_c.bind(self.addr) + self.asServer = True + else: +# self.configAsClient() + self.asServer = False #Socket is opened at the sendData function + +# def configAsClient(self): + #Buscar broadcast TCP +# self.socket_c.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) +# self.socket_c.connect(self.addr) +# pass + +# def configAsServer(self): +# +# self.socket_c.bind(self.addr) + + def waitData(self, nbytes = 1024): + + print "\nWaiting some client." + + if self.asServer == False: + # Short data through ethernet + trama_rx = self.socket_c.recv(nbytes) + else: + self.socket_c.listen(1) + sc, addr = self.socket_c.accept() + self.sc = sc + self.answer = addr + # Big data through ethernet + trama_rx = "" + while True: + tmp = self.sc.recv(nbytes) + trama_rx = trama_rx + tmp + if trama_rx[-4:] == "quit": + break + + print "\nThis socket has received some data." + + ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) + + return ipSource, ipDestino, cmd, data + + def waitServer(self, nbytes = 1024): + + print "\nWaiting some client." + self.socket_c.listen(1) + sc, addr = self.socket_c.accept() + self.sc = sc + self.answer = addr + # Big data through ethernet + trama_rx = "" + while True: + tmp = self.sc.recv(nbytes) + trama_rx = trama_rx + tmp + if trama_rx[-4:] == "quit": + break + + print "\nThis socket has received some data from: " + str(self.answer) + + ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) + + return ipSource, ipDestino, cmd, data + + def waitClient(self, nbytes = 1024): + + print "\nWaiting the server." + # Short data through ethernet + trama_rx = self.socket_c.recv(nbytes) + + print "\nThis socket has received this data: " + str(trama_rx) + + ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) + + return ipSource, ipDestino, cmd, data + + def sendData(self, cmd, data, id): + + trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + \ + ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + + if self.portDestino == 7000: + trama_tx = trama_tx + ":" + "quit" + # Send messages + if self.asServer == False: + host = "192.168.1." + str(id) + self.socket_c.connect((host, self.portDestino)) + self.socket_c.send(trama_tx) + else: + self.sc.send(trama_tx) + print "Sending message:[" + trama_tx + "]" + + def sendData2(self, cmd, data, ipDestino): + + trama_tx = self.__HEADER + ":" + self.__TYPE + ":" + str(self.ipSource) + ":" + str(ipDestino) + \ + ":" + str(self.len) + ":" + str(cmd) + ":" + str(data) + ":" + str(self.crc) + ":" + "quit" + + if self.asServer == True: + self.SendAsServer(trama_tx) + else: + self.SendAsClient(ipDestino, trama_tx) + + def SendAsServer(self, trama_tx): + + self.sc.send(trama_tx) + print "Sending message:[" + trama_tx + "] to: " + str(self.answer) + + + def SendAsClient(self, ipDestino, trama_tx): + + self.socket_c.connect((ipDestino, self.portDestino)) + self.socket_c.send(trama_tx) + print "Sending message:[" + trama_tx + "] to: " + ipDestino + + def __getTrama(self, trama): + + FrameList = trama.split(':') + + header = FrameList[0] + TypeOfInstrument = FrameList[1] + ipSource = FrameList[2] + ipDestino = FrameList[3] + len = FrameList[4] + cmd = FrameList[5] + data = FrameList[6] + crc = FrameList[7] + trash = FrameList[8] + + return ipSource, ipDestino, cmd, data + + def close_socket(self): + self.socket_c.close() + + def open_socket(self): + self.socket_c = socket.socket() \ No newline at end of file Index: trunk/absroot/source/abspy/abscontrol/server2.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/server2.py b/trunk/absroot/source/abspy/abscontrol/server2.py --- a/trunk/absroot/source/abspy/abscontrol/server2.py (revision 92) +++ b/trunk/absroot/source/abspy/abscontrol/server2.py (revision 93) @@ -1,10 +1,10 @@ import os -import library +import library2 import time class ABSServer: - def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000, ipDestino2="192.168.1.225", portDestino2=5500, ftpPortDestino=None): + def __init__(self,ipSource="localhost", ipDestino="192.168.1.117", portDestino=7000, ipDestino2="192.168.1.11", portDestino2=5500): self.ipSource = ipSource self.ipDestino = ipDestino @@ -13,37 +13,34 @@ self.ipDestino2 = ipDestino2 self.portDestino2 = portDestino2 - self.ftpPortDestino = ftpPortDestino - self.experiment_name = "default" self.tx_buffer = "default" + self.rx_buffer = "default" self.createObjects() def createObjects(self): asServer = True - self.commServerObj = library.TCPComm(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) - + self.commServerObj = library2.TCPComm("Central_Control", self.ipDestino, self.portDestino, asServer) + self.commClientObj = library2.TCPComm("Central_Control", self.ipDestino2, self.portDestino2) def waitRequest(self): - ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData() - - datarpta = "OK" +# Using rx buffer + ipSource, ipDestino, cmd, self.rx_buffer = self.commServerObj.waitServer() if cmd == "SNDF": - self.sendFile2Modules() + datarpta = self.sendFile2Modules(mode=2, cmd = cmd) if cmd == "CHGB": - self.changeBeam() + datarpta = self.changeBeam(cmd = cmd) if cmd == "ANST": - self.getStatus(mode=3) + self.getStatus(mode=4, cmd = cmd) +# Using tx buffer datarpta = self.tx_buffer - self.commServerObj.sendData(cmd=cmd, data=datarpta) + self.commServerObj.sendData2(cmd=cmd, data=datarpta, ipDestino = ipSource) def checkModule(self, address): @@ -62,7 +59,6 @@ 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") @@ -92,10 +88,17 @@ self.__writeReport(enaModules) return enaModules - def sendFile2Modules(self): + def sendFile2Modules(self, mode, cmd): + + if mode == 1: + self.__sendFile2Modules1() + else: + self.__sendFile2Modules2(cmd) + + def __sendFile2Modules1(self): #Needed for the loop - rx_frame_list = self.datarx.split('\n',2) + rx_frame_list = self.rx_buffer.split('\n',2) self.experiment_name = rx_frame_list[0] experiment_number = rx_frame_list[1] @@ -103,7 +106,8 @@ lst_control_modules = str_control_modules.split("------\n") - enaModules = self.checkAntenna() +# enaModules = self.checkAntenna() + enaModules = [11,12,13,14] for address in range(1,65): @@ -117,6 +121,44 @@ os.system(cmd) self.__loadFile() + + def __sendFile2Modules2(self,cmd): + + #Needed for the loop + rx_frame_list = self.rx_buffer.split('\n',2) + correct = 0 + failure = 0 + header = rx_frame_list[0] + "\n" + str_control_modules = rx_frame_list[2] + + lst_control_modules = str_control_modules.split("------\n") + +# enaModules = self.checkAntenna() + enaModules = [11,12,13,14] + + for id in range(1,65): + + if id not in enaModules: + continue + #tcp client needed + self.commClientObj.open_socket() + ip = "192.168.1." + str(id) + self.commClientObj.sendData2(cmd, header + lst_control_modules[id-1], ip) + ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient() + self.commClientObj.close_socket() + + if tmp == "OK": + correct = correct + 1 + else: + failure = failure + 1 + + if correct == len(enaModules): + rpta = "OK" + else: + rpta = "Failure" + + return rpta + def __writeModuleFile(self, filename, str): @@ -142,25 +184,52 @@ self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":") self.commClientObj.sendData("none", "CAMBIA:0:") - def changeBeam(self): - - #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0") - self.commClientObj.sendData("none", "CAMBIA:" + self.datarx + ":") - - def getStatus(self,mode): + def changeBeam(self, cmd): + + correct = 0 + failure = 0 +# enaModules = self.checkAntenna() + enaModules = [11,12,13,14] + + for id in range(1,65): + if id not in enaModules: + continue + + self.commClientObj.open_socket() + ip = "192.168.1." + str(id) + self.commClientObj.sendData2(cmd, self.rx_buffer, ip) +# ipSource, ipDestino, cmd, tmp = self.commClientObj.waitData() + ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient() + self.commClientObj.close_socket() + + if tmp == "OK": + correct = correct + 1 + else: + failure = failure + 1 + + if correct == len(enaModules): + rpta = "OK" + else: + rpta = "Failure" + + return rpta + + def getStatus(self, mode, cmd): if mode == 1: self.__getStsMode1() elif mode == 2: self.__getStsMode2() + elif mode == 3: + self.__getStsMode3() else: - self.__getStsMode3() + self.__getStsMode4(cmd) def __getStsMode1(self): #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") - self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":") - seconds = int (self.datarx) + self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":") + seconds = int (self.rx_buffer) # Give 5 seconds to the control modules time.sleep(seconds) # Checking the module connection @@ -199,8 +268,8 @@ def __getStsMode2(self): #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") - self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":") - seconds = int (self.datarx) + self.commClientObj.sendData("CHEQUEO:" + self.rx_buffer + ":") + seconds = int (self.rx_buffer) # Give 5 seconds to the control modules time.sleep(seconds) # Checking the module connection @@ -264,13 +333,33 @@ def __getStsMode3(self): - self.commClientObj.sendData("none", "CHEQUEO:" + self.datarx + ":") - seconds = int (self.datarx) + self.commClientObj.sendData("none", "CHEQUEO:" + self.rx_buffer + ":") + seconds = int (self.rx_buffer) # Give 5 seconds to the control modules time.sleep(seconds) self.__getModuleFile(filename = "Verificacion") - + + def __getStsMode4(self, cmd): + + content_str = "" +# enaModules = self.checkAntenna() + enaModules = [11,12,13,14] + + for id in range(1,65): + if id not in enaModules: + continue + + self.commClientObj.open_socket() + ip = "192.168.1." + str(id) + self.commClientObj.sendData2(cmd, self.rx_buffer, ip) + ipSource, ipDestino, cmd, tmp = self.commClientObj.waitClient() + self.commClientObj.close_socket() + + content_str = content_str + tmp +# self.__AddingHeader(content_list, title = "Verification_file") +#Using tx buffer + self.tx_buffer = content_str if __name__ == '__main__':