D Tutorial


Introduction

All Apache Thrift tutorials require that you have:

  1. Built and installed the Apache Thrift Compiler and Libraries, see Building from source for more details.
  2. Generated the tutorial.thrift and shared.thrift files as discussed here.

    thrift -r --gen d tutorial.thrift
    
  3. Followed all prerequisites listed below.

Prerequisites

Client

module server;

import std.conv : to;
import std.stdio;
import thrift.codegen.processor;
import thrift.protocol.binary;
import thrift.server.simple;
import thrift.server.transport.socket;
import thrift.transport.buffered;

import share.SharedService;
import share.shared_types;
import tutorial.Calculator;
import tutorial.tutorial_types;

/**
 * The actual implementation of the Calculator interface that is called by
 * the server to answer the requests.
 */
class CalculatorHandler : Calculator {
  void ping() {
    writeln("ping()");
  }

  int add(int n1, int n2) {
    writefln("add(%s,%s)", n1, n2);
    return n1 + n2;
  }

  int calculate(int logid, ref const(Work) work) {
    writefln("calculate(%s, {%s, %s, %s})", logid, work.op, work.num1, work.num2);
    int val;

    switch (work.op) {
    case Operation.ADD:
      val = work.num1 + work.num2;
      break;
    case Operation.SUBTRACT:
      val = work.num1 - work.num2;
      break;
    case Operation.MULTIPLY:
      val = work.num1 * work.num2;
      break;
    case Operation.DIVIDE:
      if (work.num2 == 0) {
        auto io = new InvalidOperation();
        io.whatOp = work.op;
        io.why = "Cannot divide by 0";
        throw io;
      }
      val = work.num1 / work.num2;
      break;
    default:
      auto io = new InvalidOperation();
      io.whatOp = work.op;
      io.why = "Invalid Operation";
      throw io;
    }

    auto ss = SharedStruct();
    ss.key = logid;
    ss.value = to!string(val);
    log[logid] = ss;

    return val;
  }

  SharedStruct getStruct(int logid) {
    writefln("getStruct(%s)", logid);
    return log[logid];
  }

  void zip() {
    writeln("zip()");
  }

protected:
  SharedStruct[int] log;
}

void main() {
  auto protocolFactory = new TBinaryProtocolFactory!();
  auto processor = new TServiceProcessor!Calculator(new CalculatorHandler);
  auto serverTransport = new TServerSocket(9090);
  auto transportFactory = new TBufferedTransportFactory;

  auto server = new TSimpleServer(
    processor, serverTransport, transportFactory, protocolFactory);

  writeln("Starting the server on port 9090...");
  server.serve();
  writeln("done.");
}

Server

module client;

import std.stdio;
import thrift.base;
import thrift.codegen.client;
import thrift.protocol.binary;
import thrift.transport.buffered;
import thrift.transport.socket;

import tutorial.Calculator;
import tutorial.tutorial_types;

void main() {
  auto socket = new TSocket("localhost", 9090);
  auto transport = new TBufferedTransport(socket);
  auto protocol = tBinaryProtocol(transport);
  auto client = tClient!Calculator(protocol);

  transport.open();

  client.ping();
  writeln("ping()");

  int sum = client.add(1, 1);
  writefln("1 + 1 = %s", sum);

  auto work = Work();
  work.op = Operation.DIVIDE;
  work.num1 = 1;
  work.num2 = 0;
  try {
    int quotient = client.calculate(1, work);
    writeln("Whoa we can divide by 0");
  } catch (InvalidOperation io) {
    writeln("Invalid operation: " ~ io.why);
  }

  work.op = Operation.SUBTRACT;
  work.num1 = 15;
  work.num2 = 10;
  int diff = client.calculate(1, work);
  writefln("15 - 10 = %s", diff);

  auto log = client.getStruct(1);
  writefln("Check log: %s", log.value);
}

Additional Information