Class: Tork::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/tork/server.rb

Direct Known Subclasses

CLIApp, Driver, Engine, Master

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tork/server.rb', line 14

def initialize
  begin
    @welcome = UNIXServer.open(address = Server.address)
    # UNIX domain socket files are not automatically deleted on close
    at_exit { File.delete address if File.socket? address }
  rescue Errno::EADDRINUSE
    # another instance of this program is already running in the same
    # directory so become a remote control for it rather than exiting
    warn "#{$0}: remotely controlling existing instance..."
    exec 'tork-remote', $0, *ARGV
  end

  # only JSON messages are supposed to be emitted on STDOUT
  # so make puts() in the user code write to STDERR instead
  @stdout = STDOUT.dup
  STDOUT.reopen STDERR

  @servers = Set.new.add(@welcome)
  @clients = Set.new; join STDIN # parent process connected on STDIN
end

Class Method Details

.address(program = $0) ⇒ Object



10
11
12
# File 'lib/tork/server.rb', line 10

def self.address program=$0
  ".#{program}.sock"
end

Instance Method Details

#loopObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tork/server.rb', line 35

def loop
  catch :quit do
    while @clients.include? STDIN
      IO.select((@servers + @clients).to_a).first.each do |stream|
        if stream == @welcome
          join stream.accept

        elsif (stream.eof? rescue true)
          part stream

        elsif @command = hear(stream, stream.gets) and not @command.empty?
          recv stream, @command
        end
      end
    end
  end
end

#quitObject



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

def quit
  throw :quit
end