Class: Zold::Entrance

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/node/entrance.rb

Overview

The entrance

Instance Method Summary collapse

Constructor Details

#initialize(wallets, remotes, copies, address, log: Log::Quiet.new) ⇒ Entrance

Returns a new instance of Entrance.


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zold/node/entrance.rb', line 38

def initialize(wallets, remotes, copies, address, log: Log::Quiet.new)
  raise 'Wallets can\'t be nil' if wallets.nil?
  raise 'Wallets must be of type Wallets' unless wallets.is_a?(Wallets)
  @wallets = wallets
  raise 'Remotes can\'t be nil' if remotes.nil?
  raise "Remotes must be of type Remotes: #{remotes.class.name}" unless remotes.is_a?(Remotes)
  @remotes = remotes
  raise 'Copies can\'t be nil' if copies.nil?
  @copies = copies
  raise 'Address can\'t be nil' if address.nil?
  @address = address
  raise 'Log can\'t be nil' if log.nil?
  @log = log
  @history = []
  @mutex = Mutex.new
end

Instance Method Details

#push(id, body) ⇒ Object

Returns a list of modifed wallets (as Zold::Id)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/zold/node/entrance.rb', line 66

def push(id, body)
  raise 'Id can\'t be nil' if id.nil?
  raise 'Id must be of type Id' unless id.is_a?(Id)
  raise 'Body can\'t be nil' if body.nil?
  start = Time.now
  copies = Copies.new(File.join(@copies, id.to_s))
  localhost = '0.0.0.0'
  copies.add(body, localhost, Remotes::PORT, 0)
  unless @remotes.all.empty?
    Fetch.new(
      wallets: @wallets, remotes: @remotes, copies: copies.root, log: @log
    ).run(['fetch', id.to_s, "--ignore-node=#{@address}"])
  end
  modified = Merge.new(
    wallets: @wallets, copies: copies.root, log: @log
  ).run(['merge', id.to_s, '--no-baseline'])
  Clean.new(wallets: @wallets, copies: copies.root, log: @log).run(['clean', id.to_s])
  copies.remove(localhost, Remotes::PORT)
  sec = (Time.now - start).round(2)
  if modified.empty?
    @log.info("Accepted #{id} in #{sec}s and not modified anything")
  else
    @log.info("Accepted #{id} in #{sec}s and modified #{modified.join(', ')}")
  end
  @mutex.synchronize do
    @history.shift if @history.length > 16
    @history << "#{id}/#{sec}/#{modified.count}"
  end
  modified
end

#start {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:


55
56
57
# File 'lib/zold/node/entrance.rb', line 55

def start
  yield(self)
end

#to_jsonObject


59
60
61
62
63
# File 'lib/zold/node/entrance.rb', line 59

def to_json
  {
    history: @history.join(', ')
  }
end