Module jc_protocol

.

Copyright © (C) 2015, Jim Rosenblum

Behaviours: gen_server, ranch_protocol.

Authors: Jim Rosenblum (jrosenblum).

Description

JC Protocol 1.0 A binary-encoded, string protocol used to provide socket-based interoperability with JC. Incoming messages are string representations of a tuple. Responses are JSON.

The protocol defines three message types: CONNECT, CLOSE and COMMAND all of which are binary strings consisting of a header, indicating the size of the message in bytes, follwed by the actual message itself. The header is an 8-byte, big endian, unsigned integer indicating the size of the message in bytes.

A RESPONSE is structured the same as messages - 8-byte size header followed by the content of the response.

The CONNECT command initiates a session,

      M = <<"{connect,{version,\"1.0\"}}">>

The byte size is 26, so the CONNECT message is:

      <<26:8/integer-unit:8, M/binary>> =
      <<0,0,0,0,0,0,0,26,123,99,111,110,110,101,99,116,44,123,118,101,114,
        115,105,111,110,44,32,34,49,46,48,34,125,125>>

The server will respond to a CONNECT command with either an error or the appropriately encoded version of {\"version\":\"1.0\"}

         <<17:8/integer-unit:8, <<"{\"version\":1.0}">> =
         <0,0,0,0,0,0,0,17,123,34,118,101,114,115,105,111,110,34,
          58,34,49,46,48,34,125>>

The CLOSE command closes the socket, ending the session

      M = <<"{close}">>
Size is 7 so the CLOSE message is:
       <0,0,0,0,0,0,0,7,123,99,108,111,115,101,125>>

COMMAND messages are string versions of the tuple-messages which jc_bridge uses, only without the self() parameter. For example the jc_brdige message, {self(), {put, Map, Key, Value}} becomes {put, Map, Key, Value}

The RESPONSE will be an appropriately encoded, binary version of a JSON response representing the Erlang return value.

A client session might look as follows:

          client:send("{put, evs, 1, \"a string value\"}")
       ==> <<"{\"ok\":1}">>
  
       client:send("{get, evs, 1}"),
       ==> <<"{\"ok\": \"a string value\"}">>

Function Index

init/1
init/4
start_link/4

Function Details

init/1

init(X1::[]) -> {ok, undefined}

init/4

init(Ref::ranch:ref(), S::any(), T::jc_protocol | undefined, Port::[Port::integer()]) -> any()

start_link/4

start_link(Ref::ranch:ref(), Socket::any(), Trans::jc_protocol, Opts::any()) -> {ok, pid()}


Generated by EDoc, May 24 2016, 22:38:41.