Created
November 28, 2012 17:38
-
-
Save bawNg/4162767 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'ffi' | |
module FFI::Library | |
attr_reader :arg_types | |
alias_method :ffi_attach_function, :attach_function | |
def attach_function(name, func, args, returns = nil, options = nil) | |
mname, a2, a3, a4, a5 = name, func, args, returns, options | |
cname, arg_types, ret_type, opts = (a4 && (a2.is_a?(String) || a2.is_a?(Symbol))) ? [ a2, a3, a4, a5 ] : [ mname.to_s, a2, a3, a4 ] | |
(@arg_types ||= {})[mname] = arg_types.dup | |
ffi_attach_function(name, func, args, returns, options) | |
end | |
end | |
require '~/temp/krad.rb' | |
class KradStation | |
include Krad | |
@c_function_names = Krad.class_variables.map {|var_name| var_name.to_s[2..-1].to_sym } | |
@c_function_names.each do |function_name| | |
if Krad.arg_types[function_name].first == Krad::ClientSt | |
alias_method "ipc_#{function_name}", function_name | |
define_method(function_name) do |*args| | |
send("ipc_#{function_name}", @station, *args) | |
end | |
end | |
end | |
def self.response_methods(*method_names) | |
method_names.each do |method_name| | |
alias_method "#{method_name}_without_response", method_defined?(method_name) ? method_name : "get_#{method_name}" | |
define_method(method_name) do |*args| | |
send("#{method_name}_without_response", *args) | |
print_response | |
end | |
end | |
end | |
response_methods :uptime, :system_info | |
def initialize(name_or_url) | |
@name = name_or_url | |
@station = connect(@name) | |
raise RuntimeError, "Station #@name is not running!" unless @station | |
Kernel.srand | |
end | |
alias_method :_disconnect, :disconnect | |
def disconnect | |
_disconnect | |
@station = nil | |
end | |
end | |
station_name = "monkey2" | |
station = KradStation.new(station_name) | |
puts station.uptime | |
puts station.system_info | |
station.disconnect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment