Class: Mycel::RPC::Endpoint

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Endpoint

Returns a new instance of Endpoint.



1413
1414
1415
1416
1417
# File 'lib/mycel.rb', line 1413

def initialize(config = {})
  @config = config
  @hub  = Mycel::Channel::Hub.new
  @peer = Peer.new(@hub, config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



1411
1412
1413
# File 'lib/mycel.rb', line 1411

def config
  @config
end

#hubObject (readonly)

Returns the value of attribute hub.



1411
1412
1413
# File 'lib/mycel.rb', line 1411

def hub
  @hub
end

#peerObject (readonly)

Returns the value of attribute peer.



1411
1412
1413
# File 'lib/mycel.rb', line 1411

def peer
  @peer
end

Instance Method Details

#broadcast(method_name, *params, role: :all, timeout: 10) ⇒ Object



1443
1444
1445
1446
1447
1448
1449
1450
1451
# File 'lib/mycel.rb', line 1443

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



1433
1434
1435
1436
1437
1438
1439
1440
1441
# File 'lib/mycel.rb', line 1433

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



1429
1430
1431
# File 'lib/mycel.rb', line 1429

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_idsObject



1457
1458
1459
# File 'lib/mycel.rb', line 1457

def client_session_ids
  @hub.client_sessions.keys
end

#connect_to(host, port) ⇒ Object



1424
1425
1426
1427
# File 'lib/mycel.rb', line 1424

def connect_to(host, port)
  @hub.connect(host, port)
  self
end

#server_session_idsObject



1453
1454
1455
# File 'lib/mycel.rb', line 1453

def server_session_ids
  @hub.server_sessions.keys
end

#shutdownObject



1461
1462
1463
# File 'lib/mycel.rb', line 1461

def shutdown
  @hub.shutdown
end

#start_server(port) ⇒ Object



1419
1420
1421
1422
# File 'lib/mycel.rb', line 1419

def start_server(port)
  @hub.open(port)
  self
end