Sha256: 90c9b20d07adcae09dc5ff53830209608011f7f1d29e038cb07d9a8c7a64b091

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require_relative "./negotiation"

module P2pStreamsChannel
    # TODO: support optional :db -> save session to db
    class Session
        attr_reader :id, :secret_key, :negotiation, :session_state
        
        def initialize(id, secret_key: "")
            @id = id
            @secret_key = secret_key
            @negotiation = Negotiation.new
            @session_state = SessionState.new
        end

        def signature
            {id: @id}
        end

        def to_param
            signature.to_param
        end

        def to_json
            signature.to_json
        end

        def join_peer(peer_id)
            @negotiation.join(peer_id, session_state).tap do |response|
                save!
            end
        end

        def disconnect_peer(peer_id)
            if session_state.host_peer_id == peer_id
                reset_state
            else
                session_state.remove_peer(peer_id)
                save!
            end
        end

        def is_host_peer?(peer_id)
            session_state.host_peer_id == peer_id
        end

        def reset_state
            @session_state = SessionState.new
            save!
        end

        def save!
            P2pStreamsChannel.save_session(self)
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
p2p_streams_channel-0.0.3 lib/p2p_streams_channel/session.rb
p2p_streams_channel-0.0.2 lib/p2p_streams_channel/session.rb