~chicharreros/magicicada-protocol/1.0

« back to all changes in this revision

Viewing changes to ubuntuone/storageprotocol/request.py

  • Committer: Magicicada Bot
  • Author(s): Natalia
  • Date: 2018-04-08 19:46:34 UTC
  • mfrom: (169.1.1 update-copyright-headers)
  • Revision ID: magicicada_bot-20180408194634-ydg0ytcwmo683l2z
[r=nataliabidart] - Added proper copyright notices and coding headers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# ubuntuone.storageprotocol.request - base classes for
2
 
#                                             network client and server
3
 
#
4
 
# Author: Lucio Torre <lucio.torre@canonical.com>
 
1
# -*- coding: utf-8 -*-
5
2
#
6
3
# Copyright 2009-2012 Canonical Ltd.
 
4
# Copyright 2015-2018 Chicharreros (https://launchpad.net/~chicharreros)
7
5
#
8
6
# This program is free software: you can redistribute it and/or modify it
9
7
# under the terms of the GNU Affero General Public License version 3,
29
27
# do not wish to do so, delete this exception statement from your
30
28
# version.  If you delete this exception statement from all source
31
29
# files in the program, then also delete it here.
32
 
"""
33
 
The base classes for the network client and server.
 
30
 
 
31
"""The base classes for the network client and server.
34
32
 
35
33
This classes provide the message serialization, delivery, request
36
34
tracking and message handling.
 
35
 
37
36
"""
38
37
 
39
 
 
40
38
import struct
41
39
import time
42
40
 
43
41
from twisted.internet.protocol import Protocol, connectionDone
44
42
from twisted.internet.interfaces import IPushProducer
45
43
from twisted.internet import defer
46
 
# pylint and zope dont work
47
 
# pylint: disable=E0611,F0401
48
44
from zope.interface import implements
49
45
 
50
46
from ubuntuone.storageprotocol import protocol_pb2, validators
228
224
                target = self.requests[message.id].processMessage
229
225
                try:
230
226
                    result = target(message)
231
 
                except Exception, e:  # pylint: disable=W0703
 
227
                except Exception as e:
232
228
                    self.requests[message.id].error(e)
233
229
            else:
234
230
                name = protocol_pb2.Message.DESCRIPTOR \
450
446
        'error_errback' func.
451
447
        """
452
448
        def _f(*args, **kwargs):
453
 
            '''Function to be called from twisted when its time arrives.'''
 
449
            """Function to be called from twisted when its time arrives."""
454
450
            if self.cancelled:
455
 
                raise RequestCancelledError("The request id=%d is cancelled! "
456
 
                                            "(before calling %r)" %
457
 
                                                        (self.id, function))
 
451
                msg = "The request id=%d is cancelled! (before calling %r)"
 
452
                raise RequestCancelledError(msg % (self.id, function))
458
453
            return function(*args, **kwargs)
459
454
        return _f
460
455
 
467
462
 
468
463
    __slots__ = ('source_message',)
469
464
 
470
 
    # pylint: disable=W0223
471
465
    def __init__(self, protocol, message):
472
466
        """Create a request response.
473
467
 
493
487
 
494
488
    def _start(self):
495
489
        """start the request sending a ping message."""
496
 
        # pylint: disable=W0201
497
490
        self.rtt = 0
498
491
        self._start_time = time.time()
499
492
        message = protocol_pb2.Message()
504
497
    def processMessage(self, message):
505
498
        """calculate rtt if message is pong, error otherwise"""
506
499
        if message.type == protocol_pb2.Message.PONG:
507
 
            # pylint: disable=W0201
508
 
            # attributes are created in completion
509
500
            self.rtt = time.time() - self._start_time
510
501
            self.done()
511
502
        else: