lib/eatmysoul.rb in eatmysoul-0.1.1 vs lib/eatmysoul.rb in eatmysoul-0.1.2
- old
+ new
@@ -30,19 +30,17 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
require 'eventmachine'
+require 'singleton'
require 'yaml'
require 'logger'
require 'digest/md5'
require 'trollop'
module EatMySoul
- @@active_connection = nil
- @@monitor = nil
-
class Settings
# edit the next line to change the default path
def initialize()
begin
@options = {
@@ -131,11 +129,11 @@
def initialize(settings)
@o = settings
@status = NOT_CONNECTED
@last_ping = Time.now
- @@active_connection = self
+ Manager.instance.active_connection = self
end
def post_init
@status = CONNECTED
end
@@ -155,11 +153,11 @@
@o.logger.warn "Receiving data, but connection is in ERROR state"
end
end
def unbind
- @@active_connection = nil
+ Manager.instance.active_connection = nil
EM.add_timer(5) { EM.connect @o.server, @o.port, Netsoul, @o }
end
private
@@ -220,42 +218,50 @@
str = s.gsub("\n", "\\n")
str.gsub!(/[^a-zA-Z0-9_.\-\\]/) {|s| "%%%02X" % s.ord }
str.gsub!(' ', '+')
str
end
+ end # class Netsoul
- end
- def self.run(o)
- EM.run do
- @@monitor = EM::add_periodic_timer(5) { self.monitor o } unless @@monitor
- EM.connect o.server, o.port, Netsoul, o
+ class Manager
+ include Singleton
+ attr_accessor :active_connection, :monitor_timer
+
+ def run(o)
+ EM.run do
+ monitor_timer = EM::add_periodic_timer(1) { monitor o } unless monitor_timer
+ EM.connect o.server, o.port, Netsoul, o
+ end
end
- end
- def self.connect_loop(o)
- while true do
- sleep_time = 0
+ def connect_loop(o)
+ while true do
+ sleep_time = 0
- begin
- self.run(o)
- rescue => e
- o.logger.fatal "Rescued from exception #{e}"
- o.logger.debug e.backtrace.join "\n"
- end
+ begin
+ self.run(o)
+ rescue => e
+ o.logger.fatal "Rescued from exception #{e}"
+ o.logger.debug e.backtrace.join "\n"
+ end
- sleep sleep_time
- sleep_time = sleep_time + 5 if sleep_time < 120
+ sleep sleep_time
+ sleep_time = sleep_time + 5 if sleep_time < 120
+ end
end
- end
- def self.monitor(o)
- if @@active_connection
- if (Time.now - @@active_connection.last_ping) > 700
- o.logger.warn "Connection seems inactive, restarting ..."
- @@active_connection.close_connection
+ def monitor(o)
+ if active_connection
+ o.logger.debug "Checking for connection inactivity"
+ if (Time.now - active_connection.last_ping) > 650
+ o.logger.warn "Connection seems inactive, restarting ..."
+ active_connection.close_connection
+ end
end
end
- end
-end
+ end # class Manager
+
+
+end # module EatmySoul