Learn R Programming

thriftr (version 1.1.7)

make_server: Create server side thrift API

Description

Create server side thrift API

Usage

make_server(service, handler, host = "localhost", port = 9090,
  proto_factory = TBinaryProtocolFactory$new(),
  trans_factory = TBufferedTransportFactory$new())

Arguments

service

parsed service

handler

R6 class implementing service

host

server host

port

port server tcp port

proto_factory

factory that generates protocol implementation

trans_factory

factory that generates transport implementation

Examples

Run this code
# NOT RUN {
# File calc.thrift content:
# service Calculator {
#   i32 add(1:i32 a, 2:i32 b);
#   i32 sub(1:i32 a, 2:i32 b);
#   i32 mult(1:i32 a, 2:i32 b);
#   i32 div(1:i32 a, 2:i32 b);
# }
#

calc_thrift <- thriftr::t_load("calc.thrift", module_name="calc_thrift")

Dispatcher <- R6::R6Class("Dispatcher",
  public = list(
    add = function(a, b) {
      print(sprintf("add -> %s + %s", a, b))
      return(a + b)
    },
    sub = function(a, b) {
      print(sprintf("sub -> %s - %s", a, b))
      return(a - b)
    },
    mult = function(a, b) {
      print(sprintf("mult -> %s * %s", a, b))
      return(a * b)
    },
    div = function(a, b) {
      print(sprintf("div -> %s / %s", a, b))
      return(a / b)
    }
  )
)

server <- thriftr::make_server(
    calc_thrift$Calculator,
    Dispatcher$new(),
    "127.0.0.1",
    6000)

print("serving...")

server$serve()
# }

Run the code above in your browser using DataLab