Module: TonClient::TonBinding

Extended by:
FFI::Library
Defined in:
lib/ton-client-ruby/Binding/struct.rb,
lib/ton-client-ruby/Binding/binding.rb,
lib/ton-client-ruby/Binding/binding.rb

Defined Under Namespace

Classes: Response, TcStringDataT, TcStringHandleT

Constant Summary collapse

@@request_id =
0
@@requests =
{}

Class Method Summary collapse

Class Method Details

.delete_request(id) ⇒ Object



44
45
46
# File 'lib/ton-client-ruby/Binding/binding.rb', line 44

def self.delete_request(id)
  @@requests[id] = nil
end

.generate_request_idObject



35
36
37
38
# File 'lib/ton-client-ruby/Binding/binding.rb', line 35

def self.generate_request_id
  @@request_id = 0 if @@request_id == 4294967295
  @@request_id += 1
end

.get_request(id) ⇒ Object



40
41
42
# File 'lib/ton-client-ruby/Binding/binding.rb', line 40

def self.get_request(id)
  @@requests[id]
end

.make_string(string) ⇒ Object



118
119
120
121
122
123
# File 'lib/ton-client-ruby/Binding/binding.rb', line 118

def self.make_string(string)
  result = TonBinding::TcStringDataT.new
  result[:content] = FFI::MemoryPointer.from_string(string)
  result[:len] = string.bytesize
  result
end

.read_string(tc_string_handle) ⇒ Object

if string.address > 1

  string = string[:content].read_string(string[:len]).force_encoding('UTF-8') + ''
  tc_destroy_string(tc_string_handle) if is_ref
  return string
end
nil

end



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ton-client-ruby/Binding/binding.rb', line 141

def self.read_string(tc_string_handle)
  is_ref = tc_string_handle.class == FFI::Pointer
  if is_ref
    string = tc_read_string(tc_string_handle)
  else
    string = tc_string_handle
  end

  if string[:content].address > 1
    string = string[:content].read_string(string[:len]).force_encoding('UTF-8') + ''
    tc_destroy_string(tc_string_handle) if is_ref
    return string
  end
  nil
end

.read_string_to_hash(tc_string_handle_t_ref) ⇒ Object



157
158
159
160
# File 'lib/ton-client-ruby/Binding/binding.rb', line 157

def self.read_string_to_hash(tc_string_handle_t_ref)
  json_string = read_string(tc_string_handle_t_ref)
  JSON.parse(json_string, {max_nesting: false}) if json_string
end

.requestLibrary(context: 1, method_name: '', payload: {}, &block) ⇒ Object

block = { |response| }



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ton-client-ruby/Binding/binding.rb', line 188

def self.requestLibrary(context: 1, method_name: '', payload: {}, &block)
  request_id = generate_request_id
  set_request(request_id, &block)
  send_request(context: context, method_name: method_name, payload: payload, request_id: request_id) do |request_id, string_data, response_type, finished|
    if get_request(request_id)
      response = Response.new
      response.update(request_id, string_data, response_type, finished)
      get_request(request_id).call(response)
      delete_request(request_id) if finished
    end
  end
end

.send_request(context: 1, method_name: '', payload: {}, request_id: 1, &block) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/ton-client-ruby/Binding/binding.rb', line 176

def self.send_request(context: 1, method_name: '', payload: {}, request_id: 1, &block)
  raise 'context not found' unless context
  raise 'method_name is empty' if method_name.empty?

  if block
    method_name_string = make_string(method_name)
    payload_string = make_string(payload.to_json)
    tc_request(context, method_name_string, payload_string, request_id, &block)
  end
end

.send_request_sync(context: 1, method_name: '', payload: {}) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ton-client-ruby/Binding/binding.rb', line 162

def self.send_request_sync(context: 1, method_name: '', payload: {})
  raise 'context not found' unless context
  raise 'method_name is empty' if method_name.empty?

  method_name_string = make_string(method_name)
  payload_string = make_string(payload.to_json)

  sdk_json_response = tc_request_sync(context, method_name_string, payload_string)
  response = read_string_to_hash(sdk_json_response)
  
  return response['result'] if response['result']
  return response['error'] if response['error']
end

.set_request(id, &request_block) ⇒ Object



48
49
50
# File 'lib/ton-client-ruby/Binding/binding.rb', line 48

def self.set_request(id, &request_block)
  @@requests[id] = request_block
end

.setup_bindingsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ton-client-ruby/Binding/binding.rb', line 72

def self.setup_bindings

  # tc_string_handle_t* tc_create_context(tc_string_data_t config);
  # attach_function :tc_create_context, [TcStringDataT.by_value], TcStringHandleT.by_ref
  attach_function :tc_create_context, [TcStringDataT.by_value], :pointer

  # fn tc_destroy_context(context: InteropContext)
  attach_function :tc_destroy_context, [:uint32], :void

  # tc_string_handle_t* tc_request_sync(
  #   uint32_t context,
  #   tc_string_data_t function_name,
  #   tc_string_data_t function_params_json);
  # attach_function :tc_request_sync, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value], TcStringHandleT.by_ref
  attach_function :tc_request_sync, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value], :pointer

  # enum tc_response_types_t {
  #     tc_response_success = 0,
  #     tc_response_error = 1,
  #     tc_response_nop = 2,
  #     tc_response_custom = 100,
  # };
  # typedef void (*tc_response_handler_t)(
  #   uint32_t request_id,
  #   tc_string_data_t params_json,
  #   uint32_t response_type,
  #   bool finished);
  callback :tc_response_handler_t, [:uint32, TcStringDataT.by_value, :uint32, :bool], :void

  # void tc_request(
  #   uint32_t context,
  #   tc_string_data_t function_name,
  #   tc_string_data_t function_params_json,
  #   uint32_t request_id,
  #   tc_response_handler_t response_handler);
  attach_function :tc_request, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value, :uint32, :tc_response_handler_t], :void

  # tc_string_data_t tc_read_string(const tc_string_handle_t* handle);
  # attach_function :tc_read_string, [TcStringHandleT.by_ref], TcStringDataT.by_value
  attach_function :tc_read_string, [:pointer], TcStringDataT.by_value

  # void tc_destroy_string(const tc_string_handle_t* handle)
  # attach_function :tc_destroy_string, [TcStringHandleT.by_ref], :void
  attach_function :tc_destroy_string, [:pointer], :void
end