Class: Rake::Subproject::Server::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/subproject/server/port.rb

Overview

:nodoc: all

Constant Summary collapse

DELIMITER =
"\0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, name = nil) ⇒ Port

Returns a new instance of Port.



15
16
17
18
19
# File 'lib/rake/subproject/server/port.rb', line 15

def initialize(io, name = nil)
  fail ArgumentError, "Requires an IO object" unless io.kind_of?(IO)
  @io = io
  self.name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/rake/subproject/server/port.rb', line 4

def name
  @name
end

Class Method Details

.open(fd, option) ⇒ Object



6
7
8
9
10
11
# File 'lib/rake/subproject/server/port.rb', line 6

def self.open(fd, option)
  return unless block_given?
  IO.open(fd, option) do |io|
    yield port = self.new(io, "server")
  end
end

Instance Method Details

#closeObject



39
40
41
42
# File 'lib/rake/subproject/server/port.rb', line 39

def close
  log "closing #{self.inspect}"
  @io.close
end

#closed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rake/subproject/server/port.rb', line 44

def closed?
  @io.closed?
end

#inspectObject



21
22
23
# File 'lib/rake/subproject/server/port.rb', line 21

def inspect
  "Port(#{self.name || @io.fileno})"
end

#readObject



25
26
27
28
29
30
# File 'lib/rake/subproject/server/port.rb', line 25

def read
  log "reading #{self.inspect}..."
  json_message = @io.readline(DELIMITER).chomp(DELIMITER)
  log "... #{self.inspect} received: #{json_message}"
  ::JSON.parse(json_message)
end

#write(object) ⇒ Object



32
33
34
35
36
37
# File 'lib/rake/subproject/server/port.rb', line 32

def write(object)
  json_message = ::JSON.generate(object)
  @io.print(json_message+DELIMITER)
  @io.flush
  log "#{self.inspect} wrote: #{json_message}"
end