Back to index

rabbitmq-server  2.8.4
codegen.py
Go to the documentation of this file.
00001 ##  The contents of this file are subject to the Mozilla Public License
00002 ##  Version 1.1 (the "License"); you may not use this file except in
00003 ##  compliance with the License. You may obtain a copy of the License
00004 ##  at http://www.mozilla.org/MPL/
00005 ##
00006 ##  Software distributed under the License is distributed on an "AS IS"
00007 ##  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00008 ##  the License for the specific language governing rights and
00009 ##  limitations under the License.
00010 ##
00011 ##  The Original Code is RabbitMQ.
00012 ##
00013 ##  The Initial Developer of the Original Code is VMware, Inc.
00014 ##  Copyright (c) 2007-2012 VMware, Inc.  All rights reserved.
00015 ##
00016 
00017 from __future__ import nested_scopes
00018 
00019 import sys
00020 sys.path.append("../rabbitmq-codegen")  # in case we're next to an experimental revision
00021 sys.path.append("codegen")              # in case we're building from a distribution package
00022 
00023 from amqp_codegen import *
00024 import string
00025 import re
00026 
00027 erlangTypeMap = {
00028     'octet': 'octet',
00029     'shortstr': 'shortstr',
00030     'longstr': 'longstr',
00031     'short': 'shortint',
00032     'long': 'longint',
00033     'longlong': 'longlongint',
00034     'bit': 'bit',
00035     'table': 'table',
00036     'timestamp': 'timestamp',
00037 }
00038 
00039 # Coming up with a proper encoding of AMQP tables in JSON is too much
00040 # hassle at this stage. Given that the only default value we are
00041 # interested in is for the empty table, we only support that.
00042 def convertTable(d):
00043     if len(d) == 0:
00044         return "[]"
00045     else:
00046         raise Exception('Non-empty table defaults not supported ' + d)
00047 
00048 erlangDefaultValueTypeConvMap = {
00049     bool : lambda x: str(x).lower(),
00050     str : lambda x: "<<\"" + x + "\">>",
00051     int : lambda x: str(x),
00052     float : lambda x: str(x),
00053     dict: convertTable,
00054     unicode: lambda x: "<<\"" + x.encode("utf-8") + "\">>"
00055 }
00056 
00057 def erlangize(s):
00058     s = s.replace('-', '_')
00059     s = s.replace(' ', '_')
00060     return s
00061 
00062 AmqpMethod.erlangName = lambda m: "'" + erlangize(m.klass.name) + '.' + erlangize(m.name) + "'"
00063 
00064 AmqpClass.erlangName = lambda c: "'" + erlangize(c.name) + "'"
00065 
00066 def erlangConstantName(s):
00067     return '_'.join(re.split('[- ]', s.upper()))
00068 
00069 class PackedMethodBitField:
00070     def __init__(self, index):
00071         self.index = index
00072         self.domain = 'bit'
00073         self.contents = []
00074 
00075     def extend(self, f):
00076         self.contents.append(f)
00077 
00078     def count(self):
00079         return len(self.contents)
00080 
00081     def full(self):
00082         return self.count() == 8
00083 
00084 def multiLineFormat(things, prologue, separator, lineSeparator, epilogue, thingsPerLine = 4):
00085     r = [prologue]
00086     i = 0
00087     for t in things:
00088         if i != 0:
00089             if i % thingsPerLine == 0:
00090                 r += [lineSeparator]
00091             else:
00092                 r += [separator]
00093         r += [t]
00094         i += 1
00095     r += [epilogue]
00096     return "".join(r)
00097 
00098 def prettyType(typeName, subTypes, typesPerLine = 4):
00099     """Pretty print a type signature made up of many alternative subtypes"""
00100     sTs = multiLineFormat(subTypes,
00101                           "( ", " | ", "\n       | ", " )",
00102                           thingsPerLine = typesPerLine)
00103     return "-type(%s ::\n       %s)." % (typeName, sTs)
00104 
00105 def printFileHeader():
00106     print """%%   Autogenerated code. Do not edit.
00107 %%
00108 %%  The contents of this file are subject to the Mozilla Public License
00109 %%  Version 1.1 (the "License"); you may not use this file except in
00110 %%  compliance with the License. You may obtain a copy of the License
00111 %%  at http://www.mozilla.org/MPL/
00112 %%
00113 %%  Software distributed under the License is distributed on an "AS IS"
00114 %%  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00115 %%  the License for the specific language governing rights and
00116 %%  limitations under the License.
00117 %%
00118 %%  The Original Code is RabbitMQ.
00119 %%
00120 %%  The Initial Developer of the Original Code is VMware, Inc.
00121 %%  Copyright (c) 2007-2012 VMware, Inc.  All rights reserved.
00122 %%"""
00123 
00124 def genErl(spec):
00125     def erlType(domain):
00126         return erlangTypeMap[spec.resolveDomain(domain)]
00127 
00128     def fieldTypeList(fields):
00129         return '[' + ', '.join([erlType(f.domain) for f in fields]) + ']'
00130 
00131     def fieldNameList(fields):
00132         return '[' + ', '.join([erlangize(f.name) for f in fields]) + ']'
00133 
00134     def fieldTempList(fields):
00135         return '[' + ', '.join(['F' + str(f.index) for f in fields]) + ']'
00136 
00137     def fieldMapList(fields):
00138         return ', '.join([erlangize(f.name) + " = F" + str(f.index) for f in fields])
00139 
00140     def genLookupMethodName(m):
00141         print "lookup_method_name({%d, %d}) -> %s;" % (m.klass.index, m.index, m.erlangName())
00142 
00143     def genLookupClassName(c):
00144         print "lookup_class_name(%d) -> %s;" % (c.index, c.erlangName())
00145 
00146     def genMethodId(m):
00147         print "method_id(%s) -> {%d, %d};" % (m.erlangName(), m.klass.index, m.index)
00148 
00149     def genMethodHasContent(m):
00150         print "method_has_content(%s) -> %s;" % (m.erlangName(), str(m.hasContent).lower())
00151 
00152     def genMethodIsSynchronous(m):
00153         hasNoWait = "nowait" in fieldNameList(m.arguments)
00154         if m.isSynchronous and hasNoWait:
00155           print "is_method_synchronous(#%s{nowait = NoWait}) -> not(NoWait);" % (m.erlangName())
00156         else:
00157           print "is_method_synchronous(#%s{}) -> %s;" % (m.erlangName(), str(m.isSynchronous).lower())
00158 
00159     def genMethodFieldTypes(m):
00160         """Not currently used - may be useful in future?"""
00161         print "method_fieldtypes(%s) -> %s;" % (m.erlangName(), fieldTypeList(m.arguments))
00162 
00163     def genMethodFieldNames(m):
00164         print "method_fieldnames(%s) -> %s;" % (m.erlangName(), fieldNameList(m.arguments))
00165 
00166     def packMethodFields(fields):
00167         packed = []
00168         bitfield = None
00169         for f in fields:
00170             if erlType(f.domain) == 'bit':
00171                 if not(bitfield) or bitfield.full():
00172                     bitfield = PackedMethodBitField(f.index)
00173                     packed.append(bitfield)
00174                 bitfield.extend(f)
00175             else:
00176                 bitfield = None
00177                 packed.append(f)
00178         return packed
00179 
00180     def methodFieldFragment(f):
00181         type = erlType(f.domain)
00182         p = 'F' + str(f.index)
00183         if type == 'shortstr':
00184             return p+'Len:8/unsigned, '+p+':'+p+'Len/binary'
00185         elif type == 'longstr':
00186             return p+'Len:32/unsigned, '+p+':'+p+'Len/binary'
00187         elif type == 'octet':
00188             return p+':8/unsigned'
00189         elif type == 'shortint':
00190             return p+':16/unsigned'
00191         elif type == 'longint':
00192             return p+':32/unsigned'
00193         elif type == 'longlongint':
00194             return p+':64/unsigned'
00195         elif type == 'timestamp':
00196             return p+':64/unsigned'
00197         elif type == 'bit':
00198             return p+'Bits:8'
00199         elif type == 'table':
00200             return p+'Len:32/unsigned, '+p+'Tab:'+p+'Len/binary'
00201 
00202     def genFieldPostprocessing(packed):
00203         for f in packed:
00204             type = erlType(f.domain)
00205             if type == 'bit':
00206                 for index in range(f.count()):
00207                     print "  F%d = ((F%dBits band %d) /= 0)," % \
00208                           (f.index + index,
00209                            f.index,
00210                            1 << index)
00211             elif type == 'table':
00212                 print "  F%d = rabbit_binary_parser:parse_table(F%dTab)," % \
00213                       (f.index, f.index)
00214             else:
00215                 pass
00216 
00217     def genMethodRecord(m):
00218         print "method_record(%s) -> #%s{};" % (m.erlangName(), m.erlangName())
00219 
00220     def genDecodeMethodFields(m):
00221         packedFields = packMethodFields(m.arguments)
00222         binaryPattern = ', '.join([methodFieldFragment(f) for f in packedFields])
00223         if binaryPattern:
00224             restSeparator = ', '
00225         else:
00226             restSeparator = ''
00227         recordConstructorExpr = '#%s{%s}' % (m.erlangName(), fieldMapList(m.arguments))
00228         print "decode_method_fields(%s, <<%s>>) ->" % (m.erlangName(), binaryPattern)
00229         genFieldPostprocessing(packedFields)
00230         print "  %s;" % (recordConstructorExpr,)
00231 
00232     def genDecodeProperties(c):
00233         def presentBin(fields):
00234             ps = ', '.join(['P' + str(f.index) + ':1' for f in fields])
00235             return '<<' + ps + ', _:%d, R0/binary>>' % (16 - len(fields),)
00236         def mkMacroName(field):
00237             return '?' + field.domain.upper() + '_PROP'
00238         def writePropFieldLine(field, bin_next = None):
00239             i = str(field.index)
00240             if not bin_next:
00241                 bin_next = 'R' + str(field.index + 1)
00242             if field.domain in ['octet', 'timestamp']:
00243                 print ("  {%s, %s} = %s(%s, %s, %s, %s)," %
00244                        ('F' + i, bin_next, mkMacroName(field), 'P' + i,
00245                         'R' + i, 'I' + i, 'X' + i))
00246             else:
00247                 print ("  {%s, %s} = %s(%s, %s, %s, %s, %s)," %
00248                        ('F' + i, bin_next, mkMacroName(field), 'P' + i,
00249                         'R' + i, 'L' + i, 'S' + i, 'X' + i))
00250 
00251         if len(c.fields) == 0:
00252             print "decode_properties(%d, _) ->" % (c.index,)
00253         else:
00254             print ("decode_properties(%d, %s) ->" %
00255                    (c.index, presentBin(c.fields)))
00256             for field in c.fields[:-1]:
00257                 writePropFieldLine(field)
00258             writePropFieldLine(c.fields[-1], "<<>>")
00259         print "  #'P_%s'{%s};" % (erlangize(c.name), fieldMapList(c.fields))
00260 
00261     def genFieldPreprocessing(packed):
00262         for f in packed:
00263             type = erlType(f.domain)
00264             if type == 'bit':
00265                 print "  F%dBits = (%s)," % \
00266                       (f.index,
00267                        ' bor '.join(['(bitvalue(F%d) bsl %d)' % (x.index, x.index - f.index)
00268                                      for x in f.contents]))
00269             elif type == 'table':
00270                 print "  F%dTab = rabbit_binary_generator:generate_table(F%d)," % (f.index, f.index)
00271                 print "  F%dLen = size(F%dTab)," % (f.index, f.index)
00272             elif type == 'shortstr':
00273                 print "  F%dLen = shortstr_size(F%d)," % (f.index, f.index)
00274             elif type == 'longstr':
00275                 print "  F%dLen = size(F%d)," % (f.index, f.index)
00276             else:
00277                 pass
00278 
00279     def genEncodeMethodFields(m):
00280         packedFields = packMethodFields(m.arguments)
00281         print "encode_method_fields(#%s{%s}) ->" % (m.erlangName(), fieldMapList(m.arguments))
00282         genFieldPreprocessing(packedFields)
00283         print "  <<%s>>;" % (', '.join([methodFieldFragment(f) for f in packedFields]))
00284 
00285     def genEncodeProperties(c):
00286         print "encode_properties(#'P_%s'{%s}) ->" % (erlangize(c.name), fieldMapList(c.fields))
00287         print "  rabbit_binary_generator:encode_properties(%s, %s);" % \
00288               (fieldTypeList(c.fields), fieldTempList(c.fields))
00289 
00290     def messageConstantClass(cls):
00291         # We do this because 0.8 uses "soft error" and 8.1 uses "soft-error".
00292         return erlangConstantName(cls)
00293 
00294     def genLookupException(c,v,cls):
00295         mCls = messageConstantClass(cls)
00296         if mCls == 'SOFT_ERROR': genLookupException1(c,'false')
00297         elif mCls == 'HARD_ERROR': genLookupException1(c, 'true')
00298         elif mCls == '': pass
00299         else: raise Exception('Unknown constant class' + cls)
00300 
00301     def genLookupException1(c,hardErrorBoolStr):
00302         n = erlangConstantName(c)
00303         print 'lookup_amqp_exception(%s) -> {%s, ?%s, <<"%s">>};' % \
00304               (n.lower(), hardErrorBoolStr, n, n)
00305 
00306     def genAmqpException(c,v,cls):
00307         n = erlangConstantName(c)
00308         print 'amqp_exception(?%s) -> %s;' % \
00309             (n, n.lower())
00310 
00311     methods = spec.allMethods()
00312 
00313     printFileHeader()
00314     module = "rabbit_framing_amqp_%d_%d" % (spec.major, spec.minor)
00315     if spec.revision != 0:
00316         module = "%s_%d" % (module, spec.revision)
00317     if module == "rabbit_framing_amqp_8_0":
00318         module = "rabbit_framing_amqp_0_8"
00319     print "-module(%s)." % module
00320     print """-include("rabbit_framing.hrl").
00321 
00322 -export([version/0]).
00323 -export([lookup_method_name/1]).
00324 -export([lookup_class_name/1]).
00325 
00326 -export([method_id/1]).
00327 -export([method_has_content/1]).
00328 -export([is_method_synchronous/1]).
00329 -export([method_record/1]).
00330 -export([method_fieldnames/1]).
00331 -export([decode_method_fields/2]).
00332 -export([decode_properties/2]).
00333 -export([encode_method_fields/1]).
00334 -export([encode_properties/1]).
00335 -export([lookup_amqp_exception/1]).
00336 -export([amqp_exception/1]).
00337 
00338 """
00339     print "%% Various types"
00340     print "-ifdef(use_specs)."
00341 
00342     print """-export_type([amqp_field_type/0, amqp_property_type/0,
00343               amqp_table/0, amqp_array/0, amqp_value/0,
00344               amqp_method_name/0, amqp_method/0, amqp_method_record/0,
00345               amqp_method_field_name/0, amqp_property_record/0,
00346               amqp_exception/0, amqp_exception_code/0, amqp_class_id/0]).
00347 
00348 -type(amqp_field_type() ::
00349       'longstr' | 'signedint' | 'decimal' | 'timestamp' |
00350       'table' | 'byte' | 'double' | 'float' | 'long' |
00351       'short' | 'bool' | 'binary' | 'void' | 'array').
00352 -type(amqp_property_type() ::
00353       'shortstr' | 'longstr' | 'octet' | 'shortint' | 'longint' |
00354       'longlongint' | 'timestamp' | 'bit' | 'table').
00355 
00356 -type(amqp_table() :: [{binary(), amqp_field_type(), amqp_value()}]).
00357 -type(amqp_array() :: [{amqp_field_type(), amqp_value()}]).
00358 -type(amqp_value() :: binary() |    % longstr
00359                       integer() |   % signedint
00360                       {non_neg_integer(), non_neg_integer()} | % decimal
00361                       amqp_table() |
00362                       amqp_array() |
00363                       byte() |      % byte
00364                       float() |     % double
00365                       integer() |   % long
00366                       integer() |   % short
00367                       boolean() |   % bool
00368                       binary() |    % binary
00369                       'undefined' | % void
00370                       non_neg_integer() % timestamp
00371      ).
00372 """
00373 
00374     print prettyType("amqp_method_name()",
00375                      [m.erlangName() for m in methods])
00376     print prettyType("amqp_method()",
00377                      ["{%s, %s}" % (m.klass.index, m.index) for m in methods],
00378                      6)
00379     print prettyType("amqp_method_record()",
00380                      ["#%s{}" % (m.erlangName()) for m in methods])
00381     fieldNames = set()
00382     for m in methods:
00383         fieldNames.update(m.arguments)
00384     fieldNames = [erlangize(f.name) for f in fieldNames]
00385     print prettyType("amqp_method_field_name()",
00386                      fieldNames)
00387     print prettyType("amqp_property_record()",
00388                      ["#'P_%s'{}" % erlangize(c.name) for c in spec.allClasses()])
00389     print prettyType("amqp_exception()",
00390                      ["'%s'" % erlangConstantName(c).lower() for (c, v, cls) in spec.constants])
00391     print prettyType("amqp_exception_code()",
00392                      ["%i" % v for (c, v, cls) in spec.constants])
00393     classIds = set()
00394     for m in spec.allMethods():
00395         classIds.add(m.klass.index)
00396     print prettyType("amqp_class_id()",
00397                      ["%i" % ci for ci in classIds])
00398     print prettyType("amqp_class_name()",
00399                      ["%s" % c.erlangName() for c in spec.allClasses()])
00400     print "-endif. % use_specs"
00401 
00402     print """
00403 %% Method signatures
00404 -ifdef(use_specs).
00405 -spec(version/0 :: () -> {non_neg_integer(), non_neg_integer(), non_neg_integer()}).
00406 -spec(lookup_method_name/1 :: (amqp_method()) -> amqp_method_name()).
00407 -spec(lookup_class_name/1 :: (amqp_class_id()) -> amqp_class_name()).
00408 -spec(method_id/1 :: (amqp_method_name()) -> amqp_method()).
00409 -spec(method_has_content/1 :: (amqp_method_name()) -> boolean()).
00410 -spec(is_method_synchronous/1 :: (amqp_method_record()) -> boolean()).
00411 -spec(method_record/1 :: (amqp_method_name()) -> amqp_method_record()).
00412 -spec(method_fieldnames/1 :: (amqp_method_name()) -> [amqp_method_field_name()]).
00413 -spec(decode_method_fields/2 ::
00414         (amqp_method_name(), binary()) -> amqp_method_record() | rabbit_types:connection_exit()).
00415 -spec(decode_properties/2 :: (non_neg_integer(), binary()) -> amqp_property_record()).
00416 -spec(encode_method_fields/1 :: (amqp_method_record()) -> binary()).
00417 -spec(encode_properties/1 :: (amqp_property_record()) -> binary()).
00418 -spec(lookup_amqp_exception/1 :: (amqp_exception()) -> {boolean(), amqp_exception_code(), binary()}).
00419 -spec(amqp_exception/1 :: (amqp_exception_code()) -> amqp_exception()).
00420 -endif. % use_specs
00421 
00422 bitvalue(true) -> 1;
00423 bitvalue(false) -> 0;
00424 bitvalue(undefined) -> 0.
00425 
00426 shortstr_size(S) ->
00427     case size(S) of
00428         Len when Len =< 255 -> Len;
00429         _                   -> exit(method_field_shortstr_overflow)
00430     end.
00431 
00432 -define(SHORTSTR_PROP(P, R, L, S, X),
00433         if P =:= 0 -> {undefined, R};
00434            true    -> <<L:8/unsigned, S:L/binary, X/binary>> = R,
00435                       {S, X}
00436         end).
00437 -define(TABLE_PROP(P, R, L, T, X),
00438         if P =:= 0 -> {undefined, R};
00439            true    -> <<L:32/unsigned, T:L/binary, X/binary>> = R,
00440                       {rabbit_binary_parser:parse_table(T), X}
00441         end).
00442 -define(OCTET_PROP(P, R, I, X),
00443         if P =:= 0 -> {undefined, R};
00444            true    -> <<I:8/unsigned, X/binary>> = R,
00445                       {I, X}
00446         end).
00447 -define(TIMESTAMP_PROP(P, R, I, X),
00448         if P =:= 0 -> {undefined, R};
00449            true    -> <<I:64/unsigned, X/binary>> = R,
00450                       {I, X}
00451         end).
00452 """
00453     version = "{%d, %d, %d}" % (spec.major, spec.minor, spec.revision)
00454     if version == '{8, 0, 0}': version = '{0, 8, 0}'
00455     print "version() -> %s." % (version)
00456 
00457     for m in methods: genLookupMethodName(m)
00458     print "lookup_method_name({_ClassId, _MethodId} = Id) -> exit({unknown_method_id, Id})."
00459 
00460     for c in spec.allClasses(): genLookupClassName(c)
00461     print "lookup_class_name(ClassId) -> exit({unknown_class_id, ClassId})."
00462 
00463     for m in methods: genMethodId(m)
00464     print "method_id(Name) -> exit({unknown_method_name, Name})."
00465 
00466     for m in methods: genMethodHasContent(m)
00467     print "method_has_content(Name) -> exit({unknown_method_name, Name})."
00468 
00469     for m in methods: genMethodIsSynchronous(m)
00470     print "is_method_synchronous(Name) -> exit({unknown_method_name, Name})."
00471 
00472     for m in methods: genMethodRecord(m)
00473     print "method_record(Name) -> exit({unknown_method_name, Name})."
00474 
00475     for m in methods: genMethodFieldNames(m)
00476     print "method_fieldnames(Name) -> exit({unknown_method_name, Name})."
00477 
00478     for m in methods: genDecodeMethodFields(m)
00479     print "decode_method_fields(Name, BinaryFields) ->"
00480     print "  rabbit_misc:frame_error(Name, BinaryFields)."
00481 
00482     for c in spec.allClasses(): genDecodeProperties(c)
00483     print "decode_properties(ClassId, _BinaryFields) -> exit({unknown_class_id, ClassId})."
00484 
00485     for m in methods: genEncodeMethodFields(m)
00486     print "encode_method_fields(Record) -> exit({unknown_method_name, element(1, Record)})."
00487 
00488     for c in spec.allClasses(): genEncodeProperties(c)
00489     print "encode_properties(Record) -> exit({unknown_properties_record, Record})."
00490 
00491     for (c,v,cls) in spec.constants: genLookupException(c,v,cls)
00492     print "lookup_amqp_exception(Code) ->"
00493     print "  rabbit_log:warning(\"Unknown AMQP error code '~p'~n\", [Code]),"
00494     print "  {true, ?INTERNAL_ERROR, <<\"INTERNAL_ERROR\">>}."
00495 
00496     for(c,v,cls) in spec.constants: genAmqpException(c,v,cls)
00497     print "amqp_exception(_Code) -> undefined."
00498 
00499 def genHrl(spec):
00500     def erlType(domain):
00501         return erlangTypeMap[spec.resolveDomain(domain)]
00502 
00503     def fieldNameList(fields):
00504         return ', '.join([erlangize(f.name) for f in fields])
00505 
00506     def fieldNameListDefaults(fields):
00507         def fillField(field):
00508             result = erlangize(f.name)
00509             if field.defaultvalue != None:
00510                 conv_fn = erlangDefaultValueTypeConvMap[type(field.defaultvalue)]
00511                 result += ' = ' + conv_fn(field.defaultvalue)
00512             return result
00513         return ', '.join([fillField(f) for f in fields])
00514 
00515     methods = spec.allMethods()
00516 
00517     printFileHeader()
00518     print "-define(PROTOCOL_PORT, %d)." % (spec.port)
00519 
00520     for (c,v,cls) in spec.constants:
00521         print "-define(%s, %s)." % (erlangConstantName(c), v)
00522 
00523     print "%% Method field records."
00524     for m in methods:
00525         print "-record(%s, {%s})." % (m.erlangName(), fieldNameListDefaults(m.arguments))
00526 
00527     print "%% Class property records."
00528     for c in spec.allClasses():
00529         print "-record('P_%s', {%s})." % (erlangize(c.name), fieldNameList(c.fields))
00530 
00531 
00532 def generateErl(specPath):
00533     genErl(AmqpSpec(specPath))
00534 
00535 def generateHrl(specPath):
00536     genHrl(AmqpSpec(specPath))
00537 
00538 if __name__ == "__main__":
00539     do_main_dict({"header": generateHrl,
00540                   "body": generateErl})
00541