lock-free / pcp   0.1.1

MIT License GitHub

A simple Lisp-Style protocol for communication

Scala versions: 2.12

pcp

A simple Lisp-Style protocol for communication

travis-ci build status

Features

  • lisp style composation

  • function sanbox. User can control every function and detail by providing the sandbox

  • based on json grammer

Quick example

package io.github.free.lock.pcp.example

import io.github.free.lock.pcp.{PcpServer, Sandbox, PcpClient}

case class User(name: String, age: Int)

object Main {
  def main(args: Array[String]) {
    val pcpServer = new PcpServer(
      // define the sandbox
      new Sandbox(
        Map[String, BoxFun](
          // define add function
          "add" -> Sandbox.toSanboxFun((params: List[Any], pcs: PcpServer) => {
            val a = params(0).asInstanceOf[Int]
            val b = params(1).asInstanceOf[Int]
            a + b
          })
        )
      )
    )

    // we can just use json array string
    // output: 3
    pcpServer.execute("""["add", 1, 2]""")

    // we can also use PcpClient, instead of raw string
    val p = new PcpClient()
    // """["add", 1, ["add", 2, 3]]"""
    val command = p.toJson(
      p.call("add", 1, p.call("add", 2, 3))
    )
    // output: 6
    pcpServer.execute(command)
  }
}