mirror of
https://github.com/TimeCrafters/timecrafters_configuration_tool_desktop.git
synced 2025-12-15 21:32:35 +00:00
70 lines
1.7 KiB
Ruby
70 lines
1.7 KiB
Ruby
module TAC
|
|
class TACNET
|
|
class PacketHandler
|
|
TAG = "TACNET|PacketHandler"
|
|
def initialize(host_is_a_connection: false)
|
|
@host_is_a_connection = host_is_a_connection
|
|
end
|
|
|
|
def handle(message)
|
|
packet = Packet.from_stream(message)
|
|
|
|
if packet
|
|
hand_off(packet)
|
|
else
|
|
log.d(TAG, "Rejected raw packet: #{message}")
|
|
end
|
|
end
|
|
|
|
def hand_off(packet)
|
|
case packet.type
|
|
when :handshake
|
|
handle_handshake(packet)
|
|
when :heartbeat
|
|
handle_heartbeat(packet)
|
|
when :dump_config
|
|
handle_dump_config(packet)
|
|
else
|
|
log.d(TAG, "No hand off available for packet type: #{packet.type}")
|
|
end
|
|
end
|
|
|
|
def handle_handshake(packet)
|
|
if @host_is_a_connection
|
|
$window.backend.tacnet.client.uuid = packet.body
|
|
end
|
|
end
|
|
|
|
# TODO: Reset socket timeout
|
|
def handle_heartbeat(packet)
|
|
end
|
|
|
|
def handle_dump_config(packet)
|
|
begin
|
|
hash = JSON.parse(packet.body)
|
|
|
|
if @host_is_a_connection
|
|
File.open("#{TAC::ROOT_PATH}/data/config.json", "w") { |f| f.write packet.body }
|
|
|
|
$window.backend.update_config
|
|
end
|
|
rescue JSON::ParserError
|
|
end
|
|
end
|
|
|
|
def self.packet_handshake(client_uuid)
|
|
Packet.create(Packet::PACKET_TYPES[:handshake], client_uuid)
|
|
end
|
|
|
|
def self.packet_heartbeat
|
|
Packet.create(Packet::PACKET_TYPES[:heartbeat], Packet::PROTOCOL_HEARTBEAT)
|
|
end
|
|
|
|
def self.packet_dump_config(string)
|
|
string = string.gsub("\n", " ")
|
|
|
|
Packet.create(Packet::PACKET_TYPES[:dump_config], string)
|
|
end
|
|
end
|
|
end
|
|
end |