2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
23 import thrift.codegen.processor;
24 import thrift.protocol.binary;
25 import thrift.server.simple;
26 import thrift.server.transport.socket;
27 import thrift.transport.buffered;
29 import share.SharedService;
30 import share.shared_types;
31 import tutorial.Calculator;
32 import tutorial.tutorial_types;
35 * The actual implementation of the Calculator interface that is called by
36 * the server to answer the requests.
38 class CalculatorHandler : Calculator {
43 int add(int n1, int n2) {
44 writefln("add(%s,%s)", n1, n2);
48 int calculate(int logid, ref const(Work) work) {
49 writefln("calculate(%s, {%s, %s, %s})", logid, work.op, work.num1, work.num2);
54 val = work.num1 + work.num2;
56 case Operation.SUBTRACT:
57 val = work.num1 - work.num2;
59 case Operation.MULTIPLY:
60 val = work.num1 * work.num2;
62 case Operation.DIVIDE:
64 auto io = new InvalidOperation();
66 io.why = "Cannot divide by 0";
69 val = work.num1 / work.num2;
72 auto io = new InvalidOperation();
74 io.why = "Invalid Operation";
78 auto ss = SharedStruct();
80 ss.value = to!string(val);
86 SharedStruct getStruct(int logid) {
87 writefln("getStruct(%s)", logid);
96 SharedStruct[int] log;
100 auto protocolFactory = new TBinaryProtocolFactory!();
101 auto processor = new TServiceProcessor!Calculator(new CalculatorHandler);
102 auto serverTransport = new TServerSocket(9090);
103 auto transportFactory = new TBufferedTransportFactory;
105 auto server = new TSimpleServer(
106 processor, serverTransport, transportFactory, protocolFactory);
108 writeln("Starting the server on port 9090...");