Module: TonSdk::Interop

Extended by:
FFI::Library
Defined in:
lib/ton_sdk_client/interop.rb

Defined Under Namespace

Modules: TcResponseCodes Classes: TcResponse, TcStringData

Constant Summary collapse

DEFAULT_LIB_NAME =
'tonclient'
@@request_counter =
Concurrent::AtomicFixnum.new(1)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/ton_sdk_client/interop.rb', line 11

def logger
  @logger
end

Class Method Details

.request_to_native_lib(ctx, function_name, function_params = nil, client_callback: nil, is_single_thread_only: true) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ton_sdk_client/interop.rb', line 132

def self.request_to_native_lib(
  ctx,
  function_name,
  function_params = nil,
  client_callback: nil,
  is_single_thread_only: true
)
  function_name_tc_str = TcStringData.from_string(function_name)
  function_params_json_str = function_params&.to_h&.to_json || ""
  function_params_json_tc_str = TcStringData.from_string(function_params_json_str)

  @sm = Concurrent::Semaphore.new(1)
  if is_single_thread_only == true
    @sm.acquire()
  end


  # using @@request_counter here to pass a @@request_counter and handlers and then retrieve them
  # is probably isn't needed.
  # Thanks to the way Ruby is, the same affect can be achieved by a block which is an easier way.
  # Nonetheless, @@request_counter is incremented with each request and then sent out to a server
  # in order to keep a server happy,
  # because otherwise a server will, probably, reply in a wrong way.

  self.tc_request(
    ctx,
    function_name_tc_str,
    function_params_json_tc_str,
    @@request_counter.value
  ) do |req_id, params_json, response_type, is_finished|

    tc_data_json_content = if params_json[:len] > 0
      res = params_json[:content].read_string(params_json[:len])
      JSON.parse(res)
    else
      ''
    end

    begin
      case response_type

      when TcResponseCodes::SUCCESS
        if block_given?
          yield NativeLibResponseResult.new(result: tc_data_json_content)
        end
  
      when TcResponseCodes::ERROR
        if block_given?
          yield NativeLibResponseResult.new(error: tc_data_json_content)
        end

      when TcResponseCodes::NOP
        nil

      when TcResponseCodes::APP_REQUEST
        if !client_callback.nil?
          client_callback.call(:request, tc_data_json_content)
        end

      when TcResponseCodes::APP_NOTIFY
        if !client_callback.nil?
          client_callback.call(:notify, tc_data_json_content)
        end

      when TcResponseCodes::CUSTOM..TcResponseCodes::MAX
        if !client_callback.nil?
          client_callback.call(:custom, tc_data_json_content)
        end

      else
        raise ArgumentError.new("unsupported response type: #{response_type}")
      end

    rescue => e
      @logger.error(e)
    ensure
      if is_single_thread_only == true
        @sm.release()
      end
    end
  end



  if is_single_thread_only == true
    @sm.acquire()
  end

  @@request_counter.increment()
  nil
end

.request_to_native_lib_sync(ctx, function_name, function_params_json) ⇒ Object



224
225
226
227
228
# File 'lib/ton_sdk_client/interop.rb', line 224

def self.request_to_native_lib_sync(ctx, function_name, function_params_json)
  function_name_tc_str = TcStringData.from_string(function_name)
  function_params_json_tc_str = TcStringData.from_string(function_params_json)
  self.tc_request_sync(ctx, function_name_tc_str, function_params_json_tc_str)
end