Class: Fibril::ForkedNonBlockingIOWrapper

Inherits:
NonBlockingIOWrapper show all
Defined in:
lib/fibril/forked_non_blocking_io_wrapper.rb

Instance Attribute Summary collapse

Attributes inherited from NonBlockingIOWrapper

#fibrils, #guard, #response_queue, #result

Instance Method Summary collapse

Methods inherited from NonBlockingIOWrapper

#await, #ingest

Constructor Details

#initialize(&block) ⇒ ForkedNonBlockingIOWrapper

Returns a new instance of ForkedNonBlockingIOWrapper.



7
8
9
10
11
12
13
# File 'lib/fibril/forked_non_blocking_io_wrapper.rb', line 7

def initialize(*, &block)
  read, write = IO.pipe
  self.read, self.write = read, write
  self.response_queue = []
  self.fibrils = []
  define_singleton_method(:loop, &block)
end

Instance Attribute Details

#readObject

Returns the value of attribute read.



4
5
6
# File 'lib/fibril/forked_non_blocking_io_wrapper.rb', line 4

def read
  @read
end

#writeObject

Returns the value of attribute write.



4
5
6
# File 'lib/fibril/forked_non_blocking_io_wrapper.rb', line 4

def write
  @write
end

Instance Method Details

#freceive(*args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/fibril/forked_non_blocking_io_wrapper.rb', line 44

def freceive(*args)
  if args.any?
    ingest(*args)
  else
    method(:ingest)
  end
end

#receive(*args) ⇒ Object



40
41
42
# File 'lib/fibril/forked_non_blocking_io_wrapper.rb', line 40

def receive(*args)
  self.write.puts URI.escape(Marshal.dump(args)) rescue nil
end

#startObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fibril/forked_non_blocking_io_wrapper.rb', line 15

def start
  freceive = method(:freceive)
  block = method(:loop)
  read, write = self.read, self.write
  fibril{
    fork do
      read.close
      block[]
      exit(0)
    end

    write.close
    begin
      Fibril.current.tick
      while message = read.gets
        freceive[*Marshal.load(URI.unescape(message))]
        Fibril.current.tick
      end
    rescue Exception => e
      puts "Exception! : #{e}"
      puts e.backtrace
    end
  }
end