Class: Wampproto::Joiner

Inherits:
Object
  • Object
show all
Defined in:
lib/wampproto/joiner.rb

Overview

Handle Joining part of wamp protocol

Constant Summary collapse

STATE_NONE =
0
STATE_HELLO_SENT =
1
STATE_AUTHENTICATE_SENT =
2
STATE_JOINED =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(realm, serializer = Serializer::JSON, authenticator = Auth::Anonymous.new) ⇒ Joiner

Returns a new instance of Joiner.



21
22
23
24
25
26
# File 'lib/wampproto/joiner.rb', line 21

def initialize(realm, serializer = Serializer::JSON, authenticator = Auth::Anonymous.new)
  @realm = realm
  @serializer = serializer
  @authenticator = authenticator
  @state = STATE_NONE
end

Instance Attribute Details

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



18
19
20
# File 'lib/wampproto/joiner.rb', line 18

def authenticator
  @authenticator
end

#realmObject (readonly)

Returns the value of attribute realm.



18
19
20
# File 'lib/wampproto/joiner.rb', line 18

def realm
  @realm
end

#serializerObject (readonly)

Returns the value of attribute serializer.



18
19
20
# File 'lib/wampproto/joiner.rb', line 18

def serializer
  @serializer
end

#stateObject

Returns the value of attribute state.



19
20
21
# File 'lib/wampproto/joiner.rb', line 19

def state
  @state
end

Instance Method Details

#joined?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/wampproto/joiner.rb', line 28

def joined?
  @state == STATE_JOINED
end

#receive(data) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/wampproto/joiner.rb', line 47

def receive(data)
  message = serializer.deserialize(data)
  to_send = receive_message(message)

  return unless to_send.instance_of?(Message::Authenticate)

  serializer.serialize(to_send)
end

#receive_message(msg) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/wampproto/joiner.rb', line 56

def receive_message(msg) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  case msg
  when Message::Welcome
    if state != STATE_HELLO_SENT && state != STATE_AUTHENTICATE_SENT
      raise ProtocolViolation, "Received WELCOME message after session was established"
    end

    @session_details = SessionDetails.new(msg.session_id, realm, msg.authid, msg.authrole)
    self.state = STATE_JOINED
  when Message::Challenge
    raise ProtocolViolation, "Received CHALLENGE message before HELLO message was sent" if state != STATE_HELLO_SENT

    authenticate = authenticator.authenticate(msg)
    self.state = STATE_AUTHENTICATE_SENT
    authenticate
  when Message::Abort
    raise ValueError, "received abort"
  else
    raise ValueError, "received unknown message"
  end
end

#send_helloObject

rubocop:disable Metrics/MethodLength



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wampproto/joiner.rb', line 32

def send_hello # rubocop:disable Metrics/MethodLength
  hello = Message::Hello.new(
    realm,
    {
      roles: CLIENT_ROLES,
      authid: authenticator.authid,
      authmethods: [authenticator.authmethod],
      authextra: authenticator.authextra
    }
  )

  @state = STATE_HELLO_SENT
  serializer.serialize(hello)
end