~verterok/ubuntuone-client/fix-646112

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
# ubuntuone.syncdaemon.action_queue - Action queue
#
# Author: John Lenton <john.lenton@canonical.com>
# Author: Natalia B. Bidart <natalia.bidart@canonical.com>
#
# Copyright 2009 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
"""The ActionQueue is where actions to be performed on the server are
queued up and then executed.

The idea is that there are two queues,
one for metadata and another for content; the metadata queue has
priority over the content queue.

"""
import base64
import itertools
import simplejson
import logging
import os
import random
import re
import tempfile
import traceback
import uuid
import zlib

from collections import deque, defaultdict
from functools import wraps, partial
from urllib import urlencode
from urllib2 import urlopen, Request, HTTPError
from urlparse import urljoin

from zope.interface import implements
from twisted.internet import reactor, defer, threads
from twisted.internet import error as twisted_errors
from twisted.names import client as dns_client
from twisted.python.failure import Failure, DefaultException

from oauth import oauth
from ubuntuone.storageprotocol import protocol_pb2
from ubuntuone.storageprotocol import errors as protocol_errors
from ubuntuone.storageprotocol.client import (
    ThrottlingStorageClient, ThrottlingStorageClientFactory
)
from ubuntuone.storageprotocol.context import get_ssl_context
from ubuntuone.syncdaemon.interfaces import IActionQueue, IMarker
from ubuntuone.syncdaemon.logger import mklog, TRACE

logger = logging.getLogger("ubuntuone.SyncDaemon.ActionQueue")

# I want something which repr() is "---" *without* the quotes :)
UNKNOWN = type('', (), {'__repr__': lambda _: '---'})()

# Regular expression to validate an e-mail address
EREGEX = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"

# progress threshold to emit a download/upload progress event: 64Kb
TRANSFER_PROGRESS_THRESHOLD = 64*1024*1024

def passit(func):
    """Pass the value on for the next deferred, while calling func with it."""

    @wraps(func)
    def wrapper(a):
        """Do it."""
        func(a)
        return a

    return wrapper


class UploadCompressionCancelled(Exception):
    """Compression of a file for upload cancelled."""


class RequestCleanedUp(Exception):
    """The request was cancelled by ActionQueue.cleanup()."""


class NamedTemporaryFile(object):
    """Like tempfile.NamedTemporaryFile, but working in 2.5.

    Also WRT the delete argument. Actually, one of these
    NamedTemporaryFile()s is the same as a
    tempfile.NamedTemporaryFile(delete=False) from 2.6.

    Or so the theory goes.

    """

    def __init__(self):
        fileno, self.name = tempfile.mkstemp()
        self._fd = os.fdopen(fileno, 'r+w')

    def __getattr__(self, attr):
        """Proxy everything else (other than .name) on to self._fd."""
        return getattr(self._fd, attr)


class LoggingStorageClient(ThrottlingStorageClient):
    """A subclass of StorageClient that logs.

    Specifically, it adds logging to processMessage and sendMessage.
    """

    def __init__(self):
        ThrottlingStorageClient.__init__(self)
        self.log = logging.getLogger('ubuntuone.SyncDaemon.StorageClient')
        # configure the handler level to be < than DEBUG
        self.log.setLevel(TRACE)
        self.log.debug = partial(self.log.log, TRACE)

    def processMessage(self, message):
        """Wrapper that logs the message and result."""
        # don't log the full message if it's of type BYTES
        if message.type == protocol_pb2.Message.BYTES:
            self.log.debug('start - processMessage: id: %s, type: %s',
                           message.id, message.type)
        else:
            self.log.debug('start - processMessage: %s',
                          str(message).replace("\n", " "))
        if message.id in self.requests:
            req = self.requests[message.id]
            req.deferred.addCallbacks(self.log_success, self.log_error)
        result = ThrottlingStorageClient.processMessage(self, message)
        self.log.debug('end - processMessage: id: %s - result: %s',
                       message.id, result)
        return result

    def log_error(self, failure):
        """Logging errback for requests."""
        self.log.debug('request error: %s', failure)
        return failure

    def log_success(self, result):
        """Logging callback for requests."""
        self.log.debug('request finished: %s', result)
        if getattr(result, '__dict__', None):
            self.log.debug('result.__dict__: %s', result.__dict__)
        return result

    def sendMessage(self, message):
        """Wrapper that logs the message and result."""
        # don't log the full message if it's of type BYTES
        if message.type == protocol_pb2.Message.BYTES:
            self.log.debug('start - sendMessage: id: %s, type: %s',
                           message.id, message.type)
        else:
            self.log.debug('start - sendMessage: %s',
                          str(message).replace("\n", " "))
        result = ThrottlingStorageClient.sendMessage(self, message)
        self.log.debug('end - sendMessage: id: %s', message.id)
        return result


class ActionQueueProtocol(LoggingStorageClient):
    """This is the Action Queue version of the StorageClient protocol."""

    factory = None

    def connectionMade(self):
        """A new connection was made."""
        self.log.info('Connection made.')
        self.factory.event_queue.push('SYS_CONNECTION_MADE')

    def connectionLost(self, reason):
        """The connection was lost."""
        self.log.info('Connection lost, reason: %s.', reason)


class Marker(str):
    """A uuid4-based marker class."""

    implements(IMarker)

    def __new__(cls):
        return super(Marker, cls).__new__(cls, uuid.uuid4())

    def __repr__(self):
        return "marker:%s" % self


class ZipQueue(object):
    """A queue of files to be compressed for upload.

    Parts of this were shamelessly copied from
    twisted.internet.defer.DeferredSemaphore.

    See bug #373984

    """

    def __init__(self):
        self.waiting = deque()
        self.tokens = self.limit = 10

    def acquire(self):
        """Return a deferred which fires on token acquisition."""
        assert self.tokens >= 0, "Tokens should never be negative"
        d = defer.Deferred()
        if not self.tokens:
            self.waiting.append(d)
        else:
            self.tokens = self.tokens - 1
            d.callback(self)
        return d

    def release(self):
        """Release the token.

        Should be called by whoever did the acquire() when the shared
        resource is free.
        """
        assert self.tokens < self.limit, "Too many tokens!"
        self.tokens = self.tokens + 1
        if self.waiting:
            # someone is waiting to acquire token
            self.tokens = self.tokens - 1
            d = self.waiting.popleft()
            d.callback(self)

    def _compress(self, deferred, upload):
        """Compression background task."""
        try:
            fileobj = upload.fileobj_factory()
        except StandardError:
            # presumably the user deleted the file before we got to
            # upload it. Logging a warning just in case.
            upload.log.warn('unable to build fileobj'
                            ' (user deleted the file, maybe?)'
                            ' so cancelling the upload.')
            upload.cancel()
            fileobj = None

        filename = getattr(fileobj, 'name', '<?>')

        try:
            if upload.cancelled:
                raise UploadCompressionCancelled("Cancelled")
            upload.log.debug('compressing: %r', filename)
            # we need to compress the file completely to figure out its
            # compressed size. So streaming is out :(
            if upload.tempfile_factory is None:
                f = NamedTemporaryFile()
            else:
                f = upload.tempfile_factory()
            zipper = zlib.compressobj()
            while not upload.cancelled:
                data = fileobj.read(4096)
                if not data:
                    f.write(zipper.flush())
                    # no flush/sync because we don't need this to persist
                    # on disk; if the machine goes down, we'll lose it
                    # anyway (being in /tmp and all)
                    break
                f.write(zipper.compress(data))
            if upload.cancelled:
                raise UploadCompressionCancelled("Cancelled")
            upload.deflated_size = f.tell()
            # close the compressed file (thus, if you actually want to stream
            # it out, it must have a name so it can be reopnened)
            f.close()
            upload.tempfile = f
        except Exception, e: # pylint: disable-msg=W0703
            reactor.callFromThread(deferred.errback, e)
        else:
            reactor.callFromThread(deferred.callback, True)

    @defer.inlineCallbacks
    def zip(self, upload):
        """Acquire, do the compression in a thread, release."""
        deferred = defer.Deferred()

        yield self.acquire()
        try:
            reactor.callInThread(self._compress, deferred, upload)
        finally:
            self.release()

        # let's wait _compress to finish
        yield deferred


