Class: GoogleDriveCompanion::Server

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/google_drive_companion/server.rb

Instance Method Summary collapse

Instance Method Details

#check_if_running!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/google_drive_companion/server.rb', line 14

def check_if_running!
  if File.exists?(socket_file)
    puts "Socket file #{socket_file} in use"
    leave = true
  end

  if File.exists?(pid_file)
    pid = File.read(pid_file)
    puts "Server may be running on #{pid}\n**************\n"
    leave ||= true
  end
  leave && exit(1)
end

#closeObject



36
37
38
# File 'lib/google_drive_companion/server.rb', line 36

def close
  server.close
end

#close!Object



40
41
42
43
44
45
# File 'lib/google_drive_companion/server.rb', line 40

def close!
  close
  File.exists?(socket_file) && FileUtils.rm(socket_file)
  File.exists?(pid_file) && FileUtils.rm(pid_file)
  Process.kill 9, Process.pid
end

#msg_pumpObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/google_drive_companion/server.rb', line 71

def msg_pump
  loop do
    begin
      s = server.accept
      while (cmd = s.recv(1000))
        cmd = JSON.parse(cmd)
        case cmd.first
        when "stop"
          close!
        when "help"
          help_file = File.join(File.dirname(__FILE__), "..", "help.txt")
          respond(s, File.read(help_file))
        else
          GoogleDriveCompanion::Session.handle_msg(cmd)
          respond(s, "[#{cmd.first}] Success!")
        end
      end
    rescue Exception => bang
      tmp = "Server error: #{bang}"
      $stderr.puts tmp
      respond(s, tmp)
    end
  end
end

#pid_fileObject



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

def pid_file
  ENV["gdc_pid"] || File.join("", "tmp", "gdc.pid")
end

#respond(s, msg) ⇒ Object



65
66
67
68
69
# File 'lib/google_drive_companion/server.rb', line 65

def respond(s, msg)
  s.send(msg, 0)
rescue Errno::EPIPE
  $stderr.puts "Socket closed, meh"
end

#run!(arg = nil) ⇒ Object



59
60
61
62
63
# File 'lib/google_drive_companion/server.rb', line 59

def run!(arg = nil)
  server
  Process.daemon unless arg
  msg_pump.join
end

#serverObject



28
29
30
31
32
33
34
# File 'lib/google_drive_companion/server.rb', line 28

def server
  @server ||= begin
                check_if_running!
                write(Process.pid, pid_file)
                UNIXServer.new(socket_file)
              end
end

#socket_fileObject



6
7
8
# File 'lib/google_drive_companion/server.rb', line 6

def socket_file
  ENV["gdc_socket"] || File.join("", "tmp", "gdc_socket.sock")
end

#write(pid, pidfile) ⇒ Object

Attempts to write the pid of the forked process to the pid file. Kills process if write unsuccesfull.



49
50
51
52
53
54
55
56
57
# File 'lib/google_drive_companion/server.rb', line 49

def write(pid, pidfile)
  File.open pidfile, "w" do |f|
    f.write pid
    f.close
  end
  $stdout.puts "Daemon running with pid: #{pid}"
  rescue ::Exception => e
  raise "While writing the PID to file, unexpected #{e.class}: #{e}"
end