Class: GMSEC::Connection

Inherits:
Object
  • Object
show all
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

Methods included from API

bind, extended, has

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, **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

Instance Method Details

#connectObject



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

Returns:

  • (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

#disconnectObject



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_statusObject



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_nameObject



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_versionObject



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 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) ⇒ 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 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) ⇒ 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)
  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_dispatchObject



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_dispatchObject



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, 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