rsence 2.0.0.6.pre → 2.0.0.7.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,235 +11,230 @@
11
11
 
12
12
  module RSence
13
13
 
14
- =begin
15
- Transporter is the counterpart to the client's HTransporter xhr engine.
16
- =end
17
-
18
- class Transporter
14
+ # Transporter handles incoming requests targeted at RSence
15
+ # and distributes calls and data accordingly.
16
+ class Transporter
19
17
 
20
- # ValueManager syncronizes value objects
21
- require 'values/valuemanager'
18
+ # ValueManager syncronizes value objects
19
+ require 'values/valuemanager'
22
20
 
23
- # SessionManager creates, validates, stores and expires sessions
24
- require 'session/sessionmanager'
21
+ # SessionManager creates, validates, stores and expires sessions
22
+ require 'session/sessionmanager'
25
23
 
26
- # PluginManager handles all the plugins
27
- require 'plugins/pluginmanager'
24
+ # PluginManager handles all the plugins
25
+ require 'plugins/pluginmanager'
28
26
 
29
- attr_accessor :valuemanager, :sessions, :plugins
27
+ attr_accessor :valuemanager, :sessions, :plugins
30
28
 
31
- def initialize
32
- @config = ::RSence.config[:transporter_conf]
33
- @accept_req = false
34
- @valuemanager = ValueManager.new
35
- @sessions = SessionManager.new( self )
36
- @plugins = PluginManager.new( ::RSence.config[:plugin_paths], self, RSence.args[:autoupdate] )
37
- if RSence.launch_pid != Process.pid
38
- Process.kill( 'TERM', RSence.launch_pid )
29
+ def initialize
30
+ @config = ::RSence.config[:transporter_conf]
31
+ @accept_req = false
32
+ @valuemanager = ValueManager.new
33
+ @sessions = SessionManager.new( self )
34
+ @plugins = PluginManager.new( ::RSence.config[:plugin_paths], self, RSence.args[:autoupdate] )
35
+ if RSence.launch_pid != Process.pid
36
+ Process.kill( 'TERM', RSence.launch_pid )
37
+ end
39
38
  end
40
- end
41
39
 
42
- def online?
43
- @accept_req
44
- end
40
+ def online?
41
+ @accept_req
42
+ end
45
43
 
46
- def online=(state)
47
- if RSence.args[:verbose]
48
- if state
49
- puts "RSence is online now."
50
- else
51
- puts "RSence is offline now."
44
+ def online=(state)
45
+ if RSence.args[:verbose]
46
+ if state
47
+ puts "RSence is online now."
48
+ else
49
+ puts "RSence is offline now."
50
+ end
52
51
  end
52
+ @accept_req = state
53
53
  end
54
- @accept_req = state
55
- end
56
54
 
57
- def shutdown
58
- online=false
59
- @plugins.shutdown
60
- @sessions.shutdown
61
- end
55
+ def shutdown
56
+ online=false
57
+ @plugins.shutdown
58
+ @sessions.shutdown
59
+ end
62
60
 
63
- def servlet( request_type, request, response )
64
- broker_urls = ::RSence.config[:broker_urls]
65
- uri = request.fullpath
61
+ def servlet( request_type, request, response )
62
+ broker_urls = ::RSence.config[:broker_urls]
63
+ uri = request.fullpath
66
64
 
67
- if request_type == :post
68
- ## /x handles xhr without cookies
69
- if uri == broker_urls[:x] and @sessions.accept_requests
70
- xhr( request, response, { :cookies => true, :servlet => false } )
71
- return true
72
- ## /hello handles the first xhr (with cookies, for session key)
73
- elsif uri == broker_urls[:hello] and @sessions.accept_requests
74
- xhr( request, response, { :cookies => true, :servlet => false } )
75
- return true
65
+ if request_type == :post
66
+ ## /x handles xhr without cookies
67
+ if uri == broker_urls[:x] and @sessions.accept_requests
68
+ xhr( request, response, { :cookies => true, :servlet => false } )
69
+ return true
70
+ ## /hello handles the first xhr (with cookies, for session key)
71
+ elsif uri == broker_urls[:hello] and @sessions.accept_requests
72
+ xhr( request, response, { :cookies => true, :servlet => false } )
73
+ return true
74
+ else
75
+ session = {}
76
+ return @plugins.match_servlet( request_type, request, response, session )
77
+ end
76
78
  else
77
79
  session = {}
78
80
  return @plugins.match_servlet( request_type, request, response, session )
79
81
  end
80
- else
81
- session = {}
82
- return @plugins.match_servlet( request_type, request, response, session )
83
82
  end
84
- end
85
83
 
