Module: P2pStreamsChannel

Defined in:
lib/p2p_streams_channel.rb,
lib/p2p_streams_channel/cache.rb,
lib/p2p_streams_channel/engine.rb,
lib/p2p_streams_channel/session.rb,
lib/p2p_streams_channel/version.rb,
lib/p2p_streams_channel/negotiation.rb,
lib/p2p_streams_channel/session_state.rb,
lib/p2p_streams_channel/session_handler.rb,
lib/rails/generators/p2p_streams_channel/install_generator.rb,
lib/rails/generators/p2p_streams_channel/controller_generator.rb

Defined Under Namespace

Modules: TagHelper Classes: Configuration, ControllerGenerator, Engine, Error, InstallGenerator, Negotiation, Session, SessionState

Constant Summary collapse

VERSION =
"0.0.3"
STATE_SESSION_JOIN =
"SessionJoin".freeze
STATE_SESSION_READY =
"SessionReady".freeze
STATE_CONNECTED =
"connected".freeze
CONNECTION_MESSAGE_TYPE =
"Connection".freeze

Class Method Summary collapse

Class Method Details

.config {|configs| ... } ⇒ Object

Yields:

  • (configs)


20
21
22
# File 'lib/p2p_streams_channel.rb', line 20

def config
  yield(configs)
end

.disconnect_if_host_peer(session_id, peer_id) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/p2p_streams_channel/session_handler.rb', line 25

def disconnect_if_host_peer(session_id, peer_id)
    if session = P2pStreamsChannel.fetch_session(session_id)
        if session.is_host_peer?(peer_id)
            session.disconnect_peer(peer_id)
        end
    end
end

.fetch_session(session_id, **params) ⇒ Object



13
14
15
16
17
# File 'lib/p2p_streams_channel/cache.rb', line 13

def fetch_session(session_id, **params)
    P2pStreamsChannel.store.fetch(session_id, expires_in: params[:expires_in]) do
        P2pStreamsChannel::Session.new(session_id, secret_key: params[:secret_key])
    end
end

.handle_session(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/p2p_streams_channel/session_handler.rb', line 14

def handle_session(data)
    case data["state"]
    when P2pStreamsChannel::STATE_SESSION_JOIN
        if session = P2pStreamsChannel.fetch_session(data["session_id"])
            session.join_peer(data["peer_id"])
        end
    else
        data
    end
end

.resolve(message) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/p2p_streams_channel/session_handler.rb', line 6

def resolve(message)
    case message["type"]
    when CONNECTION_MESSAGE_TYPE
        handle_session(message)
    else
    end
end

.save_session(session) ⇒ Object



19
20
21
# File 'lib/p2p_streams_channel/cache.rb', line 19

def save_session(session)
    P2pStreamsChannel.store.write(session.id, session)
end

.storeObject



25
26
27
# File 'lib/p2p_streams_channel.rb', line 25

def store
    configs.store || Rails.cache
end