class Async::Container::Notify::Server

A simple UDP server that can be used to receive messages from a child process, tracking readiness, status changes, etc.

def self.generate_path

@returns [String] The path for the UNIX socket.

Generate a new unique path for the UNIX socket.
def self.generate_path
	File.expand_path(
		"async-container-#{::Process.pid}-#{SecureRandom.hex(8)}.ipc",
		Dir.tmpdir
	)
end

def self.load(message)

@returns [Hash] The parsed message.
@parameter message [String] The message to parse.

Parse a message, according to the `sd_notify` protocol.
def self.load(message)
	lines = message.split("\n")
	
	lines.pop if lines.last == ""
	
	pairs = lines.map do |line|
		key, value = line.split("=", 2)
		
		key = key.downcase.to_sym
		
		if value == "0"
			value = false
		elsif value == "1"
			value = true
		elsif key == :errno and value =~ /\A\-?\d+\z/
			value = Integer(value)
		end
		
		next [key, value]
	end
	
	return Hash[pairs]
end

def self.open(path = self.generate_path)

Open a new server instance with a temporary and unique path.
def self.open(path = self.generate_path)
	self.new(path)
end

def bind

@returns [Context] The bound context.

Generate a bound context for receiving messages.
def bind
	Context.new(@path)
end

def initialize(path)

@parameter path [String] The path to the UNIX socket.

Initialize the server with the given path.
def initialize(path)
	@path = path
end