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.



1361
1362
1363
1364
1365
# File 'lib/mycel.rb', line 1361

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.



1359
1360
1361
# File 'lib/mycel.rb', line 1359

def config
  @config
end

#hubObject (readonly)

Returns the value of attribute hub.



1359
1360
1361
# File 'lib/mycel.rb', line 1359

def hub
  @hub
end

#peerObject (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_idsObject



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_idsObject



1401
1402
1403
# File 'lib/mycel.rb', line 1401

def server_session_ids
  @hub.server_sessions.keys
end

#shutdownObject



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