Class: Universa::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/universa/client.rb

Overview

The connection to a single Universa node.

Instance Method Summary collapse

Constructor Details

#initialize(umi_client) ⇒ Connection



152
153
154
# File 'lib/universa/client.rb', line 152

def initialize umi_client
  @client = umi_client
end

Instance Method Details

#get_state(x) ⇒ ContractState

Get contract or hashId state from this single node

Parameters:

Returns:



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/universa/client.rb', line 206

def get_state x
  id = case x
         when HashId
           x
         when Contract
           x.hash_id
         else
           raise ArgumentError, "bad argument, want Contract or HashId"
       end
  ContractState.new(@client.getState(id))
end

#inspectObject



218
219
220
# File 'lib/universa/client.rb', line 218

def inspect
  "<Universa::Connection:#{url}"
end

#node_numberObject



160
161
162
# File 'lib/universa/client.rb', line 160

def node_number
  @node_number ||= @client.getNodeNumber()
end

#pingObject

Check the connected node is alive. It is adivesd to call #restart on nodes that return false on pings to reestablish connection.

Returns:

  • true if it is ok



178
179
180
# File 'lib/universa/client.rb', line 178

def ping
  @client.ping
end

#ping_node(node_number, timeout: 5000) ⇒ Hash

ping another node from this one

Parameters:

  • node_number (Numeric)

    to ping

  • timeout (Numeric) (defaults to: 5000)

Returns:

  • (Hash)

    hashie with TCP and UDP fields holding ping time in millis, -1 if not available



170
171
172
# File 'lib/universa/client.rb', line 170

def ping_node(node_number, timeout: 5000)
  Hashie::Mash.new(@client.pingNode(node_number, timeout).to_h)
end

#register_single(contract, timeout = 25) ⇒ ContractState

Register a single contract (on private network or if you have white key allowing free operations) with the current node. Client must check returned contract state. It requires “open” network or special key that has a right to register contracts without payment.

Parameters:

Returns:



199
200
201
# File 'lib/universa/client.rb', line 199

def register_single(contract, timeout = 25)
  ContractState.new(@client.register(contract.packed, timeout * 1000))
end

#restartObject

Attempt to reestablish connection to the node



183
184
185
# File 'lib/universa/client.rb', line 183

def restart
  @client.restart
end

#to_sObject



222
223
224
# File 'lib/universa/client.rb', line 222

def to_s
  inspect
end

#umi_clientObject



156
157
158
# File 'lib/universa/client.rb', line 156

def umi_client
  @client
end

#urlObject

node url (IP-based)



188
189
190
# File 'lib/universa/client.rb', line 188

def url
  @url ||= @client.get_url
end