|
| 1 | +# Controllers |
| 2 | + |
| 3 | +This guide explains how to use controllers in `async-bus` to build explicit remote interfaces with pass-by-reference semantics, enabling bidirectional communication and shared state across connections. |
| 4 | + |
| 5 | +## Why Controllers? |
| 6 | + |
| 7 | +While any object can be bound and proxied directly (like `Array` or `Hash`), controllers provide important advantages: |
| 8 | + |
| 9 | +1. **Pass by Reference**: Controllers are always passed by reference when serialized as arguments or return values, enabling bidirectional communication. |
| 10 | +2. **Explicit Interface**: Controllers wrap objects with a well-defined interface, making the remote API clear and preventing confusion about what methods are available. |
| 11 | +3. **Automatic Proxying**: When controllers are registered as reference types, they are automatically proxied when serialized, enabling chaining and composition. |
| 12 | + |
| 13 | +## Pass by Reference vs Pass by Value |
| 14 | + |
| 15 | +The key difference between controllers and regular objects: |
| 16 | + |
| 17 | +- **Controllers**: Passed by reference - when serialized as arguments or return values, both sides share the same object. |
| 18 | +- **Other objects**: Copied by value - when serialized, each side gets its own copy. |
| 19 | + |
| 20 | +Note that when you bind an object directly (like `connection.bind(:items, array)`), clients can still access it via a proxy (`connection[:items]`). The difference only matters when objects are serialized as arguments or return values (or as a part thereof). |
| 21 | + |
| 22 | +## Creating Controllers |
| 23 | + |
| 24 | +Controllers inherit from {ruby Async::Bus::Controller} and define methods that can be called remotely: |
| 25 | + |
| 26 | +```ruby |
| 27 | +class ChatRoomController < Async::Bus::Controller |
| 28 | + def initialize(name) |
| 29 | + @name = name |
| 30 | + @messages = [] |
| 31 | + @subscribers = [] |
| 32 | + end |
| 33 | + |
| 34 | + def send_message(author, text) |
| 35 | + message = {author: author, text: text, time: Time.now} |
| 36 | + @messages << message |
| 37 | + |
| 38 | + # Notify all subscribers: |
| 39 | + @subscribers.each do |subscriber| |
| 40 | + subscriber.on_message(message) |
| 41 | + end |
| 42 | + |
| 43 | + message |
| 44 | + end |
| 45 | + |
| 46 | + def subscribe(subscriber) |
| 47 | + @subscribers << subscriber |
| 48 | + @messages.size # Return message count |
| 49 | + end |
| 50 | + |
| 51 | + def get_messages(count = 10) |
| 52 | + @messages.last(count) |
| 53 | + end |
| 54 | +end |
| 55 | +``` |
| 56 | + |
| 57 | +To use a controller, bind it instead of the raw object: |
| 58 | + |
| 59 | +```ruby |
| 60 | +# Server: |
| 61 | +room = ChatRoomController.new("general") |
| 62 | + |
| 63 | +server.accept do |connection| |
| 64 | + connection.bind(:room, room) |
| 65 | +end |
| 66 | + |
| 67 | +# Client: |
| 68 | +client.connect do |connection| |
| 69 | + room = connection[:room] |
| 70 | + room.send_message("Alice", "Hello, world!") |
| 71 | + messages = room.get_messages(5) |
| 72 | +end |
| 73 | +``` |
| 74 | + |
| 75 | +## Returning Controllers |
| 76 | + |
| 77 | +Controllers can return other controllers, and they are automatically proxied when registered as reference types. This enables sharing the same controller instance across multiple clients: |
| 78 | + |
| 79 | +```ruby |
| 80 | +class ChatServerController < Async::Bus::Controller |
| 81 | + def initialize |
| 82 | + @rooms = {} |
| 83 | + end |
| 84 | + |
| 85 | + def get_room(name) |
| 86 | + # Return existing room or create new one - automatically proxied: |
| 87 | + @rooms[name] ||= ChatRoomController.new(name) |
| 88 | + end |
| 89 | + |
| 90 | + def list_rooms |
| 91 | + @rooms.keys |
| 92 | + end |
| 93 | +end |
| 94 | + |
| 95 | +class ChatRoomController < Async::Bus::Controller |
| 96 | + def initialize(name) |
| 97 | + @name = name |
| 98 | + @messages = [] |
| 99 | + end |
| 100 | + |
| 101 | + def send_message(author, text) |
| 102 | + @messages << {author: author, text: text, time: Time.now} |
| 103 | + end |
| 104 | + |
| 105 | + def name |
| 106 | + @name |
| 107 | + end |
| 108 | +end |
| 109 | +``` |
| 110 | + |
| 111 | +When a controller method returns another controller, the client receives a proxy to that controller. Multiple clients accessing the same room will share the same controller instance: |
| 112 | + |
| 113 | +```ruby |
| 114 | +# Server: |
| 115 | +chat = ChatServerController.new |
| 116 | + |
| 117 | +server.accept do |connection| |
| 118 | + connection.bind(:chat, chat) |
| 119 | +end |
| 120 | + |
| 121 | +# Client 1: |
| 122 | +client1.connect do |connection| |
| 123 | + chat = connection[:chat] |
| 124 | + room = chat.get_room("general") # Returns controller, auto-proxied |
| 125 | + room.send_message("Alice", "Hello!") |
| 126 | +end |
| 127 | + |
| 128 | +# Client 2: |
| 129 | +client2.connect do |connection| |
| 130 | + chat = connection[:chat] |
| 131 | + room = chat.get_room("general") # Returns same controller instance |
| 132 | + # Can see messages from Client 1 because they share the same room |
| 133 | +end |
| 134 | +``` |
| 135 | + |
| 136 | +## Passing Controllers as Arguments |
| 137 | + |
| 138 | +Because controllers are passed by reference, you can pass them as arguments to enable bidirectional communication. When a client passes a proxy as an argument, the server receives a proxy that points back to the client's controller. This enables the server to call methods on the client's controller. This pattern is useful for event handlers, callbacks, or subscription systems: |
| 139 | + |
| 140 | +```ruby |
| 141 | +class ChatRoomController < Async::Bus::Controller |
| 142 | + def initialize(name) |
| 143 | + @name = name |
| 144 | + @messages = [] |
| 145 | + @subscribers = [] |
| 146 | + end |
| 147 | + |
| 148 | + def subscribe(subscriber) |
| 149 | + # subscriber is a proxy to the client's controller: |
| 150 | + @subscribers << subscriber |
| 151 | + # Send existing messages to the new subscriber: |
| 152 | + @messages.each{|msg| subscriber.on_message(msg)} |
| 153 | + true |
| 154 | + end |
| 155 | + |
| 156 | + def send_message(author, text) |
| 157 | + message = {author: author, text: text, time: Time.now} |
| 158 | + @messages << message |
| 159 | + |
| 160 | + # Notify all subscribers by calling back to their controllers: |
| 161 | + @subscribers.each do |subscriber| |
| 162 | + subscriber.on_message(message) |
| 163 | + end |
| 164 | + |
| 165 | + message |
| 166 | + end |
| 167 | +end |
| 168 | + |
| 169 | +# Client: Subscribes to room messages |
| 170 | +class MessageSubscriberController < Async::Bus::Controller |
| 171 | + def initialize |
| 172 | + @received = [] |
| 173 | + end |
| 174 | + |
| 175 | + def on_message(message) |
| 176 | + @received << message |
| 177 | + puts "#{message[:author]}: #{message[:text]}" |
| 178 | + end |
| 179 | + |
| 180 | + attr :received |
| 181 | +end |
| 182 | + |
| 183 | +# Server setup: |
| 184 | +room = ChatRoomController.new("general") |
| 185 | + |
| 186 | +server.accept do |connection| |
| 187 | + connection.bind(:room, room) |
| 188 | +end |
| 189 | + |
| 190 | +# Client subscription: |
| 191 | +client.connect do |connection| |
| 192 | + room = connection[:room] |
| 193 | + |
| 194 | + # Create a subscriber controller: |
| 195 | + subscriber = MessageSubscriberController.new |
| 196 | + subscriber_proxy = connection.bind(:subscriber, subscriber) |
| 197 | + |
| 198 | + # Pass the proxy as an argument - the server can now call back: |
| 199 | + room.subscribe(subscriber_proxy) |
| 200 | + |
| 201 | + # Now when messages are sent, subscriber.on_message will be called: |
| 202 | + room.send_message("Bob", "Hello, everyone!") |
| 203 | +end |
| 204 | +``` |
0 commit comments