# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
from concurrent import futures

import google
import grpc

import test_pb2
import test_pb2_grpc

class TestServicer(test_pb2_grpc.TestServicer):
    def __init__(self):
        self.counter = 0
    def Add(self, req, resp):
        return test_pb2.AddReply(c = req.a + req.b)
    def Dup(self, req, resp):
        return test_pb2.DupReply(s2 = req.s + req.s)
    def Process(self, req, resp):
        return test_pb2.Data(iv = req.iv + 1, sv = req.sv + 'X')
    def GetCounter(self, req, resp):
        self.counter = self.counter + 1
        return test_pb2.GetCounterReply(counter = self.counter)
    def Noop(self, req, resp):
        return google.protobuf.empty_pb2.Empty()
    def StreamProcess(self, reqit, ctx):
        for req in reqit:
            yield test_pb2.Data(iv = req.iv + 1, sv = req.sv + 'X')
    def StreamNoop(self, reqit, ctx):
        for req in reqit:
            yield google.protobuf.empty_pb2.Empty()

srv = grpc.server(futures.ThreadPoolExecutor(max_workers = 10))
test_pb2_grpc.add_TestServicer_to_server(TestServicer(), srv)
srv.add_insecure_port('localhost:12348')
srv.start()
input('Press enter to stop...')