class RequestQueue(object):
    """
    RequestQueue is a queue that ensures that there is at most one
    request at a time 'on the wire', and that uses the action queue's
    states for its syncrhonization.
    """
    commutative = False

    def __init__(self, name, action_queue):
        super(RequestQueue, self).__init__()
        self.name = name
        self.action_queue = action_queue
        self.waiting = deque()
        self._head = None

    def __len__(self):
        """return the length of the queue"""
        return len(self.waiting)

    def cleanup_and_retry(self):
        """call cleanup_and_retry on the head"""
        if self._head is not None:
            cmd = self._head
            self._head = None
            cmd.cleanup_and_retry()

    def schedule_next(self, share_id, node_id):
        """
        Promote the newest command on the given share and node to
        the front of the queue, if it is possible to do so.
        """
        if not self.commutative:
            return
        for cmd in reversed(self.waiting):
            try:
                if cmd.share_id == share_id and cmd.node_id == node_id:
                    break
            except AttributeError:
                # not a command we can reschedule
                pass
        else:
            raise KeyError("No waiting command for that share and node")
        self.waiting.remove(cmd)
        self.waiting.appendleft(cmd)

    def queue(self, command, on_top=False):
        """Add a command to the queue."""
        # puts the command where it was asked for
        if on_top:
            self.waiting.appendleft(command)
        else:
            self.waiting.append(command)

        # if nothing running, and this command is the first in the
        # queue, send the signal
        if len(self.waiting) == 1 and not self._head:
            self.action_queue.event_queue.push('SYS_' + self.name + '_WAITING')

        # check conditions if the command is runnable
        if command.is_runnable:
            self.check_conditions()

    def done(self):
        """A command must call done() on the queue when it has finished."""
        self._head = None
        if len(self.waiting):
            event = 'SYS_' + self.name + '_WAITING'
        else:
            event = 'SYS_' + self.name + '_DONE'
        self.action_queue.event_queue.push(event)

    def _is_empty_or_runnable_waiting(self):
        """Returns True if there is an immediately runnable command in the
        queue, or if the queue is empty.
        """
        if not self.waiting:
            return True
        if self.commutative:
            for command in self.waiting:
                if command.is_runnable:
                    return True
            return False
        else:
            return self.waiting[0].is_runnable

    def _get_next_runnable_command(self):
        """Returns the next runnable command, if there is one."""
        if self.commutative:
            n_skipped = 0
            for command in self.waiting:
                if command.is_runnable:
                    break
                n_skipped += 1
            if n_skipped < len(self.waiting):
                # we found a runnable command
                if n_skipped > 0:
                    # move the skipped commands to the end
                    self.waiting.rotate(-n_skipped)
                command = self.waiting.popleft()
            else:
                command = None
        else:
            if self.waiting and self.waiting[0].is_runnable:
                command = self.waiting.popleft()
            else:
                command = None
        if command is None:
            flag = self._is_empty_or_runnable_waiting
            command = WaitForCondition(self, flag)
            command.pre_queue_setup()
        return command

    def check_conditions(self):
        """Check conditions on which the head command may be waiting."""
        if self._head is not None:
            self._head.check_conditions()

    def run(self):
        """Run the next available command in the queue."""
        if not self.waiting:
            logger.warning("run() called with %s queue empty!", self.name)
            return

        if self._head is not None:
            # already executing something, not start another command
            return

        self._head = command = self._get_next_runnable_command()
        d = command.run()
        d.addBoth(passit(lambda _: self.done()))
        return d

    def node_is_queued(self, command, share_id, node_id):
        """True if a command is queued for that node."""
        for cmd in itertools.chain((self._head,), self.waiting):
            if isinstance(cmd, command):
                if getattr(cmd, "node_id", None) == node_id and \
                   getattr(cmd, "share_id", None) == share_id:
                    return True

    def full_queue(self):
        """Get the full queue (head + waiting)."""
        return [self._head] + list(self.waiting)

    def remove(self, command):
        """Removes a command from 'waiting', if there."""
        if command in self.waiting:
            self.waiting.remove(command)


class UniqueRequestQueue(RequestQueue):
    """A unique RequestQueue.

    It only ever queues one command for each (share_id, node_id) pair.
    """

    def queue(self, command, on_top=False):
        """Add a command to the queue.

        If there is a command in the queue for the same node, it (the old
        one) will be removed, leaving queued only the new one.
        """
        for wc in self.waiting:
            if wc.share_id == command.share_id and \
               wc.node_id == command.node_id:
                self.waiting.remove(wc)
                m = "Request removed (%s), queing other (%s) for same node."
                logger.debug(m, wc, command)
                break
        return super(UniqueRequestQueue, self).queue(command, on_top)


class NoisyRequestQueue(RequestQueue):
    """A RequestQueue that notifies when it's changed."""

    def __init__(self, name, action_queue, change_notification_cb=None):
        self.set_change_notification_cb(change_notification_cb)
        super(NoisyRequestQueue, self).__init__(name, action_queue)

    def set_change_notification_cb(self, change_notification_cb):
        """Set the change notification callback."""
        if change_notification_cb is None:
            self.change_notification_cb = lambda *_: None
        else:
            self.change_notification_cb = change_notification_cb

    def queue(self, command, on_top=False):
        """Add a command to the queue.

        Calls the change notification callback
        """
        super(NoisyRequestQueue, self).queue(command, on_top)
        self.change_notification_cb(self._head, self.waiting)

    def get_head(self):
        """Getter for the queue's head."""
        return self.__head

    def set_head(self, head):
        """Setter for the queue's head.

        Calls he change notification callback.
        """
        self.change_notification_cb(head, self.waiting)
        self.__head = head

    _head = property(get_head, set_head)


class ContentQueue(NoisyRequestQueue, UniqueRequestQueue):
    """The content queue is a commutative (uniquified), noisy request queue."""
    commutative = True


class DeferredMap(object):
    """A mapping of deferred values.

    Return deferreds for a key that are fired (succesfully or not) later.
    """

    def __init__(self):
        self.waiting = defaultdict(list)

    def get(self, key):
        """Return a deferred for the given key."""
        d = defer.Deferred()
        self.waiting[key].append(d)
        return d

    def set(self, key, value):
        """We've got the value for a key!

        If it was waited, fire the waiting deferreds and remove the key.
        """
        if key in self.waiting:
            deferreds = self.waiting.pop(key)
            for d in deferreds:
                d.callback(value)

    def err(self, key, failure):
        """Something went terribly wrong in the process of getting a value.

        Break the news to the waiting deferreds and remove the key.
        """
        if key in self.waiting:
            deferreds = self.waiting.pop(key)
            for d in deferreds:
                d.errback(failure)


class UploadProgressWrapper(object):
    """A wrapper around the file-like object used for Uploads.

    It can be used to keep track of the number of bytes that have been
    written to the store and invokes a hook on progress.

    """

    __slots__ = ('fd', 'data_dict', 'n_bytes_read', 'progress_hook')

    def __init__(self, fd, data_dict, progress_hook):
        """
        fd is the file-like object used for uploads. data_dict is the
        entry in the uploading dictionary.
        """
        self.fd = fd
        self.data_dict = data_dict
        self.n_bytes_read = 0
        self.progress_hook = progress_hook

    def read(self, size=None):
        """
        read at most size bytes from the file-like object.

        Keep track of the number of bytes that have been read, and the
        number of bytes that have been written (assumed to be equal to
        the number of bytes read on the previews call to read). The
        latter is done directly in the data_dict.
        """
        self.data_dict['n_bytes_written'] = self.n_bytes_read
        self.progress_hook(self.n_bytes_read)

        data = self.fd.read(size)
        self.n_bytes_read += len(data)
        return data

    def __getattr__(self, attr):
        """
        Proxy all the rest.
        """
        return getattr(self.fd, attr)


