blob: 019df4c81737f28d2eec648d1593cae1cbfa3a3e [file] [log] [blame]
csharptest68d831e2011-05-03 13:47:34 -05001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14// * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18// * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections.Generic;
37using Google.ProtocolBuffers.DescriptorProtos;
38using Google.ProtocolBuffers.Descriptors;
39
40namespace Google.ProtocolBuffers.ProtoGen {
41 internal class ServiceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator {
42
43 readonly CSharpServiceType svcType;
44 ISourceGenerator _generator;
45
46 internal ServiceGenerator(ServiceDescriptor descriptor)
47 : base(descriptor) {
48 svcType = descriptor.File.CSharpOptions.ServiceGeneratorType;
49 switch (svcType) {
csharptestf1816be2011-05-19 12:01:16 -050050 case CSharpServiceType.NONE:
51 _generator = new NoServicesGenerator(descriptor);
52 break;
csharptest68d831e2011-05-03 13:47:34 -050053 case CSharpServiceType.GENERIC:
54 _generator = new GenericServiceGenerator(descriptor);
55 break;
56 case CSharpServiceType.INTERFACE:
57 _generator = new ServiceInterfaceGenerator(descriptor);
58 break;
59 case CSharpServiceType.IRPCDISPATCH:
60 _generator = new RpcServiceGenerator(descriptor);
61 break;
62 default: throw new ApplicationException("Unknown ServiceGeneratorType = " + svcType.ToString());
63 }
64 }
65
66 public void Generate(TextGenerator writer) {
67 _generator.Generate(writer);
68 }
csharptestf1816be2011-05-19 12:01:16 -050069
70 class NoServicesGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator {
71
72 public NoServicesGenerator(ServiceDescriptor descriptor)
73 : base(descriptor) {
74 }
75 public virtual void Generate(TextGenerator writer) {
76 writer.WriteLine("/*");
77 writer.WriteLine("* Service generation is now disabled by default, use the following option to enable:");
78 writer.WriteLine("* option (google.protobuf.csharp_file_options).service_generator_type = GENERIC;");
79 writer.WriteLine("*/");
80 }
81 }
csharptest68d831e2011-05-03 13:47:34 -050082
83 class ServiceInterfaceGenerator : SourceGeneratorBase<ServiceDescriptor>, ISourceGenerator {
84
85 public ServiceInterfaceGenerator(ServiceDescriptor descriptor)
86 : base(descriptor) {
87 }
88
89 public virtual void Generate(TextGenerator writer) {
90
91 CSharpServiceOptions options = Descriptor.Options.GetExtension(CSharpOptions.CsharpServiceOptions);
92 if (options != null && options.HasInterfaceId) {
93 writer.WriteLine("[global::System.Runtime.InteropServices.GuidAttribute(\"{0}\")]", new Guid(options.InterfaceId));
94 }
95 writer.WriteLine("{0} partial interface I{1} {{", ClassAccessLevel, Descriptor.Name);
96 writer.Indent();
97
98 foreach (MethodDescriptor method in Descriptor.Methods)
99 {
100 CSharpMethodOptions mth = method.Options.GetExtension(CSharpOptions.CsharpMethodOptions);
101 if(mth.HasDispatchId) {
102 writer.WriteLine("[global::System.Runtime.InteropServices.DispId({0})]", mth.DispatchId);
103 }
104 writer.WriteLine("{0} {1}({2} {3});", GetClassName(method.OutputType), NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType), NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
105 }
106
107 writer.Outdent();
108 writer.WriteLine("}");
109 }
110 }
111
112 class RpcServiceGenerator : ServiceInterfaceGenerator {
113
114 public RpcServiceGenerator(ServiceDescriptor descriptor)
115 : base(descriptor) {
116 }
117
118 public override void Generate(TextGenerator writer)
119 {
120 base.Generate(writer);
121
122 writer.WriteLine();
123
124 // CLIENT Proxy
125 {
126 if (Descriptor.File.CSharpOptions.ClsCompliance)
127 writer.WriteLine("[global::System.CLSCompliant(false)]");
128 writer.WriteLine("{0} partial class {1} : I{1}, pb::IRpcDispatch, global::System.IDisposable {{", ClassAccessLevel, Descriptor.Name);
129 writer.Indent();
130 writer.WriteLine("private readonly bool dispose;");
131 writer.WriteLine("private readonly pb::IRpcDispatch dispatch;");
132
133 writer.WriteLine("public {0}(pb::IRpcDispatch dispatch) : this(dispatch, true) {{", Descriptor.Name);
134 writer.WriteLine("}");
135 writer.WriteLine("public {0}(pb::IRpcDispatch dispatch, bool dispose) {{", Descriptor.Name);
136 writer.WriteLine(" if (null == (this.dispatch = dispatch)) throw new global::System.ArgumentNullException();");
137 writer.WriteLine(" this.dispose = dispose && dispatch is global::System.IDisposable;");
138 writer.WriteLine("}");
139 writer.WriteLine();
140
141 writer.WriteLine("public void Dispose() {");
142 writer.WriteLine(" if (dispose) ((global::System.IDisposable)dispatch).Dispose();");
143 writer.WriteLine("}");
144 writer.WriteLine();
145 writer.WriteLine("TMessage pb::IRpcDispatch.CallMethod<TMessage, TBuilder>(string method, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response) {");
146 writer.WriteLine(" return dispatch.CallMethod(method, request, response);");
147 writer.WriteLine("}");
148 writer.WriteLine();
149
150 foreach (MethodDescriptor method in Descriptor.Methods) {
151 writer.WriteLine("public {0} {1}({2} {3}) {{", GetClassName(method.OutputType), NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType), NameHelpers.UnderscoresToCamelCase(method.InputType.Name));
152 writer.WriteLine(" return dispatch.CallMethod(\"{0}\", {1}, {2}.CreateBuilder());",
153 method.Name,
154 NameHelpers.UnderscoresToCamelCase(method.InputType.Name),
155 GetClassName(method.OutputType)
156 );
157 writer.WriteLine("}");
158 writer.WriteLine();
159 }
160 }
161 // SERVER - DISPATCH
162 {
163 if (Descriptor.File.CSharpOptions.ClsCompliance)
164 writer.WriteLine("[global::System.CLSCompliant(false)]");
165 writer.WriteLine("public partial class Dispatch : pb::IRpcDispatch, global::System.IDisposable {");
166 writer.Indent();
167 writer.WriteLine("private readonly bool dispose;");
168 writer.WriteLine("private readonly I{0} implementation;", Descriptor.Name);
169
170 writer.WriteLine("public Dispatch(I{0} implementation) : this(implementation, true) {{", Descriptor.Name);
171 writer.WriteLine("}");
172 writer.WriteLine("public Dispatch(I{0} implementation, bool dispose) {{", Descriptor.Name);
173 writer.WriteLine(" if (null == (this.implementation = implementation)) throw new global::System.ArgumentNullException();");
174 writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
175 writer.WriteLine("}");
176 writer.WriteLine();
177
178 writer.WriteLine("public void Dispose() {");
179 writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
180 writer.WriteLine("}");
181 writer.WriteLine();
182
183 writer.WriteLine("public TMessage CallMethod<TMessage, TBuilder>(string methodName, pb::IMessageLite request, pb::IBuilderLite<TMessage, TBuilder> response)");
184 writer.WriteLine(" where TMessage : IMessageLite<TMessage, TBuilder>");
185 writer.WriteLine(" where TBuilder : IBuilderLite<TMessage, TBuilder> {");
186 writer.Indent();
187 writer.WriteLine("switch(methodName) {");
188 writer.Indent();
189
190 foreach (MethodDescriptor method in Descriptor.Methods) {
191 writer.WriteLine("case \"{0}\": return response.MergeFrom(implementation.{1}(({2})request)).Build();", method.Name, NameHelpers.UnderscoresToPascalCase(method.Name), GetClassName(method.InputType));
192 }
193 writer.WriteLine("default: throw new global::System.MissingMethodException(typeof(ISearchService).FullName, methodName);");
194 writer.Outdent();
195 writer.WriteLine("}");//end switch
196 writer.Outdent();
197 writer.WriteLine("}");//end invoke
198 writer.Outdent();
199 writer.WriteLine("}");//end server
200 }
201 // SERVER - STUB
202 {
203 if (Descriptor.File.CSharpOptions.ClsCompliance)
204 writer.WriteLine("[global::System.CLSCompliant(false)]");
205 writer.WriteLine("public partial class ServerStub : pb::IRpcServerStub, global::System.IDisposable {");
206 writer.Indent();
207 writer.WriteLine("private readonly bool dispose;");
208 writer.WriteLine("private readonly pb::IRpcDispatch implementation;", Descriptor.Name);
209
210 writer.WriteLine("public ServerStub(I{0} implementation) : this(implementation, true) {{", Descriptor.Name);
211 writer.WriteLine("}");
212 writer.WriteLine("public ServerStub(I{0} implementation, bool dispose) : this(new Dispatch(implementation, dispose), dispose) {{", Descriptor.Name);
213 writer.WriteLine("}");
214
215 writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation) : this(implementation, true) {");
216 writer.WriteLine("}");
217 writer.WriteLine("public ServerStub(pb::IRpcDispatch implementation, bool dispose) {");
218 writer.WriteLine(" if (null == (this.implementation = implementation)) throw new global::System.ArgumentNullException();");
219 writer.WriteLine(" this.dispose = dispose && implementation is global::System.IDisposable;");
220 writer.WriteLine("}");
221 writer.WriteLine();
222
223 writer.WriteLine("public void Dispose() {");
224 writer.WriteLine(" if (dispose) ((global::System.IDisposable)implementation).Dispose();");
225 writer.WriteLine("}");
226 writer.WriteLine();
227
228 writer.WriteLine("public pb::IMessageLite CallMethod(string methodName, pb::CodedInputStream input, pb::ExtensionRegistry registry) {{", Descriptor.Name);
229 writer.Indent();
230 writer.WriteLine("switch(methodName) {");
231 writer.Indent();
232
233 foreach (MethodDescriptor method in Descriptor.Methods)
234 {
235 writer.WriteLine("case \"{0}\": return implementation.CallMethod(methodName, {1}.ParseFrom(input, registry), {2}.CreateBuilder());", method.Name, GetClassName(method.InputType), GetClassName(method.OutputType));
236 }
237 writer.WriteLine("default: throw new global::System.MissingMethodException(typeof(ISearchService).FullName, methodName);");
238 writer.Outdent();
239 writer.WriteLine("}");//end switch
240 writer.Outdent();
241 writer.WriteLine("}");//end invoke
242 writer.Outdent();
243 writer.WriteLine("}");//end server
244 }
245
246 writer.Outdent();
247 writer.WriteLine("}");
248 }
249 }
250 }
251}