Permalink
Browse files

demo ui

  • Loading branch information...
1 parent 0c0e22a commit 4794671d1e6990bbdaf1450230e7a205a7321d72 @seki committed Aug 30, 2011
Showing with 92 additions and 17 deletions.
  1. +10 −7 sample/demo4book/crawl.rb
  2. +71 −0 sample/demo4book/demo_ui.rb
  3. +11 −10 sample/demo4book/index.rb
View
@@ -41,13 +41,16 @@ def quit
end
end
-crawler = Crawler.new
-Thread.new do
- while true
- pp crawler.do_crawl
- sleep 60
+if __FILE__ == $0
+ crawler = Crawler.new
+ Thread.new do
+ while true
+ pp crawler.do_crawl
+ sleep 60
+ end
end
+
+ gets
+ crawler.quit
end
-gets
-crawler.quit
@@ -0,0 +1,71 @@
+require 'index'
+require 'crawl'
+require 'webrick/cgi'
+require 'erb'
+
+class DemoListView
+ include ERB::Util
+ extend ERB::DefMethod
+ def_erb_method('to_html(word, list)', ERB.new(<<EOS))
+<html><head><title>Demo UI</title></head><body>
+<form method="post"><input type="text" name="w" value="<%=h word %>" /></form>
+<% if word %>
+<p>search: <%=h word %></p>
+<ul>
+<% list.each do |fname| %>
+<li><%=h fname%></li>
+<% end %>
+</ul>
+<% end %>
+</body></html>
+EOS
+end
+
+class DemoUICGI < WEBrick::CGI
+ def initialize(crawler, indexer, *args)
+ super(*args)
+ @crawler = crawler
+ @indexer = indexer
+ @list_view = DemoListView.new
+ end
+
+ def req_query(req, key)
+ value ,= req.query[key]
+ return nil unless value
+ value.force_encoding('utf-8')
+ value
+ end
+
+ def do_GET(req, res)
+ if req.path_info == '/quit'
+ Thread.new do
+ @crawler.quit
+ end
+ end
+ word = req_query(req, 'w') || ''
+ list = word.empty? ? [] : @indexer.dict.query(word)
+ res['content-type'] = 'text/html; charset=utf-8'
+ res.body = @list_view.to_html(word, list)
+ end
+
+ alias do_POST do_GET
+end
+
+if __FILE__ == $0
+ crawler = Crawler.new
+ Thread.new do
+ while true
+ pp crawler.do_crawl
+ sleep 60
+ end
+ end
+
+ indexer = Indexer.new
+ Thread.new do
+ indexer.update_dict
+ end
+
+ cgi = DemoUICGI.new(crawler, indexer)
+ DRb.start_service('druby://localhost:50830', cgi)
+ DRb.thread.join
+end
View
@@ -81,15 +81,16 @@ def each_tree_key(fname, mtime, src)
end
end
-indexer ||= Indexer.new(0)
-Thread.new do
- indexer.update_dict
-end
-
-while line = gets
- ary = indexer.dict.query(line.chomp)
- pp ary
- pp ary.size
+if __FILE__ == $0
+ indexer ||= Indexer.new(0)
+ Thread.new do
+ indexer.update_dict
+ end
+
+ while line = gets
+ ary = indexer.dict.query(line.chomp)
+ pp ary
+ pp ary.size
+ end
end
-

0 comments on commit 4794671

Please sign in to comment.