class ActionQueue(ThrottlingStorageClientFactory, object):
    """The ActionQueue itself."""

    implements(IActionQueue)
    protocol = ActionQueueProtocol

    def __init__(self, event_queue, main, host, port, dns_srv,
                 use_ssl=False, disable_ssl_verify=False,
                 read_limit=None, write_limit=None, throttling_enabled=False,
                 connection_timeout=30):
        ThrottlingStorageClientFactory.__init__(self, read_limit=read_limit,
                                      write_limit=write_limit,
                                      throttling_enabled=throttling_enabled)
        self.event_queue = event_queue
        self.main = main
        self.host = host
        self.port = port
        self.dns_srv = dns_srv
        self.use_ssl = use_ssl
        self.disable_ssl_verify = disable_ssl_verify
        self.connection_timeout = connection_timeout

        # credentials
        self.token = None
        self.consumer = None

        self.client = None # an instance of self.protocol

        # is a twisted.internet.tcp/ssl.Connector instance
        self.connector = None # created on reactor.connectTCP/SSL
        # we need to track down if a connection is in progress
        # to avoid double connections
        self.connect_in_progress = False

        self.content_queue = ContentQueue('CONTENT_QUEUE', self)
        self.meta_queue = RequestQueue('META_QUEUE', self)
        self.uuid_map = DeferredMap()
        self.zip_queue = ZipQueue()

        self.estimated_free_space = {}

        self.uploading = {}
        self.downloading = {}

        event_queue.subscribe(self)

    def check_conditions(self):
        """Poll conditions on which running actions may be waiting."""
        self.content_queue.check_conditions()
        self.meta_queue.check_conditions()

    def have_sufficient_space_for_upload(self, share_id, upload_size):
        """Returns True if we have sufficient space for the given upload."""
        free = self.main.vm.get_free_space(share_id)
        enough = free >= upload_size
        if not enough:
            logger.info("Not enough space for upload %s bytes (available: %s)",
                        upload_size, free)
            self.event_queue.push('SYS_QUOTA_EXCEEDED', volume_id=share_id,
                                  free_bytes=free)

        return enough

    def handle_SYS_USER_CONNECT(self, access_token):
        """Stow the access token away for later use."""
        self.token = oauth.OAuthToken(access_token['token'],
                                      access_token['token_secret'])
        self.consumer = oauth.OAuthConsumer(access_token['consumer_key'],
                                            access_token['consumer_secret'])

    def cleanup(self):
        """Cancel, clean up, and reschedule things that were in progress."""
        self.meta_queue.cleanup_and_retry()
        self.content_queue.cleanup_and_retry()

    def _cleanup_connection_state(self, *args):
        """Reset connection state."""
        self.client = None
        self.connector = None
        self.connect_in_progress = False

    def _share_change_callback(self, info):
        """Called by the client when notified that a share changed."""
        self.event_queue.push('SV_SHARE_CHANGED', info=info)

    def _share_delete_callback(self, share_id):
        """Called by the client when notified that a share was deleted."""
        self.event_queue.push('SV_SHARE_DELETED', share_id=share_id)

    def _share_answer_callback(self, share_id, answer):
        """Called by the client when it gets a share answer notification."""
        self.event_queue.push('SV_SHARE_ANSWERED',
                              share_id=str(share_id), answer=answer)

    def _free_space_callback(self, share_id, free_bytes):
        """Called by the client when it gets a free space notification."""
        self.event_queue.push('SV_FREE_SPACE',
                              share_id=str(share_id), free_bytes=free_bytes)

    def _account_info_callback(self, account_info):
        """Called by the client when it gets an account info notification."""
        self.event_queue.push('SV_ACCOUNT_CHANGED',
                              account_info=account_info)

    def _volume_created_callback(self, volume):
        """Process new volumes."""
        self.event_queue.push('SV_VOLUME_CREATED', volume=volume)

    def _volume_deleted_callback(self, volume_id):
        """Process volume deletion."""
        self.event_queue.push('SV_VOLUME_DELETED', volume_id=volume_id)

    def _volume_new_generation_callback(self, volume_id, generation):
        """Process new volumes."""
        self.event_queue.push('SV_VOLUME_NEW_GENERATION',
                              volume_id=volume_id, generation=generation)

    def _lookup_srv(self):
        """Do the SRV lookup.

        Return a deferred whose callback is going to be called with
        (host, port). If we can't do the lookup, the default host, port
        is used.

        """

        def on_lookup_ok(results):
            """Get a random host from the SRV result."""
            logger.debug('SRV lookup done, choosing a server.')
            # pylint: disable-msg=W0612
            records, auth, add = results
            if not records:
                raise ValueError('No available records.')
            # pick a random server
            record = random.choice(records)
            logger.debug('Using record: %r', record)
            if record.payload:
                return record.payload.target.name, record.payload.port
            else:
                logger.info('Empty SRV record, fallback to %r:%r',
                            self.host, self.port)
                return self.host, self.port

        def on_lookup_error(failure):
            """Return the default host/post on a DNS SRV lookup failure."""
            logger.info("SRV lookup error, fallback to %r:%r \n%s",
                        self.host, self.port, failure.getTraceback())
            return self.host, self.port

        if self.dns_srv:
            # lookup the DNS SRV records
            d = dns_client.lookupService(self.dns_srv, timeout=[3, 2])
            d.addCallback(on_lookup_ok)
            d.addErrback(on_lookup_error)
            return d
        else:
            return defer.succeed((self.host, self.port))

    def _make_connection(self, result):
        """Do the real connect call."""
        host, port = result
        ssl_context = get_ssl_context(self.disable_ssl_verify)
        if self.use_ssl:
            self.connector = reactor.connectSSL(host, port, factory=self,
                                                contextFactory=ssl_context,
                                                timeout=self.connection_timeout)
        else:
            self.connector = reactor.connectTCP(host, port, self,
                                                timeout=self.connection_timeout)

    def connect(self):
        """Start the circus going."""
        # avoid multiple connections
        if self.connect_in_progress:
            msg = "Discarding new connection attempt, there is a connector!"
            logger.warning(msg)
            return

        self.connect_in_progress = True
        d = self._lookup_srv()
        # DNS lookup always succeeds, proceed to actually connect
        d.addCallback(self._make_connection)

    def buildProtocol(self, addr):
        """Build the client and store it. Connect callbacks."""
        # XXX: Very Important Note: within the storageprotocol project,
        # ThrottlingStorageClient.connectionMade sets self.factory.client
        # to self *if* self.factory.client is not None.
        # Since buildProcotol is called before connectionMade, the latter
        # does nothing (safely).
        self.client = ThrottlingStorageClientFactory.buildProtocol(self, addr)

        self.client.set_share_change_callback(self._share_change_callback)
        self.client.set_share_answer_callback(self._share_answer_callback)
        self.client.set_free_space_callback(self._free_space_callback)
        self.client.set_account_info_callback(self._account_info_callback)
        # volumes
        self.client.set_volume_created_callback(self._volume_created_callback)
        self.client.set_volume_deleted_callback(self._volume_deleted_callback)
        self.client.set_volume_new_generation_callback(
                                        self._volume_new_generation_callback)

        logger.info('Connection made.')
        return self.client

    def startedConnecting(self, connector):
        """Called when a connection has been started."""
        logger.info('Connection started to host %s, port %s.',
                    self.host, self.port)

    def disconnect(self):
        """Disconnect the client.

        This shouldn't be called if the client is already disconnected.

        """
        if self.connector is not None:
            self.connector.disconnect()
            self._cleanup_connection_state()
        else:
            msg = 'disconnect() was called when the connector was None.'
            logger.warning(msg)

        logger.debug("Disconnected.")

    def clientConnectionFailed(self, connector, reason):
        """Called when the connect() call fails."""
        self._cleanup_connection_state()
        self.event_queue.push('SYS_CONNECTION_FAILED')
        logger.info('Connection failed: %s', reason.getErrorMessage())

    def clientConnectionLost(self, connector, reason):
        """The client connection went down."""
        self._cleanup_connection_state()
        self.event_queue.push('SYS_CONNECTION_LOST')
        logger.warning('Connection lost: %s', reason.getErrorMessage())

    @defer.inlineCallbacks
    def _send_request_and_handle_errors(self, request, request_error,
                                        event_error, event_ok,
                                        handle_exception=True,
                                        args=(), kwargs={}):
        """Send 'request' to the server, using params 'args' and 'kwargs'.

        Expect 'request_error' as valid error, and push 'event_error' in that
        case. Do generic error handling for the rest of the protocol errors.

        """
        # if the client changes while we're waiting, this message is
        # old news and should be discarded (the message would
        # typically be a failure: timeout or disconnect). So keep the
        # original client around for comparison.
        client = self.client
        req_name = request.__name__
        failure = None
        event = None
        result = None
        try:
            try:
                result = yield request(*args, **kwargs)
            finally:
                # common handling for all cases
                if client is not self.client:
                    msg = "Client mismatch while processing the request '%s'" \
                          ", client (%r) is not self.client (%r)."
                    logger.warning(msg, req_name, client, self.client)
                    return
        except request_error, failure:
            event = event_error
            self.event_queue.push(event_error, error=str(failure))
        except protocol_errors.AuthenticationRequiredError, failure:
            # we need to separate this case from the rest because an
            # AuthenticationRequiredError is an StorageRequestError,
            # and we treat it differently.
            event = 'SYS_UNKNOWN_ERROR'
            self.event_queue.push(event)
        except protocol_errors.StorageRequestError, failure:
            event = 'SYS_SERVER_ERROR'
            self.event_queue.push(event, error=str(failure))
        except Exception, failure:
            if handle_exception:
                event = 'SYS_UNKNOWN_ERROR'
                self.event_queue.push(event)
            else:
                raise
        else:
            logger.info("The request '%s' finished OK.", req_name)
            if event_ok is not None:
                self.event_queue.push(event_ok)

        if failure is not None:
            logger.info("The request '%s' failed with the error: %s "
                        "and was handled with the event: %s",
                         req_name, failure, event)
        else:
            defer.returnValue(result)

    def check_version(self):
        """Check if the client protocol version matches that of the server."""
        check_version_d = self._send_request_and_handle_errors(
            request=self.client.protocol_version,
            request_error=protocol_errors.UnsupportedVersionError,
            event_error='SYS_PROTOCOL_VERSION_ERROR',
            event_ok='SYS_PROTOCOL_VERSION_OK'
        )
        return check_version_d

    @defer.inlineCallbacks
    def set_capabilities(self, caps):
        """Set the capabilities with the server."""

        @defer.inlineCallbacks
        def caps_raising_if_not_accepted(capability_method, caps, msg):
            """Discuss capabilities with the server."""
            client_caps = getattr(self.client, capability_method)
            req = yield client_caps(caps)
            if not req.accepted:
                raise StandardError(msg)
            defer.returnValue(req)

        error_msg = "The server doesn't have the requested capabilities"
        query_caps_d = self._send_request_and_handle_errors(
            request=caps_raising_if_not_accepted,
            request_error=StandardError,
            event_error='SYS_SET_CAPABILITIES_ERROR',
            event_ok=None,
            args=('query_caps', caps, error_msg)
        )
        req = yield query_caps_d

        # req can be None if set capabilities failed, error is handled by
        # _send_request_and_handle_errors
        if not req:
            return

        error_msg = "The server denied setting '%s' capabilities" % caps
        set_caps_d = self._send_request_and_handle_errors(
            request=caps_raising_if_not_accepted,
            request_error=StandardError,
            event_error='SYS_SET_CAPABILITIES_ERROR',
            event_ok='SYS_SET_CAPABILITIES_OK',
            args=('set_caps', caps, error_msg)
        )
        yield set_caps_d

    @defer.inlineCallbacks
    def authenticate(self):
        """Authenticate against the server using stored credentials."""
        authenticate_d = self._send_request_and_handle_errors(
            request=self.client.oauth_authenticate,
            request_error=protocol_errors.AuthenticationFailedError,
            event_error='SYS_AUTH_ERROR', event_ok='SYS_AUTH_OK',
            # XXX: handle self.token is None or self.consumer is None?
            args=(self.consumer, self.token)
        )
        req = yield authenticate_d

        # req can be None if the auth failed, but it's handled by
        # _send_request_and_handle_errors
        if req:
            # log the session_id
            logger.note('Session ID: %r', str(req.session_id))

    @defer.inlineCallbacks
    def query_volumes(self):
        """Get the list of volumes.

        This method will *not* queue a command, the request will be
        executed right away.
        """
        result = yield self._send_request_and_handle_errors(
            request=self.client.list_volumes,
            request_error=None, event_error=None,
            event_ok=None, handle_exception=False)
        defer.returnValue(result.volumes)

    def make_file(self, share_id, parent_id, name, marker):
        """See .interfaces.IMetaQueue."""
        return MakeFile(self.meta_queue, share_id, parent_id,
                        name, marker).queue()

    def make_dir(self, share_id, parent_id, name, marker):
        """See .interfaces.IMetaQueue."""
        return MakeDir(self.meta_queue, share_id, parent_id,
                       name, marker).queue()

    def move(self, share_id, node_id, old_parent_id, new_parent_id, new_name):
        """See .interfaces.IMetaQueue."""
        return Move(self.meta_queue, share_id, node_id, old_parent_id,
                    new_parent_id, new_name).queue()

    def unlink(self, share_id, parent_id, node_id):
        """See .interfaces.IMetaQueue."""
        return Unlink(self.meta_queue, share_id, parent_id, node_id).queue()

    def inquire_free_space(self, share_id):
        """See .interfaces.IMetaQueue."""
        return FreeSpaceInquiry(self.meta_queue, share_id).queue()

    def inquire_account_info(self):
        """See .interfaces.IMetaQueue."""
        return AccountInquiry(self.meta_queue).queue()

    def list_shares(self):
        """List the shares; put the result on the event queue."""
        return ListShares(self.meta_queue).queue()

    def answer_share(self, share_id, answer):
        """Answer the offer of a share."""
        return AnswerShare(self.meta_queue, share_id, answer).queue()

    def create_share(self, node_id, share_to, name, access_level, marker):
        """Share a node with somebody."""
        return CreateShare(self.meta_queue, node_id, share_to, name,
                           access_level, marker).queue()

    def create_udf(self, path, name, marker):
        """Create a User Defined Folder.

        @param path: the path in disk to the UDF.
        @param name: the name of the UDF.
        @param marker: a marker identifying this UDF request.

        Result will be signaled using events:
            - AQ_CREATE_UDF_OK on succeess.
            - AQ_CREATE_UDF_ERROR on failure.

        """
        return CreateUDF(self.meta_queue, path, name, marker).queue()

    def list_volumes(self):
        """List all the volumes the user has.

        This includes the volumes:
            - all the user's UDFs.
            - all the shares the user has accepted.
            - the root-root volume.

        Result will be signaled using events.
            - AQ_LIST_VOLUMES for the volume list.

        """
        return ListVolumes(self.meta_queue).queue()

    def delete_volume(self, volume_id):
        """Delete a volume on the server, removing the associated tree.

        @param volume_id: the id of the volume to delete.

        Result will be signaled using events:
            - AQ_DELETE_VOLUME_OK on succeess.
            - AQ_DELETE_VOLUME_ERROR on failure.

        """
        return DeleteVolume(self.meta_queue, volume_id).queue()

    def change_public_access(self, share_id, node_id, is_public):
        """Change the public access of a file.

        @param share_id: the id of the share holding the file.
        @param node_id: the id of the file.
        @param is_public: whether to make the file public.

        Result will be signaled using events:
            - AQ_CHANGE_PUBLIC_ACCESS_OK on success.
            - AQ_CHANGE_PUBLIC_ACCESS_ERROR on failure.
        """
        return ChangePublicAccess(
            self.meta_queue, share_id, node_id, is_public).queue()

    def get_public_files(self):
        """Get the list of public files.

        Result will be signaled using events:
            - AQ_PUBLIC_FILES_LIST_OK on success.
            - AQ_PUBLIC_FILES_LIST_ERROR on failure.
        """
        return GetPublicFiles(self.meta_queue).queue()

    def download(self, share_id, node_id, server_hash, fileobj_factory):
        """See .interfaces.IContentQueue.download."""
        return Download(self.content_queue, share_id, node_id, server_hash,
                        fileobj_factory).queue()

    def upload(self, share_id, node_id, previous_hash, hash, crc32,
               size, fileobj_factory, tempfile_factory=None):
        """See .interfaces.IContentQueue."""
        return Upload(self.content_queue, share_id, node_id, previous_hash,
                      hash, crc32, size,
                      fileobj_factory, tempfile_factory).queue()

    def _cancel_op(self, share_id, node_id, cmdclass, logstr):
        """Generalized form of cancel_upload and cancel_download."""
        log = mklog(logger, logstr, share_id, node_id,
                    share=share_id, node=node_id)
        log.debug('starting')
        for queue in self.meta_queue, self.content_queue:
            for cmd in queue.full_queue():
                if isinstance(cmd, cmdclass) \
                        and share_id == cmd.share_id \
                        and node_id in (cmd.marker_maybe, cmd.node_id):
                    log.debug('cancelling')
                    cmd.cancel()
        log.debug('finished')

    def cancel_upload(self, share_id, node_id):
        """See .interfaces.IContentQueue."""
        self._cancel_op(share_id, node_id, Upload, 'cancel_upload')

    def cancel_download(self, share_id, node_id):
        """See .interfaces.IContentQueue."""
        self._cancel_op(share_id, node_id, Download, 'cancel_download')

    def node_is_with_queued_move(self, share_id, node_id):
        """True if a Move is queued for that node."""
        return self.meta_queue.node_is_queued(Move, share_id, node_id)

    def get_delta(self, volume_id, generation):
        """Get a delta from generation for the volume.

        @param volume_id: the id of the volume to get the delta.
        @param generation: the generation to get the delta from.

        Result will be signaled using events:
            - AQ_DELTA_OK on succeess.
            - AQ_DELTA_ERROR on generic failure.
            - AQ_DELTA_NOT_POSSIBLE if the server told so

        """
        return GetDelta(self.meta_queue, volume_id, generation).queue()

    def rescan_from_scratch(self, volume_id):
        """Get a delta from scratch for the volume.

        @param volume_id: the id of the volume to get the delta.

        Result will be signaled using events:
            - AQ_RESCAN_FROM_SCRATCH_OK on succeess.
            - AQ_RESCAN_FROM_SCRATCH_ERROR on generic failure.
        """
        return GetDeltaFromScratch(self.meta_queue, volume_id).queue()

    def handle_SYS_ROOT_RECEIVED(self, root_id, mdid):
        """Demark the root node_id."""
        self.uuid_map.set(mdid, root_id)


