forked from Tribler/dispersy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolution.py
112 lines (85 loc) · 3.5 KB
/
resolution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from .meta import MetaObject
class Resolution(MetaObject):
class Implementation(MetaObject.Implementation):
pass
def setup(self, message):
"""
Setup is called after the meta message is initially created.
"""
from .message import Message
assert isinstance(message, Message)
class PublicResolution(Resolution):
"""
PublicResolution allows any member to create a message.
"""
class Implementation(Resolution.Implementation):
pass
class LinearResolution(Resolution):
"""
LinearResolution allows only members that have a specific permission to create a message.
"""
class Implementation(Resolution.Implementation):
pass
class DynamicResolution(Resolution):
"""
DynamicResolution allows the resolution policy to change.
A special dispersy-dynamic-settings message needs to be created and distributed to change the
resolution policy. Currently the policy can dynamically switch between either PublicResolution
and LinearResolution.
"""
class Implementation(Resolution.Implementation):
def __init__(self, meta, policy):
"""
Create a DynamicResolution.Implementation instance.
This object will contain the resolution policy used for a single message. This message
must use one of the available policies defined in the associated meta_message object.
"""
assert isinstance(policy, (PublicResolution.Implementation, LinearResolution.Implementation))
assert policy.meta in meta._policies, (policy.meta, meta._policies)
super(DynamicResolution.Implementation, self).__init__(meta)
self._policy = policy
@property
def default(self):
return self._meta._default
@property
def policies(self):
return self._meta._policies
@property
def policy(self):
return self._policy
def __init__(self, *policies):
"""
Create a DynamicResolution instance.
The DynamicResolution allows the resolution policy to change by creating and distributing a
dispersy-dynamic-settings message. The availabe policies is given by POLICIES.
The first policy will be used by default until a dispersy-dynamic-settings message is
received that changes the policy.
Warning! The order of the given policies is -very- important. Each policy is assigned a
number based on the order (0, 1, ... etc) and this number is used by the
dispersy-dynamic-settings message to change the policies.
@param *policies: A list with available policies.
@type *policies: (Resolution, ...)
"""
assert isinstance(policies, tuple)
assert 0 < len(policies) < 255
assert all(isinstance(x, (PublicResolution, LinearResolution)) for x in policies)
self._policies = policies
@property
def default(self):
"""
Returns the default policy, i.e. policies[0].
@rtype Resolution
"""
return self._policies[0]
@property
def policies(self):
"""
Returns a tuple containing all available policies.
@rtype (Resolution, ...)
"""
return self._policies
def setup(self, message):
if __debug__:
assert message.undo_callback, "a message with DynamicResolution policy must have an undo callback"
for policy in self._policies:
policy.setup(message)