1 /* 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 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 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 17 * under the License. 18 */ 19 module server; 20 21 import std.conv : to; 22 import std.stdio; 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; 28 29 import share.SharedService; 30 import share.shared_types; 31 import tutorial.Calculator; 32 import tutorial.tutorial_types; 33 34 /** 35 * The actual implementation of the Calculator interface that is called by 36 * the server to answer the requests. 37 */ 38 class CalculatorHandler : Calculator { 39 void ping() { 40 writeln("ping()"); 41 } 42 43 int add(int n1, int n2) { 44 writefln("add(%s,%s)", n1, n2); 45 return n1 + n2; 46 } 47 48 int calculate(int logid, ref const(Work) work) { 49 writefln("calculate(%s, {%s, %s, %s})", logid, work.op, work.num1, work.num2); 50 int val; 51 52 switch (work.op) { 53 case Operation.ADD: 54 val = work.num1 + work.num2; 55 break; 56 case Operation.SUBTRACT: 57 val = work.num1 - work.num2; 58 break; 59 case Operation.MULTIPLY: 60 val = work.num1 * work.num2; 61 break; 62 case Operation.DIVIDE: 63 if (work.num2 == 0) { 64 auto io = new InvalidOperation(); 65 io.whatOp = work.op; 66 io.why = "Cannot divide by 0"; 67 throw io; 68 } 69 val = work.num1 / work.num2; 70 break; 71 default: 72 auto io = new InvalidOperation(); 73 io.whatOp = work.op; 74 io.why = "Invalid Operation"; 75 throw io; 76 } 77 78 auto ss = SharedStruct(); 79 ss.key = logid; 80 ss.value = to!string(val); 81 log[logid] = ss; 82 83 return val; 84 } 85 86 SharedStruct getStruct(int logid) { 87 writefln("getStruct(%s)", logid); 88 return log[logid]; 89 } 90 91 void zip() { 92 writeln("zip()"); 93 } 94 95 protected: 96 SharedStruct[int] log; 97 } 98 99 void main() { 100 auto protocolFactory = new TBinaryProtocolFactory!(); 101 auto processor = new TServiceProcessor!Calculator(new CalculatorHandler); 102 auto serverTransport = new TServerSocket(9090); 103 auto transportFactory = new TBufferedTransportFactory; 104 105 auto server = new TSimpleServer( 106 processor, serverTransport, transportFactory, protocolFactory); 107 108 writeln("Starting the server on port 9090..."); 109 server.serve(); 110 writeln("done."); 111 }