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



143
144
145
# File 'lib/universa/client.rb', line 143

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:



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/universa/client.rb', line 197

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



209
210
211
# File 'lib/universa/client.rb', line 209

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

#node_numberObject



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

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



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

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



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

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:



190
191
192
# File 'lib/universa/client.rb', line 190

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

#restartObject

Attempt to reestablish connection to the node



174
175
176
# File 'lib/universa/client.rb', line 174

def restart
  @client.restart
end

#to_sObject



213
214
215
# File 'lib/universa/client.rb', line 213

def to_s
  inspect
end

#umi_clientObject



147
148
149
# File 'lib/universa/client.rb', line 147

def umi_client
  @client
end

#urlObject

node url (IP-based)



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

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