class ActionQueueCommand(object):
    """Base of all the action queue commands."""

    # the info used in the protocol errors is hidden, but very useful!
    # pylint: disable-msg=W0212
    suppressed_error_messages = (
        [x for x in protocol_errors._error_mapping.values()
         if x is not protocol_errors.InternalError] +
        [protocol_errors.RequestCancelledError,
         twisted_errors.ConnectionDone, RequestCleanedUp]
    )

    retryable_errors = (
        protocol_errors.TryAgainError,
        twisted_errors.ConnectionDone,
        twisted_errors.ConnectionLost,
    )

    logged_attrs = ()
    possible_markers = ()
    is_runnable = True

    __slots__ = ('_queue', 'start_done', 'running', 'log',
                 'markers_resolved_deferred')

    def __init__(self, request_queue):
        """Initialize a command instance."""
        self._queue = request_queue
        self.start_done = False
        self.running = False
        self.log = None
        self.markers_resolved_deferred = defer.Deferred()

    def to_dict(self):
        """Dump logged attributes to a dict."""
        return dict((n, getattr(self, n, None)) for n in self.logged_attrs)

    def make_logger(self):
        """Create a logger for this object."""
        share_id = getattr(self, "share_id", UNKNOWN)
        node_id = getattr(self, "node_id", None) or \
                      getattr(self, "marker", UNKNOWN)
        return mklog(logger, self.__class__.__name__,
                     share_id, node_id, **self.to_dict())

    def check_conditions(self):
        """Check conditions on which the command may be waiting."""

    @defer.inlineCallbacks
    def demark(self):
        """Arrange to have maybe_markers realized."""
        # we need to issue all the DeferredMap.get's right now, to be
        # dereferenced later
        waiting_structure = []
        for name in self.possible_markers:
            marker = getattr(self, name)

            # if a marker, get the real value; if not, it's already there, so
            # no action needed
            if IMarker.providedBy(marker):
                self.log.debug("waiting for the real value of %r", marker)
                d = self.action_queue.uuid_map.get(marker)
                waiting_structure.append((name, marker, d))

        # now, we wait for all the dereferencings... if any
        for (name, marker, deferred) in waiting_structure:
            try:
                value = yield deferred
            except Exception, e:
                # on first failure, errback the marker resolved flag, and
                # quit waiting for other deferreds
                self.log.error("failed %r", marker)
                self.markers_resolved_deferred.errback(e)
                break
            else:
                self.log.debug("for %r got value %r", marker, value)
                setattr(self, name, value)
        else:
            # fire the deferred only if all markers finished ok
            self.markers_resolved_deferred.callback(True)

    def end_callback(self, arg):
        """It worked!"""
        if self.running:
            self.log.debug('success')
            return self.handle_success(arg)
        else:
            self.log.debug('not running, so no success')

    def end_errback(self, failure):
        """It failed!"""
        error_message = failure.getErrorMessage()
        if failure.check(*self.suppressed_error_messages):
            self.log.warn('failure: %s', error_message)
        else:
            self.log.error('failure: %s', error_message)
            self.log.debug('traceback follows:\n\n' + failure.getTraceback())
        was_running = self.running
        self.cleanup()
        if failure.check(*self.retryable_errors):
            if was_running:
                reactor.callLater(0.1, self.retry)
        else:
            return self.handle_failure(failure)

    def pre_queue_setup(self):
        """Set up before the command gets really queued."""
        self.log = self.make_logger()
        self.demark()

    def queue(self):
        """Queue the command."""
        self.pre_queue_setup()
        self.log.debug('queueing in the %s', self._queue.name)
        self._queue.queue(self)

    def cleanup(self):
        """Do whatever is needed to clean up from a failure.

        For example, stop producers and others that aren't cleaned up
        appropriately on their own.
        """
        self.running = False
        self.log.debug('cleanup')

    def _start(self):
        """Do the specialized pre-run setup."""
        return defer.succeed(None)

    @defer.inlineCallbacks
    def run(self):
        """Do the deed."""
        self.running = True
        try:
            if self.start_done:
                self.log.debug('retrying')
            else:
                self.log.debug('starting')
                yield self._start()
                self.start_done = True
                yield self.markers_resolved_deferred
        except Exception, e:
            yield self.end_errback(Failure(e))
        else:
            yield self._ready_to_run()
        finally:
            self.running = False

    def _ready_to_run(self):
        self.log.debug('running')
        if self.running:
            d = self._run()
        else:
            d = defer.succeed(None)
        d.addCallbacks(self.end_callback, self.end_errback)
        return d

    def handle_success(self, success):
        """Do anthing that's needed to handle success of the operation."""
        return success

    def handle_failure(self, failure):
        """Do anthing that's needed to handle failure of the operation.

        Note that cancellation and TRY_AGAIN are already handled.
        """
        return failure

    def cleanup_and_retry(self):
        """First, cleanup; then, retry :)"""
        self.log.debug('cleanup and retry')
        self.cleanup()
        return self.retry()

    def retry(self):
        """Request cancelled or TRY_AGAIN. Well then, try again!"""
        self.running = False
        self.log.debug('will retry')
        return self._queue.queue(self, on_top=True)

    @property
    def action_queue(self):
        """Return the action queue."""
        return self._queue.action_queue

    def __str__(self, str_attrs=None):
        """Return a str representation of the instance."""
        if str_attrs is None:
            str_attrs = self.logged_attrs
        name = self.__class__.__name__
        if len(str_attrs) == 0:
            return name
        attrs = [str(attr) + '=' + str(getattr(self, attr, None) or 'None') \
                 for attr in str_attrs]
        return ''.join([name, '(', ', '.join([attr for attr in attrs]), ')'])


