Class: SimpleRPC

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-rpc.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SimpleRPC



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple-rpc.rb', line 6

def initialize(client)
  @client = client
  is_child = ENV['CHILD'] ? true : false
  parent_channel = ENV['CHANNEL'] + '_parent'
  child_channel = ENV['CHANNEL'] + '_child'
  receive_channel = is_child ? parent_channel : child_channel
  send_channel = is_child ? child_channel : parent_channel
  @socket = LocalSocket.new(receive_channel, send_channel)

  @socket.message_received do | json_array_string |
    array = JSON.parse(json_array_string)
    @client.send(*array)
  end
  @socket.connection_changed do |connected|
    puts "Connection changed: connected = #{connected}"
  end
end

Instance Method Details

#send_msg(*args) ⇒ Object



24
25
26
27
# File 'lib/simple-rpc.rb', line 24

def send_msg(*args)
  msg = JSON.generate(args)
  @socket.send_msg msg
end