Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/ServiceGenerator.cs @ 11623

Last change on this file since 11623 was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 9.7 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using Google.ProtocolBuffers.Descriptors;
38
39namespace Google.ProtocolBuffers.ProtoGen
40{
41    internal class GenericServiceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator
42    {
43        private enum RequestOrResponse
44        {
45            Request,
46            Response
47        }
48
49        internal GenericServiceGenerator(ServiceDescriptor descriptor)
50            : base(descriptor)
51        {
52        }
53
54        public void Generate(TextGenerator writer)
55        {
56            writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
57            writer.WriteLine("[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]");
58            writer.WriteLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"{0}\", \"{1}\")]",
59                             GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version);
60            writer.WriteLine("{0} abstract class {1} : pb::IService {{", ClassAccessLevel, Descriptor.Name);
61            writer.Indent();
62
63            foreach (MethodDescriptor method in Descriptor.Methods)
64            {
65                writer.WriteLine("{0} abstract void {1}(", ClassAccessLevel,
66                                 NameHelpers.UnderscoresToPascalCase(method.Name));
67                writer.WriteLine("    pb::IRpcController controller,");
68                writer.WriteLine("    {0} request,", GetClassName(method.InputType));
69                writer.WriteLine("    global::System.Action<{0}> done);", GetClassName(method.OutputType));
70            }
71
72            // Generate Descriptor and DescriptorForType.
73            writer.WriteLine();
74            writer.WriteLine("{0} static pbd::ServiceDescriptor Descriptor {{", ClassAccessLevel);
75            writer.WriteLine("  get {{ return {0}.Descriptor.Services[{1}]; }}",
76                             DescriptorUtil.GetQualifiedUmbrellaClassName(Descriptor.File.CSharpOptions),
77                             Descriptor.Index);
78            writer.WriteLine("}");
79            writer.WriteLine("public pbd::ServiceDescriptor DescriptorForType {");
80            writer.WriteLine("  get { return Descriptor; }");
81            writer.WriteLine("}");
82
83            GenerateCallMethod(writer);
84            GenerateGetPrototype(RequestOrResponse.Request, writer);
85            GenerateGetPrototype(RequestOrResponse.Response, writer);
86            GenerateStub(writer);
87
88            writer.Outdent();
89            writer.WriteLine("}");
90        }
91
92        private void GenerateCallMethod(TextGenerator writer)
93        {
94            writer.WriteLine();
95            writer.WriteLine("public void CallMethod(");
96            writer.WriteLine("    pbd::MethodDescriptor method,");
97            writer.WriteLine("    pb::IRpcController controller,");
98            writer.WriteLine("    pb::IMessage request,");
99            writer.WriteLine("    global::System.Action<pb::IMessage> done) {");
100            writer.Indent();
101            writer.WriteLine("if (method.Service != Descriptor) {");
102            writer.WriteLine("  throw new global::System.ArgumentException(");
103            writer.WriteLine("      \"Service.CallMethod() given method descriptor for wrong service type.\");");
104            writer.WriteLine("}");
105            writer.WriteLine("switch(method.Index) {");
106            writer.Indent();
107            foreach (MethodDescriptor method in Descriptor.Methods)
108            {
109                writer.WriteLine("case {0}:", method.Index);
110                writer.WriteLine("  this.{0}(controller, ({1}) request,",
111                                 NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType));
112                writer.WriteLine("      pb::RpcUtil.SpecializeCallback<{0}>(", GetClassName(method.OutputType));
113                writer.WriteLine("      done));");
114                writer.WriteLine("  return;");
115            }
116            writer.WriteLine("default:");
117            writer.WriteLine("  throw new global::System.InvalidOperationException(\"Can't get here.\");");
118            writer.Outdent();
119            writer.WriteLine("}");
120            writer.Outdent();
121            writer.WriteLine("}");
122            writer.WriteLine();
123        }
124
125        private void GenerateGetPrototype(RequestOrResponse which, TextGenerator writer)
126        {
127            writer.WriteLine("public pb::IMessage Get{0}Prototype(pbd::MethodDescriptor method) {{", which);
128            writer.Indent();
129            writer.WriteLine("if (method.Service != Descriptor) {");
130            writer.WriteLine("  throw new global::System.ArgumentException(");
131            writer.WriteLine("      \"Service.Get{0}Prototype() given method descriptor for wrong service type.\");",
132                             which);
133            writer.WriteLine("}");
134            writer.WriteLine("switch(method.Index) {");
135            writer.Indent();
136
137            foreach (MethodDescriptor method in Descriptor.Methods)
138            {
139                writer.WriteLine("case {0}:", method.Index);
140                writer.WriteLine("  return {0}.DefaultInstance;",
141                                 GetClassName(which == RequestOrResponse.Request ? method.InputType : method.OutputType));
142            }
143            writer.WriteLine("default:");
144            writer.WriteLine("  throw new global::System.InvalidOperationException(\"Can't get here.\");");
145            writer.Outdent();
146            writer.WriteLine("}");
147            writer.Outdent();
148            writer.WriteLine("}");
149            writer.WriteLine();
150        }
151
152        private void GenerateStub(TextGenerator writer)
153        {
154            writer.WriteLine("public static Stub CreateStub(pb::IRpcChannel channel) {");
155            writer.WriteLine("  return new Stub(channel);");
156            writer.WriteLine("}");
157            writer.WriteLine();
158            writer.WriteLine("[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
159            writer.WriteLine("[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]");
160            writer.WriteLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"{0}\", \"{1}\")]",
161                             GetType().Assembly.GetName().Name, GetType().Assembly.GetName().Version);
162            writer.WriteLine("{0} class Stub : {1} {{", ClassAccessLevel, GetClassName(Descriptor));
163            writer.Indent();
164            writer.WriteLine("internal Stub(pb::IRpcChannel channel) {");
165            writer.WriteLine("  this.channel = channel;");
166            writer.WriteLine("}");
167            writer.WriteLine();
168            writer.WriteLine("private readonly pb::IRpcChannel channel;");
169            writer.WriteLine();
170            writer.WriteLine("public pb::IRpcChannel Channel {");
171            writer.WriteLine("  get { return channel; }");
172            writer.WriteLine("}");
173
174            foreach (MethodDescriptor method in Descriptor.Methods)
175            {
176                writer.WriteLine();
177                writer.WriteLine("{0} override void {1}(", ClassAccessLevel,
178                                 NameHelpers.UnderscoresToPascalCase(method.Name));
179                writer.WriteLine("    pb::IRpcController controller,");
180                writer.WriteLine("    {0} request,", GetClassName(method.InputType));
181                writer.WriteLine("    global::System.Action<{0}> done) {{", GetClassName(method.OutputType));
182                writer.Indent();
183                writer.WriteLine("channel.CallMethod(Descriptor.Methods[{0}],", method.Index);
184                writer.WriteLine("    controller, request, {0}.DefaultInstance,", GetClassName(method.OutputType));
185                writer.WriteLine("    pb::RpcUtil.GeneralizeCallback<{0}, {0}.Builder>(done, {0}.DefaultInstance));",
186                                 GetClassName(method.OutputType));
187                writer.Outdent();
188                writer.WriteLine("}");
189            }
190            writer.Outdent();
191            writer.WriteLine("}");
192        }
193    }
194}
Note: See TracBrowser for help on using the repository browser.