At the Forge - Incremental Form Submission
Listing 2. pl-words.cgi
#!/usr/bin/env ruby
# *-ruby-*-
require 'cgi'
require 'xmlrpc/client'
def pl_sentence(sentence)
server = XMLRPC::Client.new2('http://127.0.0.1:9000', nil, 240)
sentence_array = sentence.split
# Send the words, and catch any faults that we find
begin
results = server.call("atf.pl_sentence", sentence_array)
rescue XMLRPC::FaultException => e
exit
# puts "Error:"
# puts e.faultCode
# puts e.faultString
end
return results.join(' ')
end
# Create an instance of CGI
cgi = CGI.new("html4")
# Get the words to translate
words = cgi.params['words']
if words.empty?
words = ''
else
words = words[0].downcase
end
# Send some output to the end user
cgi.out {
cgi.html {
# Produce a header
cgi.head { cgi.title { "Your Pig Latin translation" }
} +
# Produce a body
cgi.body {
cgi.h1 { "Pig Latin translation results" } +
cgi.p { "Original sentence: '#{words}'" } +
cgi.p { "Translated sentence: '#{pl_sentence(words)}'" }
}
}
}
The key to making all this work is shown in Listing 3, which provides the code for our XML-RPC server. We begin by reading from a simple cache of English words and their Pig Latin equivalents. Again, it seems silly to store things in this way, when it's much faster simply to write the code that handles the Pig Latin rules. If you imagine that each translation takes several seconds, you can see how things could pile up quickly.
Listing 3. pl-server.rb
#!/usr/bin/ruby
require 'rubygems'
require 'net/http'
require 'rexml/document'
require 'xmlrpc/server'
# ------------------------------------------------------------
# Load the translation cache
# ------------------------------------------------------------
dictionary = { }
puts "Loading cached translations"
translation_file = 'translations.txt'
if FileTest.exists?(translation_file)
File.open(translation_file, "r").each do |line|
(english, piglatin) = line.chomp.split('=')
dictionary[english] = piglatin
puts "'#{english}' => '#{piglatin}'"
end
else
File.open(translation_file, 'w') do |line|
end
end
# ------------------------------------------------------------
# XML-RPC
# ------------------------------------------------------------
# Start an HTTP server on port 9000, to listen for clients
server = XMLRPC::Server.new(port=9000, host='127.0.0.1')
server.add_handler(name="atf.pl_sentence",
signature=['array', 'array']) do |words|
output = [ ]
words.map {|word| word.to_s}.each do |word|
# Have we already seen this word? Don't bother to translate it
if dictionary.has_key?(word)
puts "Grabbing translation of '#{word}' from the dictionary"
output << dictionary[word]
next
end
# If it's not in the cache, then go for it.
piglatin = ''
if word =~ /^[aeiou]/
piglatin << word
piglatin << 'way'
else
piglatin = word[1..-1]
piglatin << word[0]
piglatin << 'ay'
end
puts "Translated '#{word}' => '#{piglatin}'"
# Cache it
puts "Trying to cache..."
dictionary[word] = piglatin
File.open(translation_file, 'a') {|f| f.puts "#{word}=#{piglatin}"}
output << piglatin
end
output
end
server.serve
There are several things to notice in this program. One of the first is the use of an on-disk cache to store recently processed inputs. (Please don't try to emulate the simple and foolish way in which I implemented this; I ignored locking and permission issues.) The cache itself is a simple text file containing name-value pairs. Before computing the Pig Latin translation of each item, the Web service consults the cache. If the word is in the cache, the service grabs that value and almost immediately returns the translated value.
If the word isn't in the cache, it translates the English into Pig Latin, storing the values for the next time around. Again, this ensures that we have to work hard (that is, translate the word into Pig Latin) only if it fails to appear in the cache.
If you've never programmed in Ruby before, you might be put off a bit by this line:
words.map {|word| word.to_s}.each do |word|
This tells Ruby that it should take the array named words and turn each of its elements into a string. (If the element already is a string, it is unaffected.) We then iterate over each string (word) in the array, assigning the local variable word to each element in sequence.
With Listings 1, 2 and 3 in place, you should be able to translate sentences from English into Pig Latin without too much difficulty. You enter the English words into the HTML form, the server-side program calls the Web service, and the Web service takes care of things quickly.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Speed Up Your Web Site with Varnish | Jun 19, 2013 |
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
- Speed Up Your Web Site with Varnish
- Containers—Not Virtual Machines—Are the Future Cloud
- Linux Systems Administrator
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Senior Perl Developer
- Technical Support Rep
- Non-Linux FOSS: libnotify, OS X Style
- UX Designer
- RSS Feeds
- Web & UI Developer (JavaScript & j Query)
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




1 hour 53 min ago
2 hours 10 min ago
3 hours 27 min ago
4 hours 16 min ago
4 hours 18 min ago
4 hours 27 min ago
4 hours 57 min ago
7 hours 23 min ago
11 hours 23 min ago
12 hours 39 min ago