Skip to content

Commit

Permalink
• Merging Lucas's latest updates for the fcsh daemon.
Browse files Browse the repository at this point in the history
The fcshd is now being cleanly daemonised and it's possilbe to start the server
and go on to build in one step - nice work!
  • Loading branch information
simongregory committed Apr 3, 2010
1 parent 8e31675 commit fbb3897
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 99 deletions.
15 changes: 9 additions & 6 deletions Support/lib/fc/fcshd.rb
Expand Up @@ -24,7 +24,7 @@ def self.status
end
end

def self.generate_view
def self.generate_view(compiler_state=nil)

puts html_head(:window_title => "ActionScript 3", :page_title => "fcshd", :sub_title => "__" );

Expand All @@ -37,16 +37,19 @@ def self.generate_view
<a id='refresh' href='javascript:refreshStatus()' title='Check daemon status'>Check Status</a><br/>
<a id='toggle' href='javascript:toggleClick();'>Toggle State</a><br/>
</div>"

compiler_state = FCSHD_SERVER.running ? "up" : "down"
puts '<script type="text/javascript" charset="utf-8">setState("'+compiler_state+'")</script>'


compiler_state = FCSHD_SERVER.running ? "up" : "down" if compiler_state.nil?
set_status(compiler_state)
end

def self.set_status(compiler_state='unknown')
puts '<script type="text/javascript" charset="utf-8">setState("'+compiler_state+'")</script>'
end

def self.stop_server
return unless FCSHD_SERVER.running
FCSHD_SERVER.stop_server
sleep 0.5
sleep 1
status
end

Expand Down
161 changes: 89 additions & 72 deletions Support/lib/fc/fcshd_server.rb
Expand Up @@ -45,97 +45,116 @@ def webrick_log_file
#remembering wich swfs we asked for compiling
def start_server

Daemons.daemonize(:mulitple => false)
Daemons.call(:multiple => false) {

@commands = Hash.new if @commands.nil?
@commands = Hash.new if @commands.nil?

log = Logger.new(log_file)
log.info("Initializing server")
log = Logger.new(log_file)
log.info("Initializing server")

fcsh = ::IO.popen("#{ENV['TM_FLEX_PATH']}/bin/fcsh 2>&1", "w+")
read_to_prompt(fcsh)
fcsh = ::IO.popen("#{ENV['TM_FLEX_PATH']}/bin/fcsh 2>&1", "w+")
read_to_prompt(fcsh)

#Creating the HTTP Server
s = WEBrick::HTTPServer.new(
:Port => PORT,
:Logger => WEBrick::Log.new(webrick_log_file, WEBrick::BasicLog::DEBUG), #WARN
:AccessLog => []
)
#Creating the HTTP Server
s = WEBrick::HTTPServer.new(
:Port => PORT,
:Logger => WEBrick::Log.new(webrick_log_file, WEBrick::BasicLog::DEBUG), #WARN
:AccessLog => []
)

#giving it an action
s.mount_proc("/build"){|req, res|
#giving it an action
s.mount_proc("/build"){|req, res|

#response variable
output = ""
#response variable
output = ""

#Searching for an id for this command
if @commands.has_key?(req.body)
# Exists, incremental
fcsh.puts "compile #{@commands[req.body]}"
output = read_to_prompt(fcsh)
else
# Does not exist, compile for the first time
fcsh.puts req.body
output = read_to_prompt(fcsh)
match = output.scan(ASSIGNED_REGEXP)
@commands[req.body] = match[0][0]
end
#Searching for an id for this command
if @commands.has_key?(req.body)
# Exists, incremental
fcsh.puts "compile #{@commands[req.body]}"
output = read_to_prompt(fcsh)
else
# Does not exist, compile for the first time
fcsh.puts req.body
output = read_to_prompt(fcsh)
match = output.scan(ASSIGNED_REGEXP)
@commands[req.body] = match[0][0]
end

log.debug("asked: #{req.body}")
log.debug("output: #{output}")
log.debug("asked: #{req.body}")
log.debug("output: #{output}")

res.body = output
res['Content-Type'] = "text/html"
}
res.body = output
res['Content-Type'] = "text/html"
}

s.mount_proc("/exit"){|req, res|
log.debug("shutting down")
s.shutdown
fcsh.puts "quit" #TODO: Check that this is really necessary.
sleep 0.2
fcsh.close
}
s.mount_proc("/exit"){|req, res|
log.debug("shutting down")
s.shutdown
fcsh.puts "quit" #TODO: Check that this is really necessary.
sleep 0.2
fcsh.close
exit
}

s.mount_proc("/status"){|req, res|
log.debug("getting status")
res.body = "UP"
}
s.mount_proc("/status"){|req, res|
begin
fcsh.puts("info 0")
output = read_to_prompt(fcsh)
res.body = "UP"
rescue Exception => e
res.body = "DOWN"
end
log.debug("getting status #{res.body}")
exit
}

s.mount_proc("/info"){|req, res|
log.debug("getting info")
fcsh.puts 'info'
output = read_to_prompt(fcsh)
res.body = output
res['Content-Type'] = "text/html"
}
s.mount_proc("/info"){|req, res|
log.debug("getting info")
fcsh.puts 'info'
output = read_to_prompt(fcsh)
res.body = output
res['Content-Type'] = "text/html"
exit
}

trap("INT"){
s.shutdown
fcsh.close
}
trap("INT"){
s.shutdown
fcsh.close
}

#Starting webrick
log.info("Starting Webrick at http://#{HOST}:#{PORT}")
#Starting webrick
log.info("Starting Webrick at http://#{HOST}:#{PORT}")

begin
begin

s.start
s.start

rescue Exception => e
rescue Exception => e

#Do not show error if we're trying to start the server more than once
if e.message =~ /Address already in use/ < 0
log.debug(e.message)
else
log.debug(e)
end
#Do not show error if we're trying to start the server more than once
if e.message =~ /Address already in use/ < 0
log.debug(e.message)
else
log.debug(e)
end

end

} if not running