class WaitForCondition(ActionQueueCommand):
    """A command which waits for some condition to be satisfied."""

    __slots__ = ('_condition', '_deferred')

    def __init__(self, request_queue, condition):
        """Initializes the command instance."""
        ActionQueueCommand.__init__(self, request_queue)
        self._condition = condition
        self._deferred = None

    def _run(self):
        """Returns a deferred which blocks until the condition is satisfied."""
        if self._condition():
            d = defer.succeed(None)
            self._deferred = None
        else:
            d = defer.Deferred()
            self._deferred = d
        return d

    def check_conditions(self):
        """Poll the action's condition."""
        if self._deferred is not None and self._condition():
            try:
                self._deferred.callback(None)
                self._deferred = None
            except defer.AlreadyCalledError:
                return

    def cleanup(self):
        """Does cleanup."""
        self._deferred = None
        ActionQueueCommand.cleanup(self)


class MakeThing(ActionQueueCommand):
    """Base of MakeFile and MakeDir."""

    __slots__ = ('share_id', 'parent_id', 'name', 'marker')
    logged_attrs = __slots__
    possible_markers = 'parent_id',

    def __init__(self, request_queue, share_id, parent_id, name, marker):
        super(MakeThing, self).__init__(request_queue)
        self.share_id = share_id
        self.parent_id = parent_id
        # Unicode boundary! the name is Unicode in protocol and server, but
        # here we use bytes for paths
        self.name = name.decode("utf8")
        self.marker = marker

    def _run(self):
        """Do the actual running."""
        maker = getattr(self.action_queue.client, self.client_method)
        return maker(self.share_id, self.parent_id, self.name)

    def handle_success(self, request):
        """It worked! Push the event."""
        # note that we're not getting the new name from the answer
        # message, if we would get it, we would have another Unicode
        # boundary with it
        d = dict(marker=self.marker, new_id=request.new_id,
                 new_generation=request.new_generation,
                 volume_id=self.share_id)
        self.action_queue.event_queue.push(self.ok_event_name, **d)
        return request

    def handle_failure(self, failure):
        """It didn't work! Push the event."""
        self.action_queue.event_queue.push(self.error_event_name,
                                           marker=self.marker,
                                           failure=failure)


class MakeFile(MakeThing):
    """Make a file."""
    __slots__ = ()
    ok_event_name = 'AQ_FILE_NEW_OK'
    error_event_name = 'AQ_FILE_NEW_ERROR'
    client_method = 'make_file'


class MakeDir(MakeThing):
    """Make a directory."""
    __slots__ = ()
    ok_event_name = 'AQ_DIR_NEW_OK'
    error_event_name = 'AQ_DIR_NEW_ERROR'
    client_method = 'make_dir'


class Move(ActionQueueCommand):
    """Move a file or directory."""
    __slots__ = ('share_id', 'node_id', 'old_parent_id',
                 'new_parent_id', 'new_name')
    logged_attrs = __slots__
    possible_markers = 'node_id', 'old_parent_id', 'new_parent_id'

    def __init__(self, request_queue, share_id, node_id, old_parent_id,
                 new_parent_id, new_name):
        super(Move, self).__init__(request_queue)
        self.share_id = share_id
        self.node_id = node_id
        self.old_parent_id = old_parent_id
        self.new_parent_id = new_parent_id
        # Unicode boundary! the name is Unicode in protocol and server, but
        # here we use bytes for paths
        self.new_name = new_name.decode("utf8")

    def _run(self):
        """Do the actual running."""
        return self.action_queue.client.move(self.share_id,
                                             self.node_id,
                                             self.new_parent_id,
                                             self.new_name)

    def handle_success(self, request):
        """It worked! Push the event."""
        d = dict(share_id=self.share_id, node_id=self.node_id,
                 new_generation=request.new_generation)
        self.action_queue.event_queue.push('AQ_MOVE_OK', **d)
        return request

    def handle_failure(self, failure):
        """It didn't work! Push the event."""
        self.action_queue.event_queue.push('AQ_MOVE_ERROR',
                                           error=failure.getErrorMessage(),
                                           share_id=self.share_id,
                                           node_id=self.node_id,
                                           old_parent_id=self.old_parent_id,
                                           new_parent_id=self.new_parent_id,
                                           new_name=self.new_name)


class Unlink(ActionQueueCommand):
    """
    Unlink a file or dir
    """
    __slots__ = ('share_id', 'node_id', 'parent_id')
    logged_attrs = __slots__
    possible_markers = 'node_id', 'parent_id'

    def __init__(self, request_queue, share_id, parent_id, node_id):
        super(Unlink, self).__init__(request_queue)
        self.share_id = share_id
        self.node_id = node_id
        self.parent_id = parent_id

    def _run(self):
        """Do the actual running."""
        return self.action_queue.client.unlink(self.share_id, self.node_id)

    def handle_success(self, request):
        """It worked! Push the event."""
        d = dict(share_id=self.share_id, parent_id=self.parent_id,
                 node_id=self.node_id, new_generation=request.new_generation)
        self.action_queue.event_queue.push('AQ_UNLINK_OK', **d)
        return request

    def handle_failure(self, failure):
        """It didn't work! Push the event."""
        self.action_queue.event_queue.push('AQ_UNLINK_ERROR',
                                           error=failure.getErrorMessage(),
                                           share_id=self.share_id,
                                           parent_id=self.parent_id,
                                           node_id=self.node_id)


class ListShares(ActionQueueCommand):
    """
    List shares shared to me
    """
    __slots__ = ()

    def _run(self):
        """
        Do the actual running
        """
        return self.action_queue.client.list_shares()

    def handle_success(self, success):
        """
        It worked! Push the event.
        """
        self.action_queue.event_queue.push('AQ_SHARES_LIST',
                                           shares_list=success)

    def handle_failure(self, failure):
        """
        It didn't work! Push the event.
        """
        self.action_queue.event_queue.push('AQ_LIST_SHARES_ERROR',
                                           error=failure.getErrorMessage())


