Module: Przn::AudienceLink

Defined in:
lib/przn/audience_link.rb

Overview

Tiny line-delimited JSON protocol over a Unix-domain socket, joining the presenter and audience ‘przn` processes in extended-display mode.

Messages currently exchanged:

{"type": "ready"}                  audience -> presenter
{"type": "goto", "index": N}       presenter -> audience
{"type": "quit"}                   presenter -> audience

Class Method Summary collapse

Class Method Details

.connect(path) ⇒ Object

Presenter-side: connect to an audience socket at ‘path` and return a client object that responds to `#send` and `#close`. Caller drives the protocol from the controller.



41
42
43
# File 'lib/przn/audience_link.rb', line 41

def connect(path)
  UNIXSocket.new(path)
end

.send(io, msg) ⇒ Object



45
46
47
48
49
# File 'lib/przn/audience_link.rb', line 45

def send(io, msg)
  io.puts(JSON.generate(msg))
rescue Errno::EPIPE, IOError
  # Other side hung up — caller decides whether to keep going.
end

.serve(path) ⇒ Object

Audience-side: open a UNIXServer at ‘path`, wait for the presenter to connect, then yield each decoded message until EOF or “type”:“quit”. The socket file is unlinked on exit.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/przn/audience_link.rb', line 20

def serve(path)
  File.unlink(path) if File.exist?(path)
  server = UNIXServer.new(path)
  client = server.accept
  send(client, {type: "ready"})
  while (line = client.gets)
    msg = JSON.parse(line.chomp, symbolize_names: true)
    break if msg[:type] == "quit"
    yield msg
  end
rescue Errno::EPIPE, EOFError, IOError
  # Presenter went away; let the caller exit cleanly.
ensure
  client&.close
  server&.close
  File.unlink(path) if path && File.exist?(path)
end