Class: P2pStreamsChannel::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/p2p_streams_channel/session.rb

Overview

TODO: support optional :db -> save session to db

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, secret_key: "") ⇒ Session

Returns a new instance of Session.



10
11
12
13
14
15
# File 'lib/p2p_streams_channel/session.rb', line 10

def initialize(id, secret_key: "")
    @id = id
    @secret_key = secret_key
    @negotiation = Negotiation.new
    @session_state = SessionState.new
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/p2p_streams_channel/session.rb', line 8

def id
  @id
end

#negotiationObject (readonly)

Returns the value of attribute negotiation.



8
9
10
# File 'lib/p2p_streams_channel/session.rb', line 8

def negotiation
  @negotiation
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



8
9
10
# File 'lib/p2p_streams_channel/session.rb', line 8

def secret_key
  @secret_key
end

#session_stateObject (readonly)

Returns the value of attribute session_state.



8
9
10
# File 'lib/p2p_streams_channel/session.rb', line 8

def session_state
  @session_state
end

Instance Method Details

#disconnect_peer(peer_id) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/p2p_streams_channel/session.rb', line 35

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

#is_host_peer?(peer_id) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/p2p_streams_channel/session.rb', line 44

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

#join_peer(peer_id) ⇒ Object



29
30
31
32
33
# File 'lib/p2p_streams_channel/session.rb', line 29

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

#reset_stateObject



48
49
50
51
# File 'lib/p2p_streams_channel/session.rb', line 48

def reset_state
    @session_state = SessionState.new
    save!
end

#save!Object



53
54
55
# File 'lib/p2p_streams_channel/session.rb', line 53

def save!
    P2pStreamsChannel.save_session(self)
end

#signatureObject



17
18
19
# File 'lib/p2p_streams_channel/session.rb', line 17

def signature
    {id: @id}
end

#to_jsonObject



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

def to_json
    signature.to_json
end

#to_paramObject



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

def to_param
    signature.to_param
end