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