Class: Ruddy::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruddy/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, topic, options = {}) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
14
# File 'lib/ruddy/connection.rb', line 7

def initialize(service, topic, options = {})
  @service = service
  @topic   = topic
  @timeout = options.fetch(:timeout, 3000)

  start
  connect
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



4
5
6
# File 'lib/ruddy/connection.rb', line 4

def service
  @service
end

#timeoutObject

Returns the value of attribute timeout.



5
6
7
# File 'lib/ruddy/connection.rb', line 5

def timeout
  @timeout
end

#topicObject (readonly)

Returns the value of attribute topic.



4
5
6
# File 'lib/ruddy/connection.rb', line 4

def topic
  @topic
end

Class Method Details

.open(*args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/ruddy/connection.rb', line 38

def self.open(*args)
  connection = new(*args)
  return connection unless block_given?

  begin
    yield connection
  ensure
    connection.close
  end
end

Instance Method Details

#closeObject



16
17
18
19
# File 'lib/ruddy/connection.rb', line 16

def close
  @closed = true
  DDE.stop(instance)
end

#closed?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ruddy/connection.rb', line 21

def closed?
  @closed
end

#execute(command) ⇒ Object

Raises:

  • (IOError)


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruddy/connection.rb', line 25

def execute(command)
  raise IOError.new("closed connection") if closed?

  buffer = FFI::MemoryPointer.from_string(command)
  result = FFI::MemoryPointer.new(:uint)

  data = DDE.client_transaction(buffer, buffer.size, conversation, nil, 0, DDE::XTYP_EXECUTE, timeout, result)
  raise Error.new("command execution failed", DDE.last_error(instance)) if data.null?
  DDE.free_data_handle(data)

  return result.read_uint
end