Subsecciones

Un server

~/ruby/eventmachine/simple_server]$ cat server.rb 
 require 'eventmachine'

 module EchoServer
   def post_init
     puts "-- someone connected to the echo server!"
   end

   def receive_data data
     send_data ">>>you sent: #{data}"
     puts "received #{data}"
     close_connection if data =~ /quit/i
   end

   def unbind
     puts "-- someone disconnected from the echo server!"
  end
end

# Note that this will block current thread.
EventMachine.run {
  EventMachine.start_server "127.0.0.1", 8081, EchoServer
}

[~/ruby/eventmachine/simple_server]$ cat Rakefile 
task :default => :server

desc "run server"
task :server do
 sh "ruby server.rb"
end

SERVER = "127.0.0.1 8081"
M = %q{
And now, the end is near 
And so I face the final curtain 
My friend, I'll say it clear 
I'll state my case, of which I'm certain 
I've lived a life that's full 
I traveled each and ev'ry highway 
And more, much more than this, I did it my way 
quit
}.split(/\n+/)

desc "run a client"
task :client do
  IO.popen("telnet #{SERVER}", "r+", 
           :external_encoding=>"utf-8", 
           :err=>[:child, :out]) do |chan|
    3.times do
      output = chan.gets
      puts "SERVER says: #{output}"
    end

    M.each do |line|
      puts "client dending #{line}"
      chan.puts line
      output = chan.gets
      puts "SERVER says: #{output}"
    end
  end # popen
end

[~/ruby/eventmachine/simple_server]$ rake
ruby server.rb

[~/ruby/eventmachine/simple_server]$ rake client
SERVER says: Trying 127.0.0.1...
SERVER says: Connected to localhost.
SERVER says: Escape character is '^]'.
client dending 
SERVER says: >>>you sent: 
client dending And now, the end is near 
SERVER says: >>>you sent: And now, the end is near 
client dending And so I face the final curtain 
SERVER says: >>>you sent: And so I face the final curtain >>>you sent: 
client dending My friend, I'll say it clear 
SERVER says: >>>you sent: My friend, I'll say it clear 
client dending I'll state my case, of which I'm certain 
SERVER says: >>>you sent: I'll state my case, of which I'm certain 
client dending I've lived a life that's full 
SERVER says: >>>you sent: I've lived a life that's full >>>you sent: 
client dending I traveled each and ev'ry highway 
SERVER says: >>>you sent: I traveled each and ev'ry highway 
client dending And more, much more than this, I did it my way 
SERVER says: >>>you sent: And more, much more than this, I did it my way 
client dending quit
SERVER says: Connection closed by foreign host.

[~/ruby/eventmachine/simple_server]$ rake
ruby server.rb
-- someone connected to the echo server!
received 
received And now, the end is near 
received And so I face the final curtain 
received 
received My friend, I'll say it clear 
received I'll state my case, of which I'm certain 
received I've lived a life that's full 
received 
received I traveled each and ev'ry highway 
received And more, much more than this, I did it my way 
received quit
-- someone disconnected from the echo server!

callback y errback

[~/ruby/eventmachine/simple(master)]$ cat callback_errback.rb 
require 'rubygems'
require 'eventmachine'
require 'em-http'

urls = ARGV 
if urls.size < 1
  puts "Usage: #{$0} <url> <url> <...>"
  exit
end

pending = urls.size

EM.run do
  urls.each do |url|
    http = EM::HttpRequest.new(url).get
    http.callback {
      puts "#{url}\n#{http.response_header.status} - #{http.response.length} bytes\n"
      puts http.response

      pending -= 1
      EM.stop if pending < 1
    }
    http.errback {
      puts "#{url}\n" + http.error

      pending -= 1
      EM.stop if pending < 1
    }
  end
end

[~/ruby/eventmachine/simple(master)]$ ruby callback_errback.rb 'http://www.google.com'
http://www.google.com
302 - 258 bytes
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.es/?gws_rd=cr&amp;ei=x2OSUtLYD4aatQb-74GgCQ">here</A>.
</BODY></HTML>



Casiano Rodriguez León 2015-06-18