class LibComponent::Component

Entry point of LibComponent

Attributes

bus[R]
main[R]
name[R]
options[R]

Public Class Methods

new(argv_ = ARGV) { |self| ... } click to toggle source

Please provide ARGV in argument

# File lib/openplacos/libcomponent.rb, line 233
def initialize(argv_ = ARGV)
  @argv        = argv_
  @description = ""
  @bus         = nil
  @main        = nil
  @inputs      = Array.new
  @outputs     = Array.new
  @parser      = Parser.new
  @parser.option(:introspect, "Return introspection of the component",{})
  @parser.option(:debug, "debug flag")
  yield self if block_given?      
  @options = @parser.process!(@argv)
  @name = @options[:name].downcase
end

Public Instance Methods

<<(pin_) click to toggle source

Push pin to component

# File lib/openplacos/libcomponent.rb, line 270
def <<(pin_)
  if pin_.kind_of?(Input)
    @inputs << pin_
  elsif pin_.kind_of?(Output)
    @outputs << pin_
  elsif pin_.kind_of?(Array) 
    # push an array of pin
    pin_.each { |p|
      self << p
    }
  end
  pin_.set_component(self)
end
default_name(name_) click to toggle source

Default name for identiying your component

# File lib/openplacos/libcomponent.rb, line 260
def default_name(name_)
  @parser.option(:name,"Dbus name of the composant", :default => name_)
end
description(desc_) click to toggle source

provide a string describing your component

# File lib/openplacos/libcomponent.rb, line 249
def description(desc_)
  @description   = desc_
  @parser.banner = desc_
end
get_input_iface(object_name_,iface_name_) click to toggle source
# File lib/openplacos/libcomponent.rb, line 356
def get_input_iface(object_name_,iface_name_)
  @inputs.each { |input|
    return input if (input.name==object_name_) and (input.interface == iface_name_)
  }
  return nil
end
get_output_iface(object_name_,iface_name_) click to toggle source
# File lib/openplacos/libcomponent.rb, line 377
def get_output_iface(object_name_,iface_name_)
  @outputs.each { |output|
    return output if (output.name==object_name_) and (output.interface == iface_name_)
  }
  return nil
end
introspect() click to toggle source

Parse inputs and outputs to communicate with server

# File lib/openplacos/libcomponent.rb, line 337
def introspect
  inputs_h = Hash.new
  outputs_h = Hash.new
  
  #call all inputs introspect and merge values
  @inputs.each { |input|
    inputs_h.merge!(input.introspect) { |key, old, new| old.merge(new) }
  }
  #call all outputs introspect and merge values
  @outputs.each { |output|
    outputs_h.merge!(output.introspect) { |key, old, new| old.merge(new) }
  }
  
  res = Hash.new
  res["input"] = {"pin" => inputs_h}
  res["output"] = {"pin" => outputs_h}
  return res
end
on_quit(&block) click to toggle source

Event style for quit method

# File lib/openplacos/libcomponent.rb, line 330
def on_quit(&block)
  self.singleton_class.instance_eval {
    define_method(:quit , &block)
  }
end
option(*args_) click to toggle source

define an option in command line (micro-optparse syntaxe)

# File lib/openplacos/libcomponent.rb, line 265
def option(*args_)
  @parser.option(*args_)
end
print_debug(arg_) click to toggle source

print function please use this method rather than puts

quit_callback() click to toggle source

Method called when signal “quit” from server is raised

# File lib/openplacos/libcomponent.rb, line 385
def quit_callback
  self.quit if self.respond_to?(:quit)
  @main.quit if !@main.nil?
end
run() click to toggle source

Let’s rock! Run the component

# File lib/openplacos/libcomponent.rb, line 293
def run
  intro = introspect
  if @options[:introspect]
    print intro.to_yaml
  else
    @bus = create_bus
    
    #create dbus input pins
    dbusinputs = LibComponent::DbusInput.create_dbusinputs_from_introspect(intro["input"]["pin"],self)
    name = "org.openplacos.components.#{@name.downcase}"
    if (@bus.proxy.ListNames[0].member?(name))
      LibError.quit_server(255, "#{name} already exists")
    end
    @service = @bus.request_service(name)
    dbusinputs.each { |pin|
      @service.export(pin)
    }
    
    #create and connect output pins
    if options[:debug]
      @dbusoutputs = LibComponent::DebugOutput.create_dbusoutputs_from_introspect(intro["output"]["pin"],self)
    else
      @dbusoutputs = LibComponent::DbusOutput.create_dbusoutputs_from_introspect(intro["output"]["pin"],self)
      @servicesignal = Servicesignal.new(@bus, self) # listen for service signal from server
    end
    
    Signal.trap('INT') do 
      self.quit_callback
    end
            
    @main = DBus::Main.new
    @main << @bus
    @main.run
  end
end
set_input(object_name_, input_) click to toggle source
# File lib/openplacos/libcomponent.rb, line 363
def set_input(object_name_, input_)
  @inputs.each { |input|
    input.input= input_ if (input.name==object_name_) 
  }
  return nil     
end
set_last_iface_init(object_name_, iface_name_) click to toggle source
# File lib/openplacos/libcomponent.rb, line 370
def set_last_iface_init(object_name_, iface_name_)
  @inputs.each { |input|
    input.last_iface_init = iface_name_ if (input.name==object_name_) 
  }
  return nil     
end
version(version_) click to toggle source

Set version of your component

# File lib/openplacos/libcomponent.rb, line 255
def version(version_)
  @parser.version = version_
end

Private Instance Methods

create_bus() click to toggle source
# File lib/openplacos/libcomponent.rb, line 392
def create_bus
  return DBus::ASessionBus.new
end