86
- # wrapper for the session manager stop client functionality
87
- def xhr_error_handler(msg,err_name,err_extra_descr='')
88
- @sessions.stop_client_with_message( msg,
89
- @config[:messages][err_name][:title],
90
- @config[:messages][err_name][:descr]+err_extra_descr,
91
- @config[:messages][err_name][:uri]
92
- )
93
- end
84
+ # wrapper for the session manager stop client functionality
85
+ def xhr_error_handler(msg,err_name,err_extra_descr='')
86
+ @sessions.stop_client_with_message( msg,
87
+ @config[:messages][err_name][:title],
88
+ @config[:messages][err_name][:descr]+err_extra_descr,
89
+ @config[:messages][err_name][:uri]
90
+ )
91
+ end
94
92
 
95
- # wrapper for tracebacks in xhr
96
- def xhr_traceback_handler(e,err_descr='Transporter::UnspecifiedError')
97
- puts "=="*40 if RSence.args[:debug]
98
- puts err_descr
99
- if RSence.args[:debug]
100
- puts "--"*40
101
- puts e.message
102
- puts " #{e.backtrace.join("\n ")}"
103
- puts "=="*40
93
+ # wrapper for tracebacks in xhr
94
+ def xhr_traceback_handler(e,err_descr='Transporter::UnspecifiedError')
95
+ puts "=="*40 if RSence.args[:debug]
96
+ puts err_descr
97
+ if RSence.args[:debug]
98
+ puts "--"*40
99
+ puts e.message
100
+ puts " #{e.backtrace.join("\n ")}"
101
+ puts "=="*40
102
+ end
104
103
  end
105
- end
106
104
 
107
- ## handles incoming XMLHttpRequests from the browser
108
- def xhr(request, response, options = { :cookies => false, :servlet => false } )
105
+ ## handles incoming XMLHttpRequests from the browser
106
+ def xhr(request, response, options = { :cookies => false, :servlet => false } )
109
107
 
110
- session_conf = ::RSence.config[:session_conf]
108
+ session_conf = ::RSence.config[:session_conf]
111
109
 
112
- options[:cookies] = false unless options.has_key?(:cookies)
110
+ options[:cookies] = false unless options.has_key?(:cookies)
113
111
 
114
- if session_conf[:session_cookies] and session_conf[:trust_cookies]
115
- options[:cookies] = true
116
- end
112
+ if session_conf[:session_cookies] and session_conf[:trust_cookies]
113
+ options[:cookies] = true
114
+ end
117
115
 
118
- # Creates the msg object, also checks or creates a new session; verifies session keys and such
119
- msg = @sessions.init_msg( request, response, options )
116
+ # Creates the msg object, also checks or creates a new session; verifies session keys and such
117
+ msg = @sessions.init_msg( request, response, options )
120
118
 
121
- response_success = true
119
+ response_success = true
122
120
 
123
- # If the client encounters an error, display error message
124
- if request.query.has_key?('err_msg')
125
- response_success = false
126
- client_error_msg = request.query['err_msg'].inspect
127
- puts "\nCLIENT ERROR:\n#{client_error_msg}\n" if RSence.args[:debug]
128
- xhr_error_handler(msg,:client_error,client_error_msg)
129
- end
121
+ # If the client encounters an error, display error message
122
+ if request.query.has_key?('err_msg')
123
+ response_success = false
124
+ client_error_msg = request.query['err_msg'].inspect
125
+ puts "\nCLIENT ERROR:\n#{client_error_msg}\n" if RSence.args[:debug]
126
+ xhr_error_handler(msg,:client_error,client_error_msg)
127
+ end
130
128
 
131
- # If the session is valid, continue:
132
- if msg.ses_valid and response_success
129
+ # If the session is valid, continue:
130
+ if msg.ses_valid and response_success
133
131
 
134
- # If cookies are true, it means the url base needs to
135
- # be changed from /hello to /x to prevent further cookie juggling.
136
- if options[:cookies] and not options[:servlet]
137
- msg.reply("COMM.Transporter.url=#{::RSence.config[:broker_urls][:x].to_json};")
138
- end
132
+ # If cookies are true, it means the url base needs to
133
+ # be changed from /hello to /x to prevent further cookie juggling.
134
+ if options[:cookies] and not options[:servlet]
135
+ msg.reply("COMM.Transporter.url=#{::RSence.config[:broker_urls][:x].to_json};")
136
+ end
139
137
 
140
- # Appends a 'new session.' message for new sessions in RSence.args[:verbose]:
141
- puts "new session." if msg.new_session and RSence.args[:verbose]
142
- puts "restored session." if msg.restored_session and RSence.args[:verbose]
143
- puts "clone source." if msg.cloned_targets and RSence.args[:verbose]
144
- puts "clone target." if msg.cloned_source and RSence.args[:verbose]
138
+ # Appends a 'new session.' message for new sessions in RSence.args[:verbose]:
139
+ puts "new session." if msg.new_session and RSence.args[:verbose]
140
+ puts "restored session." if msg.restored_session and RSence.args[:verbose]
141
+ puts "clone source." if msg.cloned_targets and RSence.args[:verbose]
142
+ puts "clone target." if msg.cloned_source and RSence.args[:verbose]
145
143
 
