Class: WAMP::Engines::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/wamp/engines/memory.rb

Overview

Engine for managing clients, and topics in system memory. This engine is best used for single servers.

Direct Known Subclasses

Redis

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Memory

Creates a new instance of the memory engine as well as some empty hashes for holding clients and topics.



14
15
16
17
18
# File 'lib/wamp/engines/memory.rb', line 14

def initialize(options)
  @options = options
  @clients = {}
  @topics  = {}
end

Instance Attribute Details

#clientsObject (readonly)

Returns the value of attribute clients.



9
10
11
# File 'lib/wamp/engines/memory.rb', line 9

def clients
  @clients
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/wamp/engines/memory.rb', line 9

def options
  @options
end

#topicsObject (readonly)

Returns the value of attribute topics.



9
10
11
# File 'lib/wamp/engines/memory.rb', line 9

def topics
  @topics
end

Instance Method Details

#all_clientsArray

Returns an array of all connected clients



52
53
54
# File 'lib/wamp/engines/memory.rb', line 52

def all_clients
  clients.values
end

#create_client(websocket) ⇒ WebSocket

Creates a new Socket object and adds it as a client.



24
25
26
27
# File 'lib/wamp/engines/memory.rb', line 24

def create_client(websocket)
  client = new_client(websocket)
  @clients[client.id] = client
end

#create_event(client, topic_uri, payload, exclude, include) ⇒ Object



89
90
91
92
93
# File 'lib/wamp/engines/memory.rb', line 89

def create_event(client, topic_uri, payload, exclude, include)
  topic = find_or_create_topic(topic_uri)

  topic.publish(client, protocol, payload, exclude, include) if topic
end

#delete_client(websocket) ⇒ WAMP::Socket

Deletes a client



44
45
46
47
48
# File 'lib/wamp/engines/memory.rb', line 44

def delete_client(websocket)
  client = find_clients(websocket: websocket).first

  clients.delete client.id
end

#find_clients(args = {}) ⇒ Array

Finds clients by the given parameters. Currently only supports one

parameter. Todo: Support multiple parameters.


33
34
35
36
37
38
39
# File 'lib/wamp/engines/memory.rb', line 33

def find_clients(args = {})
  matching_clients = clients.find_all do |id, socket|
    socket.send(args.first[0]) == args.first[1]
  end

  matching_clients.flat_map { |x| x[1] }
end

#find_or_create_topic(topic_uri) ⇒ WAMP::Topic

Finds an existing topic, if non is found it will create one.



59
60
61
# File 'lib/wamp/engines/memory.rb', line 59

def find_or_create_topic(topic_uri)
  @topics[topic_uri] ||= new_topic(topic_uri)
end

#subscribe_client_to_topic(client, topic_uri) ⇒ WAMP::Topic

Add a client to a topic and a topic to a client



67
68
69
70
71
72
73
74
# File 'lib/wamp/engines/memory.rb', line 67

def subscribe_client_to_topic(client, topic_uri)
  topic = find_or_create_topic(topic_uri)

  client.add_topic(topic)
  topic.add_client(client)

  topic
end

#unsubscribe_client_from_topic(client, topic_uri) ⇒ WAMP::Topic

Remove a client from a topic.



80
81
82
83
84
85
86
87
# File 'lib/wamp/engines/memory.rb', line 80

def unsubscribe_client_from_topic(client, topic_uri)
  topic = find_or_create_topic(topic_uri)

  client.remove_topic(topic)
  topic.remove_client(client)

  topic
end