aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/GrpcCore/ServerServiceDefinition.cs
blob: 7f1cc6284e4ea68641e8a22b306846fa29a56065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;

namespace Google.GRPC.Core
{
    public class ServerServiceDefinition
    {
        readonly string serviceName;
        // TODO: we would need an immutable dictionary here...
        readonly Dictionary<string, IServerCallHandler> callHandlers;

        private ServerServiceDefinition(string serviceName, Dictionary<string, IServerCallHandler> callHandlers)
        {
            this.serviceName = serviceName;
            this.callHandlers = new Dictionary<string, IServerCallHandler>(callHandlers);
        }

        internal Dictionary<string, IServerCallHandler> CallHandlers
        {
            get
            {
                return this.callHandlers;
            }
        }


        public static Builder CreateBuilder(String serviceName)
        {
            return new Builder(serviceName);
        }

        public class Builder
        {
            readonly string serviceName;
            readonly Dictionary<string, IServerCallHandler> callHandlers = new Dictionary<String, IServerCallHandler>();

            public Builder(string serviceName)
            {
                this.serviceName = serviceName;
            }

            public Builder AddMethod<TRequest, TResponse>(
                Method<TRequest, TResponse> method, 
                UnaryRequestServerMethod<TRequest, TResponse> handler)
            {
                callHandlers.Add(method.Name, ServerCalls.UnaryRequestCall(method, handler));
                return this;
            }

            public Builder AddMethod<TRequest, TResponse>(
                Method<TRequest, TResponse> method, 
                StreamingRequestServerMethod<TRequest, TResponse> handler)
            {
                callHandlers.Add(method.Name, ServerCalls.StreamingRequestCall(method, handler));
                return this;
            }

            public ServerServiceDefinition Build()
            {
                return new ServerServiceDefinition(serviceName, callHandlers);
            }
        }
    }
}