146
- ## Pass the client XML to the value manager
147
- if request.query.has_key?( 'values' )
148
- syncdata_str = request.query[ 'values' ]
149
- begin
150
- @valuemanager.xhr( msg, syncdata_str )
151
- rescue => e
152
- response_success = false
153
- xhr_error_handler( msg, :valuemanager_xhr_error, e.message )
154
- xhr_traceback_handler( e, "Transporter::ValueManagerXHRError: @valuemanager.xhr failed." )
144
+ ## Pass the client XML to the value manager
145
+ if request.query.has_key?( 'values' )
146
+ syncdata_str = request.query[ 'values' ]
147
+ begin
148
+ @valuemanager.xhr( msg, syncdata_str )
149
+ rescue => e
150
+ response_success = false
151
+ xhr_error_handler( msg, :valuemanager_xhr_error, e.message )
152
+ xhr_traceback_handler( e, "Transporter::ValueManagerXHRError: @valuemanager.xhr failed." )
153
+ end
155
154
  end
156
- end
157
155
 
158
- ## Calls the restore_ses of plugins, when a session is restored (page reload with previously active session)
159
- if msg.restored_session
156
+ ## Calls the restore_ses of plugins, when a session is restored (page reload with previously active session)
157
+ if msg.restored_session
158
+
159
+ msg.session[:deps] = []
160
160
 
161
- msg.session[:deps] = []
161
+ if msg.cloned_source
162
+ begin
163
+ @plugins.delegate( :cloned_target, msg, msg.cloned_source )
164
+ rescue => e
165
+ response_success = false
166
+ xhr_error_handler( msg, :plugin_delegate_cloned_target_error, e.message )
167
+ xhr_traceback_handler( e, "Transporter::PluginDelegateClonedTargetError: @plugins.delegate 'cloned_target' failed." )
168
+ end
169
+ end
162
170
 
163
- if msg.cloned_source
164
171
  begin
165
- @plugins.delegate( :cloned_target, msg, msg.cloned_source )
172
+ @plugins.delegate( :restore_ses, msg )
166
173
  rescue => e
167
174
  response_success = false
168
- xhr_error_handler( msg, :plugin_delegate_cloned_target_error, e.message )
169
- xhr_traceback_handler( e, "Transporter::PluginDelegateClonedTargetError: @plugins.delegate 'cloned_target' failed." )
175
+ xhr_error_handler( msg, :plugin_delegate_restore_ses_error, e.message )
176
+ xhr_traceback_handler( e, "Transporter::PluginDelegateRestoreSesError: @plugins.delegate 'restore_ses' failed." )
170
177
  end
171
- end
172
178
 
179
+ elsif msg.new_session
180
+ begin
181
+ @plugins.delegate( :init_ses, msg )
182
+ rescue => e
183
+ response_success = false
184
+ xhr_error_handler( msg, :plugin_delegate_init_ses_error, e.message )
185
+ xhr_traceback_handler( e, "Transporter::PluginDelegateInitSesError: @plugins.delegate 'init_ses' failed." )
186
+ end
187
+ elsif msg.cloned_targets
188
+ begin
189
+ @plugins.delegate( :cloned_source, msg, msg.cloned_targets )
190
+ rescue => e
191
+ response_success = false
192
+ xhr_error_handler( msg, :plugin_delegate_cloned_source_error, e.message )
193
+ xhr_traceback_handler( e, "Transporter::PluginDelegateClonedSourceError: @plugins.delegate 'cloned_source' failed." )
194
+ end
195
+ end
196
+
197
+ ## Calls validators for changed values
173
198
  begin
174
- @plugins.delegate( :restore_ses, msg )
199
+ @valuemanager.validate( msg )
175
200
  rescue => e
176
201
  response_success = false
177
- xhr_error_handler( msg, :plugin_delegate_restore_ses_error, e.message )
178
- xhr_traceback_handler( e, "Transporter::PluginDelegateRestoreSesError: @plugins.delegate 'restore_ses' failed." )
202
+ xhr_error_handler( msg, :valuemanager_validate_error, e.message )
203
+ xhr_traceback_handler( e, "Transporter::ValueManagerValidateError: @valuemanager.validate failed." )
179
204
  end
180
-
181
- elsif msg.new_session
205
+
206
+ ### Allows every plugin to respond to the idle call
182
207
  begin
183
- @plugins.delegate( :init_ses, msg )
208
+ @plugins.delegate( :idle, msg )
184
209
  rescue => e
185
210
  response_success = false
186
- xhr_error_handler( msg, :plugin_delegate_init_ses_error, e.message )
187
- xhr_traceback_handler( e, "Transporter::PluginDelegateInitSesError: @plugins.delegate 'init_ses' failed." )
211
+ xhr_error_handler( msg, :plugin_idle_error, e.message )
212
+ xhr_traceback_handler( e, "Transporter::PluginIdleError: @plugins.idle failed." )
188
213
  end
189
- elsif msg.cloned_targets
214
+
215
+ ### Processes outgoing values to client
190
216
  begin
191
- @plugins.delegate( :cloned_source, msg, msg.cloned_targets )
217
+ @valuemanager.sync_client( msg )
192
218
  rescue => e
193
219
  response_success = false
194
- xhr_error_handler( msg, :plugin_delegate_cloned_source_error, e.message )
195
- xhr_traceback_handler( e, "Transporter::PluginDelegateClonedSourceError: @plugins.delegate 'cloned_source' failed." )
220
+ xhr_error_handler( msg, :valuemanager_sync_client_error, e.message )
221
+ xhr_traceback_handler( e, "Transporter::ValueManagerSyncClientError: @valuemanager.sync_client failed." )
196
222
  end
197
- end
198
223
 
199
- ## Calls validators for changed values
200
- begin
201
- @valuemanager.validate( msg )
202
- rescue => e
203
- response_success = false
204
- xhr_error_handler( msg, :valuemanager_validate_error, e.message )
205
- xhr_traceback_handler( e, "Transporter::ValueManagerValidateError: @valuemanager.validate failed." )
206
- end
224
+ else
207
225
 
208
- ### Allows every plugin to respond to the idle call
209
- begin
210
- @plugins.delegate( :idle, msg )
211
- rescue => e
226
+ # session is not valid, the error was set in SessionManager
212
227
  response_success = false
213
- xhr_error_handler( msg, :plugin_idle_error, e.message )
214
- xhr_traceback_handler( e, "Transporter::PluginIdleError: @plugins.idle failed." )
215
- end
216
228
 
217
- ### Processes outgoing values to client
218
- begin
219
- @valuemanager.sync_client( msg )
220
- rescue => e
221
- response_success = false
222
- xhr_error_handler( msg, :valuemanager_sync_client_error, e.message )
223
- xhr_traceback_handler( e, "Transporter::ValueManagerSyncClientError: @valuemanager.sync_client failed." )
224
229
  end
225
-
226
- else
227
-
228
- # session is not valid, the error was set in SessionManager
229
- response_success = false
230
-
231
- end
232
-
233
- msg.response_success = response_success
234
230
 
235
- unless options[:servlet]
236
- msg.response_done()
237
- end
231
+ msg.response_success = response_success
238
232
 
239
- return msg
233
+ unless options[:servlet]
234
+ msg.response_done()
235
+ end
240
236
 
237
+ return msg
238
+ end
241
239
  end
242
-
243
- end
244
-
245
240
  end
data/lib/util/gzstring.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'zlib'
2
2
 
3
+ # Implements the +write+ method for strings, used with zlib to
4
+ # use GZStrings as the target for compression.
3
5
  class GZString < String
4
6
  alias write <<
5
7
  end
data/lib/values/hvalue.rb CHANGED
@@ -10,317 +10,317 @@
10
10
  module RSence
11
11
 
12
12
 
13
- ## HValue is the server-side representation of the client's HValue object.
14
- ## It's the 'messenger' to syncronize server-client data and is smart enough
15
- ## to validate and process itself as well as tell the client-side
16
- ## representation of itself.
17
- class HValue
13
+ ## HValue is the server-side representation of the client's HValue object.
14
+ ## It's the 'messenger' to syncronize server-client data and is smart enough
15
+ ## to validate and process itself as well as tell the client-side
16
+ ## representation of itself.
17
+ class HValue
18
18
 
19
- attr_reader :valid, :sync, :val_id, :data, :members
20
- attr_writer :valid, :val_id
19
+ attr_reader :valid, :sync, :val_id, :data, :members
20
+ attr_writer :valid, :val_id
21
21
 
22
- # Method for binding the value to the session data.
23
- def add( msg )
22
+ # Method for binding the value to the session data.
23
+ def add( msg )
24
24
 
25
- # get the value storage from the session data
26
- session_values = msg.session[:values][:by_id]
25
+ # get the value storage from the session data
26
+ session_values = msg.session[:values][:by_id]
27
27
 
28
- ## Store the object here
29
- session_values[ @val_id ] = self
28
+ ## Store the object here
29
+ session_values[ @val_id ] = self
30
30
 
31
- ## Sends the client-side description
32
- restore( msg )
31
+ ## Sends the client-side description
32
+ restore( msg )
33
33
 
34
- ## Set the valid flag, so we know that the value is initially in sync
35
- @valid = true
36
- end
34
+ ## Set the valid flag, so we know that the value is initially in sync
35
+ @valid = true
36
+ end
37
37
 
38
- ## (Re-)Send the client-size representation
39
- def restore( msg )
38
+ ## (Re-)Send the client-size representation
39
+ def restore( msg )
40
40
 
41
- ## Tags itself as a new value from the client's point of view
42
- @is_new_to_client = true
41
+ ## Tags itself as a new value from the client's point of view
42
+ @is_new_to_client = true
43
43
 
44
- add_to_sync( msg )
44
+ add_to_sync( msg )
45
45
 
46
- end
46
+ end
47
47
 
48
- # +HValue+ constructor. Binds HValue automatically to the +Message+ instance
49
- # given as parameter. Data given as second parameter.
50
- def initialize( msg, data, meta = { :name => nil } )
48
+ # +HValue+ constructor. Binds HValue automatically to the +Message+ instance
49
+ # given as parameter. Data given as second parameter.
50
+ def initialize( msg, data, meta = { :name => nil } )
51
51
 
52
- ## Get an unique integer id for the value
53
- if RSence.args[:debug] and meta[:name] and not msg.valuemanager.id_exists?( msg, meta[:name] )
54
- @val_id = meta[:name]
55
- else
56
- @val_id = msg.valuemanager.randgen.gen
57
- end
52
+ ## Get an unique integer id for the value
53
+ if RSence.args[:debug] and meta[:name] and not msg.valuemanager.id_exists?( msg, meta[:name] )
54
+ @val_id = meta[:name]
55
+ else
56
+ @val_id = msg.valuemanager.randgen.gen
57
+ end
58
58
 
59
- ## set the data of the hvalue
60
- set( msg, data, true )
59
+ ## set the data of the hvalue
60
+ set( msg, data, true )
61
61
 
62
- ## the @sync flag is raised, when the client data is older than the server data
63
- @sync = false
62
+ ## the @sync flag is raised, when the client data is older than the server data
63
+ @sync = false
64
64
 
65
- ## the @is_valid flas is lowered, when the client data is newer than the server data
66
- @is_valid = true
65
+ ## the @is_valid flas is lowered, when the client data is newer than the server data
66
+ @is_valid = true
67
67
 
68
- ## Bind the value to the value manager and report it to the client
69
- add( msg )
68
+ ## Bind the value to the value manager and report it to the client
69
+ add( msg )
70
70
 
71
- ## storage for validator bindings
72
- @members = {}
71
+ ## storage for validator bindings
72
+ @members = {}
73
73
 
74
- end
74
+ end
75
75
 
76
- # Binds the value to the plugin method (both as
77
- # strings; plugin as the name registered in PluginManager)
78
- #
79
- # It uses strings instead of '...method(...)' because
80
- # it won't work with marshal. Strings are easier and work
81
- # as well.
82
- def bind( plugin_name, method_name )
83
- @members[plugin_name] = [] unless @members.has_key?( plugin_name )
84
- @members[plugin_name].push( method_name ) unless @members[plugin_name].include?( method_name )
85
- return true
86
- end
76
+ # Binds the value to the plugin method (both as
77
+ # strings; plugin as the name registered in PluginManager)
78
+ #
79
+ # It uses strings instead of '...method(...)' because
80
+ # it won't work with marshal. Strings are easier and work
81
+ # as well.
82
+ def bind( plugin_name, method_name )
83
+ @members[plugin_name] = [] unless @members.has_key?( plugin_name )
84
+ @members[plugin_name].push( method_name ) unless @members[plugin_name].include?( method_name )
85
+ return true
86
+ end
87
87
 
88
- # Releases the binding of the value, both params as
89
- # in bind, but optional (false = 'wildcard')
90
- def release( plugin_name=false, method_name=false )
91
- return release_all if not plugin_name and not method_name
92
- return false unless @members.has_key?( plugin_name )
93
- if not method_name
94
- @members.delete( plugin_name )
95
- else
96
- @members[plugin_name].slice!(@members[plugin_name].index( method_name )) if @members[plugin_name].include?(method_name)
88
+ # Releases the binding of the value, both params as
89
+ # in bind, but optional (false = 'wildcard')
90
+ def release( plugin_name=false, method_name=false )
91
+ return release_all if not plugin_name and not method_name
92
+ return false unless @members.has_key?( plugin_name )
93
+ if not method_name
94
+ @members.delete( plugin_name )
95
+ else
96
+ @members[plugin_name].slice!(@members[plugin_name].index( method_name )) if @members[plugin_name].include?(method_name)
97
+ end
98
+ return true
97
99
  end
98
- return true
99
- end
100
100
 
101
- ## Releases all members.
102
- def release_all
103
- @members = {}
104
- return true
105
- end
101
+ ## Releases all members.
102
+ def release_all
103
+ @members = {}
104
+ return true
105
+ end
106
106
 
107
- # The unbind method can be used as an alias to release (as in the client).
108
- alias unbind release
107
+ # The unbind method can be used as an alias to release (as in the client).
108
+ alias unbind release
109
109
 
110
- # Tell all bound instances that the value is changed.
111
- def tell( msg )
112
- invalid_count = 0
113
- @members.each_key do |plugin_name|
114
- @members[plugin_name].each do |method_name|
115
- invalid_count += 1 unless msg.plugins.run_plugin( plugin_name, method_name, msg, self )
110
+ # Tell all bound instances that the value is changed.
111
+ def tell( msg )
112
+ invalid_count = 0
113
+ @members.each_key do |plugin_name|
114
+ @members[plugin_name].each do |method_name|
115
+ invalid_count += 1 unless msg.plugins.run_plugin( plugin_name, method_name, msg, self )
116
+ end
117
+ end
118
+ if invalid_count == 0
119
+ @is_valid = true
120
+ msg.session[:values][:check].delete( @val_id )
116
121
  end
117
122
  end
118
- if invalid_count == 0
119
- @is_valid = true
120
- msg.session[:values][:check].delete( @val_id )
121
- end
122
- end
123
123
 
124
- # Handle client updates.
125
- def from_client( msg, data )
124
+ # Handle client updates.
125
+ def from_client( msg, data )
126
126
 
127
- # only process changes, if different from the one already stored.
128
- if @data != data
127
+ # only process changes, if different from the one already stored.
128
+ if @data != data
129
129
 
130
- ## set takes care of the setting..
131
- @data = data
130
+ ## set takes care of the setting..
131
+ @data = data
132
132
 
133
- ## change the valid state, because the value was set by the client!
134
- @is_valid = false
133
+ ## change the valid state, because the value was set by the client!
134
+ @is_valid = false
135
135
 
136
- ## add the id to the values to be checked
137
- check_ids = msg.session[:values][:check]
138
- unless check_ids.include?( @val_id )
139
- check_ids.push( @val_id )
136
+ ## add the id to the values to be checked
137
+ check_ids = msg.session[:values][:check]
138
+ unless check_ids.include?( @val_id )
139
+ check_ids.push( @val_id )
140
+ end
140
141
  end
141
- end
142
142
 
143
- end
143
+ end
144
144
 
145
- def add_to_sync( msg )
146
- ## add the id to the values to be syncronized (to client)
147
- sync_ids = msg.session[:values][:sync]
148
- unless sync_ids.include?( @val_id )
149
- sync_ids.push( @val_id )
145
+ def add_to_sync( msg )
146
+ ## add the id to the values to be syncronized (to client)
147
+ sync_ids = msg.session[:values][:sync]
148
+ unless sync_ids.include?( @val_id )
149
+ sync_ids.push( @val_id )
150
+ end
150
151
  end
151
- end
152
152
 
153
- # Sets the data.
154
- def set( msg, data, dont_tell_client=false )
153
+ # Sets the data.
154
+ def set( msg, data, dont_tell_client=false )
155
155
 
156
- @data = data
156
+ @data = data
157
157
 
158
- # won't tell the client about the change, usually not needed
159
- unless dont_tell_client
160
- ## update the flags
161
- @sync = false
162
- @is_valid = true
158
+ # won't tell the client about the change, usually not needed
159
+ unless dont_tell_client
160
+ ## update the flags
161
+ @sync = false
162
+ @is_valid = true
163
163
 
164
- add_to_sync( msg )
164
+ add_to_sync( msg )
165
+ end
165
166
  end
166
- end
167
167
 
168
- # Tell the client that the value changed.
169
- def to_client( msg )
170
- if @is_new_to_client
171
- ## Initialize a new client value
172
- init_str = "COMM.Values.create(#{@val_id.to_json},#{@data.to_json});"
173
- msg.reply_value( init_str )
174
- @is_new_to_client = false
175
- else
176
- ## Sets the client value
177
- msg.reply_value "HVM.s(#{@val_id.to_json},#{@data.to_json});"
168
+ # Tell the client that the value changed.
169
+ def to_client( msg )
170
+ if @is_new_to_client
171
+ ## Initialize a new client value
172
+ init_str = "COMM.Values.create(#{@val_id.to_json},#{@data.to_json});"
173
+ msg.reply_value( init_str )
174
+ @is_new_to_client = false
175
+ else
176
+ ## Sets the client value
177
+ msg.reply_value "HVM.s(#{@val_id.to_json},#{@data.to_json});"
178
+ end
178
179
  end
179
- end
180
180
 
181
- # Clean up self.
182
- def die( msg=false )
181
+ # Clean up self.
182
+ def die( msg=false )
183
183
 
184
- release_all
184
+ release_all
185
185
 
186
- # get the value storage from the session data
187
- session_values = msg.session[:values][:by_id]
186
+ # get the value storage from the session data
187
+ session_values = msg.session[:values][:by_id]
188
188
 
189
- ## Store the object here
190
- session_values.delete( @val_id )
189
+ ## Store the object here
190
+ session_values.delete( @val_id )
191
191
 
192
- if msg and not @is_new_to_client
193
- msg.reply_value("HVM.del(#{@val_id.to_json});")
192
+ if msg and not @is_new_to_client
193
+ msg.reply_value("HVM.del(#{@val_id.to_json});")
194
+ end
194
195
  end
195
- end
196
196
 
197
- end
197
+ end
198
198
 
199
- class UploadValue < HValue
199
+ class UploadValue < HValue
200
200
 
201
- @state_responders = {
202
- :ready => [], # id == 0
203
- :started => [], # id == 1
204
- :process => [], # id == 2 ; also uses bind
205
- 3 => [], # id == 3
206
- 4 => [], # id == 4
207
- :error => [] # id < 0
208
- }
201
+ @state_responders = {
202
+ :ready => [], # id == 0
203
+ :started => [], # id == 1
204
+ :process => [], # id == 2 ; also uses bind
205
+ 3 => [], # id == 3
206
+ 4 => [], # id == 4
207
+ :error => [] # id < 0
208
+ }
209
209
 
210
- @upload_state = 0
211
- @upload_key = ''
212
- @uploads = []
210
+ @upload_state = 0
211
+ @upload_key = ''
212
+ @uploads = []
213
213
 
214
- # the data should contain both state and key in the value
215
- def from_client( msg, data )
214
+ # the data should contain both state and key in the value
215
+ def from_client( msg, data )
216
216
 
217
- ## change the valid state, because the value was set by the client!
218
- @is_valid = data.include?(':::')
217
+ ## change the valid state, because the value was set by the client!
218
+ @is_valid = data.include?(':::')
219
219
 
220
- # the state and key are separated by the ':::' delimitter string
221
- if @is_valid
220
+ # the state and key are separated by the ':::' delimitter string
221
+ if @is_valid
222
222
 
223
- # split state and key using the delimitter
224
- (upload_state, upload_key) = data.split(':::')
223
+ # split state and key using the delimitter
224
+ (upload_state, upload_key) = data.split(':::')
225
225
 
226
- # the state is a number
227
- upload_state = upload_state.to_i
226
+ # the state is a number
227
+ upload_state = upload_state.to_i
228
228
 
229
- @upload_state = upload_state
230
- @upload_key = upload_key
229
+ @upload_state = upload_state
230
+ @upload_key = upload_key
231
231
 
232
- # negative states are errors
233
- if upload_state < 0
234
- # "upload error: #{upload_state}"
235
- # (parse the error)
236
- unless @state_respders[:error].empty?
237
- @state_responders[:error].each do |plugin_name,method_name|
238
- msg.run( plugin_name,method_name,msg,self,upload_state )
232
+ # negative states are errors
233
+ if upload_state < 0
234
+ # "upload error: #{upload_state}"
235
+ # (parse the error)
236
+ unless @state_respders[:error].empty?
237
+ @state_responders[:error].each do |plugin_name,method_name|
238
+ msg.run( plugin_name,method_name,msg,self,upload_state )
239
+ end
239
240
  end
240
- end
241
241
 
242
- # the default state, 0 means the ui is ready to send an
243
- # upload and ticketserve is ready to receive it
244
- elsif upload_state == 0
245
- # "upload state: ready to upload."
246
- # (do nothing)
242
+ # the default state, 0 means the ui is ready to send an
243
+ # upload and ticketserve is ready to receive it
244
+ elsif upload_state == 0
245
+ # "upload state: ready to upload."
246
+ # (do nothing)
247
247
 
248
- unless @state_respders[:ready].empty?
249
- @state_responders[:ready].each do |plugin_name,method_name|
250
- msg.run( plugin_name,method_name,msg,self,upload_state )
248
+ unless @state_respders[:ready].empty?
249
+ @state_responders[:ready].each do |plugin_name,method_name|
250
+ msg.run( plugin_name,method_name,msg,self,upload_state )
251
+ end
251
252
  end
252
- end
253
253
 
254
- # this state means the upload's transfer is started and progressing
255
- elsif upload_state == 1
256
- # "upload state: upload started."
257
- # (show progress bar)
254
+ # this state means the upload's transfer is started and progressing
255
+ elsif upload_state == 1
256
+ # "upload state: upload started."
257
+ # (show progress bar)
258
258
 
259
- unless @state_respders[:started].empty?
260
- @state_responders[:started].each do |plugin_name,method_name|
261
- msg.run( plugin_name,method_name,msg,self,upload_state )
259
+ unless @state_respders[:started].empty?
260
+ @state_responders[:started].each do |plugin_name,method_name|
261
+ msg.run( plugin_name,method_name,msg,self,upload_state )
262
+ end
262
263
  end
263
- end
264
264
 
265
- # this state means the upload's transfer is complete,
266
- # but the uploaded data hasn't been handled yet.
267
- elsif upload_state == 2
268
- # "upload state: waiting to process."
265
+ # this state means the upload's transfer is complete,
266
+ # but the uploaded data hasn't been handled yet.
267
+ elsif upload_state == 2
268
+ # "upload state: waiting to process."
269
269
 
270
- uploads = msg.plugins[:ticketservices].get_uploads(upload_key,true)
271
- if uploads.size == 1
272
- uploaded_data = uploads[0]
270
+ uploads = msg.plugins[:ticketservices].get_uploads(upload_key,true)
271
+ if uploads.size == 1
272
+ uploaded_data = uploads[0]
273
273
 
274
- # only process changes, if different from the one already stored.
275
- if uploaded_data != @data
274
+ # only process changes, if different from the one already stored.
275
+ if uploaded_data != @data
276
276
 
277
- @data = uploaded_data
277
+ @data = uploaded_data
278
278
 
279
- ## add the id to the values to be checked
280
- check_ids = msg.session[:values][:check]
281
- unless check_ids.include?( @val_id )
282
- check_ids.push( @val_id )
283
- end
279
+ ## add the id to the values to be checked
280
+ check_ids = msg.session[:values][:check]
281
+ unless check_ids.include?( @val_id )
282
+ check_ids.push( @val_id )
283
+ end
284
284
 
285
+ end
286
+ msg.plugins[:ticketservices].del_uploads(upload_key,msg.ses_id)
287
+ else
288
+ # "upload, amount of uploads: #{uploads.size}"
285
289
  end
286
- msg.plugins[:ticketservices].del_uploads(upload_key,msg.ses_id)
287
- else
288
- # "upload, amount of uploads: #{uploads.size}"
289
- end
290
290
 
291
- #
292
- hvalue.set(msg,"3:::#{upload_key}")
291
+ #
292
+ hvalue.set(msg,"3:::#{upload_key}")
293
293
 
294
- msg.console( "upload state: set to ack" )
294
+ msg.console( "upload state: set to ack" )
295
295
 
296
- elsif upload_state == 3
297
- # "upload state: waiting for user ack."
298
- # (do nothing)
296
+ elsif upload_state == 3
297
+ # "upload state: waiting for user ack."
298
+ # (do nothing)
299
299
 
300
- msg.console( "upload state: waiting user ack" )
300
+ msg.console( "upload state: waiting user ack" )
301
301
 
302
302
 
303
- elsif upload_state == 4
304
- # "upload state: user wants to upload again."
305
- # (set a new upload key, )
303
+ elsif upload_state == 4
304
+ # "upload state: user wants to upload again."
305
+ # (set a new upload key, )
306
306
 
307
- msg.console( "upload state: ack, getting new key" )
307
+ msg.console( "upload state: ack, getting new key" )
308
308
 
309
309
 
310
- setup_upload( msg, hvalue )
310
+ setup_upload( msg, hvalue )
311
311
 
312
312
 
313
- else
314
- # "upload unknown state: #{upload_state.inspect}"
313
+ else
314
+ # "upload unknown state: #{upload_state.inspect}"
315
+ end
315
316
  end
317
+ return true
318
+ end
319
+ def setup_upload(msg,hvalue,size_bytes=500*1024,accept_mime=/image\/(.*?)/,allow_multi=false)
320
+ upload_key = msg.plugins[:ticketservices].upload_key(msg,hvalue.val_id,size_bytes,accept_mime,allow_multi)
321
+ hvalue.set( msg, upload_key )
316
322
  end
317
- return true
318
- end
319
- def setup_upload(msg,hvalue,size_bytes=500*1024,accept_mime=/image\/(.*?)/,allow_multi=false)
320
- upload_key = msg.plugins[:ticketservices].upload_key(msg,hvalue.val_id,size_bytes,accept_mime,allow_multi)
321
- hvalue.set( msg, upload_key )
322
323
  end
323
- end
324
324
 
325
325
  end
326
326