Created
March 3, 2011 06:16
-
-
Save sidravic/852419 to your computer and use it in GitHub Desktop.
Event Machine server in event_machine_twitter_app with deferred update (using EM.defer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
require 'socket' | |
require 'json' | |
require 'hashie' | |
require File.expand_path("twitter_lib.rb", __FILE__ + "/..") | |
require File.expand_path("tweet_Fetcher.rb", __FILE__ + "/..") | |
class Echo < EM::Connection | |
attr_reader :data, :response, :status, :fetcher | |
def post_init | |
@status = :inactive | |
ip, port = Socket.unpack_sockaddr_in( get_peername) #peer.methods.inspect | |
puts "Connection Established from #{ip} #{port}" | |
end | |
def receive_data(data) | |
puts "[LOGGING: RECEIVED] #{data}" | |
@data = JSON.parse(data) | |
puts "[LOGGING: PARSED DATA ] #{@data} #{@data.class.to_s}" | |
initialize_fetcher | |
execute_request | |
end | |
def unbind | |
puts "Connection Lost" + self.inspect | |
end | |
def respond | |
send_data(@response) | |
end | |
def execute_request | |
if @data["op"] == "fetch" | |
puts "Please wait while we fetch the data ..." | |
@status = :active | |
response = @fetcher.fetch | |
send_data(response.to_json) | |
Echo.activate_periodic_timer(self) | |
elsif @data["op"] == "update" | |
puts "Fetching update . . ." | |
response = @fetcher.fetch_update | |
# send_data(response.to_json) | |
end | |
end | |
def self.activate_event_machine(this = nil) | |
EM.run do | |
puts "Starting echo server . . . ." | |
EM.start_server('0.0.0.0', 6789, Echo) | |
puts "STARTED " | |
end | |
end | |
def self.activate_periodic_timer(this = nil) | |
response = nil | |
operations = Proc.new do | |
puts "** In Proc **" | |
this.update_operation | |
response = this.execute_request | |
end | |
callback = Proc.new{ this.send_data(response.to_json)} | |
EM.add_periodic_timer(2) do | |
EM.defer(operations, callback) | |
puts "** Defer called **" | |
end | |
end | |
def update_operation | |
@data["op"] = "update" | |
end | |
private | |
def initialize_fetcher | |
@fetcher = Fetcher.new({ :consumer_key => "xxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
:consumer_secret => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
:oauth_token => "xxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
:oauth_token_secret => "xxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
}) | |
# puts "[LOGGING FETCHER INITIALIZED] #{@fetcher.inspect}" | |
end | |
end | |
Echo.activate_event_machine | |
=begin | |
EM.run do | |
puts "Starting echo server . . . ." | |
EM.start_server('0.0.0.0', 6789, Echo) | |
puts "STARTED " | |
EM.add_periodic_timer(5) do | |
puts "Timer activated" | |
end | |
end | |
=end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment