Class: Mycel::RPC::Endpoint
- Inherits:
-
Object
- Object
- Mycel::RPC::Endpoint
- Defined in:
- lib/mycel.rb
Overview
Thin facade wrapping a Hub + Peer pair.
Endpoint owns the Hub; Peer shares a reference to it (no duplicated session tracking). Role information lives in Hub via server_sessions / client_sessions, so role-aware broadcasts are direct iterations rather than parallel bookkeeping.
The convenience methods (call_remote, call_async) target a single client session by default. When more than one client session exists, pass ‘session_id:` explicitly. For richer connection topologies, drop down to `endpoint.peer` and `endpoint.hub` directly.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#hub ⇒ Object
readonly
Returns the value of attribute hub.
-
#peer ⇒ Object
readonly
Returns the value of attribute peer.
Instance Method Summary collapse
- #broadcast(method_name, *params, role: :all, timeout: 10) ⇒ Object
- #call_async(method_name, *params, session_id: nil, timeout: nil, &callback) ⇒ Object
- #call_remote(method_name, *params, session_id: nil, timeout: nil) ⇒ Object
- #client_session_ids ⇒ Object
- #connect_to(host, port) ⇒ Object
-
#initialize(config = {}) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #server_session_ids ⇒ Object
- #shutdown ⇒ Object
- #start_server(port) ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
1359 1360 1361 |
# File 'lib/mycel.rb', line 1359 def config @config end |
#hub ⇒ Object (readonly)
Returns the value of attribute hub.
1359 1360 1361 |
# File 'lib/mycel.rb', line 1359 def hub @hub end |
#peer ⇒ Object (readonly)
Returns the value of attribute peer.
1359 1360 1361 |
# File 'lib/mycel.rb', line 1359 def peer @peer end |
Instance Method Details
#broadcast(method_name, *params, role: :all, timeout: 10) ⇒ Object
1391 1392 1393 1394 1395 1396 1397 1398 1399 |
# File 'lib/mycel.rb', line 1391 def broadcast(method_name, *params, role: :all, timeout: 10) ids = case role when :all then @hub.sessions.keys when :clients then @hub.server_sessions.keys # we serve them; they are clients when :servers then @hub.client_sessions.keys # we connect to them; they are servers else raise ArgumentError, "role must be :all, :clients, or :servers" end @peer.broadcast_method(method_name, *params, session_ids: ids, timeout: timeout) end |
#call_async(method_name, *params, session_id: nil, timeout: nil, &callback) ⇒ Object
1381 1382 1383 1384 1385 1386 1387 1388 1389 |
# File 'lib/mycel.rb', line 1381 def call_async(method_name, *params, session_id: nil, timeout: nil, &callback) sid = session_id || begin sole_client_session_id rescue ConnectionError => e callback&.call(e, nil) if callback raise end @peer.call_async(sid, method_name, *params, timeout: timeout, &callback) end |
#call_remote(method_name, *params, session_id: nil, timeout: nil) ⇒ Object
1377 1378 1379 |
# File 'lib/mycel.rb', line 1377 def call_remote(method_name, *params, session_id: nil, timeout: nil) @peer.call(session_id || sole_client_session_id, method_name, *params, timeout: timeout) end |
#client_session_ids ⇒ Object
1405 1406 1407 |
# File 'lib/mycel.rb', line 1405 def client_session_ids @hub.client_sessions.keys end |
#connect_to(host, port) ⇒ Object
1372 1373 1374 1375 |
# File 'lib/mycel.rb', line 1372 def connect_to(host, port) @hub.connect(host, port) self end |
#server_session_ids ⇒ Object
1401 1402 1403 |
# File 'lib/mycel.rb', line 1401 def server_session_ids @hub.server_sessions.keys end |
#shutdown ⇒ Object
1409 1410 1411 |
# File 'lib/mycel.rb', line 1409 def shutdown @hub.shutdown end |
#start_server(port) ⇒ Object
1367 1368 1369 1370 |
# File 'lib/mycel.rb', line 1367 def start_server(port) @hub.open(port) self end |