buschtelefon 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f97ac74a59e40f443954b18c858d246268a48a0f
4
- data.tar.gz: f417f27ecc8f99c09de25b25a69d85dab46dba03
3
+ metadata.gz: 647eca794f57af404842ca0e5807e7c9ef57bcd8
4
+ data.tar.gz: 05b6225fbcb926ad2e286dbe0904ea6b322af450
5
5
  SHA512:
6
- metadata.gz: 2c685fb7c0adbb17d41cea04bdef2c5f94f50ad9bfb5ce63e8886493251e0bdd096514232138da92691f6581c9807e4256dfa9c34c09d24c8961ef769eab6619
7
- data.tar.gz: 83ae7243238be220fd6dc57428df6961f3859388999e0afcec3a3d024c0df33d654af770c80e286371aeb3c3921634acf82348cd78f0cf581f77a52bd8281de6
6
+ metadata.gz: 6ff6c5e1dc9e2aa907f7ec47eef2b2246dbf1ced27ddd7050693527dbb4121d0966092cccebeb22410626ecb0f617198d31a0346b94a910dc61ceb5f48492e58
7
+ data.tar.gz: 571f2b5304c578859118b149c73ed3b72a8710ab212d5a5ca318d7823364a1d2827db4e7b680129c12c346de70f4f028b2c773871821f336cf9b593aeb69e2d6
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ * `NetTattler` now sends inquired payload back to requester
6
+ * `NetTattler` now attaches source information to gossip processor
7
+ * `NetTattler` now listens on a system chosen port per default
8
+ * `NetTattler` now connects to remotes via `connect_remote` and sends packets
9
+ to them via its outbound port (so it can receive responses)
10
+ * We work with IPs now instead of host names
11
+ * Default `Brain` capacity is now infinite
12
+ * `Tattler` now can load messages directly into its brain
13
+
3
14
  ## 0.3.0
4
15
 
5
16
  * `NetTattler` now yields a `Gossip` instead of a raw message string.
data/README.md CHANGED
@@ -40,7 +40,7 @@ include Buschtelefon
40
40
 
41
41
  aunt_may = NetTattler.new
42
42
  aunt_ruth = NetTattler.new
43
- remote_aunt_ruth = RemoteTattler.new(host: 'localhost', port: aunt_ruth.port)
43
+ remote_aunt_ruth = RemoteTattler.new(host: '127.0.0.1', port: aunt_ruth.port)
44
44
 
45
45
  aunt_may.connect(remote_aunt_ruth)
46
46
 
data/bin/run CHANGED
@@ -36,13 +36,19 @@ tattlers[:E].connect(tattlers[:D])
36
36
  puts 'Feeding locals'
37
37
  tattlers[:A].feed(Gossip.new('Tezos'))
38
38
 
39
- josua = NetTattler.new
40
39
  simon = NetTattler.new
41
- remote_simon = RemoteTattler.new(host: 'localhost', port: simon.port)
42
-
43
- josua.connect(remote_simon)
40
+ josua = NetTattler.new
41
+ remote_simon = josua.connect_remote(host: '127.0.0.1', port: simon.port)
44
42
 
45
- Thread.new { josua.listen }
46
- Thread.new { simon.listen }
43
+ threads = [
44
+ Thread.new { josua.listen { |gossip, source| puts "I'm Josua, I got \"#{gossip.message}\" from #{source}" } },
45
+ Thread.new { simon.listen { |gossip, source| puts "I'm Simon, I got \"#{gossip.message}\" from #{source}" } }
46
+ ]
47
47
 
48
+ sleep(0.1)
48
49
  remote_simon.feed(Gossip.new('Antshare'))
50
+ sleep(0.1)
51
+ remote_simon.inquire
52
+ sleep(0.1)
53
+
54
+ threads.each(&:exit)
@@ -2,7 +2,7 @@ module Buschtelefon
2
2
  class Brain
3
3
  attr_reader :capacity
4
4
 
5
- def initialize(capacity = 100)
5
+ def initialize(capacity = nil)
6
6
  @capacity = capacity
7
7
  @gossip_sink = []
8
8
  end
@@ -25,7 +25,9 @@ module Buschtelefon
25
25
  def reorganize
26
26
  @gossip_sink.sort! { |x, y| y.created_at <=> x.created_at }
27
27
  @gossip_sink.uniq!(&:message)
28
- @gossip_sink.slice!(capacity..-1) # only keep the newest
28
+ if @capacity
29
+ @gossip_sink.slice!(capacity..-1) # only keep the newest
30
+ end
29
31
  end
30
32
  end
31
33
  end
@@ -3,36 +3,70 @@ require_relative 'tattler'
3
3
 
4
4
  module Buschtelefon
5
5
  class NetTattler < Tattler
