Class: Voltronic::Protocol::IO

Inherits:
Object
  • Object
show all
Defined in:
lib/voltronic/protocols/io.rb

Overview

Implementation of the Protocol for IO objects

Defined Under Namespace

Classes: BufferOverflowError, DeviceError, TimeoutError

Constant Summary collapse

MAXIMUM_RESPONSE_SIZE =
1024
READ_BLOCK_SIZE =

USB device report size

8
READ_OPTIONS =
{ exception: false }.freeze
CLEAR_TIMEOUT =
2.0

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ IO

:nodoc:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/voltronic/protocols/io.rb', line 13

def initialize(io) #:nodoc:
  @io_array = [io].freeze

  @io = begin # Verify input io quacks like an IO
    begin
      io.sync = true
    rescue Exception
    end

    begin
      io.binmode = true
    rescue Exception
    end

    begin
      io.read_nonblock(READ_BLOCK_SIZE, nil, READ_OPTIONS)
    rescue Exception
      raise ArgumentError.new [io.to_s, ' does not support read_nonblock'].join
    end

    if !io.respond_to?(:write)
      raise ArgumentError.new [io.to_s, ' does not support write'].join
    end

    begin
      ::IO.select(@io_array, nil, nil, 0)
    rescue Exception
      raise ArgumentError.new [io.to_s, ' does not support IO.select(', io.to_s, ')'].join
    end

    io
  end

  @support_flush ||= begin # Assert if flush works
    @io.flush
    true
  rescue Exception
    false
  end

  clear_read_buffer
end

Instance Method Details

#closeObject



78
79
80
81
82
83
# File 'lib/voltronic/protocols/io.rb', line 78

def close
  @io.close
  true
rescue Exception
  false
end

#closed?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/voltronic/protocols/io.rb', line 85

def closed?
  @io.closed?
end

#execute(input, timeout = 1.0) ⇒ Object

:nodoc:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/voltronic/protocols/io.rb', line 56

def execute(input, timeout = 1.0) #:nodoc:
  timeout = begin
    parse = Float(timeout)
    raise if (0 >= parse)
    parse
  rescue
    raise ArgumentError.new ['Invalid timeout ', timeout.to_s].join
  end

  clear_read_buffer
  write(input)
  read(timeout)
end

#inspectObject

:nodoc:



74
75
76
# File 'lib/voltronic/protocols/io.rb', line 74

def inspect #:nodoc:
  self.to_s
end

#to_sObject

:nodoc:



70
71
72
# File 'lib/voltronic/protocols/io.rb', line 70

def to_s #:nodoc:
  'Protocol(IO)'
end