broadcast-objects 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/broadcast-objects.rb +6 -0
- metadata +1 -1
data/lib/broadcast-objects.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
#Class for organizing broadcasts.
|
1
2
|
class Channel
|
2
3
|
def initialize
|
3
4
|
@subscribers = []
|
4
5
|
end
|
6
|
+
#Called when a Broadcasting object sends a broadcast on this channel. Calls the recieve method of all objects subscribing to this channel, passing the channel itself and the data sent as arguments
|
5
7
|
def broadcast data
|
6
8
|
for i in subscribers
|
7
9
|
subscribers.recieve(self, data)
|
@@ -9,13 +11,17 @@ class Channel
|
|
9
11
|
end
|
10
12
|
attr_accessor :subscribers
|
11
13
|
end
|
14
|
+
#Basic module that implements the methods for subscribing and sending broadcasts. functionality for dealing with recieved broadcasts should be implemented by individual classes
|
12
15
|
module Broadcasting
|
16
|
+
#Subscribe to channel. While subscribed, will recieve message whenever another objects sends one on that channel.
|
13
17
|
def subscribe channel
|
14
18
|
channel.subscribers.push self
|
15
19
|
end
|
20
|
+
#send message to all objects that subscribed to channel, with data.
|
16
21
|
def send(channel, data)
|
17
22
|
channel.broadcast data
|
18
23
|
end
|
24
|
+
#Included here for debug purposes, but really should be implemented by each class individually. Called whenever a message is sent on a channel this object subscribed to. Channel is the channel in question, data is the data sent
|
19
25
|
def recieve(channel, data)
|
20
26
|
#always override in child class. Here for debug purposes
|
21
27
|
puts (data.to_s)+" was sent on channel "+(channel.to_s)+" and recieved by object "+(self.to_s)+" which did not implement the recieve function. Something is probably wrong"
|