Class: PatternPark::FCSHD

Inherits:
Hash show all
Includes:
Daemonize
Defined in:
lib/airake/fcshd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Daemonize

#daemonize, #safefork

Constructor Details

#initialize(ip = nil, port = nil) ⇒ FCSHD

Returns a new instance of FCSHD.



14
15
16
17
18
19
20
21
22
# File 'lib/airake/fcshd.rb', line 14

def initialize(ip = nil, port = nil)
  @fcsh_path = "fcsh" 

  @instances = Hash.new
  @ip = ip.nil? ? "127.0.0.1" : ip
  @port = port.nil? ? 20569 : port
  open(@ip, @port)
  start
end

Instance Attribute Details

#fcsh_pathObject (readonly)

Returns the value of attribute fcsh_path.



11
12
13
# File 'lib/airake/fcshd.rb', line 11

def fcsh_path
  @fcsh_path
end

#instancesObject (readonly)

Returns the value of attribute instances.



11
12
13
# File 'lib/airake/fcshd.rb', line 11

def instances
  @instances
end

#kill_processObject

Returns the value of attribute kill_process.



12
13
14
# File 'lib/airake/fcshd.rb', line 12

def kill_process
  @kill_process
end

#start_processObject

Returns the value of attribute start_process.



12
13
14
# File 'lib/airake/fcshd.rb', line 12

def start_process
  @start_process
end

Class Method Details

.start_from_rake(env, default_ip = "127.0.0.1", default_port = 20569) ⇒ Object



107
108
109
110
111
# File 'lib/airake/fcshd.rb', line 107

def self.start_from_rake(env, default_ip = "127.0.0.1", default_port = 20569)
  fcsh_ip = env["FCSH_IP"] || default_ip
  fcsh_port = (env["FCSH_PORT"] || default_port).to_i
  self.new(fcsh_ip, fcsh_port)
end

Instance Method Details

#execute(io, path, command) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/airake/fcshd.rb', line 64

def execute(io, path, command)
  io.puts "[FCSHD] #{path}"
  io.puts "[FCSHD] #{command}"
  begin
    fcsh = get_fcsh(io, path)
  rescue => e
    io.puts "[ERROR] Unable to launch fcsh: #{e.message}: #{e.backtrace[0..5].join("\n")}"
    exit
  end

  cmd = @compile_commands[command]
  msg = command
  if (!cmd.nil? && cmd.path == path)
    msg = "compile #{cmd.index}"
  end
  
  compile_errors = false
  e = Thread.new {
    while line = fcsh.e.gets
      if line.strip.empty?
        io.puts " "
      elsif line =~ /.?: Warning: .?/
        io.puts "[FCSH WARNING] #{line}"
      else
        io.puts "[FCSH ERROR] #{line}"
        compile_errors = true            
      end
    end
  }

  fcsh.puts msg
  io.puts fcsh.read
  
  e.kill unless compile_errors

  # Store the successful compilation
  if (cmd.nil? && !compile_errors)
    cmd = CompileCommand.new(command, path)
    @compile_commands[command] = cmd
    cmd.index = @compile_commands.size
  end
end

#get_fcsh(io, path) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/airake/fcshd.rb', line 55

def get_fcsh(io, path)
  if(instances[path].nil?)
    fcsh = FCSHProcess.new(fcsh_path, path)
    fcsh.read
    instances[path] = fcsh
  end
  return instances[path]
end

#open(ip, port) ⇒ Object



24
25
26
27
28
29
# File 'lib/airake/fcshd.rb', line 24

def open(ip, port)
  @compile_commands = {}
  @socket = TCPServer.new(@ip, @port)
  @socket.setsockopt(Socket::SOL_SOCKET, Socket::TCP_NODELAY, true)
  puts ">> [FCSHD] Started on #{@ip}:#{@port}"
end

#startObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/airake/fcshd.rb', line 31

def start
  daemonize()
  loop do
    session = @socket.accept
    dir = session.gets
    dir = dir.split("\n").join("")
    if (dir == 'SIGHUP')
      stop(session)
      return
    end
    command = session.gets
    execute(session, dir, command)
    session.close
    sleep 0.75
  end
end

#stop(io) ⇒ Object



48
49
50
51
52
53
# File 'lib/airake/fcshd.rb', line 48

def stop(io)
  io.puts ">> [FCSHD] Terminating daemon now"
  fcsh.puts "quit"
  @socket.close
  exit
end