From a21664e668038291bdbe42684d46cb112242aa7b Mon Sep 17 00:00:00 2001 From: Aman Gupta Date: Wed, 7 Dec 2011 00:32:15 -0800 Subject: [PATCH] Split out BERP processing into Ernie.process method --- lib/ernie.rb | 101 +++++++++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/lib/ernie.rb b/lib/ernie.rb index cfee7de..5838c0b 100644 --- a/lib/ernie.rb +++ b/lib/ernie.rb @@ -132,56 +132,69 @@ def self.start output.sync = true loop do - self.procline('waiting') - iruby = self.read_berp(input) - self.count += 1 - - unless iruby - puts "Could not read BERP length header. Ernie server may have gone away. Exiting now." - if self.log.level <= Logger::INFO - self.log.info("(#{Process.pid}) Could not read BERP length header. Ernie server may have gone away. Exiting now.") - end - exit! + process(input, output) + end + end + + # Processes a single BertRPC command. + # + # input - The IO to #read command BERP from. + # output - The IO to #write reply BERP to. + # + # Returns a [iruby, oruby] tuple of incoming and outgoing BERPs processed. + def self.process(input, output) + self.procline('waiting') + iruby = self.read_berp(input) + self.count += 1 + + unless iruby + puts "Could not read BERP length header. Ernie server may have gone away. Exiting now." + if self.log.level <= Logger::INFO + self.log.info("(#{Process.pid}) Could not read BERP length header. Ernie server may have gone away. Exiting now.") end + exit! + end - if iruby.size == 4 && iruby[0] == :call - mod, fun, args = iruby[1..3] - self.procline("#{mod}:#{fun}(#{args})") - self.log.info("-> " + iruby.inspect) if self.log.level <= Logger::INFO - begin - res = self.dispatch(mod, fun, args) - oruby = t[:reply, res] - self.log.debug("<- " + oruby.inspect) if self.log.level <= Logger::DEBUG - write_berp(output, oruby) - rescue ServerError => e - oruby = t[:error, t[:server, 0, e.class.to_s, e.message, e.backtrace]] - self.log.error("<- " + oruby.inspect) if self.log.level <= Logger::ERROR - self.log.error(e.backtrace.join("\n")) if self.log.level <= Logger::ERROR - write_berp(output, oruby) - rescue Object => e - oruby = t[:error, t[:user, 0, e.class.to_s, e.message, e.backtrace]] - self.log.error("<- " + oruby.inspect) if self.log.level <= Logger::ERROR - self.log.error(e.backtrace.join("\n")) if self.log.level <= Logger::ERROR - write_berp(output, oruby) - end - elsif iruby.size == 4 && iruby[0] == :cast - mod, fun, args = iruby[1..3] - self.procline("#{mod}:#{fun}(#{args})") - self.log.info("-> " + [:cast, mod, fun, args].inspect) if self.log.level <= Logger::INFO - begin - self.dispatch(mod, fun, args) - rescue Object => e - # ignore - end - write_berp(output, t[:noreply]) - else - self.procline("invalid request") - self.log.error("-> " + iruby.inspect) if self.log.level <= Logger::ERROR - oruby = t[:error, t[:server, 0, "Invalid request: #{iruby.inspect}"]] + if iruby.size == 4 && iruby[0] == :call + mod, fun, args = iruby[1..3] + self.procline("#{mod}:#{fun}(#{args})") + self.log.info("-> " + iruby.inspect) if self.log.level <= Logger::INFO + begin + res = self.dispatch(mod, fun, args) + oruby = t[:reply, res] + self.log.debug("<- " + oruby.inspect) if self.log.level <= Logger::DEBUG + write_berp(output, oruby) + rescue ServerError => e + oruby = t[:error, t[:server, 0, e.class.to_s, e.message, e.backtrace]] + self.log.error("<- " + oruby.inspect) if self.log.level <= Logger::ERROR + self.log.error(e.backtrace.join("\n")) if self.log.level <= Logger::ERROR + write_berp(output, oruby) + rescue Object => e + oruby = t[:error, t[:user, 0, e.class.to_s, e.message, e.backtrace]] self.log.error("<- " + oruby.inspect) if self.log.level <= Logger::ERROR + self.log.error(e.backtrace.join("\n")) if self.log.level <= Logger::ERROR write_berp(output, oruby) end + elsif iruby.size == 4 && iruby[0] == :cast + mod, fun, args = iruby[1..3] + self.procline("#{mod}:#{fun}(#{args})") + self.log.info("-> " + [:cast, mod, fun, args].inspect) if self.log.level <= Logger::INFO + begin + self.dispatch(mod, fun, args) + rescue Object => e + # ignore + end + oruby = t[:noreply] + write_berp(output, oruby) + else + self.procline("invalid request") + self.log.error("-> " + iruby.inspect) if self.log.level <= Logger::ERROR + oruby = t[:error, t[:server, 0, "Invalid request: #{iruby.inspect}"]] + self.log.error("<- " + oruby.inspect) if self.log.level <= Logger::ERROR + write_berp(output, oruby) end + + [iruby, oruby] end def self.procline(msg)