Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

channel.py 82KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  1. """AMQP Channels"""
  2. # Copyright (C) 2007-2008 Barry Pederson <bp@barryp.org>
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2.1 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
  17. from __future__ import absolute_import
  18. import logging
  19. from collections import defaultdict
  20. from warnings import warn
  21. from .abstract_channel import AbstractChannel
  22. from .exceptions import (
  23. ChannelError, ConsumerCancelled, NotConfirmed, error_for_code,
  24. )
  25. from .five import Queue
  26. from .protocol import basic_return_t, queue_declare_ok_t
  27. from .serialization import AMQPWriter
  28. __all__ = ['Channel']
  29. AMQP_LOGGER = logging.getLogger('amqp')
  30. EXCHANGE_AUTODELETE_DEPRECATED = """\
  31. The auto_delete flag for exchanges has been deprecated and will be removed
  32. from py-amqp v1.5.0.\
  33. """
  34. class VDeprecationWarning(DeprecationWarning):
  35. pass
  36. class Channel(AbstractChannel):
  37. """Work with channels
  38. The channel class provides methods for a client to establish a
  39. virtual connection - a channel - to a server and for both peers to
  40. operate the virtual connection thereafter.
  41. GRAMMAR::
  42. channel = open-channel *use-channel close-channel
  43. open-channel = C:OPEN S:OPEN-OK
  44. use-channel = C:FLOW S:FLOW-OK
  45. / S:FLOW C:FLOW-OK
  46. / functional-class
  47. close-channel = C:CLOSE S:CLOSE-OK
  48. / S:CLOSE C:CLOSE-OK
  49. """
  50. def __init__(self, connection, channel_id=None, auto_decode=True):
  51. """Create a channel bound to a connection and using the specified
  52. numeric channel_id, and open on the server.
  53. The 'auto_decode' parameter (defaults to True), indicates
  54. whether the library should attempt to decode the body
  55. of Messages to a Unicode string if there's a 'content_encoding'
  56. property for the message. If there's no 'content_encoding'
  57. property, or the decode raises an Exception, the message body
  58. is left as plain bytes.
  59. """
  60. if channel_id:
  61. connection._claim_channel_id(channel_id)
  62. else:
  63. channel_id = connection._get_free_channel_id()
  64. AMQP_LOGGER.debug('using channel_id: %d', channel_id)
  65. super(Channel, self).__init__(connection, channel_id)
  66. self.is_open = False
  67. self.active = True # Flow control
  68. self.returned_messages = Queue()
  69. self.callbacks = {}
  70. self.cancel_callbacks = {}
  71. self.auto_decode = auto_decode
  72. self.events = defaultdict(set)
  73. self.no_ack_consumers = set()
  74. # set first time basic_publish_confirm is called
  75. # and publisher confirms are enabled for this channel.
  76. self._confirm_selected = False
  77. if self.connection.confirm_publish:
  78. self.basic_publish = self.basic_publish_confirm
  79. self._x_open()
  80. def _do_close(self):
  81. """Tear down this object, after we've agreed to close
  82. with the server."""
  83. AMQP_LOGGER.debug('Closed channel #%d', self.channel_id)
  84. self.is_open = False
  85. channel_id, self.channel_id = self.channel_id, None
  86. connection, self.connection = self.connection, None
  87. if connection:
  88. connection.channels.pop(channel_id, None)
  89. connection._avail_channel_ids.append(channel_id)
  90. self.callbacks.clear()
  91. self.cancel_callbacks.clear()
  92. self.events.clear()
  93. self.no_ack_consumers.clear()
  94. def _do_revive(self):
  95. self.is_open = False
  96. self._x_open()
  97. def close(self, reply_code=0, reply_text='', method_sig=(0, 0)):
  98. """Request a channel close
  99. This method indicates that the sender wants to close the
  100. channel. This may be due to internal conditions (e.g. a forced
  101. shut-down) or due to an error handling a specific method, i.e.
  102. an exception. When a close is due to an exception, the sender
  103. provides the class and method id of the method which caused
  104. the exception.
  105. RULE:
  106. After sending this method any received method except
  107. Channel.Close-OK MUST be discarded.
  108. RULE:
  109. The peer sending this method MAY use a counter or timeout
  110. to detect failure of the other peer to respond correctly
  111. with Channel.Close-OK..
  112. PARAMETERS:
  113. reply_code: short
  114. The reply code. The AMQ reply codes are defined in AMQ
  115. RFC 011.
  116. reply_text: shortstr
  117. The localised reply text. This text can be logged as an
  118. aid to resolving issues.
  119. class_id: short
  120. failing method class
  121. When the close is provoked by a method exception, this
  122. is the class of the method.
  123. method_id: short
  124. failing method ID
  125. When the close is provoked by a method exception, this
  126. is the ID of the method.
  127. """
  128. try:
  129. if not self.is_open or self.connection is None:
  130. return
  131. args = AMQPWriter()
  132. args.write_short(reply_code)
  133. args.write_shortstr(reply_text)
  134. args.write_short(method_sig[0]) # class_id
  135. args.write_short(method_sig[1]) # method_id
  136. self._send_method((20, 40), args)
  137. return self.wait(allowed_methods=[
  138. (20, 40), # Channel.close
  139. (20, 41), # Channel.close_ok
  140. ])
  141. finally:
  142. self.connection = None
  143. def _close(self, args):
  144. """Request a channel close
  145. This method indicates that the sender wants to close the
  146. channel. This may be due to internal conditions (e.g. a forced
  147. shut-down) or due to an error handling a specific method, i.e.
  148. an exception. When a close is due to an exception, the sender
  149. provides the class and method id of the method which caused
  150. the exception.
  151. RULE:
  152. After sending this method any received method except
  153. Channel.Close-OK MUST be discarded.
  154. RULE:
  155. The peer sending this method MAY use a counter or timeout
  156. to detect failure of the other peer to respond correctly
  157. with Channel.Close-OK..
  158. PARAMETERS:
  159. reply_code: short
  160. The reply code. The AMQ reply codes are defined in AMQ
  161. RFC 011.
  162. reply_text: shortstr
  163. The localised reply text. This text can be logged as an
  164. aid to resolving issues.
  165. class_id: short
  166. failing method class
  167. When the close is provoked by a method exception, this
  168. is the class of the method.
  169. method_id: short
  170. failing method ID
  171. When the close is provoked by a method exception, this
  172. is the ID of the method.
  173. """
  174. reply_code = args.read_short()
  175. reply_text = args.read_shortstr()
  176. class_id = args.read_short()
  177. method_id = args.read_short()
  178. self._send_method((20, 41))
  179. self._do_revive()
  180. raise error_for_code(
  181. reply_code, reply_text, (class_id, method_id), ChannelError,
  182. )
  183. def _close_ok(self, args):
  184. """Confirm a channel close
  185. This method confirms a Channel.Close method and tells the
  186. recipient that it is safe to release resources for the channel
  187. and close the socket.
  188. RULE:
  189. A peer that detects a socket closure without having
  190. received a Channel.Close-Ok handshake method SHOULD log
  191. the error.
  192. """
  193. self._do_close()
  194. def flow(self, active):
  195. """Enable/disable flow from peer
  196. This method asks the peer to pause or restart the flow of
  197. content data. This is a simple flow-control mechanism that a
  198. peer can use to avoid oveflowing its queues or otherwise
  199. finding itself receiving more messages than it can process.
  200. Note that this method is not intended for window control. The
  201. peer that receives a request to stop sending content should
  202. finish sending the current content, if any, and then wait
  203. until it receives a Flow restart method.
  204. RULE:
  205. When a new channel is opened, it is active. Some
  206. applications assume that channels are inactive until
  207. started. To emulate this behaviour a client MAY open the
  208. channel, then pause it.
  209. RULE:
  210. When sending content data in multiple frames, a peer
  211. SHOULD monitor the channel for incoming methods and
  212. respond to a Channel.Flow as rapidly as possible.
  213. RULE:
  214. A peer MAY use the Channel.Flow method to throttle
  215. incoming content data for internal reasons, for example,
  216. when exchangeing data over a slower connection.
  217. RULE:
  218. The peer that requests a Channel.Flow method MAY
  219. disconnect and/or ban a peer that does not respect the
  220. request.
  221. PARAMETERS:
  222. active: boolean
  223. start/stop content frames
  224. If True, the peer starts sending content frames. If
  225. False, the peer stops sending content frames.
  226. """
  227. args = AMQPWriter()
  228. args.write_bit(active)
  229. self._send_method((20, 20), args)
  230. return self.wait(allowed_methods=[
  231. (20, 21), # Channel.flow_ok
  232. ])
  233. def _flow(self, args):
  234. """Enable/disable flow from peer
  235. This method asks the peer to pause or restart the flow of
  236. content data. This is a simple flow-control mechanism that a
  237. peer can use to avoid oveflowing its queues or otherwise
  238. finding itself receiving more messages than it can process.
  239. Note that this method is not intended for window control. The
  240. peer that receives a request to stop sending content should
  241. finish sending the current content, if any, and then wait
  242. until it receives a Flow restart method.
  243. RULE:
  244. When a new channel is opened, it is active. Some
  245. applications assume that channels are inactive until
  246. started. To emulate this behaviour a client MAY open the
  247. channel, then pause it.
  248. RULE:
  249. When sending content data in multiple frames, a peer
  250. SHOULD monitor the channel for incoming methods and
  251. respond to a Channel.Flow as rapidly as possible.
  252. RULE:
  253. A peer MAY use the Channel.Flow method to throttle
  254. incoming content data for internal reasons, for example,
  255. when exchangeing data over a slower connection.
  256. RULE:
  257. The peer that requests a Channel.Flow method MAY
  258. disconnect and/or ban a peer that does not respect the
  259. request.
  260. PARAMETERS:
  261. active: boolean
  262. start/stop content frames
  263. If True, the peer starts sending content frames. If
  264. False, the peer stops sending content frames.
  265. """
  266. self.active = args.read_bit()
  267. self._x_flow_ok(self.active)
  268. def _x_flow_ok(self, active):
  269. """Confirm a flow method
  270. Confirms to the peer that a flow command was received and
  271. processed.
  272. PARAMETERS:
  273. active: boolean
  274. current flow setting
  275. Confirms the setting of the processed flow method:
  276. True means the peer will start sending or continue
  277. to send content frames; False means it will not.
  278. """
  279. args = AMQPWriter()
  280. args.write_bit(active)
  281. self._send_method((20, 21), args)
  282. def _flow_ok(self, args):
  283. """Confirm a flow method
  284. Confirms to the peer that a flow command was received and
  285. processed.
  286. PARAMETERS:
  287. active: boolean
  288. current flow setting
  289. Confirms the setting of the processed flow method:
  290. True means the peer will start sending or continue
  291. to send content frames; False means it will not.
  292. """
  293. return args.read_bit()
  294. def _x_open(self):
  295. """Open a channel for use
  296. This method opens a virtual connection (a channel).
  297. RULE:
  298. This method MUST NOT be called when the channel is already
  299. open.
  300. PARAMETERS:
  301. out_of_band: shortstr (DEPRECATED)
  302. out-of-band settings
  303. Configures out-of-band transfers on this channel. The
  304. syntax and meaning of this field will be formally
  305. defined at a later date.
  306. """
  307. if self.is_open:
  308. return
  309. args = AMQPWriter()
  310. args.write_shortstr('') # out_of_band: deprecated
  311. self._send_method((20, 10), args)
  312. return self.wait(allowed_methods=[
  313. (20, 11), # Channel.open_ok
  314. ])
  315. def _open_ok(self, args):
  316. """Signal that the channel is ready
  317. This method signals to the client that the channel is ready
  318. for use.
  319. """
  320. self.is_open = True
  321. AMQP_LOGGER.debug('Channel open')
  322. #############
  323. #
  324. # Exchange
  325. #
  326. #
  327. # work with exchanges
  328. #
  329. # Exchanges match and distribute messages across queues.
  330. # Exchanges can be configured in the server or created at runtime.
  331. #
  332. # GRAMMAR::
  333. #
  334. # exchange = C:DECLARE S:DECLARE-OK
  335. # / C:DELETE S:DELETE-OK
  336. #
  337. # RULE:
  338. #
  339. # The server MUST implement the direct and fanout exchange
  340. # types, and predeclare the corresponding exchanges named
  341. # amq.direct and amq.fanout in each virtual host. The server
  342. # MUST also predeclare a direct exchange to act as the default
  343. # exchange for content Publish methods and for default queue
  344. # bindings.
  345. #
  346. # RULE:
  347. #
  348. # The server SHOULD implement the topic exchange type, and
  349. # predeclare the corresponding exchange named amq.topic in
  350. # each virtual host.
  351. #
  352. # RULE:
  353. #
  354. # The server MAY implement the system exchange type, and
  355. # predeclare the corresponding exchanges named amq.system in
  356. # each virtual host. If the client attempts to bind a queue to
  357. # the system exchange, the server MUST raise a connection
  358. # exception with reply code 507 (not allowed).
  359. #
  360. def exchange_declare(self, exchange, type, passive=False, durable=False,
  361. auto_delete=True, nowait=False, arguments=None):
  362. """Declare exchange, create if needed
  363. This method creates an exchange if it does not already exist,
  364. and if the exchange exists, verifies that it is of the correct
  365. and expected class.
  366. RULE:
  367. The server SHOULD support a minimum of 16 exchanges per
  368. virtual host and ideally, impose no limit except as
  369. defined by available resources.
  370. PARAMETERS:
  371. exchange: shortstr
  372. RULE:
  373. Exchange names starting with "amq." are reserved
  374. for predeclared and standardised exchanges. If
  375. the client attempts to create an exchange starting
  376. with "amq.", the server MUST raise a channel
  377. exception with reply code 403 (access refused).
  378. type: shortstr
  379. exchange type
  380. Each exchange belongs to one of a set of exchange
  381. types implemented by the server. The exchange types
  382. define the functionality of the exchange - i.e. how
  383. messages are routed through it. It is not valid or
  384. meaningful to attempt to change the type of an
  385. existing exchange.
  386. RULE:
  387. If the exchange already exists with a different
  388. type, the server MUST raise a connection exception
  389. with a reply code 507 (not allowed).
  390. RULE:
  391. If the server does not support the requested
  392. exchange type it MUST raise a connection exception
  393. with a reply code 503 (command invalid).
  394. passive: boolean
  395. do not create exchange
  396. If set, the server will not create the exchange. The
  397. client can use this to check whether an exchange
  398. exists without modifying the server state.
  399. RULE:
  400. If set, and the exchange does not already exist,
  401. the server MUST raise a channel exception with
  402. reply code 404 (not found).
  403. durable: boolean
  404. request a durable exchange
  405. If set when creating a new exchange, the exchange will
  406. be marked as durable. Durable exchanges remain active
  407. when a server restarts. Non-durable exchanges
  408. (transient exchanges) are purged if/when a server
  409. restarts.
  410. RULE:
  411. The server MUST support both durable and transient
  412. exchanges.
  413. RULE:
  414. The server MUST ignore the durable field if the
  415. exchange already exists.
  416. auto_delete: boolean
  417. auto-delete when unused
  418. If set, the exchange is deleted when all queues have
  419. finished using it.
  420. RULE:
  421. The server SHOULD allow for a reasonable delay
  422. between the point when it determines that an
  423. exchange is not being used (or no longer used),
  424. and the point when it deletes the exchange. At
  425. the least it must allow a client to create an
  426. exchange and then bind a queue to it, with a small
  427. but non-zero delay between these two actions.
  428. RULE:
  429. The server MUST ignore the auto-delete field if
  430. the exchange already exists.
  431. nowait: boolean
  432. do not send a reply method
  433. If set, the server will not respond to the method. The
  434. client should not wait for a reply method. If the
  435. server could not complete the method it will raise a
  436. channel or connection exception.
  437. arguments: table
  438. arguments for declaration
  439. A set of arguments for the declaration. The syntax and
  440. semantics of these arguments depends on the server
  441. implementation. This field is ignored if passive is
  442. True.
  443. """
  444. arguments = {} if arguments is None else arguments
  445. args = AMQPWriter()
  446. args.write_short(0)
  447. args.write_shortstr(exchange)
  448. args.write_shortstr(type)
  449. args.write_bit(passive)
  450. args.write_bit(durable)
  451. args.write_bit(auto_delete)
  452. args.write_bit(False) # internal: deprecated
  453. args.write_bit(nowait)
  454. args.write_table(arguments)
  455. self._send_method((40, 10), args)
  456. if auto_delete:
  457. warn(VDeprecationWarning(EXCHANGE_AUTODELETE_DEPRECATED))
  458. if not nowait:
  459. return self.wait(allowed_methods=[
  460. (40, 11), # Channel.exchange_declare_ok
  461. ])
  462. def _exchange_declare_ok(self, args):
  463. """Confirms an exchange declaration
  464. This method confirms a Declare method and confirms the name of
  465. the exchange, essential for automatically-named exchanges.
  466. """
  467. pass
  468. def exchange_delete(self, exchange, if_unused=False, nowait=False):
  469. """Delete an exchange
  470. This method deletes an exchange. When an exchange is deleted
  471. all queue bindings on the exchange are cancelled.
  472. PARAMETERS:
  473. exchange: shortstr
  474. RULE:
  475. The exchange MUST exist. Attempting to delete a
  476. non-existing exchange causes a channel exception.
  477. if_unused: boolean
  478. delete only if unused
  479. If set, the server will only delete the exchange if it
  480. has no queue bindings. If the exchange has queue
  481. bindings the server does not delete it but raises a
  482. channel exception instead.
  483. RULE:
  484. If set, the server SHOULD delete the exchange but
  485. only if it has no queue bindings.
  486. RULE:
  487. If set, the server SHOULD raise a channel
  488. exception if the exchange is in use.
  489. nowait: boolean
  490. do not send a reply method
  491. If set, the server will not respond to the method. The
  492. client should not wait for a reply method. If the
  493. server could not complete the method it will raise a
  494. channel or connection exception.
  495. """
  496. args = AMQPWriter()
  497. args.write_short(0)
  498. args.write_shortstr(exchange)
  499. args.write_bit(if_unused)
  500. args.write_bit(nowait)
  501. self._send_method((40, 20), args)
  502. if not nowait:
  503. return self.wait(allowed_methods=[
  504. (40, 21), # Channel.exchange_delete_ok
  505. ])
  506. def _exchange_delete_ok(self, args):
  507. """Confirm deletion of an exchange
  508. This method confirms the deletion of an exchange.
  509. """
  510. pass
  511. def exchange_bind(self, destination, source='', routing_key='',
  512. nowait=False, arguments=None):
  513. """This method binds an exchange to an exchange.
  514. RULE:
  515. A server MUST allow and ignore duplicate bindings - that
  516. is, two or more bind methods for a specific exchanges,
  517. with identical arguments - without treating these as an
  518. error.
  519. RULE:
  520. A server MUST allow cycles of exchange bindings to be
  521. created including allowing an exchange to be bound to
  522. itself.
  523. RULE:
  524. A server MUST not deliver the same message more than once
  525. to a destination exchange, even if the topology of
  526. exchanges and bindings results in multiple (even infinite)
  527. routes to that exchange.
  528. PARAMETERS:
  529. reserved-1: short
  530. destination: shortstr
  531. Specifies the name of the destination exchange to
  532. bind.
  533. RULE:
  534. A client MUST NOT be allowed to bind a non-
  535. existent destination exchange.
  536. RULE:
  537. The server MUST accept a blank exchange name to
  538. mean the default exchange.
  539. source: shortstr
  540. Specifies the name of the source exchange to bind.
  541. RULE:
  542. A client MUST NOT be allowed to bind a non-
  543. existent source exchange.
  544. RULE:
  545. The server MUST accept a blank exchange name to
  546. mean the default exchange.
  547. routing-key: shortstr
  548. Specifies the routing key for the binding. The routing
  549. key is used for routing messages depending on the
  550. exchange configuration. Not all exchanges use a
  551. routing key - refer to the specific exchange
  552. documentation.
  553. no-wait: bit
  554. arguments: table
  555. A set of arguments for the binding. The syntax and
  556. semantics of these arguments depends on the exchange
  557. class.
  558. """
  559. arguments = {} if arguments is None else arguments
  560. args = AMQPWriter()
  561. args.write_short(0)
  562. args.write_shortstr(destination)
  563. args.write_shortstr(source)
  564. args.write_shortstr(routing_key)
  565. args.write_bit(nowait)
  566. args.write_table(arguments)
  567. self._send_method((40, 30), args)
  568. if not nowait:
  569. return self.wait(allowed_methods=[
  570. (40, 31), # Channel.exchange_bind_ok
  571. ])
  572. def exchange_unbind(self, destination, source='', routing_key='',
  573. nowait=False, arguments=None):
  574. """This method unbinds an exchange from an exchange.
  575. RULE:
  576. If a unbind fails, the server MUST raise a connection
  577. exception.
  578. PARAMETERS:
  579. reserved-1: short
  580. destination: shortstr
  581. Specifies the name of the destination exchange to
  582. unbind.
  583. RULE:
  584. The client MUST NOT attempt to unbind an exchange
  585. that does not exist from an exchange.
  586. RULE:
  587. The server MUST accept a blank exchange name to
  588. mean the default exchange.
  589. source: shortstr
  590. Specifies the name of the source exchange to unbind.
  591. RULE:
  592. The client MUST NOT attempt to unbind an exchange
  593. from an exchange that does not exist.
  594. RULE:
  595. The server MUST accept a blank exchange name to
  596. mean the default exchange.
  597. routing-key: shortstr
  598. Specifies the routing key of the binding to unbind.
  599. no-wait: bit
  600. arguments: table
  601. Specifies the arguments of the binding to unbind.
  602. """
  603. arguments = {} if arguments is None else arguments
  604. args = AMQPWriter()
  605. args.write_short(0)
  606. args.write_shortstr(destination)
  607. args.write_shortstr(source)
  608. args.write_shortstr(routing_key)
  609. args.write_bit(nowait)
  610. args.write_table(arguments)
  611. self._send_method((40, 40), args)
  612. if not nowait:
  613. return self.wait(allowed_methods=[
  614. (40, 51), # Channel.exchange_unbind_ok
  615. ])
  616. def _exchange_bind_ok(self, args):
  617. """Confirm bind successful
  618. This method confirms that the bind was successful.
  619. """
  620. pass
  621. def _exchange_unbind_ok(self, args):
  622. """Confirm unbind successful
  623. This method confirms that the unbind was successful.
  624. """
  625. pass
  626. #############
  627. #
  628. # Queue
  629. #
  630. #
  631. # work with queues
  632. #
  633. # Queues store and forward messages. Queues can be configured in
  634. # the server or created at runtime. Queues must be attached to at
  635. # least one exchange in order to receive messages from publishers.
  636. #
  637. # GRAMMAR::
  638. #
  639. # queue = C:DECLARE S:DECLARE-OK
  640. # / C:BIND S:BIND-OK
  641. # / C:PURGE S:PURGE-OK
  642. # / C:DELETE S:DELETE-OK
  643. #
  644. # RULE:
  645. #
  646. # A server MUST allow any content class to be sent to any
  647. # queue, in any mix, and queue and delivery these content
  648. # classes independently. Note that all methods that fetch
  649. # content off queues are specific to a given content class.
  650. #
  651. def queue_bind(self, queue, exchange='', routing_key='',
  652. nowait=False, arguments=None):
  653. """Bind queue to an exchange
  654. This method binds a queue to an exchange. Until a queue is
  655. bound it will not receive any messages. In a classic
  656. messaging model, store-and-forward queues are bound to a dest
  657. exchange and subscription queues are bound to a dest_wild
  658. exchange.
  659. RULE:
  660. A server MUST allow ignore duplicate bindings - that is,
  661. two or more bind methods for a specific queue, with
  662. identical arguments - without treating these as an error.
  663. RULE:
  664. If a bind fails, the server MUST raise a connection
  665. exception.
  666. RULE:
  667. The server MUST NOT allow a durable queue to bind to a
  668. transient exchange. If the client attempts this the server
  669. MUST raise a channel exception.
  670. RULE:
  671. Bindings for durable queues are automatically durable and
  672. the server SHOULD restore such bindings after a server
  673. restart.
  674. RULE:
  675. The server SHOULD support at least 4 bindings per queue,
  676. and ideally, impose no limit except as defined by
  677. available resources.
  678. PARAMETERS:
  679. queue: shortstr
  680. Specifies the name of the queue to bind. If the queue
  681. name is empty, refers to the current queue for the
  682. channel, which is the last declared queue.
  683. RULE:
  684. If the client did not previously declare a queue,
  685. and the queue name in this method is empty, the
  686. server MUST raise a connection exception with
  687. reply code 530 (not allowed).
  688. RULE:
  689. If the queue does not exist the server MUST raise
  690. a channel exception with reply code 404 (not
  691. found).
  692. exchange: shortstr
  693. The name of the exchange to bind to.
  694. RULE:
  695. If the exchange does not exist the server MUST
  696. raise a channel exception with reply code 404 (not
  697. found).
  698. routing_key: shortstr
  699. message routing key
  700. Specifies the routing key for the binding. The
  701. routing key is used for routing messages depending on
  702. the exchange configuration. Not all exchanges use a
  703. routing key - refer to the specific exchange
  704. documentation. If the routing key is empty and the
  705. queue name is empty, the routing key will be the
  706. current queue for the channel, which is the last
  707. declared queue.
  708. nowait: boolean
  709. do not send a reply method
  710. If set, the server will not respond to the method. The
  711. client should not wait for a reply method. If the
  712. server could not complete the method it will raise a
  713. channel or connection exception.
  714. arguments: table
  715. arguments for binding
  716. A set of arguments for the binding. The syntax and
  717. semantics of these arguments depends on the exchange
  718. class.
  719. """
  720. arguments = {} if arguments is None else arguments
  721. args = AMQPWriter()
  722. args.write_short(0)
  723. args.write_shortstr(queue)
  724. args.write_shortstr(exchange)
  725. args.write_shortstr(routing_key)
  726. args.write_bit(nowait)
  727. args.write_table(arguments)
  728. self._send_method((50, 20), args)
  729. if not nowait:
  730. return self.wait(allowed_methods=[
  731. (50, 21), # Channel.queue_bind_ok
  732. ])
  733. def _queue_bind_ok(self, args):
  734. """Confirm bind successful
  735. This method confirms that the bind was successful.
  736. """
  737. pass
  738. def queue_unbind(self, queue, exchange, routing_key='',
  739. nowait=False, arguments=None):
  740. """Unbind a queue from an exchange
  741. This method unbinds a queue from an exchange.
  742. RULE:
  743. If a unbind fails, the server MUST raise a connection exception.
  744. PARAMETERS:
  745. queue: shortstr
  746. Specifies the name of the queue to unbind.
  747. RULE:
  748. The client MUST either specify a queue name or have
  749. previously declared a queue on the same channel
  750. RULE:
  751. The client MUST NOT attempt to unbind a queue that
  752. does not exist.
  753. exchange: shortstr
  754. The name of the exchange to unbind from.
  755. RULE:
  756. The client MUST NOT attempt to unbind a queue from an
  757. exchange that does not exist.
  758. RULE:
  759. The server MUST accept a blank exchange name to mean
  760. the default exchange.
  761. routing_key: shortstr
  762. routing key of binding
  763. Specifies the routing key of the binding to unbind.
  764. arguments: table
  765. arguments of binding
  766. Specifies the arguments of the binding to unbind.
  767. """
  768. arguments = {} if arguments is None else arguments
  769. args = AMQPWriter()
  770. args.write_short(0)
  771. args.write_shortstr(queue)
  772. args.write_shortstr(exchange)
  773. args.write_shortstr(routing_key)
  774. args.write_table(arguments)
  775. self._send_method((50, 50), args)
  776. if not nowait:
  777. return self.wait(allowed_methods=[
  778. (50, 51), # Channel.queue_unbind_ok
  779. ])
  780. def _queue_unbind_ok(self, args):
  781. """Confirm unbind successful
  782. This method confirms that the unbind was successful.
  783. """
  784. pass
  785. def queue_declare(self, queue='', passive=False, durable=False,
  786. exclusive=False, auto_delete=True, nowait=False,
  787. arguments=None):
  788. """Declare queue, create if needed
  789. This method creates or checks a queue. When creating a new
  790. queue the client can specify various properties that control
  791. the durability of the queue and its contents, and the level of
  792. sharing for the queue.
  793. RULE:
  794. The server MUST create a default binding for a newly-
  795. created queue to the default exchange, which is an
  796. exchange of type 'direct'.
  797. RULE:
  798. The server SHOULD support a minimum of 256 queues per
  799. virtual host and ideally, impose no limit except as
  800. defined by available resources.
  801. PARAMETERS:
  802. queue: shortstr
  803. RULE:
  804. The queue name MAY be empty, in which case the
  805. server MUST create a new queue with a unique
  806. generated name and return this to the client in
  807. the Declare-Ok method.
  808. RULE:
  809. Queue names starting with "amq." are reserved for
  810. predeclared and standardised server queues. If
  811. the queue name starts with "amq." and the passive
  812. option is False, the server MUST raise a connection
  813. exception with reply code 403 (access refused).
  814. passive: boolean
  815. do not create queue
  816. If set, the server will not create the queue. The
  817. client can use this to check whether a queue exists
  818. without modifying the server state.
  819. RULE:
  820. If set, and the queue does not already exist, the
  821. server MUST respond with a reply code 404 (not
  822. found) and raise a channel exception.
  823. durable: boolean
  824. request a durable queue
  825. If set when creating a new queue, the queue will be
  826. marked as durable. Durable queues remain active when
  827. a server restarts. Non-durable queues (transient
  828. queues) are purged if/when a server restarts. Note
  829. that durable queues do not necessarily hold persistent
  830. messages, although it does not make sense to send
  831. persistent messages to a transient queue.
  832. RULE:
  833. The server MUST recreate the durable queue after a
  834. restart.
  835. RULE:
  836. The server MUST support both durable and transient
  837. queues.
  838. RULE:
  839. The server MUST ignore the durable field if the
  840. queue already exists.
  841. exclusive: boolean
  842. request an exclusive queue
  843. Exclusive queues may only be consumed from by the
  844. current connection. Setting the 'exclusive' flag
  845. always implies 'auto-delete'.
  846. RULE:
  847. The server MUST support both exclusive (private)
  848. and non-exclusive (shared) queues.
  849. RULE:
  850. The server MUST raise a channel exception if
  851. 'exclusive' is specified and the queue already
  852. exists and is owned by a different connection.
  853. auto_delete: boolean
  854. auto-delete queue when unused
  855. If set, the queue is deleted when all consumers have
  856. finished using it. Last consumer can be cancelled
  857. either explicitly or because its channel is closed. If
  858. there was no consumer ever on the queue, it won't be
  859. deleted.
  860. RULE:
  861. The server SHOULD allow for a reasonable delay
  862. between the point when it determines that a queue
  863. is not being used (or no longer used), and the
  864. point when it deletes the queue. At the least it
  865. must allow a client to create a queue and then
  866. create a consumer to read from it, with a small
  867. but non-zero delay between these two actions. The
  868. server should equally allow for clients that may
  869. be disconnected prematurely, and wish to re-
  870. consume from the same queue without losing
  871. messages. We would recommend a configurable
  872. timeout, with a suitable default value being one
  873. minute.
  874. RULE:
  875. The server MUST ignore the auto-delete field if
  876. the queue already exists.
  877. nowait: boolean
  878. do not send a reply method
  879. If set, the server will not respond to the method. The
  880. client should not wait for a reply method. If the
  881. server could not complete the method it will raise a
  882. channel or connection exception.
  883. arguments: table
  884. arguments for declaration
  885. A set of arguments for the declaration. The syntax and
  886. semantics of these arguments depends on the server
  887. implementation. This field is ignored if passive is
  888. True.
  889. Returns a tuple containing 3 items:
  890. the name of the queue (essential for automatically-named queues)
  891. message count
  892. consumer count
  893. """
  894. arguments = {} if arguments is None else arguments
  895. args = AMQPWriter()
  896. args.write_short(0)
  897. args.write_shortstr(queue)
  898. args.write_bit(passive)
  899. args.write_bit(durable)
  900. args.write_bit(exclusive)
  901. args.write_bit(auto_delete)
  902. args.write_bit(nowait)
  903. args.write_table(arguments)
  904. self._send_method((50, 10), args)
  905. if not nowait:
  906. return self.wait(allowed_methods=[
  907. (50, 11), # Channel.queue_declare_ok
  908. ])
  909. def _queue_declare_ok(self, args):
  910. """Confirms a queue definition
  911. This method confirms a Declare method and confirms the name of
  912. the queue, essential for automatically-named queues.
  913. PARAMETERS:
  914. queue: shortstr
  915. Reports the name of the queue. If the server generated
  916. a queue name, this field contains that name.
  917. message_count: long
  918. number of messages in queue
  919. Reports the number of messages in the queue, which
  920. will be zero for newly-created queues.
  921. consumer_count: long
  922. number of consumers
  923. Reports the number of active consumers for the queue.
  924. Note that consumers can suspend activity
  925. (Channel.Flow) in which case they do not appear in
  926. this count.
  927. """
  928. return queue_declare_ok_t(
  929. args.read_shortstr(),
  930. args.read_long(),
  931. args.read_long(),
  932. )
  933. def queue_delete(self, queue='',
  934. if_unused=False, if_empty=False, nowait=False):
  935. """Delete a queue
  936. This method deletes a queue. When a queue is deleted any
  937. pending messages are sent to a dead-letter queue if this is
  938. defined in the server configuration, and all consumers on the
  939. queue are cancelled.
  940. RULE:
  941. The server SHOULD use a dead-letter queue to hold messages
  942. that were pending on a deleted queue, and MAY provide
  943. facilities for a system administrator to move these
  944. messages back to an active queue.
  945. PARAMETERS:
  946. queue: shortstr
  947. Specifies the name of the queue to delete. If the
  948. queue name is empty, refers to the current queue for
  949. the channel, which is the last declared queue.
  950. RULE:
  951. If the client did not previously declare a queue,
  952. and the queue name in this method is empty, the
  953. server MUST raise a connection exception with
  954. reply code 530 (not allowed).
  955. RULE:
  956. The queue must exist. Attempting to delete a non-
  957. existing queue causes a channel exception.
  958. if_unused: boolean
  959. delete only if unused
  960. If set, the server will only delete the queue if it
  961. has no consumers. If the queue has consumers the
  962. server does does not delete it but raises a channel
  963. exception instead.
  964. RULE:
  965. The server MUST respect the if-unused flag when
  966. deleting a queue.
  967. if_empty: boolean
  968. delete only if empty
  969. If set, the server will only delete the queue if it
  970. has no messages. If the queue is not empty the server
  971. raises a channel exception.
  972. nowait: boolean
  973. do not send a reply method
  974. If set, the server will not respond to the method. The
  975. client should not wait for a reply method. If the
  976. server could not complete the method it will raise a
  977. channel or connection exception.
  978. """
  979. args = AMQPWriter()
  980. args.write_short(0)
  981. args.write_shortstr(queue)
  982. args.write_bit(if_unused)
  983. args.write_bit(if_empty)
  984. args.write_bit(nowait)
  985. self._send_method((50, 40), args)
  986. if not nowait:
  987. return self.wait(allowed_methods=[
  988. (50, 41), # Channel.queue_delete_ok
  989. ])
  990. def _queue_delete_ok(self, args):
  991. """Confirm deletion of a queue
  992. This method confirms the deletion of a queue.
  993. PARAMETERS:
  994. message_count: long
  995. number of messages purged
  996. Reports the number of messages purged.
  997. """
  998. return args.read_long()
  999. def queue_purge(self, queue='', nowait=False):
  1000. """Purge a queue
  1001. This method removes all messages from a queue. It does not
  1002. cancel consumers. Purged messages are deleted without any
  1003. formal "undo" mechanism.
  1004. RULE:
  1005. A call to purge MUST result in an empty queue.
  1006. RULE:
  1007. On transacted channels the server MUST not purge messages
  1008. that have already been sent to a client but not yet
  1009. acknowledged.
  1010. RULE:
  1011. The server MAY implement a purge queue or log that allows
  1012. system administrators to recover accidentally-purged
  1013. messages. The server SHOULD NOT keep purged messages in
  1014. the same storage spaces as the live messages since the
  1015. volumes of purged messages may get very large.
  1016. PARAMETERS:
  1017. queue: shortstr
  1018. Specifies the name of the queue to purge. If the
  1019. queue name is empty, refers to the current queue for
  1020. the channel, which is the last declared queue.
  1021. RULE:
  1022. If the client did not previously declare a queue,
  1023. and the queue name in this method is empty, the
  1024. server MUST raise a connection exception with
  1025. reply code 530 (not allowed).
  1026. RULE:
  1027. The queue must exist. Attempting to purge a non-
  1028. existing queue causes a channel exception.
  1029. nowait: boolean
  1030. do not send a reply method
  1031. If set, the server will not respond to the method. The
  1032. client should not wait for a reply method. If the
  1033. server could not complete the method it will raise a
  1034. channel or connection exception.
  1035. if nowait is False, returns a message_count
  1036. """
  1037. args = AMQPWriter()
  1038. args.write_short(0)
  1039. args.write_shortstr(queue)
  1040. args.write_bit(nowait)
  1041. self._send_method((50, 30), args)
  1042. if not nowait:
  1043. return self.wait(allowed_methods=[
  1044. (50, 31), # Channel.queue_purge_ok
  1045. ])
  1046. def _queue_purge_ok(self, args):
  1047. """Confirms a queue purge
  1048. This method confirms the purge of a queue.
  1049. PARAMETERS:
  1050. message_count: long
  1051. number of messages purged
  1052. Reports the number of messages purged.
  1053. """
  1054. return args.read_long()
  1055. #############
  1056. #
  1057. # Basic
  1058. #
  1059. #
  1060. # work with basic content
  1061. #
  1062. # The Basic class provides methods that support an industry-
  1063. # standard messaging model.
  1064. #
  1065. # GRAMMAR::
  1066. #
  1067. # basic = C:QOS S:QOS-OK
  1068. # / C:CONSUME S:CONSUME-OK
  1069. # / C:CANCEL S:CANCEL-OK
  1070. # / C:PUBLISH content
  1071. # / S:RETURN content
  1072. # / S:DELIVER content
  1073. # / C:GET ( S:GET-OK content / S:GET-EMPTY )
  1074. # / C:ACK
  1075. # / C:REJECT
  1076. #
  1077. # RULE:
  1078. #
  1079. # The server SHOULD respect the persistent property of basic
  1080. # messages and SHOULD make a best-effort to hold persistent
  1081. # basic messages on a reliable storage mechanism.
  1082. #
  1083. # RULE:
  1084. #
  1085. # The server MUST NOT discard a persistent basic message in
  1086. # case of a queue overflow. The server MAY use the
  1087. # Channel.Flow method to slow or stop a basic message
  1088. # publisher when necessary.
  1089. #
  1090. # RULE:
  1091. #
  1092. # The server MAY overflow non-persistent basic messages to
  1093. # persistent storage and MAY discard or dead-letter non-
  1094. # persistent basic messages on a priority basis if the queue
  1095. # size exceeds some configured limit.
  1096. #
  1097. # RULE:
  1098. #
  1099. # The server MUST implement at least 2 priority levels for
  1100. # basic messages, where priorities 0-4 and 5-9 are treated as
  1101. # two distinct levels. The server MAY implement up to 10
  1102. # priority levels.
  1103. #
  1104. # RULE:
  1105. #
  1106. # The server MUST deliver messages of the same priority in
  1107. # order irrespective of their individual persistence.
  1108. #
  1109. # RULE:
  1110. #
  1111. # The server MUST support both automatic and explicit
  1112. # acknowledgements on Basic content.
  1113. #
  1114. def basic_ack(self, delivery_tag, multiple=False):
  1115. """Acknowledge one or more messages
  1116. This method acknowledges one or more messages delivered via
  1117. the Deliver or Get-Ok methods. The client can ask to confirm
  1118. a single message or a set of messages up to and including a
  1119. specific message.
  1120. PARAMETERS:
  1121. delivery_tag: longlong
  1122. server-assigned delivery tag
  1123. The server-assigned and channel-specific delivery tag
  1124. RULE:
  1125. The delivery tag is valid only within the channel
  1126. from which the message was received. I.e. a client
  1127. MUST NOT receive a message on one channel and then
  1128. acknowledge it on another.
  1129. RULE:
  1130. The server MUST NOT use a zero value for delivery
  1131. tags. Zero is reserved for client use, meaning "all
  1132. messages so far received".
  1133. multiple: boolean
  1134. acknowledge multiple messages
  1135. If set to True, the delivery tag is treated as "up to
  1136. and including", so that the client can acknowledge
  1137. multiple messages with a single method. If set to
  1138. False, the delivery tag refers to a single message.
  1139. If the multiple field is True, and the delivery tag
  1140. is zero, tells the server to acknowledge all
  1141. outstanding mesages.
  1142. RULE:
  1143. The server MUST validate that a non-zero delivery-
  1144. tag refers to an delivered message, and raise a
  1145. channel exception if this is not the case.
  1146. """
  1147. args = AMQPWriter()
  1148. args.write_longlong(delivery_tag)
  1149. args.write_bit(multiple)
  1150. self._send_method((60, 80), args)
  1151. def basic_cancel(self, consumer_tag, nowait=False):
  1152. """End a queue consumer
  1153. This method cancels a consumer. This does not affect already
  1154. delivered messages, but it does mean the server will not send
  1155. any more messages for that consumer. The client may receive
  1156. an abitrary number of messages in between sending the cancel
  1157. method and receiving the cancel-ok reply.
  1158. RULE:
  1159. If the queue no longer exists when the client sends a
  1160. cancel command, or the consumer has been cancelled for
  1161. other reasons, this command has no effect.
  1162. PARAMETERS:
  1163. consumer_tag: shortstr
  1164. consumer tag
  1165. Identifier for the consumer, valid within the current
  1166. connection.
  1167. RULE:
  1168. The consumer tag is valid only within the channel
  1169. from which the consumer was created. I.e. a client
  1170. MUST NOT create a consumer in one channel and then
  1171. use it in another.
  1172. nowait: boolean
  1173. do not send a reply method
  1174. If set, the server will not respond to the method. The
  1175. client should not wait for a reply method. If the
  1176. server could not complete the method it will raise a
  1177. channel or connection exception.
  1178. """
  1179. if self.connection is not None:
  1180. self.no_ack_consumers.discard(consumer_tag)
  1181. args = AMQPWriter()
  1182. args.write_shortstr(consumer_tag)
  1183. args.write_bit(nowait)
  1184. self._send_method((60, 30), args)
  1185. return self.wait(allowed_methods=[
  1186. (60, 31), # Channel.basic_cancel_ok
  1187. ])
  1188. def _basic_cancel_notify(self, args):
  1189. """Consumer cancelled by server.
  1190. Most likely the queue was deleted.
  1191. """
  1192. consumer_tag = args.read_shortstr()
  1193. callback = self._on_cancel(consumer_tag)
  1194. if callback:
  1195. callback(consumer_tag)
  1196. else:
  1197. raise ConsumerCancelled(consumer_tag, (60, 30))
  1198. def _basic_cancel_ok(self, args):
  1199. """Confirm a cancelled consumer
  1200. This method confirms that the cancellation was completed.
  1201. PARAMETERS:
  1202. consumer_tag: shortstr
  1203. consumer tag
  1204. Identifier for the consumer, valid within the current
  1205. connection.
  1206. RULE:
  1207. The consumer tag is valid only within the channel
  1208. from which the consumer was created. I.e. a client
  1209. MUST NOT create a consumer in one channel and then
  1210. use it in another.
  1211. """
  1212. consumer_tag = args.read_shortstr()
  1213. self._on_cancel(consumer_tag)
  1214. def _on_cancel(self, consumer_tag):
  1215. self.callbacks.pop(consumer_tag, None)
  1216. return self.cancel_callbacks.pop(consumer_tag, None)
  1217. def basic_consume(self, queue='', consumer_tag='', no_local=False,
  1218. no_ack=False, exclusive=False, nowait=False,
  1219. callback=None, arguments=None, on_cancel=None):
  1220. """Start a queue consumer
  1221. This method asks the server to start a "consumer", which is a
  1222. transient request for messages from a specific queue.
  1223. Consumers last as long as the channel they were created on, or
  1224. until the client cancels them.
  1225. RULE:
  1226. The server SHOULD support at least 16 consumers per queue,
  1227. unless the queue was declared as private, and ideally,
  1228. impose no limit except as defined by available resources.
  1229. PARAMETERS:
  1230. queue: shortstr
  1231. Specifies the name of the queue to consume from. If
  1232. the queue name is null, refers to the current queue
  1233. for the channel, which is the last declared queue.
  1234. RULE:
  1235. If the client did not previously declare a queue,
  1236. and the queue name in this method is empty, the
  1237. server MUST raise a connection exception with
  1238. reply code 530 (not allowed).
  1239. consumer_tag: shortstr
  1240. Specifies the identifier for the consumer. The
  1241. consumer tag is local to a connection, so two clients
  1242. can use the same consumer tags. If this field is empty
  1243. the server will generate a unique tag.
  1244. RULE:
  1245. The tag MUST NOT refer to an existing consumer. If
  1246. the client attempts to create two consumers with
  1247. the same non-empty tag the server MUST raise a
  1248. connection exception with reply code 530 (not
  1249. allowed).
  1250. no_local: boolean
  1251. do not deliver own messages
  1252. If the no-local field is set the server will not send
  1253. messages to the client that published them.
  1254. no_ack: boolean
  1255. no acknowledgement needed
  1256. If this field is set the server does not expect
  1257. acknowledgments for messages. That is, when a message
  1258. is delivered to the client the server automatically and
  1259. silently acknowledges it on behalf of the client. This
  1260. functionality increases performance but at the cost of
  1261. reliability. Messages can get lost if a client dies
  1262. before it can deliver them to the application.
  1263. exclusive: boolean
  1264. request exclusive access
  1265. Request exclusive consumer access, meaning only this
  1266. consumer can access the queue.
  1267. RULE:
  1268. If the server cannot grant exclusive access to the
  1269. queue when asked, - because there are other
  1270. consumers active - it MUST raise a channel
  1271. exception with return code 403 (access refused).
  1272. nowait: boolean
  1273. do not send a reply method
  1274. If set, the server will not respond to the method. The
  1275. client should not wait for a reply method. If the
  1276. server could not complete the method it will raise a
  1277. channel or connection exception.
  1278. callback: Python callable
  1279. function/method called with each delivered message
  1280. For each message delivered by the broker, the
  1281. callable will be called with a Message object
  1282. as the single argument. If no callable is specified,
  1283. messages are quietly discarded, no_ack should probably
  1284. be set to True in that case.
  1285. """
  1286. args = AMQPWriter()
  1287. args.write_short(0)
  1288. args.write_shortstr(queue)
  1289. args.write_shortstr(consumer_tag)
  1290. args.write_bit(no_local)
  1291. args.write_bit(no_ack)
  1292. args.write_bit(exclusive)
  1293. args.write_bit(nowait)
  1294. args.write_table(arguments or {})
  1295. self._send_method((60, 20), args)
  1296. if not nowait:
  1297. consumer_tag = self.wait(allowed_methods=[
  1298. (60, 21), # Channel.basic_consume_ok
  1299. ])
  1300. self.callbacks[consumer_tag] = callback
  1301. if on_cancel:
  1302. self.cancel_callbacks[consumer_tag] = on_cancel
  1303. if no_ack:
  1304. self.no_ack_consumers.add(consumer_tag)
  1305. return consumer_tag
  1306. def _basic_consume_ok(self, args):
  1307. """Confirm a new consumer
  1308. The server provides the client with a consumer tag, which is
  1309. used by the client for methods called on the consumer at a
  1310. later stage.
  1311. PARAMETERS:
  1312. consumer_tag: shortstr
  1313. Holds the consumer tag specified by the client or
  1314. provided by the server.
  1315. """
  1316. return args.read_shortstr()
  1317. def _basic_deliver(self, args, msg):
  1318. """Notify the client of a consumer message
  1319. This method delivers a message to the client, via a consumer.
  1320. In the asynchronous message delivery model, the client starts
  1321. a consumer using the Consume method, then the server responds
  1322. with Deliver methods as and when messages arrive for that
  1323. consumer.
  1324. RULE:
  1325. The server SHOULD track the number of times a message has
  1326. been delivered to clients and when a message is
  1327. redelivered a certain number of times - e.g. 5 times -
  1328. without being acknowledged, the server SHOULD consider the
  1329. message to be unprocessable (possibly causing client
  1330. applications to abort), and move the message to a dead
  1331. letter queue.
  1332. PARAMETERS:
  1333. consumer_tag: shortstr
  1334. consumer tag
  1335. Identifier for the consumer, valid within the current
  1336. connection.
  1337. RULE:
  1338. The consumer tag is valid only within the channel
  1339. from which the consumer was created. I.e. a client
  1340. MUST NOT create a consumer in one channel and then
  1341. use it in another.
  1342. delivery_tag: longlong
  1343. server-assigned delivery tag
  1344. The server-assigned and channel-specific delivery tag
  1345. RULE:
  1346. The delivery tag is valid only within the channel
  1347. from which the message was received. I.e. a client
  1348. MUST NOT receive a message on one channel and then
  1349. acknowledge it on another.
  1350. RULE:
  1351. The server MUST NOT use a zero value for delivery
  1352. tags. Zero is reserved for client use, meaning "all
  1353. messages so far received".
  1354. redelivered: boolean
  1355. message is being redelivered
  1356. This indicates that the message has been previously
  1357. delivered to this or another client.
  1358. exchange: shortstr
  1359. Specifies the name of the exchange that the message
  1360. was originally published to.
  1361. routing_key: shortstr
  1362. Message routing key
  1363. Specifies the routing key name specified when the
  1364. message was published.
  1365. """
  1366. consumer_tag = args.read_shortstr()
  1367. delivery_tag = args.read_longlong()
  1368. redelivered = args.read_bit()
  1369. exchange = args.read_shortstr()
  1370. routing_key = args.read_shortstr()
  1371. msg.channel = self
  1372. msg.delivery_info = {
  1373. 'consumer_tag': consumer_tag,
  1374. 'delivery_tag': delivery_tag,
  1375. 'redelivered': redelivered,
  1376. 'exchange': exchange,
  1377. 'routing_key': routing_key,
  1378. }
  1379. try:
  1380. fun = self.callbacks[consumer_tag]
  1381. except KeyError:
  1382. pass
  1383. else:
  1384. fun(msg)
  1385. def basic_get(self, queue='', no_ack=False):
  1386. """Direct access to a queue
  1387. This method provides a direct access to the messages in a
  1388. queue using a synchronous dialogue that is designed for
  1389. specific types of application where synchronous functionality
  1390. is more important than performance.
  1391. PARAMETERS:
  1392. queue: shortstr
  1393. Specifies the name of the queue to consume from. If
  1394. the queue name is null, refers to the current queue
  1395. for the channel, which is the last declared queue.
  1396. RULE:
  1397. If the client did not previously declare a queue,
  1398. and the queue name in this method is empty, the
  1399. server MUST raise a connection exception with
  1400. reply code 530 (not allowed).
  1401. no_ack: boolean
  1402. no acknowledgement needed
  1403. If this field is set the server does not expect
  1404. acknowledgments for messages. That is, when a message
  1405. is delivered to the client the server automatically and
  1406. silently acknowledges it on behalf of the client. This
  1407. functionality increases performance but at the cost of
  1408. reliability. Messages can get lost if a client dies
  1409. before it can deliver them to the application.
  1410. Non-blocking, returns a message object, or None.
  1411. """
  1412. args = AMQPWriter()
  1413. args.write_short(0)
  1414. args.write_shortstr(queue)
  1415. args.write_bit(no_ack)
  1416. self._send_method((60, 70), args)
  1417. return self.wait(allowed_methods=[
  1418. (60, 71), # Channel.basic_get_ok
  1419. (60, 72), # Channel.basic_get_empty
  1420. ])
  1421. def _basic_get_empty(self, args):
  1422. """Indicate no messages available
  1423. This method tells the client that the queue has no messages
  1424. available for the client.
  1425. PARAMETERS:
  1426. cluster_id: shortstr
  1427. Cluster id
  1428. For use by cluster applications, should not be used by
  1429. client applications.
  1430. """
  1431. cluster_id = args.read_shortstr() # noqa
  1432. def _basic_get_ok(self, args, msg):
  1433. """Provide client with a message
  1434. This method delivers a message to the client following a get
  1435. method. A message delivered by 'get-ok' must be acknowledged
  1436. unless the no-ack option was set in the get method.
  1437. PARAMETERS:
  1438. delivery_tag: longlong
  1439. server-assigned delivery tag
  1440. The server-assigned and channel-specific delivery tag
  1441. RULE:
  1442. The delivery tag is valid only within the channel
  1443. from which the message was received. I.e. a client
  1444. MUST NOT receive a message on one channel and then
  1445. acknowledge it on another.
  1446. RULE:
  1447. The server MUST NOT use a zero value for delivery
  1448. tags. Zero is reserved for client use, meaning "all
  1449. messages so far received".
  1450. redelivered: boolean
  1451. message is being redelivered
  1452. This indicates that the message has been previously
  1453. delivered to this or another client.
  1454. exchange: shortstr
  1455. Specifies the name of the exchange that the message
  1456. was originally published to. If empty, the message
  1457. was published to the default exchange.
  1458. routing_key: shortstr
  1459. Message routing key
  1460. Specifies the routing key name specified when the
  1461. message was published.
  1462. message_count: long
  1463. number of messages pending
  1464. This field reports the number of messages pending on
  1465. the queue, excluding the message being delivered.
  1466. Note that this figure is indicative, not reliable, and
  1467. can change arbitrarily as messages are added to the
  1468. queue and removed by other clients.
  1469. """
  1470. delivery_tag = args.read_longlong()
  1471. redelivered = args.read_bit()
  1472. exchange = args.read_shortstr()
  1473. routing_key = args.read_shortstr()
  1474. message_count = args.read_long()
  1475. msg.channel = self
  1476. msg.delivery_info = {
  1477. 'delivery_tag': delivery_tag,
  1478. 'redelivered': redelivered,
  1479. 'exchange': exchange,
  1480. 'routing_key': routing_key,
  1481. 'message_count': message_count
  1482. }
  1483. return msg
  1484. def _basic_publish(self, msg, exchange='', routing_key='',
  1485. mandatory=False, immediate=False):
  1486. """Publish a message
  1487. This method publishes a message to a specific exchange. The
  1488. message will be routed to queues as defined by the exchange
  1489. configuration and distributed to any active consumers when the
  1490. transaction, if any, is committed.
  1491. PARAMETERS:
  1492. exchange: shortstr
  1493. Specifies the name of the exchange to publish to. The
  1494. exchange name can be empty, meaning the default
  1495. exchange. If the exchange name is specified, and that
  1496. exchange does not exist, the server will raise a
  1497. channel exception.
  1498. RULE:
  1499. The server MUST accept a blank exchange name to
  1500. mean the default exchange.
  1501. RULE:
  1502. The exchange MAY refuse basic content in which
  1503. case it MUST raise a channel exception with reply
  1504. code 540 (not implemented).
  1505. routing_key: shortstr
  1506. Message routing key
  1507. Specifies the routing key for the message. The
  1508. routing key is used for routing messages depending on
  1509. the exchange configuration.
  1510. mandatory: boolean
  1511. indicate mandatory routing
  1512. This flag tells the server how to react if the message
  1513. cannot be routed to a queue. If this flag is True, the
  1514. server will return an unroutable message with a Return
  1515. method. If this flag is False, the server silently
  1516. drops the message.
  1517. RULE:
  1518. The server SHOULD implement the mandatory flag.
  1519. immediate: boolean
  1520. request immediate delivery
  1521. This flag tells the server how to react if the message
  1522. cannot be routed to a queue consumer immediately. If
  1523. this flag is set, the server will return an
  1524. undeliverable message with a Return method. If this
  1525. flag is zero, the server will queue the message, but
  1526. with no guarantee that it will ever be consumed.
  1527. RULE:
  1528. The server SHOULD implement the immediate flag.
  1529. """
  1530. args = AMQPWriter()
  1531. args.write_short(0)
  1532. args.write_shortstr(exchange)
  1533. args.write_shortstr(routing_key)
  1534. args.write_bit(mandatory)
  1535. args.write_bit(immediate)
  1536. self._send_method((60, 40), args, msg)
  1537. basic_publish = _basic_publish
  1538. def basic_publish_confirm(self, *args, **kwargs):
  1539. if not self._confirm_selected:
  1540. self._confirm_selected = True
  1541. self.confirm_select()
  1542. ret = self._basic_publish(*args, **kwargs)
  1543. # Basic.Ack / Basic.Nack
  1544. self.wait([(60, 80), (60, 120)])
  1545. return ret
  1546. def basic_qos(self, prefetch_size, prefetch_count, a_global):
  1547. """Specify quality of service
  1548. This method requests a specific quality of service. The QoS
  1549. can be specified for the current channel or for all channels
  1550. on the connection. The particular properties and semantics of
  1551. a qos method always depend on the content class semantics.
  1552. Though the qos method could in principle apply to both peers,
  1553. it is currently meaningful only for the server.
  1554. PARAMETERS:
  1555. prefetch_size: long
  1556. prefetch window in octets
  1557. The client can request that messages be sent in
  1558. advance so that when the client finishes processing a
  1559. message, the following message is already held
  1560. locally, rather than needing to be sent down the
  1561. channel. Prefetching gives a performance improvement.
  1562. This field specifies the prefetch window size in
  1563. octets. The server will send a message in advance if
  1564. it is equal to or smaller in size than the available
  1565. prefetch size (and also falls into other prefetch
  1566. limits). May be set to zero, meaning "no specific
  1567. limit", although other prefetch limits may still
  1568. apply. The prefetch-size is ignored if the no-ack
  1569. option is set.
  1570. RULE:
  1571. The server MUST ignore this setting when the
  1572. client is not processing any messages - i.e. the
  1573. prefetch size does not limit the transfer of
  1574. single messages to a client, only the sending in
  1575. advance of more messages while the client still
  1576. has one or more unacknowledged messages.
  1577. prefetch_count: short
  1578. prefetch window in messages
  1579. Specifies a prefetch window in terms of whole
  1580. messages. This field may be used in combination with
  1581. the prefetch-size field; a message will only be sent
  1582. in advance if both prefetch windows (and those at the
  1583. channel and connection level) allow it. The prefetch-
  1584. count is ignored if the no-ack option is set.
  1585. RULE:
  1586. The server MAY send less data in advance than
  1587. allowed by the client's specified prefetch windows
  1588. but it MUST NOT send more.
  1589. a_global: boolean
  1590. apply to entire connection
  1591. By default the QoS settings apply to the current
  1592. channel only. If this field is set, they are applied
  1593. to the entire connection.
  1594. """
  1595. args = AMQPWriter()
  1596. args.write_long(prefetch_size)
  1597. args.write_short(prefetch_count)
  1598. args.write_bit(a_global)
  1599. self._send_method((60, 10), args)
  1600. return self.wait(allowed_methods=[
  1601. (60, 11), # Channel.basic_qos_ok
  1602. ])
  1603. def _basic_qos_ok(self, args):
  1604. """Confirm the requested qos
  1605. This method tells the client that the requested QoS levels
  1606. could be handled by the server. The requested QoS applies to
  1607. all active consumers until a new QoS is defined.
  1608. """
  1609. pass
  1610. def basic_recover(self, requeue=False):
  1611. """Redeliver unacknowledged messages
  1612. This method asks the broker to redeliver all unacknowledged
  1613. messages on a specified channel. Zero or more messages may be
  1614. redelivered. This method is only allowed on non-transacted
  1615. channels.
  1616. RULE:
  1617. The server MUST set the redelivered flag on all messages
  1618. that are resent.
  1619. RULE:
  1620. The server MUST raise a channel exception if this is
  1621. called on a transacted channel.
  1622. PARAMETERS:
  1623. requeue: boolean
  1624. requeue the message
  1625. If this field is False, the message will be redelivered
  1626. to the original recipient. If this field is True, the
  1627. server will attempt to requeue the message,
  1628. potentially then delivering it to an alternative
  1629. subscriber.
  1630. """
  1631. args = AMQPWriter()
  1632. args.write_bit(requeue)
  1633. self._send_method((60, 110), args)
  1634. def basic_recover_async(self, requeue=False):
  1635. args = AMQPWriter()
  1636. args.write_bit(requeue)
  1637. self._send_method((60, 100), args)
  1638. def _basic_recover_ok(self, args):
  1639. """In 0-9-1 the deprecated recover solicits a response."""
  1640. pass
  1641. def basic_reject(self, delivery_tag, requeue):
  1642. """Reject an incoming message
  1643. This method allows a client to reject a message. It can be
  1644. used to interrupt and cancel large incoming messages, or
  1645. return untreatable messages to their original queue.
  1646. RULE:
  1647. The server SHOULD be capable of accepting and process the
  1648. Reject method while sending message content with a Deliver
  1649. or Get-Ok method. I.e. the server should read and process
  1650. incoming methods while sending output frames. To cancel a
  1651. partially-send content, the server sends a content body
  1652. frame of size 1 (i.e. with no data except the frame-end
  1653. octet).
  1654. RULE:
  1655. The server SHOULD interpret this method as meaning that
  1656. the client is unable to process the message at this time.
  1657. RULE:
  1658. A client MUST NOT use this method as a means of selecting
  1659. messages to process. A rejected message MAY be discarded
  1660. or dead-lettered, not necessarily passed to another
  1661. client.
  1662. PARAMETERS:
  1663. delivery_tag: longlong
  1664. server-assigned delivery tag
  1665. The server-assigned and channel-specific delivery tag
  1666. RULE:
  1667. The delivery tag is valid only within the channel
  1668. from which the message was received. I.e. a client
  1669. MUST NOT receive a message on one channel and then
  1670. acknowledge it on another.
  1671. RULE:
  1672. The server MUST NOT use a zero value for delivery
  1673. tags. Zero is reserved for client use, meaning "all
  1674. messages so far received".
  1675. requeue: boolean
  1676. requeue the message
  1677. If this field is False, the message will be discarded.
  1678. If this field is True, the server will attempt to
  1679. requeue the message.
  1680. RULE:
  1681. The server MUST NOT deliver the message to the
  1682. same client within the context of the current
  1683. channel. The recommended strategy is to attempt
  1684. to deliver the message to an alternative consumer,
  1685. and if that is not possible, to move the message
  1686. to a dead-letter queue. The server MAY use more
  1687. sophisticated tracking to hold the message on the
  1688. queue and redeliver it to the same client at a
  1689. later stage.
  1690. """
  1691. args = AMQPWriter()
  1692. args.write_longlong(delivery_tag)
  1693. args.write_bit(requeue)
  1694. self._send_method((60, 90), args)
  1695. def _basic_return(self, args, msg):
  1696. """Return a failed message
  1697. This method returns an undeliverable message that was
  1698. published with the "immediate" flag set, or an unroutable
  1699. message published with the "mandatory" flag set. The reply
  1700. code and text provide information about the reason that the
  1701. message was undeliverable.
  1702. PARAMETERS:
  1703. reply_code: short
  1704. The reply code. The AMQ reply codes are defined in AMQ
  1705. RFC 011.
  1706. reply_text: shortstr
  1707. The localised reply text. This text can be logged as an
  1708. aid to resolving issues.
  1709. exchange: shortstr
  1710. Specifies the name of the exchange that the message
  1711. was originally published to.
  1712. routing_key: shortstr
  1713. Message routing key
  1714. Specifies the routing key name specified when the
  1715. message was published.
  1716. """
  1717. self.returned_messages.put(basic_return_t(
  1718. args.read_short(),
  1719. args.read_shortstr(),
  1720. args.read_shortstr(),
  1721. args.read_shortstr(),
  1722. msg,
  1723. ))
  1724. #############
  1725. #
  1726. # Tx
  1727. #
  1728. #
  1729. # work with standard transactions
  1730. #
  1731. # Standard transactions provide so-called "1.5 phase commit". We
  1732. # can ensure that work is never lost, but there is a chance of
  1733. # confirmations being lost, so that messages may be resent.
  1734. # Applications that use standard transactions must be able to
  1735. # detect and ignore duplicate messages.
  1736. #
  1737. # GRAMMAR::
  1738. #
  1739. # tx = C:SELECT S:SELECT-OK
  1740. # / C:COMMIT S:COMMIT-OK
  1741. # / C:ROLLBACK S:ROLLBACK-OK
  1742. #
  1743. # RULE:
  1744. #
  1745. # An client using standard transactions SHOULD be able to
  1746. # track all messages received within a reasonable period, and
  1747. # thus detect and reject duplicates of the same message. It
  1748. # SHOULD NOT pass these to the application layer.
  1749. #
  1750. #
  1751. def tx_commit(self):
  1752. """Commit the current transaction
  1753. This method commits all messages published and acknowledged in
  1754. the current transaction. A new transaction starts immediately
  1755. after a commit.
  1756. """
  1757. self._send_method((90, 20))
  1758. return self.wait(allowed_methods=[
  1759. (90, 21), # Channel.tx_commit_ok
  1760. ])
  1761. def _tx_commit_ok(self, args):
  1762. """Confirm a successful commit
  1763. This method confirms to the client that the commit succeeded.
  1764. Note that if a commit fails, the server raises a channel
  1765. exception.
  1766. """
  1767. pass
  1768. def tx_rollback(self):
  1769. """Abandon the current transaction
  1770. This method abandons all messages published and acknowledged
  1771. in the current transaction. A new transaction starts
  1772. immediately after a rollback.
  1773. """
  1774. self._send_method((90, 30))
  1775. return self.wait(allowed_methods=[
  1776. (90, 31), # Channel.tx_rollback_ok
  1777. ])
  1778. def _tx_rollback_ok(self, args):
  1779. """Confirm a successful rollback
  1780. This method confirms to the client that the rollback
  1781. succeeded. Note that if an rollback fails, the server raises a
  1782. channel exception.
  1783. """
  1784. pass
  1785. def tx_select(self):
  1786. """Select standard transaction mode
  1787. This method sets the channel to use standard transactions.
  1788. The client must use this method at least once on a channel
  1789. before using the Commit or Rollback methods.
  1790. """
  1791. self._send_method((90, 10))
  1792. return self.wait(allowed_methods=[
  1793. (90, 11), # Channel.tx_select_ok
  1794. ])
  1795. def _tx_select_ok(self, args):
  1796. """Confirm transaction mode
  1797. This method confirms to the client that the channel was
  1798. successfully set to use standard transactions.
  1799. """
  1800. pass
  1801. def confirm_select(self, nowait=False):
  1802. """Enables publisher confirms for this channel (an RabbitMQ
  1803. extension).
  1804. Can now be used if the channel is in transactional mode.
  1805. :param nowait:
  1806. If set, the server will not respond to the method.
  1807. The client should not wait for a reply method. If the
  1808. server could not complete the method it will raise a channel
  1809. or connection exception.
  1810. """
  1811. args = AMQPWriter()
  1812. args.write_bit(nowait)
  1813. self._send_method((85, 10), args)
  1814. if not nowait:
  1815. self.wait(allowed_methods=[
  1816. (85, 11), # Confirm.select_ok
  1817. ])
  1818. def _confirm_select_ok(self, args):
  1819. """With this method the broker confirms to the client that
  1820. the channel is now using publisher confirms."""
  1821. pass
  1822. def _basic_ack_recv(self, args):
  1823. delivery_tag = args.read_longlong()
  1824. multiple = args.read_bit()
  1825. self._apply_callbacks('basic_ack', delivery_tag, multiple)
  1826. def _apply_callbacks(self, event, *args):
  1827. return [callback(*args) for callback in self.events[event]]
  1828. def _basic_nack(self, args):
  1829. delivery_tag = args.read_longlong()
  1830. multiple = args.read_bit()
  1831. requeue = args.read_bit()
  1832. if not self._apply_callbacks(
  1833. 'basic_nack', delivery_tag, multiple, requeue):
  1834. raise NotConfirmed(delivery_tag, (60, 120), 'basic.nack')
  1835. _METHOD_MAP = {
  1836. (20, 11): _open_ok,
  1837. (20, 20): _flow,
  1838. (20, 21): _flow_ok,
  1839. (20, 40): _close,
  1840. (20, 41): _close_ok,
  1841. (40, 11): _exchange_declare_ok,
  1842. (40, 21): _exchange_delete_ok,
  1843. (40, 31): _exchange_bind_ok,
  1844. (40, 51): _exchange_unbind_ok,
  1845. (50, 11): _queue_declare_ok,
  1846. (50, 21): _queue_bind_ok,
  1847. (50, 31): _queue_purge_ok,
  1848. (50, 41): _queue_delete_ok,
  1849. (50, 51): _queue_unbind_ok,
  1850. (60, 11): _basic_qos_ok,
  1851. (60, 21): _basic_consume_ok,
  1852. (60, 30): _basic_cancel_notify,
  1853. (60, 31): _basic_cancel_ok,
  1854. (60, 50): _basic_return,
  1855. (60, 60): _basic_deliver,
  1856. (60, 71): _basic_get_ok,
  1857. (60, 72): _basic_get_empty,
  1858. (60, 80): _basic_ack_recv,
  1859. (60, 120): _basic_nack,
  1860. (60, 111): _basic_recover_ok,
  1861. (85, 11): _confirm_select_ok,
  1862. (90, 11): _tx_select_ok,
  1863. (90, 21): _tx_commit_ok,
  1864. (90, 31): _tx_rollback_ok,
  1865. }
  1866. _IMMEDIATE_METHODS = [
  1867. (60, 50), # basic_return
  1868. ]