1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
6 //---------------------------------------------------------------------------
7 // Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 // Technologies LLC., and Rabbit Technologies Ltd.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
14 // http://www.apache.org/licenses/LICENSE-2.0
16 // Unless required by applicable law or agreed to in writing, software
17 // distributed under the License is distributed on an "AS IS" BASIS,
18 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 // See the License for the specific language governing permissions and
20 // limitations under the License.
21 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
26 // The contents of this file are subject to the Mozilla Public License
27 // Version 1.1 (the "License"); you may not use this file except in
28 // compliance with the License. You may obtain a copy of the License at
29 // http://www.rabbitmq.com/mpl.html
31 // Software distributed under the License is distributed on an "AS IS"
32 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 // License for the specific language governing rights and limitations
36 // The Original Code is The RabbitMQ .NET Client.
38 // The Initial Developers of the Original Code are LShift Ltd,
39 // Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
41 // Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 // Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 // are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 // Technologies LLC, and Rabbit Technologies Ltd.
46 // Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 // Ltd. Portions created by Cohesive Financial Technologies LLC are
48 // Copyright (C) 2007-2009 Cohesive Financial Technologies
49 // LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 // (C) 2007-2009 Rabbit Technologies Ltd.
52 // All Rights Reserved.
54 // Contributor(s): ______________________________________.
56 //---------------------------------------------------------------------------
58 using System.Collections;
60 using System.Reflection;
64 using RabbitMQ.Client.Apigen.Attributes;
66 namespace RabbitMQ.Client.Apigen {
68 ///////////////////////////////////////////////////////////////////////////
71 public static void Main(string[] args) {
72 Apigen instance = new Apigen(new ArrayList(args));
76 ///////////////////////////////////////////////////////////////////////////
79 public static XmlNodeList GetNodes(XmlNode n0, string path) {
80 return n0.SelectNodes(path);
83 public static string GetString(XmlNode n0, string path, string d) {
84 XmlNode n = n0.SelectSingleNode(path);
85 return (n == null) ? d : n.InnerText;
88 public static string GetString(XmlNode n0, string path) {
89 string s = GetString(n0, path, null);
91 throw new Exception("Missing spec XML node: " + path);
96 public static int GetInt(XmlNode n0, string path, int d) {
97 string s = GetString(n0, path, null);
98 return (s == null) ? d : int.Parse(s);
101 public static int GetInt(XmlNode n0, string path) {
102 return int.Parse(GetString(n0, path));
105 ///////////////////////////////////////////////////////////////////////////
106 // Name manipulation and mangling for C#
108 public static string MangleConstant(string name) {
109 // Previously, we used C_STYLE_CONSTANT_NAMES:
116 // ... but TheseKindsOfNames are more in line with .NET style guidelines.
117 return MangleClass(name);
120 public static ArrayList IdentifierParts(string name) {
121 ArrayList result = new ArrayList();
122 foreach (String s1 in name.Split(new Char[] { '-' })) {
123 foreach (String s2 in s1.Split(new Char[] { ' ' })) {
130 public static string MangleClass(string name) {
131 StringBuilder sb = new StringBuilder();
132 foreach (String s in IdentifierParts(name)) {
133 sb.Append(Char.ToUpper(s[0]) + s.Substring(1).ToLower());
135 return sb.ToString();
138 public static string MangleMethod(string name) {
139 StringBuilder sb = new StringBuilder();
140 bool useUpper = false;
141 foreach (String s in IdentifierParts(name)) {
143 sb.Append(Char.ToUpper(s[0]) + s.Substring(1).ToLower());
145 sb.Append(s.ToLower());
149 return sb.ToString();
152 public static string MangleMethodClass(AmqpClass c, AmqpMethod m) {
153 return MangleClass(c.Name) + MangleClass(m.Name);
156 ///////////////////////////////////////////////////////////////////////////
158 public string m_framingSubnamespace = null;
159 public string m_inputXmlFilename;
160 public string m_outputFilename;
162 public XmlDocument m_spec = null;
163 public TextWriter m_outputFile = null;
165 public bool m_versionOverridden = false;
166 public int m_majorVersion;
167 public int m_minorVersion;
168 public string m_apiName;
169 public bool m_emitComments = false;
171 public Type m_modelType = typeof(RabbitMQ.Client.Impl.IFullModel);
172 public ArrayList m_modelTypes = new ArrayList();
173 public ArrayList m_constants = new ArrayList();
174 public ArrayList m_classes = new ArrayList();
175 public Hashtable m_domains = new Hashtable();
177 public static Hashtable m_primitiveTypeMap;
178 public static Hashtable m_primitiveTypeFlagMap;
180 m_primitiveTypeMap = new Hashtable();
181 m_primitiveTypeFlagMap = new Hashtable();
182 InitPrimitiveType("octet", "byte", false);
183 InitPrimitiveType("shortstr", "string", true);
184 InitPrimitiveType("longstr", "byte[]", true);
185 InitPrimitiveType("short", "ushort", false);
186 InitPrimitiveType("long", "uint", false);
187 InitPrimitiveType("longlong", "ulong", false);
188 InitPrimitiveType("bit", "bool", false);
189 InitPrimitiveType("table", "System.Collections.IDictionary", true);
190 InitPrimitiveType("timestamp", "AmqpTimestamp", false);
191 InitPrimitiveType("content", "byte[]", true);
194 public static void InitPrimitiveType(string amqpType, string dotnetType, bool isReference)
196 m_primitiveTypeMap[amqpType] = dotnetType;
197 m_primitiveTypeFlagMap[amqpType] = isReference;
200 public void HandleOption(string opt) {
201 if (opt.StartsWith("/n:")) {
202 m_framingSubnamespace = opt.Substring(3);
203 } else if (opt.StartsWith("/apiName:")) {
204 m_apiName = opt.Substring(9);
205 } else if (opt.StartsWith("/v:")) {
206 string[] parts = opt.Substring(3).Split(new char[] { '-' });
207 m_versionOverridden = true;
208 m_majorVersion = int.Parse(parts[0]);
209 m_minorVersion = int.Parse(parts[1]);
210 } else if (opt == "/c") {
211 m_emitComments = true;
213 Console.Error.WriteLine("Unsupported command-line option: " + opt);
218 public void Usage() {
219 Console.Error.WriteLine("Usage: Apigen.exe [options ...] <input-spec-xml> <output-csharp-file>");
220 Console.Error.WriteLine(" Options include:");
221 Console.Error.WriteLine(" /apiName:<identifier>");
222 Console.Error.WriteLine(" /n:<name.space.prefix>");
223 Console.Error.WriteLine(" /v:<majorversion>-<minorversion>");
224 Console.Error.WriteLine(" The apiName option is required.");
228 public Apigen(ArrayList args) {
229 while (args.Count > 0 && ((string) args[0]).StartsWith("/")) {
230 HandleOption((string) args[0]);
234 || (m_apiName == null))
238 m_inputXmlFilename = (string) args[0];
239 m_outputFilename = (string) args[1];
242 ///////////////////////////////////////////////////////////////////////////
244 public string FramingSubnamespace {
246 if (m_framingSubnamespace == null) {
247 return VersionToken();
249 return m_framingSubnamespace;
254 public string ApiNamespaceBase {
256 return "RabbitMQ.Client.Framing."+FramingSubnamespace;
260 public string ImplNamespaceBase {
262 return "RabbitMQ.Client.Framing.Impl."+FramingSubnamespace;
266 public void Generate() {
273 public void LoadSpec() {
274 Console.WriteLine("* Loading spec from '" + m_inputXmlFilename + "'");
275 m_spec = new XmlDocument();
276 m_spec.Load(m_inputXmlFilename);
279 public void ParseSpec() {
280 Console.WriteLine("* Parsing spec");
281 if (!m_versionOverridden) {
282 m_majorVersion = GetInt(m_spec, "/amqp/@major");
283 m_minorVersion = GetInt(m_spec, "/amqp/@minor");
285 foreach (XmlNode n in m_spec.SelectNodes("/amqp/constant")) {
286 m_constants.Add(new DictionaryEntry(GetString(n, "@name"), GetInt(n, "@value")));
288 foreach (XmlNode n in m_spec.SelectNodes("/amqp/class")) {
289 m_classes.Add(new AmqpClass(n));
291 foreach (XmlNode n in m_spec.SelectNodes("/amqp/domain")) {
292 m_domains[GetString(n, "@name")] = GetString(n, "@type");
296 public void ReflectModel() {
297 m_modelTypes.Add(m_modelType);
298 for (int i = 0; i < m_modelTypes.Count; i++)
300 foreach (Type intf in ((Type) m_modelTypes[i]).GetInterfaces())
302 m_modelTypes.Add(intf);
307 public string ResolveDomain(string d) {
308 while (m_domains[d] != null) {
309 string newD = (string) m_domains[d];
317 public string MapDomain(string d) {
318 return (string) m_primitiveTypeMap[ResolveDomain(d)];
321 public string VersionToken() {
322 return "v" + m_majorVersion + "_" + m_minorVersion;
325 public void GenerateOutput() {
326 Console.WriteLine("* Generating code into '" + m_outputFilename + "'");
327 m_outputFile = new StreamWriter(m_outputFilename);
331 m_outputFile.Close();
334 public void Emit(object o) {
335 m_outputFile.Write(o);
338 public void EmitLine(object o) {
339 m_outputFile.WriteLine(o);
342 public void EmitSpecComment(object o) {
347 public void EmitPrelude() {
348 EmitLine("// Autogenerated code. Do not edit.");
350 EmitLine("using RabbitMQ.Client;");
351 EmitLine("using RabbitMQ.Client.Exceptions;");
355 public void EmitPublic() {
356 EmitLine("namespace "+ApiNamespaceBase+" {");
357 EmitLine(" public class Protocol: "+ImplNamespaceBase+".ProtocolBase {");
358 EmitLine(" ///<summary>Protocol major version (= "+m_majorVersion+")</summary>");
359 EmitLine(" public override int MajorVersion { get { return " + m_majorVersion + "; } }");
360 EmitLine(" ///<summary>Protocol minor version (= "+m_minorVersion+")</summary>");
361 EmitLine(" public override int MinorVersion { get { return " + m_minorVersion + "; } }");
362 EmitLine(" ///<summary>Protocol API name (= "+m_apiName+")</summary>");
363 EmitLine(" public override string ApiName { get { return \"" + m_apiName + "\"; } }");
364 int port = GetInt(m_spec, "/amqp/@port");
365 EmitLine(" ///<summary>Default TCP port (= "+port+")</summary>");
366 EmitLine(" public override int DefaultPort { get { return " + port + "; } }");
368 EmitMethodArgumentReader();
370 EmitContentHeaderReader();
372 EmitLine(" public class Constants {");
373 foreach (DictionaryEntry de in m_constants) {
374 EmitLine(" ///<summary>(= "+de.Value+")</summary>");
375 EmitLine(" public const int "+MangleConstant((string) de.Key)+" = "+de.Value+";");
378 foreach (AmqpClass c in m_classes) {
381 foreach (AmqpClass c in m_classes) {
382 if (c.NeedsProperties) {
383 EmitClassProperties(c);
389 public void EmitAutogeneratedSummary(string prefixSpaces, string extra) {
390 EmitLine(prefixSpaces+"/// <summary>Autogenerated type. "+extra+"</summary>");
393 public void EmitClassMethods(AmqpClass c) {
394 foreach (AmqpMethod m in c.m_Methods) {
395 EmitAutogeneratedSummary(" ",
396 "AMQP specification method \""+c.Name+"."+m.Name+"\".");
397 EmitSpecComment(m.DocumentationCommentVariant(" ", "remarks"));
398 EmitLine(" public interface I"+MangleMethodClass(c, m)+": IMethod {");
399 foreach (AmqpField f in m.m_Fields) {
400 EmitSpecComment(f.DocumentationComment(" "));
401 EmitLine(" "+MapDomain(f.Domain)+" "+MangleClass(f.Name)+" { get; }");
407 public bool HasFactoryMethod(AmqpClass c) {
408 foreach (Type t in m_modelTypes) {
409 foreach (MethodInfo method in t.GetMethods()) {
410 AmqpContentHeaderFactoryAttribute f = (AmqpContentHeaderFactoryAttribute)
411 Attribute(method, typeof(AmqpContentHeaderFactoryAttribute));
412 if (f != null && MangleClass(f.m_contentClass) == MangleClass(c.Name)) {
420 public bool IsBoolean(AmqpField f) {
421 return ResolveDomain(f.Domain) == "bit";
424 public bool IsReferenceType(AmqpField f) {
425 return (bool) m_primitiveTypeFlagMap[ResolveDomain(f.Domain)];
428 public bool IsAmqpClass(Type t)
430 foreach (AmqpClass c in m_classes)
432 if (c.Name == t.Name)
438 public void EmitClassProperties(AmqpClass c) {
439 bool hasCommonApi = HasFactoryMethod(c);
440 string propertiesBaseClass =
442 ? "RabbitMQ.Client.Impl."+MangleClass(c.Name)+"Properties"
443 : "RabbitMQ.Client.Impl.ContentHeaderBase";
444 string maybeOverride = hasCommonApi ? "override " : "";
446 EmitAutogeneratedSummary(" ",
447 "AMQP specification content header properties for "+
448 "content class \""+c.Name+"\"");
449 EmitSpecComment(c.DocumentationCommentVariant(" ", "remarks"));
450 EmitLine(" public class "+MangleClass(c.Name)
451 +"Properties: "+propertiesBaseClass+" {");
452 foreach (AmqpField f in c.m_Fields) {
453 EmitLine(" private "+MapDomain(f.Domain)+" m_"+MangleMethod(f.Name)+";");
456 foreach (AmqpField f in c.m_Fields)
459 EmitLine(" private bool m_"+MangleMethod(f.Name)+"_present = false;");
463 foreach (AmqpField f in c.m_Fields)
465 EmitSpecComment(f.DocumentationComment(" ", "@label"));
466 EmitLine(" public "+maybeOverride+MapDomain(f.Domain)+" "+MangleClass(f.Name)+" {");
468 EmitLine(" return m_"+MangleMethod(f.Name)+";");
472 EmitLine(" m_"+MangleMethod(f.Name)+"_present = true;");
474 EmitLine(" m_"+MangleMethod(f.Name)+" = value;");
479 foreach (AmqpField f in c.m_Fields)
482 EmitLine(" public "+maybeOverride+"void Clear"+MangleClass(f.Name)+"() { m_"+MangleMethod(f.Name)+"_present = false; }");
486 EmitLine(" public "+MangleClass(c.Name)+"Properties() {}");
487 EmitLine(" public override int ProtocolClassId { get { return "+c.Index+"; } }");
488 EmitLine(" public override string ProtocolClassName { get { return \""+c.Name+"\"; } }");
490 EmitLine(" public override void ReadPropertiesFrom(RabbitMQ.Client.Impl.ContentHeaderPropertyReader reader) {");
491 foreach (AmqpField f in c.m_Fields)
494 EmitLine(" m_"+MangleMethod(f.Name)+" = reader.ReadBit();");
496 EmitLine(" m_"+MangleMethod(f.Name)+"_present = reader.ReadPresence();");
499 EmitLine(" reader.FinishPresence();");
500 foreach (AmqpField f in c.m_Fields)
503 EmitLine(" if (m_"+MangleMethod(f.Name)+"_present) { m_"+MangleMethod(f.Name)+" = reader.Read"+MangleClass(ResolveDomain(f.Domain))+"(); }");
508 EmitLine(" public override void WritePropertiesTo(RabbitMQ.Client.Impl.ContentHeaderPropertyWriter writer) {");
509 foreach (AmqpField f in c.m_Fields)
512 EmitLine(" writer.WriteBit(m_"+MangleMethod(f.Name)+");");
514 EmitLine(" writer.WritePresence(m_"+MangleMethod(f.Name)+"_present);");
517 EmitLine(" writer.FinishPresence();");
518 foreach (AmqpField f in c.m_Fields)
521 EmitLine(" if (m_"+MangleMethod(f.Name)+"_present) { writer.Write"+MangleClass(ResolveDomain(f.Domain))+"(m_"+MangleMethod(f.Name)+"); }");
526 EmitLine(" public override void AppendPropertyDebugStringTo(System.Text.StringBuilder sb) {");
527 EmitLine(" sb.Append(\"(\");");
529 int remaining = c.m_Fields.Count;
530 foreach (AmqpField f in c.m_Fields)
532 Emit(" sb.Append(\""+f.Name+"=\");");
534 Emit(" sb.Append(m_"+MangleMethod(f.Name)+");");
536 string x = MangleMethod(f.Name);
537 if (IsReferenceType(f)) {
538 Emit(" sb.Append(m_"+x+"_present ? (m_"+x+" == null ? \"(null)\" : m_"+x+".ToString()) : \"_\");");
540 Emit(" sb.Append(m_"+x+"_present ? m_"+x+".ToString() : \"_\");");
545 EmitLine(" sb.Append(\", \");");
551 EmitLine(" sb.Append(\")\");");
556 public void EmitPrivate() {
557 EmitLine("namespace "+ImplNamespaceBase+" {");
558 EmitLine(" using "+ApiNamespaceBase+";");
559 EmitLine(" public enum ClassId {");
560 foreach (AmqpClass c in m_classes) {
561 EmitLine(" "+MangleConstant(c.Name)+" = "+c.Index+",");
563 EmitLine(" Invalid = -1");
565 foreach (AmqpClass c in m_classes) {
566 EmitClassMethodImplementations(c);
569 EmitModelImplementation();
573 public void EmitClassMethodImplementations(AmqpClass c) {
574 foreach (AmqpMethod m in c.m_Methods)
576 EmitAutogeneratedSummary(" ",
577 "Private implementation class - do not use directly.");
578 EmitLine(" public class "+MangleMethodClass(c,m)
579 +": RabbitMQ.Client.Impl.MethodBase, I"+MangleMethodClass(c,m)+" {");
580 EmitLine(" public const int ClassId = "+c.Index+";");
581 EmitLine(" public const int MethodId = "+m.Index+";");
583 foreach (AmqpField f in m.m_Fields)
585 EmitLine(" public "+MapDomain(f.Domain)+" m_"+MangleMethod(f.Name)+";");
588 foreach (AmqpField f in m.m_Fields)
590 EmitLine(" "+MapDomain(f.Domain)+" I"+MangleMethodClass(c,m)+
591 "."+MangleClass(f.Name)+" { get {"
592 + " return m_" + MangleMethod(f.Name) + "; } }");
595 if (m.m_Fields.Count > 0)
597 EmitLine(" public "+MangleMethodClass(c,m)+"() {}");
599 EmitLine(" public "+MangleMethodClass(c,m)+"(");
601 int remaining = m.m_Fields.Count;
602 foreach (AmqpField f in m.m_Fields)
604 Emit(" "+MapDomain(f.Domain)+" init"+MangleClass(f.Name));
613 foreach (AmqpField f in m.m_Fields)
615 EmitLine(" m_" + MangleMethod(f.Name) + " = init" + MangleClass(f.Name) + ";");
619 EmitLine(" public override int ProtocolClassId { get { return "+c.Index+"; } }");
620 EmitLine(" public override int ProtocolMethodId { get { return "+m.Index+"; } }");
621 EmitLine(" public override string ProtocolMethodName { get { return \""+c.Name+"."+m.Name+"\"; } }");
622 EmitLine(" public override bool HasContent { get { return "
623 +(m.HasContent ? "true" : "false")+"; } }");
625 EmitLine(" public override void ReadArgumentsFrom(RabbitMQ.Client.Impl.MethodArgumentReader reader) {");
626 foreach (AmqpField f in m.m_Fields)
628 EmitLine(" m_" + MangleMethod(f.Name) + " = reader.Read" + MangleClass(ResolveDomain(f.Domain)) + "();");
632 EmitLine(" public override void WriteArgumentsTo(RabbitMQ.Client.Impl.MethodArgumentWriter writer) {");
633 foreach (AmqpField f in m.m_Fields)
635 EmitLine(" writer.Write"+MangleClass(ResolveDomain(f.Domain))
636 + "(m_" + MangleMethod(f.Name) + ");");
640 EmitLine(" public override void AppendArgumentDebugStringTo(System.Text.StringBuilder sb) {");
641 EmitLine(" sb.Append(\"(\");");
643 int remaining = m.m_Fields.Count;
644 foreach (AmqpField f in m.m_Fields)
646 Emit(" sb.Append(m_" + MangleMethod(f.Name) + ");");
649 EmitLine(" sb.Append(\",\");");
655 EmitLine(" sb.Append(\")\");");
661 public void EmitMethodArgumentReader() {
662 EmitLine(" public override RabbitMQ.Client.Impl.MethodBase DecodeMethodFrom(RabbitMQ.Util.NetworkBinaryReader reader) {");
663 EmitLine(" ushort classId = reader.ReadUInt16();");
664 EmitLine(" ushort methodId = reader.ReadUInt16();");
666 EmitLine(" switch (classId) {");
667 foreach (AmqpClass c in m_classes) {
668 EmitLine(" case "+c.Index+": {");
669 EmitLine(" switch (methodId) {");
670 foreach (AmqpMethod m in c.m_Methods)
672 EmitLine(" case "+m.Index+": {");
673 EmitLine(" "+ImplNamespaceBase+"."+MangleMethodClass(c,m)+" result = new "+ImplNamespaceBase+"."+MangleMethodClass(c,m)+"();");
674 EmitLine(" result.ReadArgumentsFrom(new RabbitMQ.Client.Impl.MethodArgumentReader(reader));");
675 EmitLine(" return result;");
678 EmitLine(" default: break;");
683 EmitLine(" default: break;");
685 EmitLine(" throw new RabbitMQ.Client.Impl.UnknownClassOrMethodException(classId, methodId);");
689 public void EmitContentHeaderReader() {
690 EmitLine(" public override RabbitMQ.Client.Impl.ContentHeaderBase DecodeContentHeaderFrom(RabbitMQ.Util.NetworkBinaryReader reader) {");
691 EmitLine(" ushort classId = reader.ReadUInt16();");
693 EmitLine(" switch (classId) {");
694 foreach (AmqpClass c in m_classes) {
695 if (c.NeedsProperties) {
696 EmitLine(" case "+c.Index+": return new "
697 +MangleClass(c.Name)+"Properties();");
700 EmitLine(" default: break;");
702 EmitLine(" throw new RabbitMQ.Client.Impl.UnknownClassOrMethodException(classId, 0);");
706 public Attribute Attribute(MemberInfo mi, Type t) {
707 return Attribute(mi.GetCustomAttributes(t, false), t);
710 public Attribute Attribute(ParameterInfo pi, Type t) {
711 return Attribute(pi.GetCustomAttributes(t, false), t);
714 public Attribute Attribute(ICustomAttributeProvider p, Type t) {
715 return Attribute(p.GetCustomAttributes(t, false), t);
718 public Attribute Attribute(IEnumerable attributes, Type t) {
719 if (t.IsSubclassOf(typeof(AmqpApigenAttribute))) {
720 AmqpApigenAttribute result = null;
721 foreach (AmqpApigenAttribute candidate in attributes) {
722 if (candidate.m_namespaceName == null && result == null) {
725 if (candidate.m_namespaceName == ApiNamespaceBase) {
731 foreach (Attribute attribute in attributes) {
738 public void EmitModelImplementation() {
739 EmitLine(" public class Model: RabbitMQ.Client.Impl.ModelBase {");
740 EmitLine(" public Model(RabbitMQ.Client.Impl.ISession session): base(session) {}");
741 ArrayList asynchronousHandlers = new ArrayList();
742 foreach (Type t in m_modelTypes) {
743 foreach (MethodInfo method in t.GetMethods()) {
744 if (method.DeclaringType.Namespace != null &&
745 method.DeclaringType.Namespace.StartsWith("RabbitMQ.Client")) {
746 if (method.Name.StartsWith("Handle") ||
747 (Attribute(method, typeof(AmqpAsynchronousHandlerAttribute)) != null))
749 asynchronousHandlers.Add(method);
751 MaybeEmitModelMethod(method);
756 EmitAsynchronousHandlers(asynchronousHandlers);
760 public void EmitContentHeaderFactory(MethodInfo method) {
761 AmqpContentHeaderFactoryAttribute factoryAnnotation = (AmqpContentHeaderFactoryAttribute)
762 Attribute(method, typeof(AmqpContentHeaderFactoryAttribute));
763 string contentClass = factoryAnnotation.m_contentClass;
764 EmitModelMethodPreamble(method);
766 EmitLine(" return new "+MangleClass(contentClass)+"Properties();");
770 public void MaybeEmitModelMethod(MethodInfo method) {
771 if (method.IsSpecialName) {
772 // It's some kind of event- or property-related method.
773 // It shouldn't be autogenerated.
774 } else if (Attribute(method, typeof(AmqpMethodDoNotImplementAttribute)) != null) {
775 // Skip this method, by request (AmqpMethodDoNotImplement)
776 } else if (Attribute(method, typeof(AmqpContentHeaderFactoryAttribute)) != null) {
777 EmitContentHeaderFactory(method);
778 } else if (Attribute(method, typeof(AmqpUnsupportedAttribute)) != null) {
779 EmitModelMethodPreamble(method);
781 EmitLine(" throw new UnsupportedMethodException(\""+method.Name+"\");");
784 EmitModelMethod(method);
788 public string SanitisedFullName(Type t) {
789 if (t == typeof(void)) {
796 public void EmitModelMethodPreamble(MethodInfo method) {
797 Emit(" public override "+SanitisedFullName(method.ReturnType)+" "+method.Name);
798 ParameterInfo[] parameters = method.GetParameters();
799 int remaining = parameters.Length;
800 if (remaining == 0) {
804 foreach (ParameterInfo pi in parameters) {
805 Emit(" "+SanitisedFullName(pi.ParameterType)+" @"+pi.Name);
816 public void LookupAmqpMethod(MethodInfo method,
818 out AmqpClass amqpClass,
819 out AmqpMethod amqpMethod)
824 // First, try autodetecting the class/method via the
825 // IModel method name.
827 foreach (AmqpClass c in m_classes) {
828 foreach (AmqpMethod m in c.m_Methods)
830 if (methodName.Equals(MangleMethodClass(c,m))) {
833 goto stopSearching; // wheee
839 // If an explicit mapping was provided as an attribute,
840 // then use that instead, whether the autodetect worked or
844 AmqpMethodMappingAttribute methodMapping =
845 Attribute(method, typeof(AmqpMethodMappingAttribute)) as AmqpMethodMappingAttribute;
846 if (methodMapping != null) {
848 foreach (AmqpClass c in m_classes) {
849 if (c.Name == methodMapping.m_className) {
854 amqpMethod = amqpClass.MethodNamed(methodMapping.m_methodName);
858 // At this point, if can't find either the class or the
859 // method, we can't proceed. Complain.
861 if (amqpClass == null || amqpMethod == null) {
862 throw new Exception("Could not find AMQP class or method for IModel method " + method.Name);
866 public void EmitModelMethod(MethodInfo method) {
867 ParameterInfo[] parameters = method.GetParameters();
869 AmqpClass amqpClass = null;
870 AmqpMethod amqpMethod = null;
871 LookupAmqpMethod(method, method.Name, out amqpClass, out amqpMethod);
873 string requestImplClass = MangleMethodClass(amqpClass, amqpMethod);
875 // At this point, we know which request method to
876 // send. Now compute whether it's an RPC or not.
878 AmqpMethod amqpReplyMethod = null;
879 AmqpMethodMappingAttribute replyMapping =
880 Attribute(method.ReturnTypeCustomAttributes, typeof(AmqpMethodMappingAttribute))
881 as AmqpMethodMappingAttribute;
882 if (Attribute(method, typeof(AmqpForceOneWayAttribute)) == null &&
883 (amqpMethod.IsSimpleRpcRequest || replyMapping != null))
885 // We're not forcing oneway, and either are a simple
886 // RPC request, or have an explicit replyMapping
887 amqpReplyMethod = amqpClass.MethodNamed(replyMapping == null
888 ? (string) amqpMethod.m_ResponseMethods[0]
889 : replyMapping.m_methodName);
890 if (amqpReplyMethod == null) {
891 throw new Exception("Could not find AMQP reply method for IModel method " + method.Name);
895 // If amqpReplyMethod is null at this point, it's a
896 // one-way operation, and no continuation needs to be
897 // consed up. Otherwise, we should expect a reply of kind
898 // identified by amqpReplyMethod - unless there's a nowait
899 // parameter thrown into the equation!
901 // Examine the parameters to discover which might be
902 // nowait, content header or content body.
904 ParameterInfo nowaitParameter = null;
905 string nowaitExpression = "null";
906 ParameterInfo contentHeaderParameter = null;
907 ParameterInfo contentBodyParameter = null;
908 foreach (ParameterInfo pi in parameters) {
909 AmqpNowaitArgumentAttribute nwAttr =
910 Attribute(pi, typeof(AmqpNowaitArgumentAttribute)) as AmqpNowaitArgumentAttribute;
911 if (nwAttr != null) {
912 nowaitParameter = pi;
913 if (nwAttr.m_replacementExpression != null) {
914 nowaitExpression = nwAttr.m_replacementExpression;
917 if (Attribute(pi, typeof(AmqpContentHeaderMappingAttribute)) != null) {
918 contentHeaderParameter = pi;
920 if (Attribute(pi, typeof(AmqpContentBodyMappingAttribute)) != null) {
921 contentBodyParameter = pi;
925 // Compute expression text for the content header and body.
927 string contentHeaderExpr =
928 contentHeaderParameter == null
930 : " ("+MangleClass(amqpClass.Name)+"Properties) "+contentHeaderParameter.Name;
931 string contentBodyExpr =
932 contentBodyParameter == null ? "null" : contentBodyParameter.Name;
934 // Emit the method declaration and preamble.
936 EmitModelMethodPreamble(method);
939 // Emit the code to build the request.
941 EmitLine(" "+requestImplClass+" __req = new "+requestImplClass+"();");
942 foreach (ParameterInfo pi in parameters) {
943 if (pi != contentHeaderParameter &&
944 pi != contentBodyParameter)
946 if (Attribute(pi, typeof(AmqpUnsupportedAttribute)) != null) {
947 EmitLine(" if (@"+pi.Name+" != null) {");
948 EmitLine(" throw new UnsupportedMethodFieldException(\""+method.Name+"\",\""+pi.Name+"\");");
951 AmqpFieldMappingAttribute fieldMapping =
952 Attribute(pi, typeof(AmqpFieldMappingAttribute)) as AmqpFieldMappingAttribute;
953 if (fieldMapping != null) {
954 EmitLine(" __req.m_"+fieldMapping.m_fieldName+" = @" + pi.Name + ";");
956 EmitLine(" __req.m_"+pi.Name+" = @" + pi.Name + ";");
962 // If we have a nowait parameter, sometimes that can turn
963 // a ModelRpc call into a ModelSend call.
965 if (nowaitParameter != null) {
966 EmitLine(" if ("+nowaitParameter.Name+") {");
967 EmitLine(" ModelSend(__req,"+contentHeaderExpr+","+contentBodyExpr+");");
968 if (method.ReturnType != typeof(void)) {
969 EmitLine(" return "+nowaitExpression+";");
974 // At this point, perform either a ModelRpc or a
977 if (amqpReplyMethod == null) {
978 EmitLine(" ModelSend(__req,"+contentHeaderExpr+","+contentBodyExpr+");");
980 string replyImplClass = MangleMethodClass(amqpClass, amqpReplyMethod);
982 EmitLine(" RabbitMQ.Client.Impl.MethodBase __repBase = ModelRpc(__req,"+contentHeaderExpr+","+contentBodyExpr+");");
983 EmitLine(" "+replyImplClass+" __rep = __repBase as "+replyImplClass+";");
984 EmitLine(" if (__rep == null) throw new UnexpectedMethodException(__repBase);");
986 if (method.ReturnType == typeof(void)) {
987 // No need to further examine the reply.
989 // At this point, we have the reply method. Extract values from it.
990 AmqpFieldMappingAttribute returnMapping =
991 Attribute(method.ReturnTypeCustomAttributes, typeof(AmqpFieldMappingAttribute))
992 as AmqpFieldMappingAttribute;
993 if (returnMapping == null) {
994 string fieldPrefix = IsAmqpClass(method.ReturnType) ? "m_" : "";
996 // No field mapping --> it's assumed to be a struct to fill in.
997 EmitLine(" "+method.ReturnType+" __result = new "+method.ReturnType+"();");
998 foreach (FieldInfo fi in method.ReturnType.GetFields()) {
999 AmqpFieldMappingAttribute returnFieldMapping =
1000 Attribute(fi, typeof(AmqpFieldMappingAttribute)) as AmqpFieldMappingAttribute;
1001 if (returnFieldMapping != null) {
1002 EmitLine(" __result." + fi.Name + " = __rep." + fieldPrefix + returnFieldMapping.m_fieldName + ";");
1004 EmitLine(" __result." + fi.Name + " = __rep." + fieldPrefix + fi.Name + ";");
1007 EmitLine(" return __result;");
1009 // Field mapping --> return just the field we're interested in.
1010 EmitLine(" return __rep.m_"+returnMapping.m_fieldName+";");
1015 // All the IO and result-extraction has been done. Emit
1016 // the method postamble.
1021 public void EmitAsynchronousHandlers(ArrayList asynchronousHandlers) {
1022 EmitLine(" public override bool DispatchAsynchronous(RabbitMQ.Client.Impl.Command cmd) {");
1023 EmitLine(" RabbitMQ.Client.Impl.MethodBase __method = (RabbitMQ.Client.Impl.MethodBase) cmd.Method;");
1024 EmitLine(" switch ((__method.ProtocolClassId << 16) | __method.ProtocolMethodId) {");
1025 foreach (MethodInfo method in asynchronousHandlers) {
1026 string methodName = method.Name;
1027 if (methodName.StartsWith("Handle")) {
1028 methodName = methodName.Substring(6);
1031 AmqpClass amqpClass = null;
1032 AmqpMethod amqpMethod = null;
1033 LookupAmqpMethod(method, methodName, out amqpClass, out amqpMethod);
1035 string implClass = MangleMethodClass(amqpClass, amqpMethod);
1037 EmitLine(" case "+((amqpClass.Index << 16) | amqpMethod.Index)+": {");
1038 ParameterInfo[] parameters = method.GetParameters();
1039 if (parameters.Length > 0) {
1040 EmitLine(" "+implClass+" __impl = ("+implClass+") __method;");
1041 EmitLine(" "+method.Name+"(");
1042 int remaining = parameters.Length;
1043 foreach (ParameterInfo pi in parameters) {
1044 if (Attribute(pi, typeof(AmqpContentHeaderMappingAttribute)) != null) {
1045 Emit(" ("+pi.ParameterType+") cmd.Header");
1046 } else if (Attribute(pi, typeof(AmqpContentBodyMappingAttribute)) != null) {
1049 AmqpFieldMappingAttribute fieldMapping =
1050 Attribute(pi, typeof(AmqpFieldMappingAttribute)) as AmqpFieldMappingAttribute;
1051 Emit(" __impl.m_"+(fieldMapping == null
1053 : fieldMapping.m_fieldName));
1056 if (remaining > 0) {
1062 EmitLine(" "+method.Name+"();");
1064 EmitLine(" return true;");
1067 EmitLine(" default: return false;");