Class: PuppetEditorServices::Server::Stdio

Inherits:
Base
  • Object
show all
Defined in:
lib/puppet_editor_services/server/stdio.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#connection_options, #handler_options, #protocol_options, #server_options

Instance Method Summary collapse

Methods inherited from Base

#log

Constructor Details

#initialize(server_options, protocol_options, handler_options) ⇒ Stdio

Returns a new instance of Stdio.



17
18
19
20
# File 'lib/puppet_editor_services/server/stdio.rb', line 17

def initialize(server_options, protocol_options, handler_options)
  super(server_options, {}, protocol_options, handler_options)
  @exiting = false
end

Instance Attribute Details

#exitingObject

Returns the value of attribute exiting.



11
12
13
# File 'lib/puppet_editor_services/server/stdio.rb', line 11

def exiting
  @exiting
end

Instance Method Details

#close_connectionObject



57
58
59
# File 'lib/puppet_editor_services/server/stdio.rb', line 57

def close_connection
  stop
end

#connection(connection_id) ⇒ Object



53
54
55
# File 'lib/puppet_editor_services/server/stdio.rb', line 53

def connection(connection_id)
  @client_connection unless @client_connection.nil? || @client_connection.id != connection_id
end

#nameObject



13
14
15
# File 'lib/puppet_editor_services/server/stdio.rb', line 13

def name
  'STDIOSRV'
end

#pipe_is_readable?(stream, timeout = 0.5) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/puppet_editor_services/server/stdio.rb', line 61

def pipe_is_readable?(stream, timeout = 0.5)
  read_ready = IO.select([stream], [], [], timeout)
  read_ready && stream == read_ready[0][0]
end

#read_from_pipe(pipe, timeout = 0.1, &_block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet_editor_services/server/stdio.rb', line 66

def read_from_pipe(pipe, timeout = 0.1, &_block)
  if pipe_is_readable?(pipe, timeout)
    l = nil
    begin
      l = pipe.readpartial(4096)
    rescue EOFError
      log('Reading from pipe has reached End of File.  Exiting STDIO server')
      stop
    rescue # rubocop:disable Style/RescueStandardError
      # Any errors here should be swallowed because the pipe could be in any state
    end
    # since readpartial may return a nil at EOF, skip returning that value
    # client_connected = true unless l.nil?
    yield l unless l.nil?
  end
  nil
end

#startObject



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
# File 'lib/puppet_editor_services/server/stdio.rb', line 22

def start
  # This is a little heavy handed but we need to suppress writes to STDOUT and STDERR
  $VERBOSE = nil
  # Some libraries use $stdout to write to the console. Suppress all of that too!
  # Copy the existing $stdout variable and then reassign to NUL to suppress it
  $editor_services_stdout = $stdout # rubocop:disable Style/GlobalVars  We need this global var
  $stdout = File.open(File::NULL, 'w')

  $editor_services_stdout.sync = true # rubocop:disable Style/GlobalVars  We need this global var
  # Stop the stupid CRLF injection when on Windows
  $editor_services_stdout.binmode unless $editor_services_stdout.binmode # rubocop:disable Style/GlobalVars  We need this global var

  @client_connection = PuppetEditorServices::Connection::Stdio.new(self)

  log('Starting STDIO server...')
  loop do
    inbound_data = nil
    read_from_pipe($stdin, 2) { |data| inbound_data = data }
    break if @exiting

    @client_connection.receive_data(inbound_data) unless inbound_data.nil?
    break if @exiting
  end
  log('STDIO server stopped')
end

#stopObject



48
49
50
51
# File 'lib/puppet_editor_services/server/stdio.rb', line 48

def stop
  log('Stopping STDIO server...')
  @exiting = true
end