fmq 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ #
2
+ # Copyright (c) 2008 Vincent Landgraf
3
+ #
4
+ # This file is part of the Free Message Queue.
5
+ #
6
+ # Foobar is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Foobar is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+ module FreeMessageQueue
20
+ class FileQueue
21
+ attr_accessor :manager
22
+
23
+ def initialize()
24
+ super
25
+ end
26
+
27
+ def poll()
28
+ item = OpenStruct.new
29
+
30
+ f = open(@file_path, "rb")
31
+ item.data = f.read
32
+ f.close
33
+
34
+ item.content_type = @content_type
35
+ item
36
+ end
37
+
38
+ def put(data)
39
+ # do nothing
40
+ end
41
+
42
+ def size
43
+ 1 # there is always a message in the queue
44
+ end
45
+
46
+ def file=(path)
47
+ @file_path = path
48
+ end
49
+
50
+ def content_type=(type)
51
+ @content_type = type
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,92 @@
1
+ #
2
+ # Copyright (c) 2008 Vincent Landgraf
3
+ #
4
+ # This file is part of the Free Message Queue.
5
+ #
6
+ # Foobar is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Foobar is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+ module FreeMessageQueue
20
+ class QueueItem
21
+ attr_accessor :next, :data, :created_at
22
+
23
+ def initialize(data, created_at = Time.new)
24
+ @data = data
25
+ @created_at = created_at
26
+ end
27
+
28
+ def bytes
29
+ @data.size
30
+ end
31
+ end
32
+
33
+ class LinkedQueue
34
+ attr_reader :size, :bytes
35
+
36
+ def initialize()
37
+ @size = 0
38
+ @first_item = nil
39
+ @last_item = nil
40
+ @bytes = 0
41
+ end
42
+
43
+ # remove all items
44
+ def clear
45
+ if size > 0
46
+ while self.poll; end
47
+ end
48
+ end
49
+
50
+ def put(data)
51
+ return false if data == nil
52
+
53
+ # create item
54
+ qi = QueueItem.new(data)
55
+
56
+ # insert at end of list
57
+ if @first_item == nil
58
+ @first_item = @last_item = qi
59
+ else
60
+ @last_item.next = qi
61
+ end
62
+ @last_item = qi
63
+
64
+ # update queue size and memory usage
65
+ @size += 1
66
+ @bytes += qi.bytes
67
+ true
68
+ end
69
+
70
+ def poll()
71
+ if @size > 0
72
+ # remove allways the first item
73
+ qi = @first_item
74
+
75
+ # cleanup list
76
+ if @first_item == @last_item # just 1 element is in the list
77
+ @first_item = @last_item = nil
78
+ else
79
+ @first_item = @first_item.next
80
+ end
81
+ qi.next = nil # remove link to next item
82
+
83
+ # update queue size and memory usage
84
+ @size -= 1
85
+ @bytes -= qi.bytes
86
+ return qi
87
+ else
88
+ return nil
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,87 @@
1
+ #
2
+ # Copyright (c) 2008 Vincent Landgraf
3
+ #
4
+ # This file is part of the Free Message Queue.
5
+ #
6
+ # Foobar is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Foobar is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+ require File.dirname(__FILE__) + '/syncronized'
20
+
21
+ module FreeMessageQueue
22
+ class LoadBalancedQueue
23
+ attr_accessor :queues, :manager
24
+
25
+ def initialize(queue_count = 5)
26
+ @queues = []
27
+ queue_count.times do
28
+ @queues << SyncronizedQueue.new
29
+ end
30
+ @poll_queue = @put_queue = 0
31
+ @semaphore = Mutex.new
32
+ end
33
+
34
+ def clear
35
+ @queues.each { |q| q.clear }
36
+ end
37
+
38
+ # size of the queue is sum of size of all load balanced queues
39
+ def size
40
+ size = 0
41
+ @queues.each { |q| size += q.size }
42
+ return size
43
+ end
44
+
45
+ # size of queue in bytes
46
+ def bytes
47
+ tmp_bytes = 0
48
+ @queues.each { |q| tmp_bytes += q.bytes }
49
+ return tmp_bytes
50
+ end
51
+
52
+ def poll
53
+ @queues[next_poll_index].poll
54
+ end
55
+
56
+ def put(data)
57
+ @queues[next_put_index].put(data)
58
+ end
59
+
60
+ private
61
+
62
+ # next index acts like 'round robin'
63
+ def next_poll_index
64
+ @semaphore.synchronize {
65
+ # continue at begin if end was reached
66
+ pq = (@poll_queue + 1 == @queues.size) ? 0 : @poll_queue + 1
67
+
68
+ # if current queue is emtpy use other instead if there are
69
+ # some items left but try just 10 times
70
+ i = 0
71
+ while @queues[pq].size == 0 && self.size > 0 && i < 10
72
+ pq = (@poll_queue + 1 == @queues.size) ? 0 : @poll_queue + 1
73
+ i += 1
74
+ end
75
+
76
+ @poll_queue = pq # return index and save for next use
77
+ }
78
+ end
79
+
80
+ # next index acts like 'round robin'
81
+ def next_put_index
82
+ @semaphore.synchronize {
83
+ @put_queue = (@put_queue + 1 == @queues.size) ? 0 : @put_queue + 1
84
+ }
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,43 @@
1
+ #
2
+ # Copyright (c) 2008 Vincent Landgraf
3
+ #
4
+ # This file is part of the Free Message Queue.
5
+ #
6
+ # Foobar is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Foobar is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+ require 'thread'
20
+ require File.dirname(__FILE__) + '/linked'
21
+
22
+ module FreeMessageQueue
23
+ class SyncronizedQueue < LinkedQueue
24
+ attr_accessor :manager
25
+
26
+ def initialize()
27
+ super
28
+ @semaphore = Mutex.new
29
+ end
30
+
31
+ def poll()
32
+ @semaphore.synchronize {
33
+ super
34
+ }
35
+ end
36
+
37
+ def put(data)
38
+ @semaphore.synchronize {
39
+ super(data)
40
+ }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # Copyright (c) 2008 Vincent Landgraf
3
+ #
4
+ # This file is part of the Free Message Queue.
5
+ #
6
+ # Foobar is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Foobar is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+ module FreeMessageQueue
20
+ module VERSION #:nodoc:
21
+ MAJOR = 0
22
+ MINOR = 1
23
+ TINY = 0
24
+
25
+ STRING = [MAJOR, MINOR, TINY].join('.')
26
+ end
27
+ end
data/lib/fmq.rb ADDED
@@ -0,0 +1,33 @@
1
+ #
2
+ # Copyright (c) 2008 Vincent Landgraf
3
+ #
4
+ # This file is part of the Free Message Queue.
5
+ #
6
+ # Foobar is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Foobar is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Foobar. If not, see <http://www.gnu.org/licenses/>.
18
+ #
19
+
20
+ # load all queues and manager and server
21
+ Dir.glob(File.dirname(__FILE__) + "/fmq/queues/*.rb").each do |file|
22
+ require file
23
+ end
24
+
25
+ # load all local queues (from project directory)
26
+ Dir.glob("queues/*.rb").each do |file|
27
+ require file
28
+ end
29
+
30
+ # load all parts in right order
31
+ ['boot', 'client'].each do |file|
32
+ require File.dirname(__FILE__) + "/fmq/#{file}"
33
+ end