class Async::Container::Notify::Socket

See <www.freedesktop.org/software/systemd/man/sd_notify.html> for more details of the underlying protocol.
Implements the systemd NOTIFY_SOCKET process readiness protocol.

def self.open!(environment = ENV)

Open a notification client attached to the current {NOTIFY_SOCKET} if possible.
def self.open!(environment = ENV)
	if path = environment.delete(NOTIFY_SOCKET)
		self.new(path)
	end
end

def dump(message)

@parameter message [Hash] Keys and values should be string convertible objects. Values which are `true`/`false` are converted to `1`/`0` respectively.
Dump a message in the format requied by `sd_notify`.
def dump(message)
	buffer = String.new
	
	message.each do |key, value|
		# Conversions required by NOTIFY_SOCKET specifications:
		if value == true
			value = 1
		elsif value == false
			value = 0
		end
		
		buffer << "#{key.to_s.upcase}=#{value}\n"
	end
	
	return buffer
end

def error!(text, **message)

`sd_notify` requires an `errno` key, which defaults to `-1` to indicate a generic error.
Send the specified error.
def error!(text, **message)
	message[:errno] ||= -1
	
	super
end

def initialize(path)

@parameter path [String] The path to the UNIX socket used for sending messages to the process manager.
Initialize the notification client.
def initialize(path)
	@path = path
	@address = Addrinfo.unix(path, ::Socket::SOCK_DGRAM)
end

def send(**message)

@parameter message [Hash]
Send the given message.
def send(**message)
	data = dump(message)
	
	if data.bytesize > MAXIMUM_MESSAGE_SIZE
		raise ArgumentError, "Message length #{data.bytesize} exceeds #{MAXIMUM_MESSAGE_SIZE}: #{message.inspect}"
	end
	
	@address.connect do |peer|
		peer.sendmsg(data)
	end
end