Giter Site home page Giter Site logo

jsonrpc's Introduction

What

Pure Fantom implementation of client and server conforming JSON-RPC 2.0 protocol. Two reference implementations are planned to make sure the core is flexible enough:

  • HTTP-based. Server is a WebMod, client sends POST requests using WebClient.
  • TCP-based. Client connects to socket and sends requests. Server processes them asynchronously and returns results once it is done.

Why?

Description

Here are the core classes used on server side:

  • Server – transport-agnostic call translator which returns Str request from Str response. All calls are delegated to a Handler. Library users are very unlikely require to change it.
  • Handler – abstract method executor
  • ReflectHandler – handler implementation which uses reflection to call methods of current object. Also gives an access to fields as methods without params.

There are two ready-to-use components which enable remote access to server:

  • RpcMod – accepts JSON requests from HTTP POSTs and writes back JSON responses
  • RpcTcpService – binds to TCP socket and asynchronously processes incoming messages by invoking InStream#.readUtf

Client side has two implementations of clients – sync and async. Sync client blocks until it gets a response, and async client receives callback along with request which will be invoked once response is received. To add custom implementation, it is required to implement either SyncTransport or AsyncTransport and then just create corresponding client by invoking Client.sync(transport) or Client.async(transport). Two ready-to-use client implementations available:

  • RpcWebClient – sync transport which sends POST requests with JSON to given URI
  • RpcTcpClient – async transport which writes JSON requests as UTF strings

Examples

Web

Simple server which exposes a sum method:

using util
using jsonrpc

class Server : AbstractMain
{
  @Opt Int port := 8080

  override Int run()
  {
    runServices([
        WispService
        {
          it.port = this.port
          it.root = RpcMod(MathHandler())
        }
       ])
  }
}

const class MathHandler : ReflectHandler
{
  Int sum(Int[] args) { args.reduce(0) |Int r, Int v->Int| { r + v } }
}

Corresponding client:

echo(RpcWebClient(`http://localhost:8080`).request("sum", [[1,2,3]]) 
//output: 6

Inet

Server:

using util
using jsonrpc

class Server : AbstractMain
{
  @Opt Int port := 4750

  override Int run() { runServices([RpcService(MathHandler(), port)]) }
}

const class MathHandler : ReflectHandler
{
  Int sum(Int[] args) { args.reduce(0) |Int r, Int v->Int| { r + v } }
}

Client:

//Callback accepts a Func as a parameter, so that invocation of this func 
//will throw RpcErr in case of error on server side
RpcTcpClient(IpAddr.local, 4750).request("sum", [[1,2,3]]) |res| { echo(res.call) }

jsonrpc's People

Stargazers

Kevin.Y avatar Miguel Carmona avatar Alan Kash avatar  avatar Jan Trtík avatar Kamil Toman avatar Adam Shannon avatar

Watchers

Andrey Platov avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.