Class: Superterm::Unix_socket

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Unix_socket

Returns a new instance of Unix_socket.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/superterm_unix_socket.rb', line 2

def initialize(args)
  @args = args
  
  #Remove the sock-file if it already exists.
  File.unlink(Superterm::CONFIG[:sock_path]) if File.exists?(Superterm::CONFIG[:sock_path])
  
  #Start Unix-socket.
  require "socket"
  @usock = UNIXServer.new(Superterm::CONFIG[:sock_path])
  
  #Remove the sock-file after this process is done.
  Kernel.at_exit do
    File.unlink(Superterm::CONFIG[:sock_path]) if File.exists?(Superterm::CONFIG[:sock_path])
  end
  
  #Start thread that listens for connections through the Unix-socket.
  Thread.new do
    begin
      while client = @usock.accept
        client.each_line do |line|
          line = line.strip
          
          if line.strip == "open_win_main"
            @args[:win_main].show
          else
            print "Unknown line: #{line}\n"
          end
        end
      end
    rescue => e
      $stderr.puts e.inspect
      $stderr.puts e.backtrace
    end
  end
end