Sha256: 4bc65b32036e4167cecf87c052160b49cfac923d8bc85c2e0db81946163d8a84

Contents?: true

Size: 1.15 KB

Versions: 10

Compression:

Stored size: 1.15 KB

Contents

require 'thread'
require 'json'

module Tork
module Client

  class Transmitter < Thread
    def initialize output_stream
      output_stream.sync = true
      @outbox = Queue.new
      super() do
        while command = @outbox.deq
          warn "#{$0}(#{$$}): SEND #{command.inspect}" if $DEBUG
          output_stream.puts JSON.dump(command)
        end
      end
    end

    def send command
      @outbox.enq command
    end
  end

  class Receiver < Thread
    def initialize input_stream
      super() do
        while command = JSON.load(input_stream.gets)
          warn "#{$0}(#{$$}): RECV #{command.inspect}" if $DEBUG
          yield command
        end
      end
    end
  end

  class Transceiver < Transmitter
    def initialize *popen_args, &receive_block
      popen_args[1] = 'w+'
      @popen_io = IO.popen(*popen_args)
      @receiver = Receiver.new(@popen_io, &receive_block)
      super @popen_io
    end

    def quit
      kill # stop transmit loop
      @receiver.kill # stop receive loop
      Process.kill :SIGTERM, @popen_io.pid
      Process.wait @popen_io.pid # reap zombie
      @popen_io.close # prevent further I/O
    end
  end

end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tork-18.2.4 lib/tork/client.rb
tork-18.2.3 lib/tork/client.rb
tork-18.2.2 lib/tork/client.rb
tork-18.2.1 lib/tork/client.rb
tork-18.2.0 lib/tork/client.rb
tork-18.1.0 lib/tork/client.rb
tork-18.0.1 lib/tork/client.rb
tork-18.0.0 lib/tork/client.rb
tork-17.1.0 lib/tork/client.rb
tork-17.0.1 lib/tork/client.rb