class FreeSpaceInquiry(ActionQueueCommand):
    """Inquire about free space."""

    def __init__(self, request_queue, share_id):
        """Initialize the instance."""
        super(FreeSpaceInquiry, self).__init__(request_queue)
        self.share_id = share_id

    def _run(self):
        """Do the query."""
        return self.action_queue.client.get_free_space(self.share_id)

    def handle_success(self, success):
        """Publish the free space information."""
        self.action_queue.event_queue.push('SV_FREE_SPACE',
                                           share_id=success.share_id,
                                           free_bytes=success.free_bytes)

    def handle_failure(self, failure):
        """Publish the error."""
        self.action_queue.event_queue.push('AQ_FREE_SPACE_ERROR',
                                           error=failure.getErrorMessage())


class AccountInquiry(ActionQueueCommand):
    """Query user account information."""

    def _run(self):
        """Make the actual request."""
        return self.action_queue.client.get_account_info()

    def handle_success(self, success):
        """Publish the account information to the event queue."""
        self.action_queue.event_queue.push('SV_ACCOUNT_CHANGED',
                                           account_info=success)

    def handle_failure(self, failure):
        """Publish the error."""
        self.action_queue.event_queue.push('AQ_ACCOUNT_ERROR',
                                           error=failure.getErrorMessage())


class AnswerShare(ActionQueueCommand):
    """Answer a share offer."""

    __slots__ = ('share_id', 'answer')

    def __init__(self, request_queue, share_id, answer):
        super(AnswerShare, self).__init__(request_queue)
        self.share_id = share_id
        self.answer = answer

    def _run(self):
        """
        Do the actual running
        """
        return self.action_queue.client.accept_share(self.share_id,
                                                     self.answer)

    def handle_success(self, success):
        """
        It worked! Push the event.
        """
        self.action_queue.event_queue.push('AQ_ANSWER_SHARE_OK',
                                           share_id=self.share_id,
                                           answer=self.answer)
        return success

    def handle_failure(self, failure):
        """
        It didn't work. Push the event.
        """
        self.action_queue.event_queue.push('AQ_ANSWER_SHARE_ERROR',
                                           share_id=self.share_id,
                                           answer=self.answer,
                                           error=failure.getErrorMessage())
        return failure


class CreateShare(ActionQueueCommand):
    """Offer a share to somebody."""

    __slots__ = ('node_id', 'share_to', 'name', 'access_level',
                 'marker', 'use_http')
    possible_markers = 'node_id',

    def __init__(self, request_queue, node_id, share_to, name, access_level,
                 marker):
        super(CreateShare, self).__init__(request_queue)
        self.node_id = node_id
        self.share_to = share_to
        self.name = name
        self.access_level = access_level
        self.marker = marker
        self.use_http = False

        if share_to and re.match(EREGEX, share_to):
            self.use_http = True

    def _create_share_http(self, node_id, user, name, read_only, deferred):
        """Create a share using the HTTP Web API method."""

        url = "https://one.ubuntu.com/files/api/offer_share/"
        method = oauth.OAuthSignatureMethod_PLAINTEXT()
        request = oauth.OAuthRequest.from_consumer_and_token(
            http_url=url,
            http_method="POST",
            oauth_consumer=self.action_queue.consumer,
            token=self.action_queue.token)
        request.sign_request(method, self.action_queue.consumer,
                             self.action_queue.token)
        data = dict(offer_to_email=user,
                    read_only=read_only,
                    node_id=node_id,
                    share_name=name)
        pdata = urlencode(data)
        headers = request.to_header()
        req = Request(url, pdata, headers)
        try:
            urlopen(req)
        except HTTPError, e:
            deferred.errback(Failure(e))

        deferred.callback(None)

    def _run(self):
        """
        Do the actual running
        """
        if self.use_http:
            # External user, do the HTTP REST method
            deferred = defer.Deferred()
            d = threads.deferToThread(self._create_share_http,
                                      self.node_id, self.share_to,
                                      self.name, self.access_level != 'Modify',
                                      deferred)
            d.addErrback(deferred.errback)
            return deferred
        else:
            return self.action_queue.client.create_share(self.node_id,
                                                         self.share_to,
                                                         self.name,
                                                         self.access_level)

    def handle_success(self, success):
        """
        It worked! Push the event.
        """
        # We don't get a share_id back from the HTTP REST method
        if not self.use_http:
            self.action_queue.event_queue.push('AQ_CREATE_SHARE_OK',
                                               share_id=success.share_id,
                                               marker=self.marker)
        return success

    def handle_failure(self, failure):
        """
        It didn't work! Push the event.
        """
        self.action_queue.event_queue.push('AQ_CREATE_SHARE_ERROR',
                                           marker=self.marker,
                                           error=failure.getErrorMessage())


class CreateUDF(ActionQueueCommand):
    """Create a new User Defined Folder."""

    __slots__ = ('path', 'name', 'marker')

    def __init__(self, request_queue, path, name, marker):
        super(CreateUDF, self).__init__(request_queue)
        self.path = path
        # XXX Unicode boundary?
        self.name = name
        self.marker = marker

    def _run(self):
        """Do the actual running."""
        return self.action_queue.client.create_udf(self.path, self.name)

    def handle_success(self, success):
        """It worked! Push the success event."""
        kwargs = dict(marker=self.marker,
                      volume_id=success.volume_id,
                      node_id=success.node_id)
        self.action_queue.event_queue.push('AQ_CREATE_UDF_OK', **kwargs)
        return success

    def handle_failure(self, failure):
        """It didn't work! Push the failure event."""
        self.action_queue.event_queue.push('AQ_CREATE_UDF_ERROR',
                                           marker=self.marker,
                                           error=failure.getErrorMessage())


class ListVolumes(ActionQueueCommand):
    """List all the volumes for a given user."""

    __slots__ = ()

    def _run(self):
        """Do the actual running."""
        return self.action_queue.client.list_volumes()

    def handle_success(self, success):
        """It worked! Push the success event."""
        self.action_queue.event_queue.push('AQ_LIST_VOLUMES',
                                           volumes=success.volumes)
        return success

    def handle_failure(self, failure):
        """It didn't work! Push the failure event."""
        self.action_queue.event_queue.push('AQ_LIST_VOLUMES_ERROR',
                                           error=failure.getErrorMessage())


class DeleteVolume(ActionQueueCommand):
    """Delete an exsistent volume."""

    __slots__ = ('volume_id', 'marker')

    def __init__(self, request_queue, volume_id):
        super(DeleteVolume, self).__init__(request_queue)
        self.volume_id = volume_id

    def _run(self):
        """Do the actual running."""
        return self.action_queue.client.delete_volume(self.volume_id)

    def handle_success(self, success):
        """It worked! Push the success event."""
        self.action_queue.event_queue.push('AQ_DELETE_VOLUME_OK',
                                           volume_id=self.volume_id)
        return success

    def handle_failure(self, failure):
        """It didn't work! Push the failure event."""
        self.action_queue.event_queue.push('AQ_DELETE_VOLUME_ERROR',
                                           volume_id=self.volume_id,
                                           error=failure.getErrorMessage())

class DeltaList(list):
    """A list with a smal and fixed representation.

    We use delta lists instead of regular lists when we push deltas into
    the event queue so when we log the arguments of the event that was pushed
    we dont flood the logs.
    """

    def __init__(self, source):
        super(DeltaList, self).__init__()
        self[:] = source

    def __repr__(self):
        """A short representation for the list."""
        return "<DeltaList(len=%s)>" % (len(self),)

    __str__ = __repr__


class GetDelta(ActionQueueCommand):
    """Gets a delta from a generation for a volume."""

    def __init__(self, request_queue, volume_id, generation):
        super(GetDelta, self).__init__(request_queue)
        self.volume_id = volume_id
        self.generation = generation

    def _run(self):
        """Do the actual running."""
        return self.action_queue.client.get_delta(self.volume_id,
                                                  self.generation)

    def queue(self):
        """Queue the command only if it should."""
        # first start part to get the logger
        self.pre_queue_setup()

        for cmd in self._queue.waiting:
            if isinstance(cmd, GetDelta) and cmd.volume_id == self.volume_id:
                # other GetDelta for same volume! leave the smaller one
                if cmd.generation > self.generation:
                    m = "removing previous command because bigger gen num: %s"
                    self.log.debug(m, cmd)
                    self._queue.waiting.remove(cmd)
                    break
                else:
                    self.log.debug("not queueing self because there's other "
                                   "command with less or same gen num")
                    return

        # no similar command
        self.log.debug('queueing in the %s', self._queue.name)
        self._queue.queue(self)

    def handle_success(self, request):
        """It worked! Push the success event."""
        data = dict(
            volume_id=self.volume_id,
            delta_content=DeltaList(request.response),
            end_generation=request.end_generation,
            full=request.full,
            free_bytes=request.free_bytes,
        )
        self.action_queue.event_queue.push('AQ_DELTA_OK', **data)
        return request

    def handle_failure(self, failure):
        """It didn't work! Push the failure event."""
        if failure.check(protocol_errors.CannotProduceDelta):
            self.action_queue.event_queue.push('AQ_DELTA_NOT_POSSIBLE',
                                               volume_id=self.volume_id)
        else:
            self.action_queue.event_queue.push('AQ_DELTA_ERROR',
                                               volume_id=self.volume_id,
                                               error=failure.getErrorMessage())

    def make_logger(self):
        """Create a logger for this object."""
        return mklog(logger, 'GetDelta', self.volume_id,
                     None, generation=self.generation)


