cod 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +1 -1
- data/HISTORY.txt +5 -1
- data/README +12 -13
- data/Rakefile +7 -1
- data/examples/{ping.rb → ping_pong/ping.rb} +1 -1
- data/examples/{pong.rb → ping_pong/pong.rb} +1 -1
- data/examples/{presence_client.rb → presence/client.rb} +0 -0
- data/examples/{presence_server.rb → presence/server.rb} +0 -0
- data/examples/queue/README +9 -0
- data/examples/queue/client.rb +31 -0
- data/examples/queue/queue.rb +51 -0
- data/examples/queue/send.rb +9 -0
- data/examples/service.rb +2 -2
- data/examples/tcp.rb +1 -1
- data/lib/cod.rb +47 -82
- data/lib/cod/beanstalk.rb +7 -0
- data/lib/cod/beanstalk/channel.rb +170 -0
- data/lib/cod/beanstalk/serializer.rb +80 -0
- data/lib/cod/beanstalk/service.rb +53 -0
- data/lib/cod/channel.rb +54 -12
- data/lib/cod/pipe.rb +188 -0
- data/lib/cod/select.rb +47 -0
- data/lib/cod/select_group.rb +87 -0
- data/lib/cod/service.rb +55 -42
- data/lib/cod/simple_serializer.rb +19 -0
- data/lib/cod/tcp_client.rb +202 -0
- data/lib/cod/tcp_server.rb +124 -0
- data/lib/cod/work_queue.rb +129 -0
- metadata +31 -45
- data/examples/pubsub/README +0 -12
- data/examples/pubsub/client.rb +0 -13
- data/examples/pubsub/directory.rb +0 -13
- data/examples/service_directory.rb +0 -32
- data/lib/cod/channel/base.rb +0 -185
- data/lib/cod/channel/beanstalk.rb +0 -69
- data/lib/cod/channel/pipe.rb +0 -137
- data/lib/cod/channel/tcp.rb +0 -16
- data/lib/cod/channel/tcpconnection.rb +0 -67
- data/lib/cod/channel/tcpserver.rb +0 -84
- data/lib/cod/client.rb +0 -81
- data/lib/cod/connection/beanstalk.rb +0 -77
- data/lib/cod/directory.rb +0 -98
- data/lib/cod/directory/countdown.rb +0 -31
- data/lib/cod/directory/subscription.rb +0 -59
- data/lib/cod/object_io.rb +0 -6
- data/lib/cod/objectio/connection.rb +0 -106
- data/lib/cod/objectio/reader.rb +0 -98
- data/lib/cod/objectio/serializer.rb +0 -26
- data/lib/cod/objectio/writer.rb +0 -27
- data/lib/cod/topic.rb +0 -95
@@ -1,26 +0,0 @@
|
|
1
|
-
module Cod::ObjectIO
|
2
|
-
class Serializer
|
3
|
-
attr_reader :transformer
|
4
|
-
|
5
|
-
def initialize(transformer=nil)
|
6
|
-
@transformer = transformer
|
7
|
-
end
|
8
|
-
|
9
|
-
# NOTE: source_io is provided to be able to provide back-channels through
|
10
|
-
# that same, not to read from it. Reading from this IO object will block
|
11
|
-
# you.
|
12
|
-
#
|
13
|
-
def deserialize(source_io, buffer_io)
|
14
|
-
if @transformer
|
15
|
-
Marshal.load(buffer_io, proc {
|
16
|
-
|obj| transformer.transform(source_io, obj) })
|
17
|
-
else
|
18
|
-
Marshal.load(buffer_io)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def serialize(message)
|
23
|
-
Marshal.dump(message)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/lib/cod/objectio/writer.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Cod::ObjectIO
|
2
|
-
# Writes objects to an IO stream.
|
3
|
-
#
|
4
|
-
class Writer
|
5
|
-
def initialize(serializer, pool)
|
6
|
-
@serializer = serializer
|
7
|
-
@pool = pool
|
8
|
-
end
|
9
|
-
|
10
|
-
def put(message)
|
11
|
-
@pool.accept
|
12
|
-
|
13
|
-
@pool.each do |connection|
|
14
|
-
connection.write(serialize(message))
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def close
|
19
|
-
@pool.close
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
def serialize(message)
|
24
|
-
@serializer.serialize(message)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/cod/topic.rb
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
module Cod
|
2
|
-
# A topic in a directory.
|
3
|
-
#
|
4
|
-
class Topic
|
5
|
-
attr_reader :answers, :directory
|
6
|
-
attr_reader :match_expr
|
7
|
-
attr_reader :identifier
|
8
|
-
attr_reader :subscription
|
9
|
-
attr_reader :renew_countdown
|
10
|
-
|
11
|
-
# Creates a topic that subscribes to a part of a directory. The match_expr
|
12
|
-
# decides which messages get forwarded to this topic, it limits the
|
13
|
-
# topic to a subset of the messages in the directory.
|
14
|
-
#
|
15
|
-
# Parameters:
|
16
|
-
# match_expr :: Topic to subscribe
|
17
|
-
# directory_channel :: Directory channel
|
18
|
-
# answer_channel :: Where the messages for this topic get sent
|
19
|
-
# opts :: See below
|
20
|
-
#
|
21
|
-
# Available options are:
|
22
|
-
# :renew :: Renew the subscription every n seconds.
|
23
|
-
#
|
24
|
-
def initialize(match_expr, directory_channel, answer_channel, opts={})
|
25
|
-
@directory, @answers = directory_channel, answer_channel
|
26
|
-
@match_expr = match_expr
|
27
|
-
@identifier = Cod.uuid
|
28
|
-
@subscription = Directory::Subscription.new(
|
29
|
-
match_expr, answers, @identifier)
|
30
|
-
|
31
|
-
# Default is to renew subscriptions every 30 minutes
|
32
|
-
@renew_countdown = Directory::Countdown.new(opts[:renew] || 30*60)
|
33
|
-
renew_countdown.start
|
34
|
-
|
35
|
-
subscribe
|
36
|
-
end
|
37
|
-
|
38
|
-
# Subscribes this topic to the directory's messages. This gets called upon
|
39
|
-
# initialization and must not be called again.
|
40
|
-
#
|
41
|
-
def subscribe(status=:new)
|
42
|
-
directory.put [
|
43
|
-
:subscribe, subscription, status]
|
44
|
-
|
45
|
-
# Start counting down to next subscription renewal
|
46
|
-
renew_countdown.start
|
47
|
-
end
|
48
|
-
def renew_subscription
|
49
|
-
subscribe(:refresh)
|
50
|
-
end
|
51
|
-
def renewal_needed?
|
52
|
-
renew_countdown.elapsed?
|
53
|
-
end
|
54
|
-
|
55
|
-
# Reads the next message from the directory that matches this topic.
|
56
|
-
#
|
57
|
-
def get(opts={})
|
58
|
-
# Read one message from the channel
|
59
|
-
subscription_id, message = next_message(opts)
|
60
|
-
# Answer back with a ping (so the directory knows we're still there)
|
61
|
-
directory.put [:ping, subscription_id]
|
62
|
-
|
63
|
-
return message
|
64
|
-
end
|
65
|
-
def next_message(opts)
|
66
|
-
if t=opts[:timeout]
|
67
|
-
timeout_at = Time.now + t
|
68
|
-
timeout = [t, renew_countdown.run_time].min
|
69
|
-
else
|
70
|
-
timeout_at = nil
|
71
|
-
timeout = renew_countdown.run_time
|
72
|
-
end
|
73
|
-
|
74
|
-
loop do
|
75
|
-
renew_subscription if renewal_needed?
|
76
|
-
|
77
|
-
begin
|
78
|
-
return answers.get(opts.merge(:timeout => timeout))
|
79
|
-
rescue Cod::Channel::TimeoutError
|
80
|
-
raise if timeout_at && Time.now > timeout_at
|
81
|
-
# DO NOTHING
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
fail "NOT REACHED"
|
86
|
-
end
|
87
|
-
|
88
|
-
# Closes all resources used by the topic.
|
89
|
-
#
|
90
|
-
def close
|
91
|
-
directory.close
|
92
|
-
answers.close
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|