ruby_q 1.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.
- checksums.yaml +7 -0
- data/bin/ruby_q +14 -0
- data/lib/message.rb +21 -0
- data/lib/message_factory.rb +20 -0
- data/lib/message_queue_router.rb +41 -0
- data/lib/mq.rb +130 -0
- data/lib/r_message_queue.rb +45 -0
- data/lib/ruby_q.rb +6 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9f6975064a0b6624835852a93c7970c0f79479ae
|
4
|
+
data.tar.gz: e232078e84306a05fe5014ef44b28aaec0fafdc3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d478c86bf16b1d4cc04ce06c4d99078392c4861d4bddc67f049b41eb89a2cb3ab219f2f3b601e4726202e126d4be3a660da54bef5d2cd76a5fb6a04140ab1faf
|
7
|
+
data.tar.gz: 05bf89ff2da630160ebe2a5ccdb0af9ec044d519284b4782e5b941e2fd77f828c2bf68f72835194fa8e8488226dc95eda9b99851712eb243201e78823274ae2a
|
data/bin/ruby_q
ADDED
data/lib/message.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# The object contains messages and properties of messages
|
3
|
+
class Message
|
4
|
+
|
5
|
+
attr_accessor :queue
|
6
|
+
attr_accessor :sender
|
7
|
+
attr_accessor :receiver
|
8
|
+
attr_accessor :message
|
9
|
+
attr_accessor :time
|
10
|
+
attr_accessor :id
|
11
|
+
attr_accessor :ttl
|
12
|
+
def initialize (queue, sender, receiver, message, id, ttl)
|
13
|
+
@queue = queue
|
14
|
+
@sender = sender
|
15
|
+
@receiver = receiver
|
16
|
+
@message = message
|
17
|
+
@time = Time.now.to_i
|
18
|
+
@id = id
|
19
|
+
@ttl = ttl
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require_relative('message')
|
3
|
+
|
4
|
+
class Message_factory
|
5
|
+
attr_accessor :currentMessageId
|
6
|
+
def initialize
|
7
|
+
@currentMessageId = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_message(message)
|
11
|
+
@currentMessageId += 1
|
12
|
+
tokens = message.gsub(/\s+/m,' ').strip.split(' ')
|
13
|
+
queue_name = tokens[0]
|
14
|
+
sender = tokens[1]
|
15
|
+
receiver = tokens[2]
|
16
|
+
ttl = tokens[3]
|
17
|
+
msg = message[message.index(ttl)+ttl.length + 1..-1]
|
18
|
+
return Message.new(queue_name, sender, receiver, msg, currentMessageId, ttl)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'mq'
|
2
|
+
class Message_queue_router
|
3
|
+
attr_accessor :mq_list
|
4
|
+
def initialize
|
5
|
+
@mq_list = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def route(input,client)
|
9
|
+
action = input.gsub(/\s+/m,' ').strip.split(' ')[0]
|
10
|
+
name = input.gsub(/\s+/m,' ').strip.split(' ')[1]
|
11
|
+
msg = input[input.index(name)+name.length + 1..-1]
|
12
|
+
ret = ''
|
13
|
+
command = input
|
14
|
+
#target_queue = queue_exists?(name)
|
15
|
+
|
16
|
+
if queue_exists?(name)
|
17
|
+
mq = mq_list.select { |q| q.name == name }[0]
|
18
|
+
while mq.in_use?
|
19
|
+
sleep 0.5
|
20
|
+
end
|
21
|
+
mq.lock
|
22
|
+
ret = mq.parse_command(command, client)
|
23
|
+
mq.unlock
|
24
|
+
else
|
25
|
+
puts Time.now.getutc.to_s + " - Queue not found, creating queue " + name
|
26
|
+
new_queue = MQStruct.new(name)
|
27
|
+
ret = new_queue.parse_command(command, client)
|
28
|
+
mq_list << new_queue
|
29
|
+
end
|
30
|
+
return ret
|
31
|
+
end
|
32
|
+
|
33
|
+
def queue_exists?(name)
|
34
|
+
mq_list.each { |q|
|
35
|
+
if q.name == name
|
36
|
+
return q
|
37
|
+
end
|
38
|
+
}
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
end
|
data/lib/mq.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require_relative('message_factory')
|
3
|
+
class MQStruct
|
4
|
+
attr_accessor :queue
|
5
|
+
attr_accessor :name
|
6
|
+
def initialize (name)
|
7
|
+
@lock = false
|
8
|
+
@name = name
|
9
|
+
@queue = Queue.new
|
10
|
+
@msg_factory = Message_factory.new
|
11
|
+
@in_use = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse_command(input, client)
|
15
|
+
input = input.chomp
|
16
|
+
command = input.gsub(/\s+/m,' ').strip.split(' ')[0]
|
17
|
+
return_string = ''
|
18
|
+
if command == 'get'
|
19
|
+
user = input.gsub(/\s+/m,' ').strip.split(' ')[2]
|
20
|
+
puts Time.now.getutc.to_s + " - Client " + client.addr.inspect + " trying to GET a message."
|
21
|
+
hasMessage = self.check_for_message?(user)
|
22
|
+
return_string = 'nil'
|
23
|
+
if hasMessage
|
24
|
+
return_string = get_message(user).message
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
if command == 'put'
|
29
|
+
puts Time.now.getutc.to_s + ' - Client ' + client.addr.inspect + ' trying to PUT a message into ' + name
|
30
|
+
self.insert_message(input[input.index(command)+command.length + 1..-1])
|
31
|
+
return_string = 'True'
|
32
|
+
end
|
33
|
+
|
34
|
+
if command == 'listAll'
|
35
|
+
puts Time.now.getutc.to_s + ' - Client ' + client.addr.inspect + ' retrieving list of all messages.'
|
36
|
+
return_string = self.list_all_messages
|
37
|
+
end
|
38
|
+
return return_string
|
39
|
+
end
|
40
|
+
def insert_message(message)
|
41
|
+
queue << @msg_factory.create_message(message)
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_head_message
|
45
|
+
if queue.empty? != true
|
46
|
+
return queue.pop
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def in_use?
|
51
|
+
return @in_use
|
52
|
+
end
|
53
|
+
|
54
|
+
def lock
|
55
|
+
if in_use?
|
56
|
+
return false
|
57
|
+
else
|
58
|
+
@in_use = true
|
59
|
+
return true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def unlock
|
64
|
+
@in_use = false
|
65
|
+
end
|
66
|
+
|
67
|
+
def list_all_messages
|
68
|
+
new_queue = Queue.new
|
69
|
+
while @queue.empty? != true do
|
70
|
+
message = @queue.pop
|
71
|
+
puts message.sender + "\t" + message.receiver + "\t" + message.ttl + "\t" + message.message
|
72
|
+
new_queue << message
|
73
|
+
end
|
74
|
+
@queue = new_queue
|
75
|
+
return 'nil'
|
76
|
+
end
|
77
|
+
|
78
|
+
def getAllMessages
|
79
|
+
ret = Array.new
|
80
|
+
while queue.empty? != true do
|
81
|
+
ret.push(queue.pop)
|
82
|
+
end
|
83
|
+
return ret
|
84
|
+
end
|
85
|
+
|
86
|
+
def empty?
|
87
|
+
queue.empty?
|
88
|
+
end
|
89
|
+
|
90
|
+
def check_for_message?(receiver)
|
91
|
+
newMessage = false
|
92
|
+
new_queue = Queue.new
|
93
|
+
while queue.empty? != true do
|
94
|
+
message = queue.pop
|
95
|
+
if message.receiver == receiver
|
96
|
+
newMessage = true
|
97
|
+
end
|
98
|
+
new_queue << message
|
99
|
+
end
|
100
|
+
@queue = new_queue
|
101
|
+
return newMessage
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_message(receiver)
|
105
|
+
returnMessage = nil
|
106
|
+
haveMessage = false
|
107
|
+
new_queue = Queue.new
|
108
|
+
while queue.empty? != true do
|
109
|
+
message = queue.pop
|
110
|
+
if message.receiver == receiver && haveMessage == false
|
111
|
+
returnMessage = message
|
112
|
+
haveMessage = true
|
113
|
+
else
|
114
|
+
new_queue << message
|
115
|
+
end
|
116
|
+
end
|
117
|
+
@queue = new_queue
|
118
|
+
return returnMessage
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def insert_front(msg)
|
123
|
+
queue << msg
|
124
|
+
for i in queue.length - 1
|
125
|
+
queue << queue.pop
|
126
|
+
end
|
127
|
+
end
|
128
|
+
if __FILE__ == $0
|
129
|
+
mq = MQStruct.new
|
130
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'socket'
|
4
|
+
#require_relative 'mq'
|
5
|
+
require_relative 'message_queue_router'
|
6
|
+
#header senderId receiverId #Time.now.to_i message
|
7
|
+
|
8
|
+
### need public facing api
|
9
|
+
|
10
|
+
class Ruby_q
|
11
|
+
attr_accessor :port
|
12
|
+
|
13
|
+
def initialize(hostname = '0.0.0.0', port = 65530)
|
14
|
+
@hostname = hostname
|
15
|
+
@port = port
|
16
|
+
@router = Message_queue_router.new
|
17
|
+
self.start
|
18
|
+
end
|
19
|
+
|
20
|
+
def start()
|
21
|
+
puts Time.now.getutc.to_s + " - Starting server on " + @hostname + " " + @port.to_s
|
22
|
+
server = TCPServer.open(port)
|
23
|
+
loop {
|
24
|
+
Thread.start(server.accept) do |client|
|
25
|
+
puts 'Connection from ' + client.addr.inspect
|
26
|
+
while 1 do
|
27
|
+
rx = client.gets
|
28
|
+
if rx.nil?
|
29
|
+
Time.now.getutc.to_s + ' - Client ' + client.addr.inspect + ' closing connection.'
|
30
|
+
break
|
31
|
+
end
|
32
|
+
puts Time.now.getutc.to_s + ' - Client ' + client.addr.inspect + ' sent: ' + rx
|
33
|
+
client.puts @router.route(rx, client).gsub("\n", ' ')
|
34
|
+
end
|
35
|
+
#client.close
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
if __FILE__ == $0
|
42
|
+
mq = Ruby_q.new
|
43
|
+
puts mq.port
|
44
|
+
mq.start
|
45
|
+
end
|
data/lib/ruby_q.rb
ADDED
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_q
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email:
|
15
|
+
- ''
|
16
|
+
executables:
|
17
|
+
- ruby_q
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/ruby_q
|
22
|
+
- lib/message.rb
|
23
|
+
- lib/message_factory.rb
|
24
|
+
- lib/message_queue_router.rb
|
25
|
+
- lib/mq.rb
|
26
|
+
- lib/r_message_queue.rb
|
27
|
+
- lib/ruby_q.rb
|
28
|
+
homepage: ''
|
29
|
+
licenses:
|
30
|
+
- ''
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 2.2.2
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: ''
|
52
|
+
test_files: []
|