Conditions | 35 |
Total Lines | 358 |
Lines | 0 |
Ratio | 0 % |
Tests | 186 |
CRAP Score | 50.1784 |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like UaProcessor._process_message() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | |||
119 | 1 | def _process_message(self, typeid, requesthdr, algohdr, seqhdr, body): |
|
120 | 1 | if typeid == ua.NodeId(ua.ObjectIds.CreateSessionRequest_Encoding_DefaultBinary): |
|
121 | 1 | self.logger.info("Create session request") |
|
122 | 1 | params = struct_from_binary(ua.CreateSessionParameters, body) |
|
123 | |||
124 | # create the session on server |
||
125 | 1 | self.session = self.iserver.create_session(self.name, external=True) |
|
126 | # get a session creation result to send back |
||
127 | 1 | sessiondata = self.session.create_session(params, sockname=self.sockname) |
|
128 | |||
129 | 1 | response = ua.CreateSessionResponse() |
|
130 | 1 | response.Parameters = sessiondata |
|
131 | 1 | response.Parameters.ServerCertificate = self._connection.security_policy.client_certificate |
|
132 | 1 | if self._connection.security_policy.server_certificate is None: |
|
133 | 1 | data = params.ClientNonce |
|
134 | else: |
||
135 | 1 | data = self._connection.security_policy.server_certificate + params.ClientNonce |
|
136 | 1 | response.Parameters.ServerSignature.Signature = \ |
|
137 | self._connection.security_policy.asymmetric_cryptography.signature(data) |
||
138 | |||
139 | 1 | response.Parameters.ServerSignature.Algorithm = "http://www.w3.org/2000/09/xmldsig#rsa-sha1" |
|
140 | |||
141 | 1 | self.logger.info("sending create session response") |
|
142 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
143 | |||
144 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.CloseSessionRequest_Encoding_DefaultBinary): |
|
145 | 1 | self.logger.info("Close session request") |
|
146 | 1 | deletesubs = ua.ua_binary.Primitives.Boolean.unpack(body) |
|
147 | |||
148 | 1 | self.session.close_session(deletesubs) |
|
149 | |||
150 | 1 | response = ua.CloseSessionResponse() |
|
151 | 1 | self.logger.info("sending close session response") |
|
152 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
153 | |||
154 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.ActivateSessionRequest_Encoding_DefaultBinary): |
|
155 | 1 | self.logger.info("Activate session request") |
|
156 | 1 | params = struct_from_binary(ua.ActivateSessionParameters, body) |
|
157 | |||
158 | 1 | if not self.session: |
|
159 | self.logger.info("request to activate non-existing session") |
||
160 | raise utils.ServiceError(ua.StatusCodes.BadSessionIdInvalid) |
||
161 | |||
162 | 1 | if self._connection.security_policy.client_certificate is None: |
|
163 | 1 | data = self.session.nonce |
|
164 | else: |
||
165 | 1 | data = self._connection.security_policy.client_certificate + self.session.nonce |
|
166 | 1 | self._connection.security_policy.asymmetric_cryptography.verify(data, params.ClientSignature.Signature) |
|
167 | |||
168 | 1 | result = self.session.activate_session(params) |
|
169 | |||
170 | 1 | response = ua.ActivateSessionResponse() |
|
171 | 1 | response.Parameters = result |
|
172 | |||
173 | 1 | self.logger.info("sending read response") |
|
174 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
175 | |||
176 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.ReadRequest_Encoding_DefaultBinary): |
|
177 | 1 | self.logger.info("Read request") |
|
178 | 1 | params = struct_from_binary(ua.ReadParameters, body) |
|
179 | |||
180 | 1 | results = self.session.read(params) |
|
181 | |||
182 | 1 | response = ua.ReadResponse() |
|
183 | 1 | response.Results = results |
|
184 | |||
185 | 1 | self.logger.info("sending read response") |
|
186 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
187 | |||
188 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.WriteRequest_Encoding_DefaultBinary): |
|
189 | 1 | self.logger.info("Write request") |
|
190 | 1 | params = struct_from_binary(ua.WriteParameters, body) |
|
191 | |||
192 | 1 | results = self.session.write(params) |
|
193 | |||
194 | 1 | response = ua.WriteResponse() |
|
195 | 1 | response.Results = results |
|
196 | |||
197 | 1 | self.logger.info("sending write response") |
|
198 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
199 | |||
200 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.BrowseRequest_Encoding_DefaultBinary): |
|
201 | 1 | self.logger.info("Browse request") |
|
202 | 1 | params = struct_from_binary(ua.BrowseParameters, body) |
|
203 | |||
204 | 1 | results = self.session.browse(params) |
|
205 | |||
206 | 1 | response = ua.BrowseResponse() |
|
207 | 1 | response.Results = results |
|
208 | |||
209 | 1 | self.logger.info("sending browse response") |
|
210 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
211 | |||
212 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.GetEndpointsRequest_Encoding_DefaultBinary): |
|
213 | 1 | self.logger.info("get endpoints request") |
|
214 | 1 | params = struct_from_binary(ua.GetEndpointsParameters, body) |
|
215 | |||
216 | 1 | endpoints = self.iserver.get_endpoints(params, sockname=self.sockname) |
|
217 | |||
218 | 1 | response = ua.GetEndpointsResponse() |
|
219 | 1 | response.Endpoints = endpoints |
|
220 | |||
221 | 1 | self.logger.info("sending get endpoints response") |
|
222 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
223 | |||
224 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.FindServersRequest_Encoding_DefaultBinary): |
|
225 | 1 | self.logger.info("find servers request") |
|
226 | 1 | params = struct_from_binary(ua.FindServersParameters, body) |
|
227 | |||
228 | 1 | servers = self.iserver.find_servers(params) |
|
229 | |||
230 | 1 | response = ua.FindServersResponse() |
|
231 | 1 | response.Servers = servers |
|
232 | |||
233 | 1 | self.logger.info("sending find servers response") |
|
234 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
235 | |||
236 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.RegisterServerRequest_Encoding_DefaultBinary): |
|
237 | 1 | self.logger.info("register server request") |
|
238 | 1 | serv = struct_from_binary(ua.RegisteredServer, body) |
|
239 | |||
240 | 1 | self.iserver.register_server(serv) |
|
241 | |||
242 | 1 | response = ua.RegisterServerResponse() |
|
243 | |||
244 | 1 | self.logger.info("sending register server response") |
|
245 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
246 | |||
247 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.RegisterServer2Request_Encoding_DefaultBinary): |
|
248 | self.logger.info("register server 2 request") |
||
249 | params = struct_from_binary(ua.RegisterServer2Parameters, body) |
||
250 | |||
251 | results = self.iserver.register_server2(params) |
||
252 | |||
253 | response = ua.RegisterServer2Response() |
||
254 | response.ConfigurationResults = results |
||
255 | |||
256 | self.logger.info("sending register server 2 response") |
||
257 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
258 | |||
259 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.TranslateBrowsePathsToNodeIdsRequest_Encoding_DefaultBinary): |
|
260 | 1 | self.logger.info("translate browsepaths to nodeids request") |
|
261 | 1 | params = struct_from_binary(ua.TranslateBrowsePathsToNodeIdsParameters, body) |
|
262 | |||
263 | 1 | paths = self.session.translate_browsepaths_to_nodeids(params.BrowsePaths) |
|
264 | |||
265 | 1 | response = ua.TranslateBrowsePathsToNodeIdsResponse() |
|
266 | 1 | response.Results = paths |
|
267 | |||
268 | 1 | self.logger.info("sending translate browsepaths to nodeids response") |
|
269 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
270 | |||
271 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.AddNodesRequest_Encoding_DefaultBinary): |
|
272 | 1 | self.logger.info("add nodes request") |
|
273 | 1 | params = struct_from_binary(ua.AddNodesParameters, body) |
|
274 | |||
275 | 1 | results = self.session.add_nodes(params.NodesToAdd) |
|
276 | |||
277 | 1 | response = ua.AddNodesResponse() |
|
278 | 1 | response.Results = results |
|
279 | |||
280 | 1 | self.logger.info("sending add node response") |
|
281 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
282 | |||
283 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.DeleteNodesRequest_Encoding_DefaultBinary): |
|
284 | 1 | self.logger.info("delete nodes request") |
|
285 | 1 | params = struct_from_binary(ua.DeleteNodesParameters, body) |
|
286 | |||
287 | 1 | results = self.session.delete_nodes(params) |
|
288 | |||
289 | 1 | response = ua.DeleteNodesResponse() |
|
290 | 1 | response.Results = results |
|
291 | |||
292 | 1 | self.logger.info("sending delete node response") |
|
293 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
294 | |||
295 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.AddReferencesRequest_Encoding_DefaultBinary): |
|
296 | 1 | self.logger.info("add references request") |
|
297 | 1 | params = struct_from_binary(ua.AddReferencesParameters, body) |
|
298 | |||
299 | 1 | results = self.session.add_references(params.ReferencesToAdd) |
|
300 | |||
301 | 1 | response = ua.AddReferencesResponse() |
|
302 | 1 | response.Results = results |
|
303 | |||
304 | 1 | self.logger.info("sending add references response") |
|
305 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
306 | |||
307 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.DeleteReferencesRequest_Encoding_DefaultBinary): |
|
308 | 1 | self.logger.info("delete references request") |
|
309 | 1 | params = struct_from_binary(ua.DeleteReferencesParameters, body) |
|
310 | |||
311 | 1 | results = self.session.delete_references(params.ReferencesToDelete) |
|
312 | |||
313 | 1 | response = ua.DeleteReferencesResponse() |
|
314 | 1 | response.Parameters.Results = results |
|
315 | |||
316 | 1 | self.logger.info("sending delete references response") |
|
317 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
318 | |||
319 | |||
320 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.CreateSubscriptionRequest_Encoding_DefaultBinary): |
|
321 | 1 | self.logger.info("create subscription request") |
|
322 | 1 | params = struct_from_binary(ua.CreateSubscriptionParameters, body) |
|
323 | |||
324 | 1 | result = self.session.create_subscription(params, self.forward_publish_response) |
|
325 | |||
326 | 1 | response = ua.CreateSubscriptionResponse() |
|
327 | 1 | response.Parameters = result |
|
328 | |||
329 | 1 | self.logger.info("sending create subscription response") |
|
330 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
331 | |||
332 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.ModifySubscriptionRequest_Encoding_DefaultBinary): |
|
333 | self.logger.info("modify subscription request") |
||
334 | params = struct_from_binary(ua.ModifySubscriptionParameters, body) |
||
335 | |||
336 | result = self.session.modify_subscription(params, self.forward_publish_response) |
||
337 | |||
338 | response = ua.ModifySubscriptionResponse() |
||
339 | response.Parameters = result |
||
340 | |||
341 | self.logger.info("sending modify subscription response") |
||
342 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
343 | |||
344 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.DeleteSubscriptionsRequest_Encoding_DefaultBinary): |
|
345 | 1 | self.logger.info("delete subscriptions request") |
|
346 | 1 | params = struct_from_binary(ua.DeleteSubscriptionsParameters, body) |
|
347 | |||
348 | 1 | results = self.session.delete_subscriptions(params.SubscriptionIds) |
|
349 | |||
350 | 1 | response = ua.DeleteSubscriptionsResponse() |
|
351 | 1 | response.Results = results |
|
352 | |||
353 | 1 | self.logger.info("sending delte subscription response") |
|
354 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
355 | |||
356 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.CreateMonitoredItemsRequest_Encoding_DefaultBinary): |
|
357 | 1 | self.logger.info("create monitored items request") |
|
358 | 1 | params = struct_from_binary(ua.CreateMonitoredItemsParameters, body) |
|
359 | 1 | results = self.session.create_monitored_items(params) |
|
360 | |||
361 | 1 | response = ua.CreateMonitoredItemsResponse() |
|
362 | 1 | response.Results = results |
|
363 | |||
364 | 1 | self.logger.info("sending create monitored items response") |
|
365 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
366 | |||
367 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.ModifyMonitoredItemsRequest_Encoding_DefaultBinary): |
|
368 | self.logger.info("modify monitored items request") |
||
369 | params = struct_from_binary(ua.ModifyMonitoredItemsParameters, body) |
||
370 | results = self.session.modify_monitored_items(params) |
||
371 | |||
372 | response = ua.ModifyMonitoredItemsResponse() |
||
373 | response.Results = results |
||
374 | |||
375 | self.logger.info("sending modify monitored items response") |
||
376 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
377 | |||
378 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.DeleteMonitoredItemsRequest_Encoding_DefaultBinary): |
|
379 | 1 | self.logger.info("delete monitored items request") |
|
380 | 1 | params = struct_from_binary(ua.DeleteMonitoredItemsParameters, body) |
|
381 | |||
382 | 1 | results = self.session.delete_monitored_items(params) |
|
383 | |||
384 | 1 | response = ua.DeleteMonitoredItemsResponse() |
|
385 | 1 | response.Results = results |
|
386 | |||
387 | 1 | self.logger.info("sending delete monitored items response") |
|
388 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
389 | |||
390 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.HistoryReadRequest_Encoding_DefaultBinary): |
|
391 | self.logger.info("history read request") |
||
392 | params = struct_from_binary(ua.HistoryReadParameters, body) |
||
393 | |||
394 | results = self.session.history_read(params) |
||
395 | |||
396 | response = ua.HistoryReadResponse() |
||
397 | response.Results = results |
||
398 | |||
399 | self.logger.info("sending history read response") |
||
400 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
401 | |||
402 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.RegisterNodesRequest_Encoding_DefaultBinary): |
|
403 | self.logger.info("register nodes request") |
||
404 | params = struct_from_binary(ua.RegisterNodesParameters, body) |
||
405 | self.logger.info("Node registration not implemented") |
||
406 | |||
407 | response = ua.RegisterNodesResponse() |
||
408 | response.Parameters.RegisteredNodeIds = params.NodesToRegister |
||
409 | |||
410 | self.logger.info("sending register nodes response") |
||
411 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
412 | |||
413 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.UnregisterNodesRequest_Encoding_DefaultBinary): |
|
414 | self.logger.info("unregister nodes request") |
||
415 | params = struct_from_binary(ua.UnregisterNodesParameters, body) |
||
416 | |||
417 | response = ua.UnregisterNodesResponse() |
||
418 | |||
419 | self.logger.info("sending unregister nodes response") |
||
420 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
421 | |||
422 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.PublishRequest_Encoding_DefaultBinary): |
|
423 | 1 | self.logger.info("publish request") |
|
424 | |||
425 | 1 | if not self.session: |
|
426 | return False |
||
427 | |||
428 | 1 | params = struct_from_binary(ua.PublishParameters, body) |
|
429 | |||
430 | 1 | data = PublishRequestData() |
|
431 | 1 | data.requesthdr = requesthdr |
|
432 | 1 | data.seqhdr = seqhdr |
|
433 | 1 | data.algohdr = algohdr |
|
434 | 1 | with self._datalock: |
|
435 | 1 | self._publishdata_queue.append(data) # will be used to send publish answers from server |
|
436 | 1 | if self._publish_result_queue: |
|
437 | result = self._publish_result_queue.pop(0) |
||
438 | self.forward_publish_response(result) |
||
439 | 1 | self.session.publish(params.SubscriptionAcknowledgements) |
|
440 | 1 | self.logger.info("publish forward to server") |
|
441 | |||
442 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.RepublishRequest_Encoding_DefaultBinary): |
|
443 | self.logger.info("re-publish request") |
||
444 | |||
445 | params = struct_from_binary(ua.RepublishParameters, body) |
||
446 | msg = self.session.republish(params) |
||
447 | |||
448 | response = ua.RepublishResponse() |
||
449 | response.NotificationMessage = msg |
||
450 | |||
451 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
452 | |||
453 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.CloseSecureChannelRequest_Encoding_DefaultBinary): |
|
454 | self.logger.info("close secure channel request") |
||
455 | self._connection.close() |
||
456 | response = ua.CloseSecureChannelResponse() |
||
457 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
||
458 | return False |
||
459 | |||
460 | 1 | elif typeid == ua.NodeId(ua.ObjectIds.CallRequest_Encoding_DefaultBinary): |
|
461 | 1 | self.logger.info("call request") |
|
462 | |||
463 | 1 | params = struct_from_binary(ua.CallParameters, body) |
|
464 | |||
465 | 1 | results = self.session.call(params.MethodsToCall) |
|
466 | |||
467 | 1 | response = ua.CallResponse() |
|
468 | 1 | response.Results = results |
|
469 | |||
470 | 1 | self.send_response(requesthdr.RequestHandle, algohdr, seqhdr, response) |
|
471 | |||
472 | else: |
||
473 | 1 | self.logger.warning("Unknown message received %s", typeid) |
|
474 | 1 | raise utils.ServiceError(ua.StatusCodes.BadNotImplemented) |
|
475 | |||
476 | 1 | return True |
|
477 | |||
486 |