Permalink
Browse files

snmp.nut: refactor agent

CachedMethod and Snmppass spun-off snmp.nut into isolated files.
This helps other usage of these files for other kind of snmp agents.
  • Loading branch information...
1 parent df5faaf commit a0a694a96ea4e731de879966e28ca6f139d5bbf2 @luizluca committed Oct 22, 2014
Showing with 4,609 additions and 1,302 deletions.
  1. +35 −0 cached_method.rb
  2. +12 −0 create_static.sh
  3. +3,323 −0 snmp-static.nut
  4. +602 −1,302 snmp.nut
  5. +637 −0 snmppass.rb
View
@@ -0,0 +1,35 @@
+#
+#
+#
+module CachedMethod
+ NOCACHE_SUFIX="_nocache"
+
+ def cached(method, args, ttf=@cache_ttf)
+ key=[method, args]
+ return nil if not @cache or not @cache.include? key
+ (result, timestamp)=@cache[key]
+ return nil if Time.now-timestamp>ttf
+ return result
+ end
+
+ def cache(method, result, args)
+ key=[method, args]
+ @cache=Hash.new if not @cache
+ @cache[key]=[result,Time.now]
+ result
+ end
+
+ def cache_method(method_sym, ttl=nil)
+ cached_version=<<-EOM
+ alias_method :#{method_sym.to_s + NOCACHE_SUFIX}, :#{method_sym.to_s}
+ def #{method_sym.to_s}(*args)
+ value = cached(:#{method_sym.to_s}, args #{",#{ttl}" if ttl})
+ return value if not value==nil
+ return cache(:#{method_sym.to_s},#{method_sym.to_s + NOCACHE_SUFIX}(*args), args)
+ end
+ EOM
+ class_eval cached_version
+ end
+
+ attr_accessor :cache_ttf
+end
View
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+sed -e '
+/require "snmppass"/{
+ s/.*/#&/
+ rsnmppass.rb
+}
+/require "cached_method"/ {
+ s/.*/#&/
+ rcached_method.rb
+}' snmp.nut > snmp-static.nut
+
Oops, something went wrong.

0 comments on commit a0a694a

Please sign in to comment.