Class: Async::Container::Notify::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/async/container/notify/server.rb

Overview

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

Defined Under Namespace

Classes: Context

Constant Summary collapse

MAXIMUM_MESSAGE_SIZE =
4096

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Server

Initialize the server with the given path.



64
65
66
# File 'lib/async/container/notify/server.rb', line 64

def initialize(path)
	@path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



69
70
71
# File 'lib/async/container/notify/server.rb', line 69

def path
  @path
end

#The path to the UNIX socket.(pathtotheUNIXsocket.) ⇒ Object (readonly)



69
# File 'lib/async/container/notify/server.rb', line 69

attr :path

Class Method Details

.generate_pathObject

Generate a new unique path for the UNIX socket.



49
50
51
52
53
54
# File 'lib/async/container/notify/server.rb', line 49

def self.generate_path
	File.expand_path(
		"async-container-#{::Process.pid}-#{SecureRandom.hex(8)}.ipc",
		Dir.tmpdir
	)
end

.load(message) ⇒ Object

Parse a message, according to the ‘sd_notify` protocol.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/async/container/notify/server.rb', line 22

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

.open(path = self.generate_path) ⇒ Object

Open a new server instance with a temporary and unique path.



57
58
59
# File 'lib/async/container/notify/server.rb', line 57

def self.open(path = self.generate_path)
	self.new(path)
end

Instance Method Details

#bindObject

Generate a bound context for receiving messages.



74
75
76
# File 'lib/async/container/notify/server.rb', line 74

def bind
	Context.new(@path)
end