RefactoringRope/JsonClient.py

branch
server_client_variant
changeset 169
fb8a4182f12e
parent 168
53d76b4fc1ac
child 173
2cdb7c48b719
equal deleted inserted replaced
168:53d76b4fc1ac 169:fb8a4182f12e
56 56
57 def __receiveJson(self): 57 def __receiveJson(self):
58 """ 58 """
59 Private method to receive a JSON encode command and data from the 59 Private method to receive a JSON encode command and data from the
60 server. 60 server.
61
62 @return tuple containing the received command and a dictionary
63 containing the associated data
64 @rtype tuple of (str, dict)
61 """ 65 """
62 line = self.__connection.recv(1024 * 1024, socket.MSG_PEEK) 66 line = self.__connection.recv(1024 * 1024, socket.MSG_PEEK)
63 # 1MB buffer 67 # 1MB buffer
64 68
65 eol = line.find(b'\n') 69 eol = line.find(b'\n')
76 self.sendJson("ClientException", { 80 self.sendJson("ClientException", {
77 "ExceptionType": "ProtocolError", 81 "ExceptionType": "ProtocolError",
78 "ExceptionValue": str(err), 82 "ExceptionValue": str(err),
79 "ProtocolData": line.strip(), 83 "ProtocolData": line.strip(),
80 }) 84 })
81 return 85 return None, None
82 86
83 method = commandDict["method"] 87 method = commandDict["method"]
84 params = commandDict["params"] 88 params = commandDict["params"]
85 89
86 if method == "Exit": 90 return method, params
87 self.__exitClient = True 91
88 else: 92 return None, None
89 self.handleCall(method, params)
90 93
91 def handleCall(self, method, params): 94 def handleCall(self, method, params):
92 """ 95 """
93 Public method to handle a method call from the server. 96 Public method to handle a method call from the server.
94 97
114 except (select.error, KeyboardInterrupt, socket.error): 117 except (select.error, KeyboardInterrupt, socket.error):
115 # just carry on 118 # just carry on
116 continue 119 continue
117 120
118 if self.__connection in rrdy: 121 if self.__connection in rrdy:
119 self.__receiveJson() 122 method, params = self.__receiveJson()
123 if method is not None:
124 if method == "Exit":
125 self.__exitClient = True
126 else:
127 self.handleCall(method, params)
120 128
121 if self.__exitClient: 129 if self.__exitClient:
122 break 130 break
123 131
124 except Exception: 132 except Exception:
136 144
137 # Give time to process latest response on server side 145 # Give time to process latest response on server side
138 self.__connection.shutdown(socket.SHUT_RDWR) 146 self.__connection.shutdown(socket.SHUT_RDWR)
139 self.__connection.close() 147 self.__connection.close()
140 148
141 def poll(self): 149 def poll(self, wait=False):
142 """ 150 """
143 Public method to check and receive one message (if available). 151 Public method to check and receive one message (if available).
152
153 @param wait flag indicating to wait until something has been received
154 @type bool
155 @return tuple containing the received command and a dictionary
156 containing the associated data
157 @rtype tuple of (str, dict)
144 """ 158 """
145 try: 159 try:
146 rrdy, wrdy, xrdy = select.select([self.__connection], [], [], 0) 160 if wait:
161 rrdy, wrdy, xrdy = select.select(
162 [self.__connection], [], [])
163 else:
164 rrdy, wrdy, xrdy = select.select(
165 [self.__connection], [], [], 0)
166
147 if self.__connection in rrdy: 167 if self.__connection in rrdy:
148 self.__receiveJson() 168 method, params = self.__receiveJson()
169 if method is not None:
170 if method == "Exit":
171 self.__exitClient = True
172 else:
173 if wait:
174 # wait means to return the data to the caller
175 return method, params
176 else:
177 # no wait means to pass on to the handler method
178 self.handleCall(method, params)
149 179
150 except (select.error, KeyboardInterrupt, socket.error): 180 except (select.error, KeyboardInterrupt, socket.error):
151 # just ignore these 181 # just ignore these
152 return 182 pass
153 183
154 except Exception: 184 except Exception:
155 exctype, excval, exctb = sys.exc_info() 185 exctype, excval, exctb = sys.exc_info()
156 tbinfofile = io.StringIO() 186 tbinfofile = io.StringIO()
157 traceback.print_tb(exctb, None, tbinfofile) 187 traceback.print_tb(exctb, None, tbinfofile)
161 self.sendJson("ClientException", { 191 self.sendJson("ClientException", {
162 "ExceptionType": str(exctype), 192 "ExceptionType": str(exctype),
163 "ExceptionValue": str(excval), 193 "ExceptionValue": str(excval),
164 "Traceback": tbinfo, 194 "Traceback": tbinfo,
165 }) 195 })
196
197 return None, None

eric ide

mercurial