root/branch/twedit/DataSocketCommunicators.py

Revision 1591, 2.7 kB (checked in by mswat, 16 months ago)

converted print statements into dbgMsg with switchable debug output option

Line 
1from PyQt4.QtCore import *
2from PyQt4.QtGui import *
3from PyQt4.QtNetwork import *
4from  PyQt4 import *
5from Messaging import stdMsg, dbgMsg, errMsg, setDebugging
6
7#This clazs does not use Qt event loop so we have to use blocking statements waitForConnected and waitForDisconnected to make sure
8# data was delivered succesfully within given time frame - we give it 3 sec waiting time
9class FileNameSender:
10    def __init__(self,_fileName):
11 
12        self.tcpSocket = QtNetwork.QTcpSocket()
13        self.fileName=_fileName
14       
15    def send(self):
16   
17        self.tcpSocket.abort()
18        self.tcpSocket.connectToHost(QHostAddress.LocalHost,47405)
19        if self.tcpSocket.waitForConnected(3000):           
20            self.tcpSocket.writeData(self.fileName)
21        else:
22            dbgMsg("Connection timed out")
23           
24        # wait here for tcp server to read fileName
25        if self.tcpSocket.waitForDisconnected(3000):
26            pass
27        else:
28            dbgMsg("server busy - did not respond within 3 secs")
29
30# this class runs inside Qt event loop we can use slots and signals to handle communication   
31         
32class FileNameReceiver(QObject):   
33
34    newlyReadFileName = QtCore.pyqtSignal( ('char*',))
35   
36    def __init__(self, parent=None):
37        super(FileNameReceiver, self).__init__(parent)
38
39        self.tcpServer = QTcpServer(self)
40        self.clientSocket=None
41       
42        if not self.tcpServer.listen(QHostAddress.LocalHost,47405):
43            QtGui.QMessageBox.critical(self, "FileNameReceiverr",
44                    "Unable to start the server: %s." % self.tcpServer.errorString())
45            # self.close()
46            return
47
48
49        self.tcpServer.newConnection.connect(self.acceptConnection)
50
51
52       
53    def acceptConnection(self):
54        dbgMsg("ACCEPTING NEW CONNECTION")
55        self.clientSocket = self.tcpServer.nextPendingConnection() # this is connecting tcp socket from the client
56        self.clientSocket.disconnected.connect(self.clientSocket.deleteLater)
57        self.clientSocket.readyRead.connect(self.readFileName)
58       
59    def readFileName(self):
60
61        # # clientSocket = self.tcpServer.nextPendingConnection() # this is connecting tcp socket from the client
62        # # clientSocket.disconnected.connect(clientSocket.deleteLater)
63
64        # # clientSocket.write(block)
65        # dbgMsg("clientSocket.bytesAvailable()=",self.clientSocket.bytesAvailable())
66        fileName=self.clientSocket.readData(self.clientSocket.bytesAvailable())
67        dbgMsg("THIS IS FILENAME READ FROM CLIENT=",fileName)
68        self.clientSocket.disconnectFromHost()
69        self.newlyReadFileName.emit(fileName)
70       
71       
Note: See TracBrowser for help on using the browser.