class GetDeltaFromScratch(ActionQueueCommand):
    """Gets a delta from scratch."""

    def __init__(self, request_queue, volume_id):
        super(GetDeltaFromScratch, self).__init__(request_queue)
        self.volume_id = volume_id

    def _run(self):
        """Do the actual running."""
        return self.action_queue.client.get_delta(self.volume_id,
                                                  from_scratch=True)

    def queue(self):
        """Queue the command only if it should."""
        # first start part to get the logger
        self.pre_queue_setup()

        for cmd in self._queue.waiting:
            if isinstance(cmd, GetDeltaFromScratch) and \
                    cmd.volume_id == self.volume_id:
                # other GetDeltaFromScratch for same volume! skip self
                m = "GetDeltaFromScratch already queued, not queueing self"
                self.log.debug(m)
                return

        # no similar command
        self.log.debug('queueing in the %s', self._queue.name)
        self._queue.queue(self)

    def handle_success(self, request):
        """It worked! Push the success event."""
        data = dict(
            volume_id=self.volume_id,
            delta_content=DeltaList(request.response),
            end_generation=request.end_generation,
            free_bytes=request.free_bytes,
        )
        self.action_queue.event_queue.push('AQ_RESCAN_FROM_SCRATCH_OK', **data)
        return request

    def handle_failure(self, failure):
        """It didn't work! Push the failure event."""
        self.action_queue.event_queue.push('AQ_RESCAN_FROM_SCRATCH_ERROR',
                                           volume_id=self.volume_id,
                                           error=failure.getErrorMessage())

    def make_logger(self):
        """Create a logger for this object."""
        return mklog(logger, 'GetDeltaFromScratch', self.volume_id, None)



class ChangePublicAccess(ActionQueueCommand):
    """Change the public access of a file."""

    __slots__ = ('share_id', 'node_id', 'is_public')
    possible_markers = 'node_id',

    def __init__(self, request_queue, share_id, node_id, is_public):
        super(ChangePublicAccess, self).__init__(request_queue)
        self.share_id = share_id
        self.node_id = node_id
        self.is_public = is_public

    def _change_public_access_http(self):
        """Change public access using the HTTP Web API method."""

        # Construct the node key.
        node_key = base64.urlsafe_b64encode(self.node_id.bytes).strip("=")
        if self.share_id is not None:
            node_key = "%s:%s" % (
                base64.urlsafe_b64encode(self.share_id.bytes).strip("="),
                node_key)

        url = "https://one.ubuntu.com/files/api/set_public/%s" % (node_key,)
        method = oauth.OAuthSignatureMethod_PLAINTEXT()
        request = oauth.OAuthRequest.from_consumer_and_token(
            http_url=url,
            http_method="POST",
            oauth_consumer=self.action_queue.consumer,
            token=self.action_queue.token)
        request.sign_request(method, self.action_queue.consumer,
                             self.action_queue.token)
        data = dict(is_public=bool(self.is_public))
        pdata = urlencode(data)
        headers = request.to_header()
        req = Request(url, pdata, headers)
        response = urlopen(req)
        return simplejson.load(response)

    def _run(self):
        """See ActionQueueCommand."""
        return threads.deferToThread(self._change_public_access_http)

    def handle_success(self, success):
        """See ActionQueueCommand."""
        self.action_queue.event_queue.push('AQ_CHANGE_PUBLIC_ACCESS_OK',
                                           share_id=self.share_id,
                                           node_id=self.node_id,
                                           is_public=success['is_public'],
                                           public_url=success['public_url'])
        return success

    def handle_failure(self, failure):
        """
        It didn't work! Push the event.
        """
        if issubclass(failure.type, HTTPError):
            message = failure.value.read()
        else:
            message = failure.getErrorMessage()
        self.action_queue.event_queue.push('AQ_CHANGE_PUBLIC_ACCESS_ERROR',
                                           share_id=self.share_id,
                                           node_id=self.node_id,
                                           error=message)


class GetPublicFiles(ActionQueueCommand):
    """Get the list of public files."""

    __slots__ = ('_url',)

    def __init__(self, request_queue, base_url='https://one.ubuntu.com'):
        super(GetPublicFiles, self).__init__(request_queue)
        self._url = urljoin(base_url, 'files/api/public_files')

    def _get_public_files_http(self):
        """Get public files list using the HTTP Web API method."""

        method = oauth.OAuthSignatureMethod_PLAINTEXT()
        request = oauth.OAuthRequest.from_consumer_and_token(
            http_url=self._url,
            http_method="GET",
            oauth_consumer=self.action_queue.consumer,
            token=self.action_queue.token)
        request.sign_request(method, self.action_queue.consumer,
                             self.action_queue.token)
        headers = request.to_header()
        req = Request(self._url, headers=headers)
        response = urlopen(req)
        files = simplejson.load(response)
        # translate nodekeys to (volume_id, node_id)
        for pf in files:
            _, node_id = self.split_nodekey(pf.pop('nodekey'))
            volume_id = pf['volume_id']
            pf['volume_id'] = '' if volume_id is None else volume_id
            pf['node_id'] = node_id
        return files

    def _run(self):
        """See ActionQueueCommand."""
        return threads.deferToThread(self._get_public_files_http)

    def handle_success(self, success):
        """See ActionQueueCommand."""
        self.action_queue.event_queue.push('AQ_PUBLIC_FILES_LIST_OK',
                                           public_files=success)
        return success

    def handle_failure(self, failure):
        """
        It didn't work! Push the event.
        """
        if issubclass(failure.type, HTTPError):
            message = failure.value.read()
        else:
            message = failure.getErrorMessage()
        self.action_queue.event_queue.push('AQ_PUBLIC_FILES_LIST_ERROR',
                                           error=message)

    def split_nodekey(self, nodekey):
        """Split a node key into a share_id, node_id."""
        if nodekey is None:
            return None, None
        if ":" in nodekey:
            parts = nodekey.split(":")
            return self.decode_uuid(parts[0]), self.decode_uuid(parts[1])
        else:
            return '', self.decode_uuid(nodekey)

    def decode_uuid(self, encoded):
        """Return a uuid from the encoded value.

        If the value isn't UUID, just return the decoded value
        """
        if encoded:
            data = str(encoded) + '=' * (len(encoded) % 4)
            value = base64.urlsafe_b64decode(data)
        try:
            return str(uuid.UUID(bytes=value))
        except ValueError:
            return value


