rhcp 0.2.17 → 0.2.18
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/lib/rhcp.rb +1 -1
- data/lib/rhcp/client/context_aware_broker.rb +5 -1
- data/lib/rhcp/command.rb +19 -3
- data/lib/rhcp/context.rb +8 -1
- data/lib/rhcp/request.rb +5 -2
- metadata +4 -5
- data/lib/rhcp/memcached_broker.rb +0 -120
data/lib/rhcp.rb
CHANGED
@@ -23,7 +23,11 @@ module RHCP
|
|
23
23
|
def get_command_list(context=@context)
|
24
24
|
@wrapped_broker.get_command_list(context)
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
|
+
def known_commands
|
28
|
+
@wrapped_broker.known_commands
|
29
|
+
end
|
30
|
+
|
27
31
|
def give_the_request_some_context_rico(request)
|
28
32
|
# TODO think about the errors of your ways, son...
|
29
33
|
@context.cookies.each do |k,v|
|
data/lib/rhcp/command.rb
CHANGED
@@ -32,6 +32,8 @@ module RHCP
|
|
32
32
|
# "true" if this command returns data, but does not modify anything.
|
33
33
|
attr_reader :is_read_only
|
34
34
|
|
35
|
+
attr_reader :accepts_extra_params
|
36
|
+
|
35
37
|
# an array of context keys that enable this command
|
36
38
|
#
|
37
39
|
# if at least one of the listed keys is found in the context, the command
|
@@ -56,6 +58,9 @@ module RHCP
|
|
56
58
|
@default_param = nil
|
57
59
|
@is_read_only = false
|
58
60
|
@enabled_through_context_keys = nil
|
61
|
+
|
62
|
+
@accepts_extra_params = false
|
63
|
+
|
59
64
|
# TODO formalize this! (we need rules how clients should react, otherwise this will be a mess)
|
60
65
|
# TODO in some cases, the display_type could also be guessed from the response, I guess
|
61
66
|
@result_hints = {
|
@@ -106,6 +111,11 @@ module RHCP
|
|
106
111
|
@is_read_only = true
|
107
112
|
self
|
108
113
|
end
|
114
|
+
|
115
|
+
def accept_extra_params()
|
116
|
+
@accepts_extra_params = true
|
117
|
+
self
|
118
|
+
end
|
109
119
|
|
110
120
|
# returns the specified CommandParam
|
111
121
|
# or throws an RhcpException if the parameter does not exist
|
@@ -115,7 +125,7 @@ module RHCP
|
|
115
125
|
end
|
116
126
|
|
117
127
|
# there shouldn't be none
|
118
|
-
raise RHCP::RhcpException.new("
|
128
|
+
raise RHCP::RhcpException.new("undefined parameter '#{param_name}' in '#{self.name}'") unless existing_params.size > 0
|
119
129
|
|
120
130
|
# there can be only one
|
121
131
|
raise RHCP::RhcpException.new("BUG: duplicate parameters found") if existing_params.size > 1
|
@@ -140,8 +150,14 @@ module RHCP
|
|
140
150
|
response = RHCP::Response.new()
|
141
151
|
|
142
152
|
# check all param values for plausibility
|
143
|
-
|
144
|
-
|
153
|
+
begin
|
154
|
+
request.param_values.each do |key,value|
|
155
|
+
get_param(key).check_param_is_valid(request, value)
|
156
|
+
end
|
157
|
+
rescue Exception => ex
|
158
|
+
if (/undefined\sparameter/.match(ex.message))
|
159
|
+
raise RHCP::RhcpException.new(ex.message) unless @accepts_extra_params
|
160
|
+
end
|
145
161
|
end
|
146
162
|
|
147
163
|
# check that we've got all mandatory params
|
data/lib/rhcp/context.rb
CHANGED
@@ -9,9 +9,12 @@ module RHCP
|
|
9
9
|
# hash holding context information; similar to HTTP cookies
|
10
10
|
# TODO should we actually use cookies for transporting this info?
|
11
11
|
attr_accessor :cookies
|
12
|
+
attr_accessor :request_context_id
|
12
13
|
|
13
|
-
def initialize(cookies = {})
|
14
|
+
def initialize(cookies = {}, request_context_id = nil)
|
14
15
|
@cookies = cookies
|
16
|
+
@request_context_id = request_context_id
|
17
|
+
@request_counter = 0
|
15
18
|
end
|
16
19
|
|
17
20
|
# def to_json(*args)
|
@@ -29,6 +32,10 @@ module RHCP
|
|
29
32
|
# instance
|
30
33
|
# end
|
31
34
|
|
35
|
+
def incr_and_get_request_counter()
|
36
|
+
@request_counter += 1
|
37
|
+
end
|
38
|
+
|
32
39
|
def to_s
|
33
40
|
result = "<Context with #{@cookies.size} cookies>"
|
34
41
|
# @cookies.each do |k,v|
|
data/lib/rhcp/request.rb
CHANGED
@@ -14,6 +14,7 @@ module RHCP
|
|
14
14
|
attr_reader :command
|
15
15
|
attr_reader :param_values
|
16
16
|
attr_reader :context
|
17
|
+
attr_reader :request_count
|
17
18
|
|
18
19
|
# default constructor; will throw exceptions on invalid values
|
19
20
|
def initialize(command, some_param_values = {}, context = RHCP::Context.new())
|
@@ -23,7 +24,8 @@ module RHCP
|
|
23
24
|
@logger.debug "initializing request #{command.name} with params #{param_values}"
|
24
25
|
|
25
26
|
@context = context
|
26
|
-
|
27
|
+
@request_count = @context.incr_and_get_request_counter()
|
28
|
+
|
27
29
|
command.params.each do |param|
|
28
30
|
|
29
31
|
value_from_context = param.find_value_in_context(context)
|
@@ -119,7 +121,8 @@ module RHCP
|
|
119
121
|
{
|
120
122
|
:command_name => @command.name,
|
121
123
|
:param_values => @param_values,
|
122
|
-
:context => @context
|
124
|
+
:context => @context,
|
125
|
+
:request_count => @request_count
|
123
126
|
}
|
124
127
|
end
|
125
128
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhcp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 51
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 18
|
10
|
+
version: 0.2.18
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Philipp Traeder
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2012-01-21 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -59,7 +59,6 @@ files:
|
|
59
59
|
- lib/rhcp/broker.rb
|
60
60
|
- lib/rhcp/context.rb
|
61
61
|
- lib/rhcp/command.rb
|
62
|
-
- lib/rhcp/memcached_broker.rb
|
63
62
|
- test/rhcp/test_base.rb
|
64
63
|
- test/rhcp/command_param_test.rb
|
65
64
|
- test/rhcp/context_aware_broker_test.rb
|
@@ -1,120 +0,0 @@
|
|
1
|
-
require 'rhcp/broker'
|
2
|
-
require 'memcache'
|
3
|
-
|
4
|
-
module RHCP
|
5
|
-
|
6
|
-
class MemcachedBroker < Broker
|
7
|
-
|
8
|
-
def initialize(wrapped_broker, memcached_server = "127.0.0.1:11211", expiration_seconds = 3600)
|
9
|
-
super()
|
10
|
-
@wrapped_broker = wrapped_broker
|
11
|
-
|
12
|
-
@cache = MemCache.new(
|
13
|
-
[memcached_server]
|
14
|
-
)
|
15
|
-
@expiry_seconds = expiration_seconds
|
16
|
-
end
|
17
|
-
|
18
|
-
# TODO would be nice if we could turn the caching on and off through commands
|
19
|
-
|
20
|
-
def get_command_list(context = RHCP::Context.new())
|
21
|
-
@wrapped_broker.get_command_list(context)
|
22
|
-
end
|
23
|
-
|
24
|
-
def register_command(command)
|
25
|
-
@wrapped_broker.register_command(command)
|
26
|
-
end
|
27
|
-
|
28
|
-
def clear
|
29
|
-
@wrapped_broker.clear()
|
30
|
-
end
|
31
|
-
|
32
|
-
def get_lookup_values(request, param_name)
|
33
|
-
sorted_param_values = []
|
34
|
-
request.param_values.keys.sort.each do |key|
|
35
|
-
sorted_param_values << request.param_values[key]
|
36
|
-
end
|
37
|
-
cache_key = 'lookup_' + request.command.name + '_' + sorted_param_values.join('|')
|
38
|
-
|
39
|
-
result = nil
|
40
|
-
if ((request.context == nil) or (not request.context.cookies.has_key?('__caching.disable')))
|
41
|
-
result = @cache.get(cache_key)
|
42
|
-
end
|
43
|
-
|
44
|
-
if result == nil then
|
45
|
-
result = @wrapped_broker.get_lookup_values(request, param_name)
|
46
|
-
@cache.add(cache_key, result, @expiry_seconds)
|
47
|
-
end
|
48
|
-
|
49
|
-
result
|
50
|
-
end
|
51
|
-
|
52
|
-
def execute(request)
|
53
|
-
command = get_command(request.command.name, request.context)
|
54
|
-
|
55
|
-
result = nil
|
56
|
-
|
57
|
-
# construct the cache key out of the command name and all parameter values
|
58
|
-
sorted_param_values = []
|
59
|
-
request.param_values.keys.sort.each do |key|
|
60
|
-
sorted_param_values << request.param_values[key]
|
61
|
-
end
|
62
|
-
cache_key = request.command.name + '_' + sorted_param_values.join('|')
|
63
|
-
|
64
|
-
should_read_from_cache =
|
65
|
-
(command.is_read_only) &&
|
66
|
-
( (not command.result_hints.has_key?(:cache)) || (command.result_hints[:cache]) ) &&
|
67
|
-
((request.context == nil) or (not request.context.cookies.has_key?('__caching.disable')))
|
68
|
-
|
69
|
-
if should_read_from_cache
|
70
|
-
cached_response_json = @cache.get(cache_key)
|
71
|
-
if cached_response_json
|
72
|
-
#cached_response = JSON.parse(cached_response_json)
|
73
|
-
cached_response = RHCP::Response.reconstruct_from_json(cached_response_json)
|
74
|
-
$logger.debug("got data from cache for #{cache_key}")
|
75
|
-
cached_data = cached_response.data
|
76
|
-
|
77
|
-
# TODO why don't we just throw back the response?
|
78
|
-
response = RHCP::Response.new()
|
79
|
-
response.data = cached_data
|
80
|
-
response.status = RHCP::Response::Status::OK
|
81
|
-
|
82
|
-
# all context that has been sent with the request should be sent back
|
83
|
-
response.context = request.context.cookies
|
84
|
-
|
85
|
-
# also, we want to add all context that has been returned by the cached response
|
86
|
-
if cached_response.context != nil then
|
87
|
-
if response.context == nil then
|
88
|
-
response.context = {}
|
89
|
-
end
|
90
|
-
$logger.debug("merging in context from cached response : #{cached_response.context}")
|
91
|
-
cached_response.context.each do |k,v|
|
92
|
-
response.context[k] = v
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
response.created_at = cached_response.created_at
|
97
|
-
|
98
|
-
result = cached_data
|
99
|
-
else
|
100
|
-
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
if result == nil
|
105
|
-
response = @wrapped_broker.execute(request)
|
106
|
-
|
107
|
-
# TODO we might want to store the result in memcached nevertheless
|
108
|
-
if should_read_from_cache
|
109
|
-
# TODO do we want to cache failed command executions as well?
|
110
|
-
$logger.debug("storing data in cache for : #{cache_key}")
|
111
|
-
@cache.add(cache_key, response.to_json, @expiry_seconds)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
response
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
end
|