Class: RiderServer::Services::CaptureIO

Inherits:
RiderServer::Service show all
Defined in:
lib/rider_server/services/capture_io.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RiderServer::Service

service_name

Constructor Details

#initialize(session) ⇒ CaptureIO

Returns a new instance of CaptureIO.



48
49
50
51
52
53
# File 'lib/rider_server/services/capture_io.rb', line 48

def initialize(session)
  @session = session
  @stdout = ::STDOUT # rubocop:disable Style/GlobalStdStream
  @stderr = ::STDERR # rubocop:disable Style/GlobalStdStream
  super
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



15
16
17
# File 'lib/rider_server/services/capture_io.rb', line 15

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



15
16
17
# File 'lib/rider_server/services/capture_io.rb', line 15

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



15
16
17
# File 'lib/rider_server/services/capture_io.rb', line 15

def stdout
  @stdout
end

Class Method Details

.create_response(id, stream_name, str) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rider_server/services/capture_io.rb', line 33

def self.create_response(id, stream_name, str)
  {
    "id" => id,
    stream_name => str,
    "time-stamp" => Time.now.strftime("%Y-%m-%d %H:%M:%S")
  }
end

.nwrite(stream_name, *string) ⇒ Object



28
29
30
31
# File 'lib/rider_server/services/capture_io.rb', line 28

def self.nwrite(stream_name, *string)
  str = string.join
  send_response(stream_name, str)
end

.send_response(io_stream, string) ⇒ Object



41
42
43
44
45
46
# File 'lib/rider_server/services/capture_io.rb', line 41

def self.send_response(io_stream, string)
  @sessions.each do |session, stream_id|
    response = create_response(stream_id, io_stream, string)
    session.response_queue.push(response)
  end
end

.sessionsObject



24
25
26
# File 'lib/rider_server/services/capture_io.rb', line 24

def self.sessions
  @sessions
end

.stdinObject



20
21
22
# File 'lib/rider_server/services/capture_io.rb', line 20

def self.stdin
  @stdin
end

Instance Method Details

#start(stream_id) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/rider_server/services/capture_io.rb', line 55

def start(stream_id)
  unless @stdin == ::STDIN && @stdout == ::STDOUT && @stderr == ::STDERR # rubocop:disable Style/GlobalStdStream
    $stdin = self.class.stdin
    $stdout = @stdout = Services::IO.new(::STDOUT, "out") # rubocop:disable Style/GlobalStdStream
    $stderr = @stderr = Services::IO.new(::STDERR, "err") # rubocop:disable Style/GlobalStdStream
  end
  self.class.sessions << [@session, stream_id]
  :running
end

#statusObject



76
77
78
79
80
81
82
# File 'lib/rider_server/services/capture_io.rb', line 76

def status
  if self.class.sessions.find { |s| s[0] == @session }
    :running
  else
    :stopped
  end
end

#stopObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/rider_server/services/capture_io.rb', line 65

def stop
  self.class.sessions.delete_if { |s| s[0] == @session }

  if self.class.sessions.empty?
    $stdin = @stdin = ::STDIN # rubocop:disable Style/GlobalStdStream
    $stdout = @stdout = ::STDOUT # rubocop:disable Style/GlobalStdStream
    $stderr = @stderr = ::STDERR # rubocop:disable Style/GlobalStdStream
  end
  :stopped
end