class Download(ActionQueueCommand):
    """Get the contents of a file."""

    __slots__ = ('share_id', 'node_id', 'server_hash', 'fileobj_factory',
                 'fileobj', 'gunzip', 'marker_maybe', 'cancelled',
                 'download_req', 'deflated_size', 'n_bytes_read_last')
    logged_attrs = ('share_id', 'node_id', 'server_hash', 'fileobj_factory')
    possible_markers = 'node_id',

    def __init__(self, request_queue, share_id, node_id, server_hash,
                 fileobj_factory):
        super(Download, self).__init__(request_queue)
        self.share_id = share_id
        self.marker_maybe = self.node_id = node_id
        self.server_hash = server_hash
        self.fileobj_factory = fileobj_factory
        self.fileobj = None
        self.gunzip = zlib.decompressobj()
        self.cancelled = False
        self.download_req = None
        self.action_queue.cancel_download(self.share_id, self.node_id)

    def cancel(self):
        """Cancel the download."""
        self.cancelled = True
        if self.download_req is not None:
            self.download_req.cancel()
        self._queue.remove(self)
        self.cleanup()

    def _run(self):
        """Do the actual running."""
        if self.cancelled:
            return defer.fail(RequestCleanedUp('CANCELLED'))
        if self.fileobj is None:
            try:
                self.fileobj = self.fileobj_factory()
            except StandardError:
                self.log.debug(traceback.format_exc())
                msg = DefaultException('unable to build fileobj'
                                       ' (file went away?)'
                                       ' so aborting the download.')
                return defer.fail(Failure(msg))
        downloading = self.action_queue.downloading
        if (self.share_id, self.node_id) not in downloading:
            downloading[self.share_id, self.node_id] = {'n_bytes_read': 0,
                                                        'command': self}
        assert downloading[self.share_id, self.node_id]['command'] is self
        offset = downloading[self.share_id, self.node_id]['n_bytes_read']
        self.progress_start(offset)
        self.action_queue.event_queue.push('AQ_DOWNLOAD_STARTED',
                                           share_id=self.share_id,
                                           node_id=self.node_id,
                                           server_hash=self.server_hash)

        req = self.action_queue.client.get_content_request(
            self.share_id, self.node_id, self.server_hash,
            offset=offset,
            callback=self.cb, node_attr_callback=self.nacb)
        self.download_req = req
        downloading[self.share_id, self.node_id]['req'] = req
        d = req.deferred
        d.addBoth(lambda x: defer.fail(RequestCleanedUp('CANCELLED'))
                  if self.cancelled else x)
        d.addCallback(passit(
                lambda _: downloading.pop((self.share_id, self.node_id))))
        return d

    def handle_success(self, _):
        """It worked! Push the event."""
        self.sync()
        # send a COMMIT, the Nanny will issue the FINISHED if it's ok
        self.action_queue.event_queue.push('AQ_DOWNLOAD_COMMIT',
                                           share_id=self.share_id,
                                           node_id=self.node_id,
                                           server_hash=self.server_hash)

    def handle_failure(self, failure):
        """It didn't work! Push the event."""
        downloading = self.action_queue.downloading
        if (self.share_id, self.node_id) in downloading and \
            downloading[self.share_id, self.node_id]['command'] is self:
            del downloading[self.share_id, self.node_id]
        self.reset_fileobj()
        if failure.check(protocol_errors.RequestCancelledError,
                         RequestCleanedUp):
            self.action_queue.event_queue.push('AQ_DOWNLOAD_CANCELLED',
                                               share_id=self.share_id,
                                               node_id=self.node_id,
                                               server_hash=self.server_hash)
        elif failure.check(protocol_errors.DoesNotExistError):
            self.action_queue.event_queue.push('AQ_DOWNLOAD_DOES_NOT_EXIST',
                                               share_id=self.share_id,
                                               node_id=self.node_id)
        else:
            self.action_queue.event_queue.push('AQ_DOWNLOAD_ERROR',
                                               error=failure.getErrorMessage(),
                                               share_id=self.share_id,
                                               node_id=self.node_id,
                                               server_hash=self.server_hash)

    def reset_fileobj(self):
        """Rewind and empty the file.

        Usefult for get it ready to try again if necessary.
        """
        self.log.debug('reset fileobj')
        if self.fileobj is not None:
            self.fileobj.seek(0, 0)
            self.fileobj.truncate(0)

    def cb(self, bytes):
        """A streaming decompressor."""
        dloading = self.action_queue.downloading[self.share_id,
                                                 self.node_id]
        dloading['n_bytes_read'] += len(bytes)
        self.fileobj.write(self.gunzip.decompress(bytes))
        self.fileobj.flush()     # not strictly necessary but nice to
                                 # see the downloaded size
        self.progress_hook(dloading['n_bytes_read'])

    def progress_start(self, n_bytes_read_already):
        """Start tracking progress.

        Consider that n_bytes_read_already have been already read.
        """
        self.n_bytes_read_last = n_bytes_read_already

    def progress_hook(self, n_bytes_read):
        """Convert downloading progress into an event."""
        n_bytes_read_last = self.n_bytes_read_last
        self.n_bytes_read_last = n_bytes_read
        # produce an event only if there has been a threshold-sized progress
        if n_bytes_read - n_bytes_read_last < TRANSFER_PROGRESS_THRESHOLD:
            return
        self.action_queue.event_queue.push('AQ_DOWNLOAD_FILE_PROGRESS',
                                      share_id=self.share_id,
                                      node_id=self.node_id,
                                      n_bytes_read=n_bytes_read,
                                      deflated_size=self.deflated_size)

    def nacb(self, **kwargs):
        """Set the node attrs in the 'currently downloading' dict."""
        self.deflated_size = kwargs['deflated_size']
        self.action_queue.downloading[self.share_id,
                                      self.node_id].update(kwargs)

    def sync(self):
        """Flush the buffers and sync them to disk if possible."""
        remains = self.gunzip.flush()
        if remains:
            self.fileobj.write(remains)
        self.fileobj.flush()
        if getattr(self.fileobj, 'fileno', None) is not None:
            # it's a real file, with a fileno! Let's sync its data
            # out to disk
            os.fsync(self.fileobj.fileno())
        self.fileobj.close()


class Upload(ActionQueueCommand):
    """Upload stuff to a file."""

    __slots__ = ('share_id', 'node_id', 'previous_hash', 'hash', 'crc32',
                 'size', 'fileobj_factory', 'tempfile_factory',
                 'deflated_size', 'tempfile', 'cancelled', 'upload_req',
                 'marker_maybe', 'n_bytes_written_last')

    logged_attrs = ('share_id', 'node_id', 'previous_hash', 'hash', 'crc32',
                    'size', 'fileobj_factory')
    retryable_errors = ActionQueueCommand.retryable_errors + (
                                        protocol_errors.UploadInProgressError,)
    possible_markers = 'node_id',

    def __init__(self, request_queue, share_id, node_id, previous_hash, hash,
                 crc32, size, fileobj_factory, tempfile_factory):
        super(Upload, self).__init__(request_queue)
        self.share_id = share_id
        self.node_id = node_id
        self.previous_hash = previous_hash
        self.hash = hash
        self.crc32 = crc32
        self.size = size
        self.fileobj_factory = fileobj_factory
        self.tempfile_factory = tempfile_factory
        self.deflated_size = None
        self.tempfile = None
        self.cancelled = False
        self.upload_req = None
        self.marker_maybe = None
        if (self.share_id, self.node_id) in self.action_queue.uploading:
            self.action_queue.cancel_upload(self.share_id, self.node_id)
        self.n_bytes_written_last = None # set by _run

    @property
    def is_runnable(self):
        """Tell if the upload is ok to be carried on.

        Return True if there is sufficient space available to complete
        the upload, or if the upload is cancelled so it can pursue
        its fate.
        """
        if self.cancelled:
            return True
        else:
            return self.action_queue.have_sufficient_space_for_upload(
                                                    self.share_id, self.size)

    def cancel(self):
        """Cancel the upload."""
        self.cancelled = True
        if self.upload_req is not None:
            self.upload_req.cancel()
        self._queue.remove(self)
        self.cleanup()

    def cleanup(self):
        """Cleanup: stop the producer."""
        super(Upload, self).cleanup()
        if self.upload_req is not None and self.upload_req.producer is not None:
            self.log.debug('stopping the producer')
            self.upload_req.producer.stopProducing()

    def _start(self):
        """Do the specialized pre-run setup."""
        d = defer.Deferred()

        uploading = {"hash": self.hash, "req": self}
        self.action_queue.uploading[self.share_id, self.node_id] = uploading

        d = self.action_queue.zip_queue.zip(self)
        d.addBoth(lambda x: defer.fail(RequestCleanedUp('CANCELLED'))
                  if self.cancelled else x)
        return d

    def _run(self):
        """Do the actual running."""
        if self.cancelled:
            return defer.fail(RequestCleanedUp('CANCELLED'))
        uploading = {"hash": self.hash, "deflated_size": self.deflated_size,
                     "req": self}
        self.action_queue.uploading[self.share_id, self.node_id] = uploading

        self.action_queue.event_queue.push('AQ_UPLOAD_STARTED',
                                           share_id=self.share_id,
                                           node_id=self.node_id,
                                           hash=self.hash)

        if getattr(self.tempfile, 'name', None) is not None:
            self.tempfile = open(self.tempfile.name)
        self.n_bytes_written_last = 0
        f = UploadProgressWrapper(self.tempfile, uploading, self.progress_hook)
        req = self.action_queue.client.put_content_request(
            self.share_id, self.node_id, self.previous_hash, self.hash,
            self.crc32, self.size, self.deflated_size, f)
        self.upload_req = req
        d = req.deferred
        d.addBoth(lambda x: defer.fail(RequestCleanedUp('CANCELLED'))
                  if self.cancelled else x)
        d.addBoth(passit(lambda _:
                             self.action_queue.uploading.pop((self.share_id,
                                                              self.node_id))))
        d.addBoth(passit(lambda _: self.tempfile.close()))
        return d

    def progress_hook(self, n_bytes_written):
        """Convert uploading progress into an event."""
        n_bytes_written_last = self.n_bytes_written_last
        self.n_bytes_written_last = n_bytes_written
        # produce an event only if there has been a threshold-sized progress
        if n_bytes_written - n_bytes_written_last < TRANSFER_PROGRESS_THRESHOLD:
            return
        self.action_queue.event_queue.push('AQ_UPLOAD_FILE_PROGRESS',
                                      share_id=self.share_id,
                                      node_id=self.node_id,
                                      n_bytes_written=n_bytes_written,
                                      deflated_size=self.deflated_size)

    def handle_success(self, request):
        """It worked! Push the event."""
        # remove the temporary file if have one
        if getattr(self.tempfile, 'name', None) is not None:
            os.unlink(self.tempfile.name)

        # send the event
        d = dict(share_id=self.share_id, node_id=self.node_id, hash=self.hash,
                 new_generation=request.new_generation)
        self.action_queue.event_queue.push('AQ_UPLOAD_FINISHED', **d)

    def handle_failure(self, failure):
        """It didn't work! Push the event."""
        if getattr(self.tempfile, 'name', None) is not None:
            os.unlink(self.tempfile.name)

        if failure.check(protocol_errors.QuotaExceededError):
            error = failure.value
            self.action_queue.event_queue.push('SYS_QUOTA_EXCEEDED',
                                               volume_id=str(error.share_id),
                                               free_bytes=error.free_bytes)

        self.action_queue.event_queue.push('AQ_UPLOAD_ERROR',
                                           error=failure.getErrorMessage(),
                                           share_id=self.share_id,
                                           node_id=self.node_id,
                                           hash=self.hash)