6
- attr_accessor :port
6
+ attr_accessor :port, :socket
7
7
 
8
- def initialize(port: nil)
8
+ def initialize(port: 0)
9
9
  super()
10
- @port = port || rand(1025..65365) # TODO: use port 0
10
+ @socket = UDPSocket.new
11
+ @socket.bind('127.0.0.1', port)
12
+ @port = @socket.local_address.ip_port
11
13
  end
12
14
 
13
15
  def listen(&_callback)
14
16
  puts "Started UDP server on #{@port}..."
15
17
 
16
- Socket.udp_server_loop(@port) do |message, message_source|
17
- # puts "Got \"#{message}\" from #{message_source}"
18
+ Socket.udp_server_loop_on([@socket]) do |message, message_source|
19
+ remote_tattler = find_or_build_remote_tattler(
20
+ host: message_source.remote_address.ip_address,
21
+ port: message_source.remote_address.ip_port
22
+ )
23
+
18
24
  if message == "\x05"
19
- handle_knowledge_inquiry(message_source)
25
+ #puts "#{@port} got inquiry from #{remote_tattler}. Is connected to #{@connections.inspect}"
26
+ handle_knowledge_inquiry(remote_tattler)
20
27
  else
21
28
  gossip = Gossip.new(message)
22
29
  handle_incoming_gossip(gossip)
23
- yield(gossip) if block_given?
30
+ yield(gossip, remote_tattler) if block_given?
24
31
  end
25
32
  end
26
33
  end
27
34
 
35
+ def connect_remote(host:, port:)
36
+ find_or_build_remote_tattler(host: host, port: port).tap do |remote_tattler|
37
+ connect(remote_tattler)
38
+ end
39
+ end
40
+
41
+ def inquire_remote_neighbors
42
+ remote_connections.each { |remote_tattler| remote_tattler.inquire }
43
+ end
44
+
45
+ def remote_connections
46
+ @connections.select { |tattler| tattler.is_a?(RemoteTattler) }
47
+ end
48
+
49
+ def to_s
50
+ "127.0.0.1:#{@port}"
51
+ end
52
+
28
53
  private
29
54
 
30
55
  def handle_incoming_gossip(gossip)
31
56
  feed(gossip)
32
57
  end
33
58
 
34
- def handle_knowledge_inquiry(message_source)
35
- transfer_knowledge(RemoteTattler.new(message_source.local_address))
59
+ # We just give out everything to the outbound port of the inquiry source
60
+ def handle_knowledge_inquiry(remote_tattler)
61
+ transfer_knowledge(remote_tattler)
62
+ end
63
+
64
+ def find_or_build_remote_tattler(host:, port:)
65
+ remote_connections.find { |t| t.host == host && t.port == port } || build_remote_tattler(host: host, port: port)
66
+ end
67
+
68
+ def build_remote_tattler(host:, port:)
69
+ RemoteTattler.new(host: host, port: port, outbound_socket: @socket)
36
70
  end
37
71
  end
38
72
  end
@@ -1,17 +1,27 @@
1
1
  require 'socket'
2
2
 
3
3
  module Buschtelefon
4
- # No need to inherit from Tattler in Ruby
4
+ # No need to inheritance from Tattler because not all its features are available here (only #feed)
5
5
  class RemoteTattler
6
- def initialize(host:, port:)
6
+ attr_reader :host, :port
7
+
8
+ def initialize(host:, port:, outbound_socket: UDPSocket.new)
7
9
  @host = host
8
10
  @port = port
9
- @outbound_socket = UDPSocket.new
11
+ @outbound_socket = outbound_socket
10
12
  end
11
13
 
12
14
  def feed(gossip)
15
+ #puts "#{@outbound_socket.local_address.ip_port} sending #{JSON.parse(gossip.message)['number']} to #{@port}"
13
16
  @outbound_socket.send(gossip.message, 0, @host, @port)
14
- # puts "Sent \"#{gossip.message}\" to #{@host}:#{@port}"
17
+ end
18
+
19
+ def inquire
20
+ @outbound_socket.send("\x05", 0, @host, @port)
21
+ end
22
+
23
+ def to_s
24
+ "#{@host}:#{@port}"
15
25
  end
16
26
  end
17
27
  end
@@ -7,6 +7,10 @@ module Buschtelefon
7
7
  @brain = Brain.new
8
8
  end
9
9
 
10
+ def load_messages(messages)
11
+ messages.each { |message| @brain << Gossip.new(message) }
12
+ end
13
+
10
14
  def knowledge
11
15
  @brain.to_a
12
16
  end
@@ -1,3 +1,3 @@
1
1
  module Buschtelefon
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buschtelefon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josua Schmid
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-11 00:00:00.000000000 Z
11
+ date: 2019-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler