codegen.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. # ***** BEGIN LICENSE BLOCK *****
  2. # Version: MIT
  3. #
  4. # Portions created by Alan Antonuk are Copyright (c) 2012-2013
  5. # Alan Antonuk. All Rights Reserved.
  6. #
  7. # Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  8. # All Rights Reserved.
  9. #
  10. # Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  11. # VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  12. #
  13. # Permission is hereby granted, free of charge, to any person
  14. # obtaining a copy of this software and associated documentation
  15. # files (the "Software"), to deal in the Software without
  16. # restriction, including without limitation the rights to use, copy,
  17. # modify, merge, publish, distribute, sublicense, and/or sell copies
  18. # of the Software, and to permit persons to whom the Software is
  19. # furnished to do so, subject to the following conditions:
  20. #
  21. # The above copyright notice and this permission notice shall be
  22. # included in all copies or substantial portions of the Software.
  23. #
  24. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. # SOFTWARE.
  32. # ***** END LICENSE BLOCK *****
  33. from __future__ import nested_scopes
  34. from __future__ import division
  35. from amqp_codegen import *
  36. import string
  37. import re
  38. class Emitter(object):
  39. """An object the trivially emits generated code lines.
  40. This largely exists to be wrapped by more sophisticated emitter
  41. classes.
  42. """
  43. def __init__(self, prefix):
  44. self.prefix = prefix
  45. def emit(self, line):
  46. """Emit a line of generated code."""
  47. print self.prefix + line
  48. class BitDecoder(object):
  49. """An emitter object that keeps track of the state involved in
  50. decoding the AMQP bit type."""
  51. def __init__(self, emitter):
  52. self.emitter = emitter
  53. self.bit = 0
  54. def emit(self, line):
  55. self.bit = 0
  56. self.emitter.emit(line)
  57. def decode_bit(self, lvalue):
  58. """Generate code to decode a value of the AMQP bit type into
  59. the given lvalue."""
  60. if self.bit == 0:
  61. self.emitter.emit("if (!amqp_decode_8(encoded, &offset, &bit_buffer)) return AMQP_STATUS_BAD_AMQP_DATA;")
  62. self.emitter.emit("%s = (bit_buffer & (1 << %d)) ? 1 : 0;"
  63. % (lvalue, self.bit))
  64. self.bit += 1
  65. if self.bit == 8:
  66. self.bit = 0
  67. class BitEncoder(object):
  68. """An emitter object that keeps track of the state involved in
  69. encoding the AMQP bit type."""
  70. def __init__(self, emitter):
  71. self.emitter = emitter
  72. self.bit = 0
  73. def flush(self):
  74. """Flush the state associated with AMQP bit types."""
  75. if self.bit:
  76. self.emitter.emit("if (!amqp_encode_8(encoded, &offset, bit_buffer)) return AMQP_STATUS_BAD_AMQP_DATA;")
  77. self.bit = 0
  78. def emit(self, line):
  79. self.flush()
  80. self.emitter.emit(line)
  81. def encode_bit(self, value):
  82. """Generate code to encode a value of the AMQP bit type from
  83. the given value."""
  84. if self.bit == 0:
  85. self.emitter.emit("bit_buffer = 0;")
  86. self.emitter.emit("if (%s) bit_buffer |= (1 << %d);"
  87. % (value, self.bit))
  88. self.bit += 1
  89. if self.bit == 8:
  90. self.flush()
  91. class SimpleType(object):
  92. """A AMQP type that corresponds to a simple scalar C value of a
  93. certain width."""
  94. def __init__(self, bits):
  95. self.bits = bits
  96. self.ctype = "uint%d_t" % (bits,)
  97. def decode(self, emitter, lvalue):
  98. emitter.emit("if (!amqp_decode_%d(encoded, &offset, &%s)) return AMQP_STATUS_BAD_AMQP_DATA;" % (self.bits, lvalue))
  99. def encode(self, emitter, value):
  100. emitter.emit("if (!amqp_encode_%d(encoded, &offset, %s)) return AMQP_STATUS_BAD_AMQP_DATA;" % (self.bits, value))
  101. def literal(self, value):
  102. return value
  103. class StrType(object):
  104. """The AMQP shortstr or longstr types."""
  105. def __init__(self, lenbits):
  106. self.lenbits = lenbits
  107. self.ctype = "amqp_bytes_t"
  108. def decode(self, emitter, lvalue):
  109. emitter.emit("{")
  110. emitter.emit(" uint%d_t len;" % (self.lenbits,))
  111. emitter.emit(" if (!amqp_decode_%d(encoded, &offset, &len)" % (self.lenbits,))
  112. emitter.emit(" || !amqp_decode_bytes(encoded, &offset, &%s, len))" % (lvalue,))
  113. emitter.emit(" return AMQP_STATUS_BAD_AMQP_DATA;")
  114. emitter.emit("}")
  115. def encode(self, emitter, value):
  116. emitter.emit("if (UINT%d_MAX < %s.len" % (self.lenbits, value))
  117. emitter.emit(" || !amqp_encode_%d(encoded, &offset, (uint%d_t)%s.len)" %
  118. (self.lenbits, self.lenbits, value))
  119. emitter.emit(" || !amqp_encode_bytes(encoded, &offset, %s))" % (value,))
  120. emitter.emit(" return AMQP_STATUS_BAD_AMQP_DATA;")
  121. def literal(self, value):
  122. if value != '':
  123. raise NotImplementedError()
  124. return "amqp_empty_bytes"
  125. class BitType(object):
  126. """The AMQP bit type."""
  127. def __init__(self):
  128. self.ctype = "amqp_boolean_t"
  129. def decode(self, emitter, lvalue):
  130. emitter.decode_bit(lvalue)
  131. def encode(self, emitter, value):
  132. emitter.encode_bit(value)
  133. def literal(self, value):
  134. return {True: 1, False: 0}[value]
  135. class TableType(object):
  136. """The AMQP table type."""
  137. def __init__(self):
  138. self.ctype = "amqp_table_t"
  139. def decode(self, emitter, lvalue):
  140. emitter.emit("{")
  141. emitter.emit(" int res = amqp_decode_table(encoded, pool, &(%s), &offset);" % (lvalue,))
  142. emitter.emit(" if (res < 0) return res;")
  143. emitter.emit("}")
  144. def encode(self, emitter, value):
  145. emitter.emit("{")
  146. emitter.emit(" int res = amqp_encode_table(encoded, &(%s), &offset);" % (value,))
  147. emitter.emit(" if (res < 0) return res;")
  148. emitter.emit("}")
  149. def literal(self, value):
  150. raise NotImplementedError()
  151. types = {
  152. 'octet': SimpleType(8),
  153. 'short': SimpleType(16),
  154. 'long': SimpleType(32),
  155. 'longlong': SimpleType(64),
  156. 'shortstr': StrType(8),
  157. 'longstr': StrType(32),
  158. 'bit': BitType(),
  159. 'table': TableType(),
  160. 'timestamp': SimpleType(64),
  161. }
  162. def typeFor(spec, f):
  163. """Get a representation of the AMQP type of a field."""
  164. return types[spec.resolveDomain(f.domain)]
  165. def c_ize(s):
  166. s = s.replace('-', '_')
  167. s = s.replace(' ', '_')
  168. return s
  169. # When generating API functions corresponding to synchronous methods,
  170. # we need some information that isn't in the protocol def: Some
  171. # methods should not be exposed, indicated here by a False value.
  172. # Some methods should be exposed but certain fields should not be
  173. # exposed as parameters.
  174. apiMethodInfo = {
  175. "amqp_connection_start": False, # application code should not use this
  176. "amqp_connection_secure": False, # application code should not use this
  177. "amqp_connection_tune": False, # application code should not use this
  178. "amqp_connection_open": False, # application code should not use this
  179. "amqp_connection_close": False, # needs special handling
  180. "amqp_channel_open": ["out_of_band"],
  181. "amqp_channel_close": False, # needs special handling
  182. "amqp_access_request": False, # huh?
  183. "amqp_basic_get": False, # get-ok has content
  184. }
  185. # When generating API functions corresponding to synchronous methods,
  186. # some fields should be suppressed everywhere. This dict names those
  187. # fields, and the fixed values to use for them.
  188. apiMethodsSuppressArgs = {"ticket": 0, "nowait": False}
  189. AmqpMethod.defName = lambda m: cConstantName(c_ize(m.klass.name) + '_' + c_ize(m.name) + "_method")
  190. AmqpMethod.fullName = lambda m: "amqp_%s_%s" % (c_ize(m.klass.name), c_ize(m.name))
  191. AmqpMethod.structName = lambda m: m.fullName() + "_t"
  192. AmqpClass.structName = lambda c: "amqp_" + c_ize(c.name) + "_properties_t"
  193. def methodApiPrototype(m):
  194. fn = m.fullName()
  195. info = apiMethodInfo.get(fn, [])
  196. docs = "/**\n * %s\n *\n" % (fn)
  197. docs += " * @param [in] state connection state\n"
  198. docs += " * @param [in] channel the channel to do the RPC on\n"
  199. args = []
  200. for f in m.arguments:
  201. n = c_ize(f.name)
  202. if n in apiMethodsSuppressArgs or n in info:
  203. continue
  204. args.append(", ")
  205. args.append(typeFor(m.klass.spec, f).ctype)
  206. args.append(" ")
  207. args.append(n)
  208. docs += " * @param [in] %s %s\n" % (n, n)
  209. docs += " * @returns %s_ok_t\n" % (fn)
  210. docs += " */\n"
  211. return "%sAMQP_PUBLIC_FUNCTION\n%s_ok_t *\nAMQP_CALL %s(amqp_connection_state_t state, amqp_channel_t channel%s)" % (docs, fn, fn, ''.join(args))
  212. AmqpMethod.apiPrototype = methodApiPrototype
  213. def cConstantName(s):
  214. return 'AMQP_' + '_'.join(re.split('[- ]', s.upper()))
  215. def cFlagName(c, f):
  216. return cConstantName(c.name + '_' + f.name) + '_FLAG'
  217. def genErl(spec):
  218. def fieldTempList(fields):
  219. return '[' + ', '.join(['F' + str(f.index) for f in fields]) + ']'
  220. def fieldMapList(fields):
  221. return ', '.join([c_ize(f.name) + " = F" + str(f.index) for f in fields])
  222. def genLookupMethodName(m):
  223. print ' case %s: return "%s";' % (m.defName(), m.defName())
  224. def genDecodeMethodFields(m):
  225. print " case %s: {" % (m.defName(),)
  226. print " %s *m = (%s *) amqp_pool_alloc(pool, sizeof(%s));" % \
  227. (m.structName(), m.structName(), m.structName())
  228. print " if (m == NULL) { return AMQP_STATUS_NO_MEMORY; }"
  229. emitter = BitDecoder(Emitter(" "))
  230. for f in m.arguments:
  231. typeFor(spec, f).decode(emitter, "m->"+c_ize(f.name))
  232. print " *decoded = m;"
  233. print " return 0;"
  234. print " }"
  235. def genDecodeProperties(c):
  236. print " case %d: {" % (c.index,)
  237. print " %s *p = (%s *) amqp_pool_alloc(pool, sizeof(%s));" % \
  238. (c.structName(), c.structName(), c.structName())
  239. print " if (p == NULL) { return AMQP_STATUS_NO_MEMORY; }"
  240. print " p->_flags = flags;"
  241. emitter = Emitter(" ")
  242. for f in c.fields:
  243. emitter.emit("if (flags & %s) {" % (cFlagName(c, f),))
  244. typeFor(spec, f).decode(emitter, "p->"+c_ize(f.name))
  245. emitter.emit("}")
  246. print " *decoded = p;"
  247. print " return 0;"
  248. print " }"
  249. def genEncodeMethodFields(m):
  250. print " case %s: {" % (m.defName(),)
  251. if m.arguments:
  252. print " %s *m = (%s *) decoded;" % (m.structName(), m.structName())
  253. emitter = BitEncoder(Emitter(" "))
  254. for f in m.arguments:
  255. typeFor(spec, f).encode(emitter, "m->"+c_ize(f.name))
  256. emitter.flush()
  257. print " return (int)offset;"
  258. print " }"
  259. def genEncodeProperties(c):
  260. print " case %d: {" % (c.index,)
  261. if c.fields:
  262. print " %s *p = (%s *) decoded;" % (c.structName(), c.structName())
  263. emitter = Emitter(" ")
  264. for f in c.fields:
  265. emitter.emit(" if (flags & %s) {" % (cFlagName(c, f),))
  266. typeFor(spec, f).encode(emitter, "p->"+c_ize(f.name))
  267. emitter.emit("}")
  268. print " return (int)offset;"
  269. print " }"
  270. methods = spec.allMethods()
  271. print """/* Generated code. Do not edit. Edit and re-run codegen.py instead.
  272. *
  273. * ***** BEGIN LICENSE BLOCK *****
  274. * Version: MIT
  275. *
  276. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  277. * Alan Antonuk. All Rights Reserved.
  278. *
  279. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  280. * All Rights Reserved.
  281. *
  282. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  283. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  284. *
  285. * Permission is hereby granted, free of charge, to any person
  286. * obtaining a copy of this software and associated documentation
  287. * files (the "Software"), to deal in the Software without
  288. * restriction, including without limitation the rights to use, copy,
  289. * modify, merge, publish, distribute, sublicense, and/or sell copies
  290. * of the Software, and to permit persons to whom the Software is
  291. * furnished to do so, subject to the following conditions:
  292. *
  293. * The above copyright notice and this permission notice shall be
  294. * included in all copies or substantial portions of the Software.
  295. *
  296. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  297. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  298. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  299. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  300. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  301. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  302. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  303. * SOFTWARE.
  304. * ***** END LICENSE BLOCK *****
  305. */
  306. #ifdef HAVE_CONFIG_H
  307. #include "config.h"
  308. #endif
  309. #include "amqp_private.h"
  310. #include <stdint.h>
  311. #include <stdio.h>
  312. #include <stdlib.h>
  313. #include <string.h>
  314. """
  315. print """
  316. char const *amqp_constant_name(int constantNumber) {
  317. switch (constantNumber) {"""
  318. for (c,v,cls) in spec.constants:
  319. print " case %s: return \"%s\";" % (cConstantName(c), cConstantName(c))
  320. print """ default: return "(unknown)";
  321. }
  322. }"""
  323. print """
  324. amqp_boolean_t amqp_constant_is_hard_error(int constantNumber) {
  325. switch (constantNumber) {"""
  326. for (c,v,cls) in spec.constants:
  327. if cls == 'hard-error':
  328. print " case %s: return 1;" % (cConstantName(c),)
  329. print """ default: return 0;
  330. }
  331. }"""
  332. print """
  333. char const *amqp_method_name(amqp_method_number_t methodNumber) {
  334. switch (methodNumber) {"""
  335. for m in methods: genLookupMethodName(m)
  336. print """ default: return NULL;
  337. }
  338. }"""
  339. print """
  340. amqp_boolean_t amqp_method_has_content(amqp_method_number_t methodNumber) {
  341. switch (methodNumber) {"""
  342. for m in methods:
  343. if m.hasContent:
  344. print ' case %s: return 1;' % (m.defName())
  345. print """ default: return 0;
  346. }
  347. }"""
  348. print """
  349. int amqp_decode_method(amqp_method_number_t methodNumber,
  350. amqp_pool_t *pool,
  351. amqp_bytes_t encoded,
  352. void **decoded)
  353. {
  354. size_t offset = 0;
  355. uint8_t bit_buffer;
  356. switch (methodNumber) {"""
  357. for m in methods: genDecodeMethodFields(m)
  358. print """ default: return AMQP_STATUS_UNKNOWN_METHOD;
  359. }
  360. }"""
  361. print """
  362. int amqp_decode_properties(uint16_t class_id,
  363. amqp_pool_t *pool,
  364. amqp_bytes_t encoded,
  365. void **decoded)
  366. {
  367. size_t offset = 0;
  368. amqp_flags_t flags = 0;
  369. int flagword_index = 0;
  370. uint16_t partial_flags;
  371. do {
  372. if (!amqp_decode_16(encoded, &offset, &partial_flags))
  373. return AMQP_STATUS_BAD_AMQP_DATA;
  374. flags |= (partial_flags << (flagword_index * 16));
  375. flagword_index++;
  376. } while (partial_flags & 1);
  377. switch (class_id) {"""
  378. for c in spec.allClasses(): genDecodeProperties(c)
  379. print """ default: return AMQP_STATUS_UNKNOWN_CLASS;
  380. }
  381. }"""
  382. print """
  383. int amqp_encode_method(amqp_method_number_t methodNumber,
  384. void *decoded,
  385. amqp_bytes_t encoded)
  386. {
  387. size_t offset = 0;
  388. uint8_t bit_buffer;
  389. switch (methodNumber) {"""
  390. for m in methods: genEncodeMethodFields(m)
  391. print """ default: return AMQP_STATUS_UNKNOWN_METHOD;
  392. }
  393. }"""
  394. print """
  395. int amqp_encode_properties(uint16_t class_id,
  396. void *decoded,
  397. amqp_bytes_t encoded)
  398. {
  399. size_t offset = 0;
  400. /* Cheat, and get the flags out generically, relying on the
  401. similarity of structure between classes */
  402. amqp_flags_t flags = * (amqp_flags_t *) decoded; /* cheating! */
  403. {
  404. /* We take a copy of flags to avoid destroying it, as it is used
  405. in the autogenerated code below. */
  406. amqp_flags_t remaining_flags = flags;
  407. do {
  408. amqp_flags_t remainder = remaining_flags >> 16;
  409. uint16_t partial_flags = remaining_flags & 0xFFFE;
  410. if (remainder != 0) { partial_flags |= 1; }
  411. if (!amqp_encode_16(encoded, &offset, partial_flags))
  412. return AMQP_STATUS_BAD_AMQP_DATA;
  413. remaining_flags = remainder;
  414. } while (remaining_flags != 0);
  415. }
  416. switch (class_id) {"""
  417. for c in spec.allClasses(): genEncodeProperties(c)
  418. print """ default: return AMQP_STATUS_UNKNOWN_CLASS;
  419. }
  420. }"""
  421. for m in methods:
  422. if not m.isSynchronous:
  423. continue
  424. info = apiMethodInfo.get(m.fullName(), [])
  425. if info is False:
  426. continue
  427. print
  428. print m.apiPrototype()
  429. print "{"
  430. print " %s req;" % (m.structName(),)
  431. for f in m.arguments:
  432. n = c_ize(f.name)
  433. val = apiMethodsSuppressArgs.get(n)
  434. if val is None and n in info:
  435. val = f.defaultvalue
  436. if val is None:
  437. val = n
  438. else:
  439. val = typeFor(spec, f).literal(val)
  440. print " req.%s = %s;" % (n, val)
  441. reply = cConstantName(c_ize(m.klass.name) + '_' + c_ize(m.name)
  442. + "_ok_method")
  443. print """
  444. return amqp_simple_rpc_decoded(state, channel, %s, %s, &req);
  445. }
  446. """ % (m.defName(), reply)
  447. def genHrl(spec):
  448. def fieldDeclList(fields):
  449. if fields:
  450. return ''.join([" %s %s; /**< %s */\n" % (typeFor(spec, f).ctype,
  451. c_ize(f.name), f.name)
  452. for f in fields])
  453. else:
  454. return " char dummy; /**< Dummy field to avoid empty struct */\n"
  455. def propDeclList(fields):
  456. return ''.join([" %s %s;\n" % (typeFor(spec, f).ctype, c_ize(f.name))
  457. for f in fields
  458. if spec.resolveDomain(f.domain) != 'bit'])
  459. methods = spec.allMethods()
  460. print """/* Generated code. Do not edit. Edit and re-run codegen.py instead.
  461. *
  462. * ***** BEGIN LICENSE BLOCK *****
  463. * Version: MIT
  464. *
  465. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  466. * Alan Antonuk. All Rights Reserved.
  467. *
  468. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  469. * All Rights Reserved.
  470. *
  471. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  472. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  473. *
  474. * Permission is hereby granted, free of charge, to any person
  475. * obtaining a copy of this software and associated documentation
  476. * files (the "Software"), to deal in the Software without
  477. * restriction, including without limitation the rights to use, copy,
  478. * modify, merge, publish, distribute, sublicense, and/or sell copies
  479. * of the Software, and to permit persons to whom the Software is
  480. * furnished to do so, subject to the following conditions:
  481. *
  482. * The above copyright notice and this permission notice shall be
  483. * included in all copies or substantial portions of the Software.
  484. *
  485. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  486. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  487. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  488. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  489. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  490. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  491. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  492. * SOFTWARE.
  493. * ***** END LICENSE BLOCK *****
  494. */
  495. /** @file amqp_framing.h */
  496. #ifndef AMQP_FRAMING_H
  497. #define AMQP_FRAMING_H
  498. #include <amqp.h>
  499. AMQP_BEGIN_DECLS
  500. """
  501. print "#define AMQP_PROTOCOL_VERSION_MAJOR %d /**< AMQP protocol version major */" % (spec.major)
  502. print "#define AMQP_PROTOCOL_VERSION_MINOR %d /**< AMQP protocol version minor */" % (spec.minor)
  503. print "#define AMQP_PROTOCOL_VERSION_REVISION %d /**< AMQP protocol version revision */" % (spec.revision)
  504. print "#define AMQP_PROTOCOL_PORT %d /**< Default AMQP Port */" % (spec.port)
  505. for (c,v,cls) in spec.constants:
  506. print "#define %s %s /**< Constant: %s */" % (cConstantName(c), v, c)
  507. print
  508. print """/* Function prototypes. */
  509. /**
  510. * Get constant name string from constant
  511. *
  512. * @param [in] constantNumber constant to get the name of
  513. * @returns string describing the constant. String is managed by
  514. * the library and should not be free()'d by the program
  515. */
  516. AMQP_PUBLIC_FUNCTION
  517. char const *
  518. AMQP_CALL amqp_constant_name(int constantNumber);
  519. /**
  520. * Checks to see if a constant is a hard error
  521. *
  522. * A hard error occurs when something severe enough
  523. * happens that the connection must be closed.
  524. *
  525. * @param [in] constantNumber the error constant
  526. * @returns true if its a hard error, false otherwise
  527. */
  528. AMQP_PUBLIC_FUNCTION
  529. amqp_boolean_t
  530. AMQP_CALL amqp_constant_is_hard_error(int constantNumber);
  531. /**
  532. * Get method name string from method number
  533. *
  534. * @param [in] methodNumber the method number
  535. * @returns method name string. String is managed by the library
  536. * and should not be freed()'d by the program
  537. */
  538. AMQP_PUBLIC_FUNCTION
  539. char const *
  540. AMQP_CALL amqp_method_name(amqp_method_number_t methodNumber);
  541. /**
  542. * Check whether a method has content
  543. *
  544. * A method that has content will receive the method frame
  545. * a properties frame, then 1 to N body frames
  546. *
  547. * @param [in] methodNumber the method number
  548. * @returns true if method has content, false otherwise
  549. */
  550. AMQP_PUBLIC_FUNCTION
  551. amqp_boolean_t
  552. AMQP_CALL amqp_method_has_content(amqp_method_number_t methodNumber);
  553. /**
  554. * Decodes a method from AMQP wireformat
  555. *
  556. * @param [in] methodNumber the method number for the decoded parameter
  557. * @param [in] pool the memory pool to allocate the decoded method from
  558. * @param [in] encoded the encoded byte string buffer
  559. * @param [out] decoded pointer to the decoded method struct
  560. * @returns 0 on success, an error code otherwise
  561. */
  562. AMQP_PUBLIC_FUNCTION
  563. int
  564. AMQP_CALL amqp_decode_method(amqp_method_number_t methodNumber,
  565. amqp_pool_t *pool,
  566. amqp_bytes_t encoded,
  567. void **decoded);
  568. /**
  569. * Decodes a header frame properties structure from AMQP wireformat
  570. *
  571. * @param [in] class_id the class id for the decoded parameter
  572. * @param [in] pool the memory pool to allocate the decoded properties from
  573. * @param [in] encoded the encoded byte string buffer
  574. * @param [out] decoded pointer to the decoded properties struct
  575. * @returns 0 on success, an error code otherwise
  576. */
  577. AMQP_PUBLIC_FUNCTION
  578. int
  579. AMQP_CALL amqp_decode_properties(uint16_t class_id,
  580. amqp_pool_t *pool,
  581. amqp_bytes_t encoded,
  582. void **decoded);
  583. /**
  584. * Encodes a method structure in AMQP wireformat
  585. *
  586. * @param [in] methodNumber the method number for the decoded parameter
  587. * @param [in] decoded the method structure (e.g., amqp_connection_start_t)
  588. * @param [in] encoded an allocated byte buffer for the encoded method
  589. * structure to be written to. If the buffer isn't large enough
  590. * to hold the encoded method, an error code will be returned.
  591. * @returns 0 on success, an error code otherwise.
  592. */
  593. AMQP_PUBLIC_FUNCTION
  594. int
  595. AMQP_CALL amqp_encode_method(amqp_method_number_t methodNumber,
  596. void *decoded,
  597. amqp_bytes_t encoded);
  598. /**
  599. * Encodes a properties structure in AMQP wireformat
  600. *
  601. * @param [in] class_id the class id for the decoded parameter
  602. * @param [in] decoded the properties structure (e.g., amqp_basic_properties_t)
  603. * @param [in] encoded an allocated byte buffer for the encoded properties to written to.
  604. * If the buffer isn't large enough to hold the encoded method, an
  605. * an error code will be returned
  606. * @returns 0 on success, an error code otherwise.
  607. */
  608. AMQP_PUBLIC_FUNCTION
  609. int
  610. AMQP_CALL amqp_encode_properties(uint16_t class_id,
  611. void *decoded,
  612. amqp_bytes_t encoded);
  613. """
  614. print "/* Method field records. */\n"
  615. for m in methods:
  616. methodid = m.klass.index << 16 | m.index
  617. print "#define %s ((amqp_method_number_t) 0x%.08X) /**< %s.%s method id @internal %d, %d; %d */" % \
  618. (m.defName(),
  619. methodid,
  620. m.klass.name,
  621. m.name,
  622. m.klass.index,
  623. m.index,
  624. methodid)
  625. print "/** %s.%s method fields */\ntypedef struct %s_ {\n%s} %s;\n" % \
  626. (m.klass.name, m.name, m.structName(), fieldDeclList(m.arguments), m.structName())
  627. print "/* Class property records. */"
  628. for c in spec.allClasses():
  629. print "#define %s (0x%.04X) /**< %s class id @internal %d */" % \
  630. (cConstantName(c.name + "_class"), c.index, c.name, c.index)
  631. index = 0
  632. for f in c.fields:
  633. if index % 16 == 15:
  634. index = index + 1
  635. shortnum = index // 16
  636. partialindex = 15 - (index % 16)
  637. bitindex = shortnum * 16 + partialindex
  638. print '#define %s (1 << %d) /**< %s.%s property flag */' % (cFlagName(c, f), bitindex, c.name, f.name)
  639. index = index + 1
  640. print "/** %s class properties */\ntypedef struct %s_ {\n amqp_flags_t _flags; /**< bit-mask of set fields */\n%s} %s;\n" % \
  641. (c.name,
  642. c.structName(),
  643. fieldDeclList(c.fields),
  644. c.structName())
  645. print "/* API functions for methods */\n"
  646. for m in methods:
  647. if m.isSynchronous and apiMethodInfo.get(m.fullName()) is not False:
  648. print "%s;" % (m.apiPrototype(),)
  649. print """
  650. AMQP_END_DECLS
  651. #endif /* AMQP_FRAMING_H */"""
  652. def generateErl(specPath):
  653. genErl(AmqpSpec(specPath))
  654. def generateHrl(specPath):
  655. genHrl(AmqpSpec(specPath))
  656. if __name__ == "__main__":
  657. do_main(generateHrl, generateErl)