Skip to content

Commit

Permalink
Add logger into discovery service
Browse files Browse the repository at this point in the history
Also add checking for the correct discovery message to be received
  • Loading branch information
sergio1990 committed Feb 2, 2023
1 parent 14a0f26 commit 4b15b2f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/alpaca_device/discovery_service.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
# frozen_string_literal: true

require 'socket'
require 'logger'

module AlpacaDevice
class DiscoveryService
def initialize(configuration:)
@discovery_host = configuration.discovery_host
@discovery_port = configuration.discovery_port
@alpaca_port = configuration.alpaca_port

@logger = Logger.new(STDOUT)
@logger.level = :info
end

def run
server = UDPSocket.new
server.bind(discovery_host, discovery_port)

logger.info('DiscoveryService') { "Discovery service started at #{discovery_host}:#{discovery_port}..." }

loop do
text, sender = server.recvfrom(24)
remote_port = sender[1]
remote_host = sender[3]
server.send "{\"alpacaport\": #{alpaca_port}}", 0, remote_host, remote_port

logger.info('DiscoveryService') { "Received message '#{text}' from #{remote_host}:#{remote_port}" }

if text == 'alpacadiscovery1'
server.send "{\"alpacaport\": #{alpaca_port}}", 0, remote_host, remote_port
end
end

server.close
end

private

attr_reader :discovery_host, :discovery_port, :alpaca_port
attr_reader :discovery_host, :discovery_port, :alpaca_port, :logger
end
end
2 changes: 1 addition & 1 deletion lib/alpaca_device/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module AlpacaDevice
VERSION = "0.1.1"
VERSION = '0.1.2'
end

0 comments on commit 4b15b2f

Please sign in to comment.