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 61) +++ b/trunk/absroot/source/abspy/abscontrol/library.py (revision 62) @@ -76,6 +76,88 @@ return ipSource, ipDestino, cmd, data +class TCPComm: + + __HEADER = "ABS" + + def __init__(self, ipSource, ipDestino, portDestino, asServer=False): + + self.ipSource = ipSource + self.ipDestino = ipDestino + self.portDestino = portDestino + self.addr = (ipDestino,portDestino) + self.addr2 = ("none",0) + self.answer = "none" #test + self.mode = "none" + + 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.configAsServer() + self.mode = "server" + else: + self.configAsClient() + self.mode = "client" + + 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.listen(1) + self.addr2 = self.socket_c.accept() + print "\nServer initialized" + + def waitData(self, nbytes = 16384): + + print "\nWaiting some data" + if self.mode == "client": + trama_rx, self.answer = self.socket_c.recv(nbytes) + else: + trama_rx, self.answer = self.addr2[0].recv(nbytes) + + print "\nThis socket has received some data from:" + print self.answer + + ipSource, ipDestino, cmd, data = self.__getTrama(trama_rx) + + return ipSource, ipDestino, cmd, data + + def sendData(self, cmd, data): + + if self.portDestino == 7000: + trama_tx = self.__HEADER + ":" + str(self.ipSource) + ":" + str(self.ipDestino) + ":" + str(cmd) + ":" + str(data) + ":" + else: + trama_tx = data + # Send messages + if self.mode == "client": + self.socket_c.send(trama_tx) + else: + self.addr2[0].send(trama_tx) + print "Sending message:[" + trama_tx + "]" + + def __getTrama(self, trama): + + FrameList = trama.split(':') + + header = FrameList[0] + ipSource = FrameList[1] + ipDestino = FrameList[2] + cmd = FrameList[3] + data = FrameList[4] + trash = FrameList[5] + + return ipSource, ipDestino, cmd, data + #class FTPComm: # # ftp_servidor = 'ftp.servidor.com'