Class: Pandemic::ServerSide::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/pandemic/server_side/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(handler) ⇒ Processor

Returns a new instance of Processor.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pandemic/server_side/processor.rb', line 4

def initialize(handler)
  read_from_parent, write_to_child = IO.pipe
  read_from_child, write_to_parent = IO.pipe
  
  @child_process_id = fork
  if @child_process_id
    # I'm the parent
    write_to_parent.close
    read_from_parent.close
    @out = write_to_child
    @in = read_from_child
  else
    # I'm the child
    write_to_child.close
    read_from_child.close
    @out = write_to_parent
    @in = read_from_parent
    @handler = handler.new
    wait_for_jobs
  end
end

Instance Method Details

#close(status = 0) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/pandemic/server_side/processor.rb', line 41

def close(status = 0)
  if parent? && child_alive?
    Process.detach(@child_process_id)
    @out.puts(status.to_s)
    @out.close
    @in.close
  else
    Process.exit!(status)
  end
end

#closed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/pandemic/server_side/processor.rb', line 52

def closed?
  !child_alive?
end

#process(body) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pandemic/server_side/processor.rb', line 26

def process(body)
  if parent?
    @out.puts(body.size.to_s)
    @out.write(body)
    ready, = IO.select([@in], nil, nil)
    if ready
      size = @in.gets.to_i
      result = @in.read(size)
      return result
    end
  else
    return @handler.process(body)
  end
end