src/eric7/EricNetwork/EricJsonStreamReader.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9264
18a7312cfdb3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
19 19
20 20
21 class EricJsonReader(QTcpServer): 21 class EricJsonReader(QTcpServer):
22 """ 22 """
23 Class implementing a JSON based reader class. 23 Class implementing a JSON based reader class.
24 24
25 The reader is responsible for opening a socket to listen for writer 25 The reader is responsible for opening a socket to listen for writer
26 connections. 26 connections.
27 27
28 @signal dataReceived(object) emitted after a data object was received 28 @signal dataReceived(object) emitted after a data object was received
29 """ 29 """
30
30 dataReceived = pyqtSignal(object) 31 dataReceived = pyqtSignal(object)
31 32
32 def __init__(self, name="", ip=None, parent=None): 33 def __init__(self, name="", ip=None, parent=None):
33 """ 34 """
34 Constructor 35 Constructor
35 36
36 @param name name of the server (used for output only) 37 @param name name of the server (used for output only)
37 @type str 38 @type str
38 @param ip IP address to listen at 39 @param ip IP address to listen at
39 @type str 40 @type str
40 @param parent parent object 41 @param parent parent object
41 @type QObject 42 @type QObject
42 """ 43 """
43 super().__init__(parent) 44 super().__init__(parent)
44 45
45 self.__name = name 46 self.__name = name
46 self.__connection = None 47 self.__connection = None
47 48
48 # setup the network interface 49 # setup the network interface
49 if ip is None: 50 if ip is None:
50 networkInterface = Preferences.getDebugger("NetworkInterface") 51 networkInterface = Preferences.getDebugger("NetworkInterface")
51 if networkInterface == "all" or '.' in networkInterface: 52 if networkInterface == "all" or "." in networkInterface:
52 # IPv4 53 # IPv4
53 self.__hostAddress = '127.0.0.1' 54 self.__hostAddress = "127.0.0.1"
54 else: 55 else:
55 # IPv6 56 # IPv6
56 self.__hostAddress = '::1' 57 self.__hostAddress = "::1"
57 else: 58 else:
58 self.__hostAddress = ip 59 self.__hostAddress = ip
59 self.listen(QHostAddress(self.__hostAddress)) 60 self.listen(QHostAddress(self.__hostAddress))
60 61
61 self.newConnection.connect(self.handleNewConnection) 62 self.newConnection.connect(self.handleNewConnection)
62 63
63 ## Note: Need the port if writer is started external in debugger. 64 ## Note: Need the port if writer is started external in debugger.
64 print('JSON reader ({1}) listening on: {0:d}' # __IGNORE_WARNING__ 65 print( # __IGNORE_WARNING_M801__
65 .format(self.serverPort(), self.__name)) 66 "JSON reader ({1}) listening on: {0:d}".format(
66 67 self.serverPort(), self.__name
68 )
69 )
70
67 def address(self): 71 def address(self):
68 """ 72 """
69 Public method to get the host address. 73 Public method to get the host address.
70 74
71 @return host address 75 @return host address
72 @rtype str 76 @rtype str
73 """ 77 """
74 return self.__hostAddress 78 return self.__hostAddress
75 79
76 def port(self): 80 def port(self):
77 """ 81 """
78 Public method to get the port number to connect to. 82 Public method to get the port number to connect to.
79 83
80 @return port number 84 @return port number
81 @rtype int 85 @rtype int
82 """ 86 """
83 return self.serverPort() 87 return self.serverPort()
84 88
85 @pyqtSlot() 89 @pyqtSlot()
86 def handleNewConnection(self): 90 def handleNewConnection(self):
87 """ 91 """
88 Public slot for new incoming connections from a writer. 92 Public slot for new incoming connections from a writer.
89 """ 93 """
90 connection = self.nextPendingConnection() 94 connection = self.nextPendingConnection()
91 if not connection.isValid(): 95 if not connection.isValid():
92 return 96 return
93 97
94 if self.__connection is not None: 98 if self.__connection is not None:
95 self.__connection.close() 99 self.__connection.close()
96 100
97 self.__connection = connection 101 self.__connection = connection
98 102
99 connection.readyRead.connect(self.__receiveJson) 103 connection.readyRead.connect(self.__receiveJson)
100 connection.disconnected.connect(self.__handleDisconnect) 104 connection.disconnected.connect(self.__handleDisconnect)
101 105
102 @pyqtSlot() 106 @pyqtSlot()
103 def __handleDisconnect(self): 107 def __handleDisconnect(self):
104 """ 108 """
105 Private slot handling a disconnect of the writer. 109 Private slot handling a disconnect of the writer.
106 """ 110 """
107 if self.__connection is not None: 111 if self.__connection is not None:
108 self.__receiveJson() # read all buffered data first 112 self.__receiveJson() # read all buffered data first
109 self.__connection.close() 113 self.__connection.close()
110 114
111 self.__connection = None 115 self.__connection = None
112 116
113 @pyqtSlot() 117 @pyqtSlot()
114 def __receiveJson(self): 118 def __receiveJson(self):
115 """ 119 """
116 Private slot handling received data from the writer. 120 Private slot handling received data from the writer.
117 """ 121 """
118 while self.__connection and self.__connection.canReadLine(): 122 while self.__connection and self.__connection.canReadLine():
119 dataStr = self.__connection.readLine() 123 dataStr = self.__connection.readLine()
120 jsonLine = bytes(dataStr).decode("utf-8", 'backslashreplace') 124 jsonLine = bytes(dataStr).decode("utf-8", "backslashreplace")
121 125
122 #- print("JSON Reader ({0}): {1}".format(self.__name, jsonLine)) 126 # - print("JSON Reader ({0}): {1}".format(self.__name, jsonLine))
123 #- this is for debugging only 127 # - this is for debugging only
124 128
125 try: 129 try:
126 data = json.loads(jsonLine.strip()) 130 data = json.loads(jsonLine.strip())
127 except (TypeError, ValueError) as err: 131 except (TypeError, ValueError) as err:
128 EricMessageBox.critical( 132 EricMessageBox.critical(
129 None, 133 None,
130 self.tr("JSON Protocol Error"), 134 self.tr("JSON Protocol Error"),
131 self.tr("""<p>The data received from the writer""" 135 self.tr(
132 """ could not be decoded. Please report""" 136 """<p>The data received from the writer"""
133 """ this issue with the received data to the""" 137 """ could not be decoded. Please report"""
134 """ eric bugs email address.</p>""" 138 """ this issue with the received data to the"""
135 """<p>Error: {0}</p>""" 139 """ eric bugs email address.</p>"""
136 """<p>Data:<br/>{1}</p>""").format( 140 """<p>Error: {0}</p>"""
137 str(err), Utilities.html_encode(jsonLine.strip())), 141 """<p>Data:<br/>{1}</p>"""
138 EricMessageBox.Ok) 142 ).format(str(err), Utilities.html_encode(jsonLine.strip())),
143 EricMessageBox.Ok,
144 )
139 return 145 return
140 146
141 self.dataReceived.emit(data) 147 self.dataReceived.emit(data)

eric ide

mercurial