Class: Farcall::JsonTransport

Inherits:
Transport show all
Includes:
TransportBase
Defined in:
lib/farcall/json_transport.rb

Overview

The transport that uses delimited texts formatted with JSON. Delimiter should be a character sequence that will never appear in data, by default “x00” is used. Also several nnn can be used, most JSON codecs never insert several empty strings

Instance Attribute Summary

Attributes inherited from Transport

#on_abort, #on_close, #on_data_received

Instance Method Summary collapse

Methods included from TransportBase

#close_connection, #setup_streams

Methods inherited from Transport

#closed?, create, #push_input, #receive_data

Constructor Details

#initialize(delimiter: "\x00", **params) ⇒ JsonTransport

Create json transport, see Farcall::Transpor#create for parameters



94
95
96
97
98
99
100
101
102
# File 'lib/farcall/json_transport.rb', line 94

def initialize delimiter: "\x00", **params
  super()
  setup_streams **params
  @delimiter = delimiter
  @dlength   = -delimiter.length
  @thread = Thread.start {
    load_loop
  }
end

Instance Method Details

#closeObject



108
109
110
111
112
113
114
115
# File 'lib/farcall/json_transport.rb', line 108

def close
  if !@closing
    @closing  = true
    close_connection
    @thread and @thread.join
    @thread = nil
  end
end

#send_data(hash) ⇒ Object



104
105
106
# File 'lib/farcall/json_transport.rb', line 104

def send_data hash
  @output << JSON.unparse(hash) + @delimiter
end