blob: 004415477ca6c79606f1e31d78b006b47d028aea [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
Craig Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#endregion
33
Jan Tattermusch15111f52015-02-05 18:15:14 -080034using System;
35using System.Collections.Generic;
Jan Tattermusch286975f2015-03-12 14:04:36 -070036using System.Collections.Immutable;
37using Grpc.Core.Internal;
Jan Tattermusch15111f52015-02-05 18:15:14 -080038
Jan Tattermusch30868622015-02-19 09:22:33 -080039namespace Grpc.Core
Jan Tattermusch15111f52015-02-05 18:15:14 -080040{
Jan Tattermusch286975f2015-03-12 14:04:36 -070041 /// <summary>
42 /// Mapping of method names to server call handlers.
43 /// </summary>
Jan Tattermusch15111f52015-02-05 18:15:14 -080044 public class ServerServiceDefinition
45 {
46 readonly string serviceName;
Jan Tattermusch286975f2015-03-12 14:04:36 -070047 readonly ImmutableDictionary<string, IServerCallHandler> callHandlers;
Jan Tattermusch15111f52015-02-05 18:15:14 -080048
Jan Tattermusch286975f2015-03-12 14:04:36 -070049 private ServerServiceDefinition(string serviceName, ImmutableDictionary<string, IServerCallHandler> callHandlers)
Jan Tattermusch15111f52015-02-05 18:15:14 -080050 {
51 this.serviceName = serviceName;
Jan Tattermusch286975f2015-03-12 14:04:36 -070052 this.callHandlers = callHandlers;
Jan Tattermusch15111f52015-02-05 18:15:14 -080053 }
54
Jan Tattermusch286975f2015-03-12 14:04:36 -070055 internal ImmutableDictionary<string, IServerCallHandler> CallHandlers
Jan Tattermusch15111f52015-02-05 18:15:14 -080056 {
57 get
58 {
59 return this.callHandlers;
60 }
61 }
62
Jan Tattermusch075dde42015-03-11 18:21:00 -070063 public static Builder CreateBuilder(string serviceName)
Jan Tattermusch15111f52015-02-05 18:15:14 -080064 {
65 return new Builder(serviceName);
66 }
67
68 public class Builder
69 {
70 readonly string serviceName;
Jan Tattermusch075dde42015-03-11 18:21:00 -070071 readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<string, IServerCallHandler>();
Jan Tattermusch15111f52015-02-05 18:15:14 -080072
73 public Builder(string serviceName)
74 {
75 this.serviceName = serviceName;
76 }
77
78 public Builder AddMethod<TRequest, TResponse>(
Craig Tiller190d3602015-02-18 09:23:38 -080079 Method<TRequest, TResponse> method,
Jan Tattermusch15111f52015-02-05 18:15:14 -080080 UnaryRequestServerMethod<TRequest, TResponse> handler)
81 {
82 callHandlers.Add(method.Name, ServerCalls.UnaryRequestCall(method, handler));
83 return this;
84 }
85
86 public Builder AddMethod<TRequest, TResponse>(
Craig Tiller190d3602015-02-18 09:23:38 -080087 Method<TRequest, TResponse> method,
Jan Tattermusch15111f52015-02-05 18:15:14 -080088 StreamingRequestServerMethod<TRequest, TResponse> handler)
89 {
90 callHandlers.Add(method.Name, ServerCalls.StreamingRequestCall(method, handler));
91 return this;
92 }
93
94 public ServerServiceDefinition Build()
95 {
Jan Tattermusch286975f2015-03-12 14:04:36 -070096 return new ServerServiceDefinition(serviceName, callHandlers.ToImmutableDictionary());
Jan Tattermusch15111f52015-02-05 18:15:14 -080097 }
98 }
99 }
100}