class GMSEC::Connection
Constants
- GMSEC_MESSAGE_TYPE
Public Class Methods
new(config=nil, **config_options)
click to toggle source
# File lib/gmsec/connection.rb, line 14 def initialize(config=nil, **config_options) if config || !config_options.nil? self.config = config_options.inject(config || GMSEC::Config.new) do |c, (k,v)| c[k] = v c end end end
Public Instance Methods
connect()
click to toggle source
# File lib/gmsec/connection.rb, line 23 def connect # We initialize the connection assuming that a config is provided before connecting. initialize_native_object do |pointer| gmsec_CreateConnectionForConfig(config, pointer, status) end gmsec_Connect(self, status) if status.is_error? raise RuntimeError.new("Unable to connect: #{status}") end end
connected?()
click to toggle source
# File lib/gmsec/connection.rb, line 36 def connected? gmsec_IsConnected(self, status) == self.class.enum_type(:GMSEC_BOOL)[:GMSEC_TRUE] end
disconnect()
click to toggle source
# File lib/gmsec/connection.rb, line 40 def disconnect gmsec_Disconnect(self, status) if status.is_error? raise RuntimeError.new("Unable to disconnect: #{status}") end end
dispatcher_status()
click to toggle source
# File lib/gmsec/connection.rb, line 169 def dispatcher_status GMSEC::Status.new.tap do |status| gmsec_GetLastDispatcherStatus(self, status) end end
library_root_name()
click to toggle source
# File lib/gmsec/connection.rb, line 143 def library_root_name with_string_pointer do |pointer| gmsec_GetLibraryRootName(self, pointer, status) if status.is_error? raise RuntimeError.new("Unable to get library root name: #{status}") end end end
library_version()
click to toggle source
# File lib/gmsec/connection.rb, line 133 def library_version with_string_pointer do |pointer| gmsec_GetLibraryVersion(self, pointer, status) if status.is_error? raise RuntimeError.new("Unable to get library version: #{status}") end end end
messages(timeout: 1000, dispatch: true)
click to toggle source
# File lib/gmsec/connection.rb, line 114 def messages(timeout: 1000, dispatch: true) Enumerator.new do |y| pointer = FFI::MemoryPointer.new(GMSEC::Message.native_type) gmsec_GetNextMsg(self, pointer, timeout, status) while status.code != GMSEC_TIMEOUT_OCCURRED && status.code == 0 message = GMSEC::Message.new(native_object: pointer.read_pointer) if dispatch gmsec_DispatchMsg(self, message, status) end y << message gmsec_GetNextMsg(self, pointer, timeout, status) end end end
new_message(subject=nil, message_type: :publish, default: true)
click to toggle source
# File lib/gmsec/connection.rb, line 48 def new_message(subject=nil, message_type: :publish, default: true) message_type = GMSEC_MESSAGE_TYPE[message_type].tap do |type| if type.nil? raise RuntimeError.new("Message type '#{message_type}' not supported.") end end pointer = FFI::MemoryPointer.new(GMSEC::Message.native_type) case default when true gmsec_CreateMessageDflt(self, pointer, status) when false gmsec_CreateMessage(self, subject, message_type, pointer, status) end GMSEC::Message.new(native_object: pointer.read_pointer).tap do |message| if subject message.subject = subject end end end
publish(payload=nil, subject: nil)
click to toggle source
# File lib/gmsec/connection.rb, line 71 def publish(payload=nil, subject: nil) message = case payload when Hash new_message(subject).tap do |message| message << payload end when GMSEC::Message payload else raise RuntimeError.new("Unable to publish payload of type #{payload.class}") end gmsec_Publish(self, message, status) if status.is_error? raise RuntimeError.new("Unable to publish message: #{status}") end end
start_auto_dispatch()
click to toggle source
# File lib/gmsec/connection.rb, line 153 def start_auto_dispatch gmsec_StartAutoDispatch(self, status) if status.is_error? raise RuntimeError.new("Unable to start auto dispatch: #{status}") end end
stop_auto_dispatch()
click to toggle source
# File lib/gmsec/connection.rb, line 161 def stop_auto_dispatch gmsec_StopAutoDispatch(self, status) if status.is_error? raise RuntimeError.new("Unable to stop auto dispatch: #{status}") end end
subscribe(subject, &block)
click to toggle source
# File lib/gmsec/connection.rb, line 90 def subscribe(subject, &block) if block_given? callback = FFI::Function.new(:void, [find_type(:GMSEC_CONNECTION_OBJECT), find_type(:GMSEC_MESSAGE_OBJECT)]) do |native_connection, native_message| connection = GMSEC::Connection.new(native_object: native_connection) message = GMSEC::Message.new(native_object: native_message) case block.arity when 1 block.call(message) when 2 block.call(message, connection) end end gmsec_SubscribeWCallback(self, subject, callback, status) else gmsec_Subscribe(self, subject, status) end if status.is_error? raise RuntimeError.new("Unable to subscribe: #{status}") end end