Class: Dde::Monitor

Inherits:
App
  • Object
show all
Defined in:
lib/dde/monitor.rb

Overview

Class encapsulates DDE Monitor that prints all DDE transactions to console

Instance Attribute Summary collapse

Attributes inherited from App

#id, #init_flags

Instance Method Summary collapse

Methods inherited from App

#dde_active?, #error, #start_dde, #stop_dde, #try

Constructor Details

#initialize(init_flags = nil, print = nil, &callback) ⇒ Monitor

Creates new DDE monitor instance



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dde/monitor.rb', line 9

def initialize(init_flags=nil, print = nil, &callback)
  init_flags ||=
          APPCLASS_MONITOR |  # this is monitor
          MF_CALLBACKS     |  # monitor callback functions
          MF_CONV          |  # monitor conversation data
          MF_ERRORS        |  # monitor DDEML errors
          MF_HSZ_INFO      |  # monitor data handle activity
          MF_LINKS         |  # monitor advise loops
          MF_POSTMSGS      |  # monitor posted DDE messages
          MF_SENDMSGS         # monitor sent DDE messages

  @print = print
  @calls = []

  callback ||= lambda do |*args|
    time = Time.now.strftime('%T.%6N')
    values = extract_values(*args)
    @calls << [time, values]
    puts "#{time} #{values}" if @print
    DDE_FACK
  end

  super init_flags, &callback
end

Instance Attribute Details

#callsObject

Returns the value of attribute calls.



6
7
8
# File 'lib/dde/monitor.rb', line 6

def calls
  @calls
end

Returns the value of attribute print.



6
7
8
# File 'lib/dde/monitor.rb', line 6

def print
  @print
end

Instance Method Details

#extract_values(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dde/monitor.rb', line 34

def extract_values(*args)
  values = args.map {|arg| interprete_value(arg)}

  # if this is a MONITOR transaction, extract hdata using the DdeAccessData
  if values.first == :XTYP_MONITOR
    data_type = case values.last
      when :MF_CALLBACKS
        MonCbStruct #.new(dde_get_data(args[5]).first)
      # cb:: Specifies the structure's size, in bytes.
      # dwTime:: Specifies the Windows time at which the transaction occurred. Windows time is the number of
      #          milliseconds that have elapsed since the system was booted.
      # hTask:: Handle to the task (app instance) containing the DDE callback function that received the transaction.
      # dwRet:: Specifies the value returned by the DDE callback function that processed the transaction.
      # wType:: Specifies the transaction type.
      # wFmt:: Specifies the format of the data exchanged (if any) during the transaction.
      # hConv:: Handle to the conversation in which the transaction took place.
      # hsz1:: Handle to a string.
      # hsz2:: Handle to a string.
      # hData:: Handle to the data exchanged (if any) during the transaction.
      # dwData1:: Specifies additional data.
      # dwData2:: Specifies additional data.
      # cc:: Specifies a CONVCONTEXT structure containing language information used to share data in different languages.
      # cbData:: Specifies the amount, in bytes, of data being passed with the transaction. This value can be
      #          more than 32 bytes.
      # Data:: Contains the first 32 bytes of data being passed with the transaction (8 * sizeof(DWORD)).

      when :MF_CONV
        MonConvStruct
      when :MF_ERRORS
        MonErrStruct
      when :MF_HSZ_INFO
        MonHszStruct
      when :MF_LINKS
        MonLinksStruct
      else
        MonMsgStruct
    end

    #casting DDE data pointer into appropriate struct type
    struct_pointer, size = dde_get_data(args[5])
    data = data_type.new(struct_pointer)

    values = [values.first, values.last] + data.members.map do |member|
      value = data[member] rescue 'plonk'
      "#{member}: #{interprete_value(value)}"
    end
  end

  values
end

#interprete_value(arg) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/dde/monitor.rb', line 85

def interprete_value(arg)
  return arg unless arg.kind_of? Fixnum rescue return 'plAnk'
  return 0 if arg == 0
  #Trying to interpete arg as a DDE string
  dde_query_string(@id, arg)\
      || Win::Dde.constants(false).inject(nil) {|res, const| arg == Win::Dde.const_get(const) ? res || const : res }\
      || arg
end