if block_given?
while not running
sleep 3
end
yield
end

log.info("Closed Webrick at http://#{HOST}:#{PORT}")
#log.info("Closed Webrick at http://#{HOST}:#{PORT}")

#cleanly quit the daemon.
exit
#exit

end

Expand Down Expand Up @@ -182,14 +201,12 @@ def stop_server
def running
begin
http = Net::HTTP.new(HOST, PORT)
resp, date = http.get('/status', nil) {
return true
}
resp = http.get('/status', nil)
return true if resp.body == "UP"
rescue => e
puts "Error #{e}" unless e.message =~ /Connection refused - connect\(2\)/ #msql connection problem... apparently.
return false
end

return false
end

Expand Down
27 changes: 11 additions & 16 deletions Support/lib/fm/compiler.rb
Expand Up @@ -146,21 +146,14 @@ def build

exhaust = get_exhaust

if not FCSHD_SERVER.running

#start_server is going to detach this process so we won't hear back from
#it until the daemon closes. So warn the user they need to build again..

puts "<h3>Starting FCSHD</h3>"
puts "<p>I'm not yet clever enough to start the server <b>and</b> then build.</p>"
puts "<p>Please <a id='refresh' href='javascript:refreshStatus()' title='Check daemon status'>Check Status</a> then build again once the server has started.</p>"

html_footer

FCSHD.start_server

else

#Update status if needed
FCSHD.set_status 'launching' if not FCSHD_SERVER.running

# run the compiler and print filtered error messages
FCSHD_SERVER.start_server do

FCSHD.set_status 'up'

STDOUT << "<h3>Compiling, #{cmd.file_specs_name}</h3>"

FCSHD_SERVER.build(cmd.line).each_line do |ln|
Expand All @@ -169,9 +162,11 @@ def build

STDOUT << exhaust.raw
STDOUT << exhaust.complete

html_footer
end

end

end

end
Expand Down
13 changes: 8 additions & 5 deletions Support/lib/fm/yaml_tool.rb
Expand Up @@ -22,11 +22,14 @@ def build
#Generate the beautiful header
FCSHD.generate_view

#Run the compiler and print filtered error messages
FCSHD_SERVER.start_server if not FCSHD_SERVER.running

#Start the magic
AS3Project.compile
#Update status if needed
FCSHD.set_status 'launching' if not FCSHD_SERVER.running

# run the compiler and print filtered error messages
FCSHD_SERVER.start_server do
FCSHD.set_status 'up'
AS3Project.compile
end

end

Expand Down

0 comments on commit fbb3897

Please sign in to comment.