Sha256: cf78318954c3068ee3495a11740f9b3303e494f664721708336790d05931a1e0

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

#
# Helpers
#
module Vesta
    class Helpers

        Thread.abort_on_exception = true

        # Print banner
        def self.banner
            "\t\tRunning Vesta v#{Vesta::VERSION}"
        end

        # Do something in every x seconds
        def every(seconds)
            Thread.new do
                loop do
                    sleep seconds
                    yield
                end
            end
        end

        # Root folder
        def self.root
            "#{File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )}"
        end
        
        # Add peer to peer object [peers_object]
        def add_peer(peer,host,port,public_key) 
            peer.push({host: host, port: port, public_key: public_key})
            peer
        end

        # Add message to [messages_object]
        def add_message(messages_object, from, message)
            messages_object.push({from: from, message: message})
            messages_object
        end

        # Print out the status
        def render_state
            puts "Node is running on " + Service::LHOST.to_s.magenta + ':'.colorize(:background => :white, :color => :magenta).bold + Service::LPORT.to_s.magenta.bold
            print "My Peers: ".upcase
            $PEERS.each do |peer|
                print "#{peer[:host]}:#{peer[:port]}".yellow.bold + ', '
            end
        end

        # Communicate with peers 
        def communicate(index,host,port)
            communicate_response = Client.communicate(host, port, YAML.dump($PEERS))
            parsed_response = YAML.load(communicate_response)
            new_peer = parsed_response['peers']
            update_peers(new_peer)
        rescue Faraday::ConnectionFailed
            $PEERS.delete_at(index)
        end

        # Update peers
        def update_peers(new_peers)
          # if !new_peers.nil? # if new peers are not nil, then append them to $PEERS
            $PEERS = $PEERS | new_peers
            $PEERS.uniq
          # end
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vesta-chat-0.1.2 lib/vesta/helpers.rb
vesta-chat-0.1.1 lib/vesta/helpers.rb