~lutostag/ubuntu/trusty/maas/1.5.2+packagefix

« back to all changes in this revision

Viewing changes to src/provisioningserver/rpc/common.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2014-03-28 10:43:53 UTC
  • mto: This revision was merged to the branch mainline in revision 57.
  • Revision ID: package-import@ubuntu.com-20140328104353-ekpolg0pm5xnvq2s
Tags: upstream-1.5+bzr2204
ImportĀ upstreamĀ versionĀ 1.5+bzr2204

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Common RPC classes and utilties."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
str = None
 
13
 
 
14
__metaclass__ = type
 
15
__all__ = [
 
16
    "Identify",
 
17
    "Client",
 
18
]
 
19
 
 
20
from provisioningserver.rpc.interfaces import IConnection
 
21
from provisioningserver.utils import asynchronous
 
22
from twisted.protocols import amp
 
23
 
 
24
 
 
25
class Identify(amp.Command):
 
26
    """Request the identity of the remote side, e.g. its UUID."""
 
27
 
 
28
    response = [(b"ident", amp.Unicode())]
 
29
 
 
30
 
 
31
class Client:
 
32
    """Wrapper around an :class:`amp.AMP` instance.
 
33
 
 
34
    Limits the API to a subset of the behaviour of :class:`amp.AMP`'s,
 
35
    with alterations to make it suitable for use from a thread outside
 
36
    of the reactor.
 
37
    """
 
38
 
 
39
    def __init__(self, conn):
 
40
        super(Client, self).__init__()
 
41
        assert IConnection.providedBy(conn), (
 
42
            "%r does not provide IConnection" % (conn,))
 
43
        self._conn = conn
 
44
 
 
45
    @property
 
46
    def ident(self):
 
47
        """Something that identifies the far end of the connection."""
 
48
        return self._conn.ident
 
49
 
 
50
    @asynchronous
 
51
    def __call__(self, cmd, **kwargs):
 
52
        return self._conn.callRemote(cmd, **kwargs)
 
53
 
 
54
    @asynchronous
 
55
    def getHostCertificate(self):
 
56
        return self._conn.hostCertificate
 
57
 
 
58
    @asynchronous
 
59
    def getPeerCertificate(self):
 
60
        return self._conn.peerCertificate
 
61
 
 
62
    @asynchronous
 
63
    def isSecure(self):
 
64
        return self._conn.peerCertificate is not None
 
65
 
 
66
    def __eq__(self, other):
 
67
        return type(other) is type(self) and other._conn is self._conn
 
68
 
 
69
    def __hash__(self):
 
70
        return hash(self._conn)