-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rb
38 lines (31 loc) · 951 Bytes
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'sinatra'
require 'colorize'
require_relative 'helpers'
PORT, *PEER_PORTS = ARGV
set :port, PORT
$messages = {}
every(seconds: 10) do
peer_message_history = gossip_with_peers(PEER_PORTS.sample)
update_messages_history(peer_message_history)
end
post '/message' do
receive_message(params[:message], params[:from_port])
end
get '/messages_history' do
JSON.dump($messages)
end
def receive_message(message, from_port)
return if message.nil? || message.empty?
$messages[timestamp] = message
puts "Received message '#{message}' from #{from_port}...".colorize(:blue)
end
def update_messages_history(messages)
parsed_messages = JSON.parse(messages)
parsed_messages.each do |k, v|
next if $messages.key?(k)
puts 'Updating message history....'.colorize(:yellow)
$messages[k] = v
puts '...Message history updated!'.colorize(:green)
end
puts JSON.dump($messages).colorize(:magenta) if $messages != messages
end