Skip to content

Instantly share code, notes, and snippets.

@rscottm
Created December 19, 2010 07:10
Show Gist options
  • Select an option

  • Save rscottm/747164 to your computer and use it in GitHub Desktop.

Select an option

Save rscottm/747164 to your computer and use it in GitHub Desktop.
Experimental webrick server to allow editing of scripts on the device
#######################################################
#
# script_server.rb (by Scott Moyer)
#
# This is an experiment in creating a web server on
# an android handset for the purpose of editing scripts.
#
#######################################################
# Need to require date separately. You get a stack overflow
# if it happens inside of webrick
require 'date'
require 'webrick'
include WEBrick
java_import "android.app.Activity"
java_import "android.content.Context"
java_import "android.content.Intent"
java_import "android.app.Notification"
java_import "android.app.PendingIntent"
java_import "java.lang.System"
java_import "android.R"
SERVER_PORT = 8005
#####################################################
#
# Used to add a notification about the ip:port to
# find the server
#
def notify(title, text, ticker, icon, note_id=1)
notification = Notification.new(icon, ticker, System.currentTimeMillis)
notification.setLatestEventInfo($activity.getApplicationContext, title, text,
PendingIntent.getActivity($activity, 0, Intent.new($activity, $activity.java_class), 0))
$activity.getSystemService(Context::NOTIFICATION_SERVICE).notify(note_id, notification)
end
#####################################################
#
# HTML helper methods
#
def edit_form(name="", script="")
"<form action=\"/#{name == 'untitled.rb' ? 'create' : name}\" method='post' style='display:inline'>
Name: <input name='name' value='#{name}' />
<input type='submit' value='Save' />
<a href='/'>Cancel</a>
<br/>
<textarea id='script' name='script' rows='25' cols='100'>#{script.gsub('<', '&l' + 't;').gsub('>', '&g' + 't;')}</textarea>
</form>
<br/>
"
end
def wrap_html(body)
"<html><body>#{body}</body></html>"
end
#####################################################
#
# Webrick Servlet that allows displaying an edit
# form (through GET) and receiving updates (through POST)
#
class ScriptServlet < HTTPServlet::AbstractServlet
def do_GET(req, resp)
resp.content_type = "text/html"
name = @options[0] || "untitled.rb"
script = File.exists?(name) ? IO.read(name) : ""
resp.body = wrap_html(edit_form(name, script))
end
def do_POST(req, resp)
File.open(req.query['name'], "w") {|file| file.write req.query['script']}
resp.set_redirect(HTTPStatus::TemporaryRedirect, '/')
end
end
#####################################################
#
# Get the ip address and setup the server
#
ip = $activity.getSystemService(Activity::WIFI_SERVICE).getConnectionInfo.getIpAddress
if ip != 0
ip = [0, 8, 16, 24].map{|n| ((ip >> n) & 0xff).to_s}.join(".")
notify("Script Server Running",
"Connect to #{ip}:#{SERVER_PORT}",
"Script server running on #{ip}:#{SERVER_PORT}",
R::drawable::stat_sys_upload)
$server = HTTPServer.new(:Port => SERVER_PORT, :DocumentRoot => "/sdcard/jruby")
$server.mount_proc('/') do |req, resp|
resp.content_type = "text/html"
body = Dir.glob("*.rb").sort.map do |i|
$server.mount("/#{i}", ScriptServlet, i)
"<a href='/#{i}'>#{i}</a><br/>"
end.join
other =[
"<a href='/create'>Create Script...</a><br/>",
"<a href='/stop'>Stop Server</a><br/>",
].join
resp.body = wrap_html("<h2>Scripts</h2>#{body}<h2>Other Actions</h2>#{other}")
end
$server.mount("/create", ScriptServlet, nil)
$server.mount_proc('/stop') do |req, resp|
$server.shutdown
$activity.getSystemService(Context::NOTIFICATION_SERVICE).cancelAll
end
$server.start
else
puts "Not connected to wifi"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment