|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "json" |
| 7 | + |
| 8 | +module Async |
| 9 | + module Container |
| 10 | + module Supervisor |
| 11 | + class Connection |
| 12 | + class Call |
| 13 | + def initialize(connection, id, message) |
| 14 | + @connection = connection |
| 15 | + @id = id |
| 16 | + @message = message |
| 17 | + |
| 18 | + @queue = ::Thread::Queue.new |
| 19 | + end |
| 20 | + |
| 21 | + # @attribute [Connection] The connection that initiated the call. |
| 22 | + attr :connection |
| 23 | + |
| 24 | + # @attribute [Hash] The message that initiated the call. |
| 25 | + attr :message |
| 26 | + |
| 27 | + def [] key |
| 28 | + @message[key] |
| 29 | + end |
| 30 | + |
| 31 | + def push(**response) |
| 32 | + @queue.push(response) |
| 33 | + end |
| 34 | + |
| 35 | + def pop(...) |
| 36 | + @queue.pop(...) |
| 37 | + end |
| 38 | + |
| 39 | + def each(&block) |
| 40 | + while response = self.pop |
| 41 | + yield response |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + def finish(**response) |
| 46 | + self.push(id: @id, finished: true, **response) |
| 47 | + @queue.close |
| 48 | + end |
| 49 | + |
| 50 | + def closed? |
| 51 | + @queue.closed? |
| 52 | + end |
| 53 | + |
| 54 | + def self.dispatch(connection, target, id, message) |
| 55 | + Async do |
| 56 | + call = self.new(connection, id, message) |
| 57 | + connection.calls[id] = call |
| 58 | + |
| 59 | + target.dispatch(call) |
| 60 | + |
| 61 | + while response = call.pop |
| 62 | + connection.write(id: id, **response) |
| 63 | + end |
| 64 | + ensure |
| 65 | + # If the queue is closed, we don't need to send a finished message. |
| 66 | + unless call.closed? |
| 67 | + connection.write(id: id, finished: true) |
| 68 | + end |
| 69 | + |
| 70 | + connection.calls.delete(id) |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def self.call(connection, **message, &block) |
| 75 | + id = connection.next_id |
| 76 | + call = self.new(connection, id, message) |
| 77 | + |
| 78 | + connection.calls[id] = call |
| 79 | + connection.write(id: id, **message) |
| 80 | + |
| 81 | + if block_given? |
| 82 | + call.each(&block) |
| 83 | + else |
| 84 | + return call.pop |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def initialize(stream, id, **state) |
| 90 | + @stream = stream |
| 91 | + @state = state |
| 92 | + |
| 93 | + @calls = {} |
| 94 | + |
| 95 | + @id = id |
| 96 | + end |
| 97 | + |
| 98 | + # @attribute [Hash(Integer, Call)] Calls in progress. |
| 99 | + attr :calls |
| 100 | + |
| 101 | + # @attribute [Hash(Symbol, Object)] State associated with this connection, for example the process ID, etc. |
| 102 | + attr_accessor :state |
| 103 | + |
| 104 | + def next_id |
| 105 | + @id += 2 |
| 106 | + end |
| 107 | + |
| 108 | + def write(**message) |
| 109 | + @stream.write(JSON.dump(message) << "\n") |
| 110 | + @stream.flush |
| 111 | + end |
| 112 | + |
| 113 | + def call(timeout: nil, **message) |
| 114 | + id = next_id |
| 115 | + calls[id] = ::Thread::Queue.new |
| 116 | + |
| 117 | + write(id: id, **message) |
| 118 | + |
| 119 | + return calls[id].pop(timeout: timeout) |
| 120 | + ensure |
| 121 | + calls.delete(id) |
| 122 | + end |
| 123 | + |
| 124 | + def read |
| 125 | + if line = @stream&.gets |
| 126 | + JSON.parse(line, symbolize_names: true) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + def each |
| 131 | + while message = self.read |
| 132 | + yield message |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + def call(...) |
| 137 | + Call.call(self, ...) |
| 138 | + end |
| 139 | + |
| 140 | + def run(target) |
| 141 | + self.each do |message| |
| 142 | + if id = message[:id] |
| 143 | + if call = @calls[id] |
| 144 | + # Response to a call: |
| 145 | + call.push(**message) |
| 146 | + else |
| 147 | + # Incoming call: |
| 148 | + Call.dispatch(self, target, id, message) |
| 149 | + end |
| 150 | + else |
| 151 | + Console.error(self, "Unknown message:", message) |
| 152 | + end |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + def close |
| 157 | + if stream = @stream |
| 158 | + @stream = nil |
| 159 | + stream.close |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | +end |
0 commit comments