Class: GMSEC::Connection
- Inherits:
-
Object
- Object
- GMSEC::Connection
- Extended by:
- API
- Defined in:
- lib/gmsec/connection.rb
Constant Summary collapse
- GMSEC_MESSAGE_TYPE =
{ publish: GMSEC_MSG_PUBLISH, reply: GMSEC_MSG_REPLY, request: GMSEC_MSG_REQUEST, unset: GMSEC_MSG_UNSET }
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #dispatcher_status ⇒ Object
-
#initialize(config = nil, **config_options) ⇒ Connection
constructor
A new instance of Connection.
- #library_root_name ⇒ Object
- #library_version ⇒ Object
- #messages(timeout: 1000, dispatch: true) ⇒ Object
- #new_message(subject = nil, message_type: :publish, default: true) ⇒ Object
- #publish(payload = nil, subject: nil) ⇒ Object
- #start_auto_dispatch ⇒ Object
- #stop_auto_dispatch ⇒ Object
- #subscribe(subject, &block) ⇒ Object
Methods included from API
Constructor Details
#initialize(config = nil, **config_options) ⇒ Connection
Returns a new instance of Connection.
14 15 16 17 18 19 20 21 |
# File 'lib/gmsec/connection.rb', line 14 def initialize(config=nil, **) if config || !.nil? self.config = .inject(config || GMSEC::Config.new) do |c, (k,v)| c[k] = v c end end end |
Instance Method Details
#connect ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# 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? ⇒ Boolean
36 37 38 |
# File 'lib/gmsec/connection.rb', line 36 def connected? gmsec_IsConnected(self, status) == self.class.enum_type(:GMSEC_BOOL)[:GMSEC_TRUE] end |
#disconnect ⇒ Object
40 41 42 43 44 45 46 |
# 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 ⇒ Object
169 170 171 172 173 |
# 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 ⇒ Object
143 144 145 146 147 148 149 150 151 |
# 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 ⇒ Object
133 134 135 136 137 138 139 140 141 |
# 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) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gmsec/connection.rb', line 114 def (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 = GMSEC::Message.new(native_object: pointer.read_pointer) if dispatch gmsec_DispatchMsg(self, , status) end y << gmsec_GetNextMsg(self, pointer, timeout, status) end end end |
#new_message(subject = nil, message_type: :publish, default: true) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gmsec/connection.rb', line 48 def (subject=nil, message_type: :publish, default: true) = GMSEC_MESSAGE_TYPE[].tap do |type| if type.nil? raise RuntimeError.new("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, , pointer, status) end GMSEC::Message.new(native_object: pointer.read_pointer).tap do || if subject .subject = subject end end end |
#publish(payload = nil, subject: nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/gmsec/connection.rb', line 71 def publish(payload=nil, subject: nil) = case payload when Hash (subject).tap do || << payload end when GMSEC::Message payload else raise RuntimeError.new("Unable to publish payload of type #{payload.class}") end gmsec_Publish(self, , status) if status.is_error? raise RuntimeError.new("Unable to publish message: #{status}") end end |
#start_auto_dispatch ⇒ Object
153 154 155 156 157 158 159 |
# 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 ⇒ Object
161 162 163 164 165 166 167 |
# 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) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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, | connection = GMSEC::Connection.new(native_object: native_connection) = GMSEC::Message.new(native_object: ) case block.arity when 1 block.call() when 2 block.call(, 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 |