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.
@@ -23,8 +23,9 @@ require 'plugins/plugin_plugins'
23
23
  module ::RSence
24
24
  module Plugins
25
25
 
26
- # plugin.rb contains the Plugin skeleton class
27
26
  require 'plugins/plugin'
27
+
28
+ # The PluginMaker mimic class creates the Plugin class from PluginTemplate
28
29
  def self.PluginMaker
29
30
  lambda do |ns|
30
31
  klass = Class.new( PluginTemplate ) do
@@ -39,9 +40,9 @@ module ::RSence
39
40
  end
40
41
  end
41
42
 
42
- # gui_plugin.rb is an extension of Plugin that uses
43
- # GUIParser to init the gui automatically.
44
43
  require 'plugins/gui_plugin'
44
+
45
+ # The GUIPluginMaker mimic class creates the GUIPlugin class from GUIPluginTemplate
45
46
  def self.GUIPluginMaker
46
47
  lambda do |ns|
47
48
  klass = Class.new( GUIPluginTemplate ) do
@@ -56,8 +57,9 @@ module ::RSence
56
57
  end
57
58
  end
58
59
 
59
- # servlet includes the Servlet class, for handling any requests / responses
60
60
  require 'plugins/servlet'
61
+
62
+ # The ServletMaker mimic class creates the Servlet class from ServletTemplate
61
63
  def self.ServletMaker
62
64
  lambda do |ns|
63
65
  klass = Class.new( ServletTemplate ) do
@@ -72,6 +74,11 @@ module ::RSence
72
74
  end
73
75
  end
74
76
 
77
+ # Loads bundle according to the +params+ hash.
78
+ # Some essential params:
79
+ # :src_path => '/path/of/the_plugin/the_plugin.rb'
80
+ # :bundle_path => '/path/of/the_plugin'
81
+ # :bundle_name => :the_plugin
75
82
  def self.bundle_loader( params )
76
83
  src_path = params[:src_path]
77
84
  begin
@@ -6,15 +6,16 @@
6
6
  # with this software package. If not, contact licensing@riassence.com
7
7
  ##
8
8
 
9
- ## Register the Servlet with a regular expression that
10
- ## should match its uri. Alternatively just a string, but
11
- ## that needs to be an exact match.
12
9
  module ::RSence
13
10
  module Plugins
11
+
12
+ ## Use the Servlet class to create responders for urls and methods.
14
13
  class ServletTemplate
14
+
15
15
  include PluginUtil
16
+
16
17
  def self.bundle_type; :Servlet; end
17
- # Initializes and registers the ServletPlugin.
18
+
18
19
  def initialize( bundle_name, bundle_info, bundle_path, plugin_manager )
19
20
  @info = bundle_info
20
21
  @name = bundle_name
@@ -24,8 +25,8 @@ module ::RSence
24
25
  @inited = false
25
26
  end
26
27
 
27
- # Servlet ID
28
28
  attr_reader :name, :path, :info, :inited
29
+
29
30
  def register # :nodoc
30
31
  @plugins.register_bundle( self, @name )
31
32
  @inited = true
@@ -44,15 +45,15 @@ module ::RSence
44
45
  def score
45
46
  return 100
46
47
  end
47
-
48
+
48
49
  # Extend to do any GET request processing. Not doing anything by default.
49
50
  def get( req, res, ses )
50
-
51
+
51
52
  end
52
-
53
+
53
54
  # Extend to do any POST request processing. Not doing anything by default.
54
55
  def post( req, res, ses )
55
-
56
+
56
57
  end
57
58
  end
58
59
  end
data/lib/session/msg.rb CHANGED
@@ -7,285 +7,286 @@
7
7
  ##
8
8
 
9
9
  module RSence
10
- require 'util/gzstring'
10
+ require 'util/gzstring'
11
11
 
12
- ## Due to the single instance architecture of +Plugin+, instances of Message
13
- ## class are used for communication between sessions and +Plugin+ instance.
14
- ## The +Message+ instance contains session and request-response related
15
- ## mappings and utility methods.
16
- ##
17
- ## The Message object is initialized as 'msg' in SessionManager.
18
- ## It's passed around the system as the user/session -object namespace,
19
- ## much like 'self' is passed around in python methods.
20
- ##
21
- ## Using the msg object saves considerate amounts of CPU cycles and memory,
22
- ## because it allows single instances of any classes that handle user data.
23
- ##
24
- ## == HValue Initialization Example
25
- ## +HValue+ is closely related to +Message+ as instances of +HValue+ are
26
- ## used to send data between sessions and the server. This is a small
27
- ## code snippet about how to initialize several HValues as the session
28
- ## is initialized.
29
- ##
30
- ## def init_ses( msg )
31
- ## msg.session[:session_name] = {
32
- ## :hvalue1 => HValue.new( msg, @firstvalue ),
33
- ## :hvalue2 => HValue.new( msg, @secondvalue ),
34
- ## :hvalue3 => HValue.new( msg, @thirdvalue )
35
- ## }
36
- ## end
37
- ##
38
-
39
- class Message
40
-
41
- # Session data placeholder, assigned by SessionManager.
42
- attr_accessor :session
43
-
44
- # New session flag, check it in your code to decide
45
- # what to do, when a new session is encountered.
46
- # In plugins, this usually means that some Values
47
- # need to be created and bound or possibly that a
48
- # user_id mapping needs to be done.
49
- attr_accessor :new_session
50
-
51
- # Old session first xhr flag, check it in your code
52
- # to decide what to do, when a restored session is
53
- # encountered. The old Values are automatically present,
54
- # so you should at least not re-create or re-bind them.
55
- attr_accessor :restored_session
56
-
57
- # Contains the source ses on the first request after this
58
- # session was cloned from another session.
59
- attr_accessor :cloned_source
60
-
61
- # Contains the target sessions packed in an array on
62
- # the first request after another session was cloned
63
- # from this session.
64
- attr_accessor :cloned_targets
65
-
66
- # The session is not valid by default, it's set
67
- # by SessionManager, if everything seems ok.
68
- attr_accessor :ses_valid
69
-
70
- # The http request object.
71
- attr_accessor :request
72
-
73
- # The http response object.
74
- attr_accessor :response
75
-
76
- # Response output.
77
- attr_accessor :buffer
78
-
79
- attr_accessor :value_buffer
80
-
81
- # The request success flag.
82
- attr_accessor :response_success
83
-
84
- # Reference to Transporter
85
- attr_accessor :transporter
86
-
87
- # Reference to ValueManager
88
- attr_accessor :valuemanager
89
-
90
- # Reference to SessionManager
91
- attr_accessor :sessions
92
-
93
- # Reference to PluginManager
94
- attr_accessor :plugins
95
-
96
- # Message is initialized with a valid +Request+ and +Response+ objects.
97
- def initialize( transporter, request, response, options )
12
+ ## Due to the single instance architecture of +Plugin+, instances of Message
13
+ ## class are used for communication between sessions and +Plugin+ instance.
14
+ ## The +Message+ instance contains session and request-response related
15
+ ## mappings and utility methods.
16
+ ##
17
+ ## The Message object is initialized as 'msg' in SessionManager.
18
+ ## It's passed around the system as the user/session -object namespace,
19
+ ## much like 'self' is passed around in python methods.
20
+ ##
21
+ ## Using the msg object saves considerate amounts of CPU cycles and memory,
22
+ ## because it allows single instances of any classes that handle user data.
23
+ ##
24
+ ## == HValue Initialization Example
25
+ ## +HValue+ is closely related to +Message+ as instances of +HValue+ are
26
+ ## used to send data between sessions and the server. This is a small
27
+ ## code snippet about how to initialize several HValues as the session
28
+ ## is initialized.
29
+ ##
30
+ ## def init_ses( msg )
31
+ ## msg.session[:session_name] = {
32
+ ## :hvalue1 => HValue.new( msg, @firstvalue ),
33
+ ## :hvalue2 => HValue.new( msg, @secondvalue ),
34
+ ## :hvalue3 => HValue.new( msg, @thirdvalue )
35
+ ## }
36
+ ## end
37
+ ##
38
+ class Message
98
39
 
99
- @config = ::RSence.config
40
+ # Session data placeholder, assigned by SessionManager.
41
+ attr_accessor :session
100
42
 
101
- @request = request
102
- @response_success = false
103
- @response = response
104
- @session = nil
105
- @buffer = []
43
+ # New session flag, check it in your code to decide
44
+ # what to do, when a new session is encountered.
45
+ # In plugins, this usually means that some Values
46
+ # need to be created and bound or possibly that a
47
+ # user_id mapping needs to be done.
48
+ attr_accessor :new_session
106
49
 
107
- @options = options
50
+ # Old session first xhr flag, check it in your code
51
+ # to decide what to do, when a restored session is
52
+ # encountered. The old Values are automatically present,
53
+ # so you should at least not re-create or re-bind them.
54
+ attr_accessor :restored_session
108
55
 
109
- # Value response output.
110
- @value_buffer = []
56
+ # Contains the source ses on the first request after this
57
+ # session was cloned from another session.
58
+ attr_accessor :cloned_source
111
59
 
112
- # The session key placeholder.
113
- @ses_key = false
114
- @new_session = false
115
- @restored_session = false
116
- @cloned_source = false
117
- @cloned_targets = false
118
- @ses_valid = false
119
- @error_js = ''
60
+ # Contains the target sessions packed in an array on
61
+ # the first request after another session was cloned
62
+ # from this session.
63
+ attr_accessor :cloned_targets
120
64
 
121
- # global instances
122
- @transporter = transporter
123
- @valuemanager = @transporter.valuemanager
124
- @sessions = @transporter.sessions
125
- @plugins = @transporter.plugins
65
+ # The session is not valid by default, it's set
66
+ # by SessionManager, if everything seems ok.
67
+ attr_accessor :ses_valid
126
68
 
127
- if options[:servlet]
128
- @do_gzip = false
129
- else
130
- @response.content_type = 'text/javascript; charset=utf-8'
131
- @response['cache-control'] = 'no-cache'
132
-
133
- # gnu-zipped responses:
134
- if @request.header['accept-encoding'] and @request.header['accept-encoding'].include?('gzip') and not @config[:no_gzip]
135
- @response['content-encoding'] = 'gzip'
136
- @do_gzip = true
137
- else
69
+ # The http request object.
70
+ attr_accessor :request
71
+
72
+ # The http response object.
73
+ attr_accessor :response
74
+
75
+ # Response output.
76
+ attr_accessor :buffer
77
+
78
+ attr_accessor :value_buffer
79
+
80
+ # The request success flag.
81
+ attr_accessor :response_success
82
+
83
+ # Reference to Transporter
84
+ attr_accessor :transporter
85
+
86
+ # Reference to ValueManager
87
+ attr_accessor :valuemanager
88
+
89
+ # Reference to SessionManager
90
+ attr_accessor :sessions
91
+
92
+ # Reference to PluginManager
93
+ attr_accessor :plugins
94
+
95
+ # Message is initialized with a valid +Request+ and +Response+ objects.
96
+ def initialize( transporter, request, response, options )
97
+
98
+ @config = ::RSence.config
99
+
100
+ @request = request
101
+ @response_success = false
102
+ @response = response
103
+ @session = nil
104
+ @buffer = []
105
+
106
+ @options = options
107
+
108
+ # Value response output.
109
+ @value_buffer = []
110
+
111
+ # The session key placeholder.
112
+ @ses_key = false
113
+ @new_session = false
114
+ @restored_session = false
115
+ @cloned_source = false
116
+ @cloned_targets = false
117
+ @ses_valid = false
118
+ @error_js = ''
119
+
120
+ # global instances
121
+ @transporter = transporter
122
+ @valuemanager = @transporter.valuemanager
123
+ @sessions = @transporter.sessions
124
+ @plugins = @transporter.plugins
125
+
126
+ if options[:servlet]
138
127
  @do_gzip = false
128
+ else
129
+ @response.content_type = 'text/javascript; charset=utf-8'
130
+ @response['cache-control'] = 'no-cache'
131
+
132
+ # gnu-zipped responses:
133
+ if @request.header['accept-encoding'] and @request.header['accept-encoding'].include?('gzip') and not @config[:no_gzip]
134
+ @response['content-encoding'] = 'gzip'
135
+ @do_gzip = true
136
+ else
137
+ @do_gzip = false
138
+ end
139
139
  end
140
- end
141
140
 
142
- @response_sent = false
143
- end
144
-
145
- # Returns true for Internet Explorer 6.0
146
- def ie6;
147
- (request.header.has_key?('user-agent') and request.header['user-agent'].include?('MSIE 6.0'))
148
- end
149
-
150
- # Expire the session.
151
- def expire_session
152
- @sessions.expire_session( @ses_id )
153
- end
154
-
155
- # Define the session key.
156
- def ses_key=(ses_key)
157
- @ses_key = ses_key
158
- end
159
-
160
- # Getter for session key.
161
- def ses_key
162
- @ses_key
163
- end
141
+ @response_sent = false
142
+ end
164
143
 
165
- # Returns the user id
166
- def user_id
167
- @session[:user_id]
168
- end
144
+ # Returns true for Internet Explorer 6.0
145
+ def ie6;
146
+ (request.header.has_key?('user-agent') and request.header['user-agent'].include?('MSIE 6.0'))
147
+ end
169
148
 
170
- # Setter for the user id
171
- def user_id=(user_id)
172
- @session[:user_id] = user_id
173
- end
149
+ # Expire the session.
150
+ def expire_session
151
+ @sessions.expire_session( @ses_id )
152
+ end
174
153
 
175
- def ses_id
176
- @session[:ses_id]
177
- end
154
+ # Define the session key.
155
+ def ses_key=(ses_key)
156
+ @ses_key = ses_key
157
+ end
178
158
 
179
- def ses_id=(ses_id)
180
- @session[:ses_id] = ses_id
181
- end
159
+ # Getter for session key.
160
+ def ses_key
161
+ @ses_key
162
+ end
182
163
 
183
- def error_msg( error_js )
184
- @error_js = error_js
185
- # response_done
186
- end
164
+ # Returns the user id
165
+ def user_id
166
+ @session[:user_id]
167
+ end
187
168
 
188
- def buf_json(buffer)
189
- buffer.to_json
190
- end
169
+ # Setter for the user id
170
+ def user_id=(user_id)
171
+ @session[:user_id] = user_id
172
+ end
173
+
174
+ # Returns the session id
175
+ def ses_id
176
+ @session[:ses_id]
177
+ end
178
+
179
+ # Sets the session id
180
+ def ses_id=(ses_id)
181
+ @session[:ses_id] = ses_id
182
+ end
183
+
184
+ # Sets the error message
185
+ def error_msg( error_js )
186
+ @error_js = error_js
187
+ # response_done
188
+ end
189
+
190
+ # Converts the buffer to JSON
191
+ def buf_json(buffer)
192
+ buffer.to_json
193
+ end
191
194
 
192
- # Called to flush buffer.
193
- def response_done
194
- return if @response_sent
195
- if not @response_success
196
- @response.status = 200
197
- #@response.status = 503
195
+ # Called to flush buffer.
196
+ def response_done
197
+ return if @response_sent
198
+ if not @response_success
199
+ @response.status = 200
200
+ #@response.status = 503
201
+
202
+ buffer = [
203
+ "" # empty session key will stop the syncing
204
+ ] + @error_js
205
+ else
206
+ ## The response status should always be 200 (OK)
207
+ @response.status = 200
198
208
 
199
- buffer = [
200
- "" # empty session key will stop the syncing
201
- ] + @error_js
202
- else
203
- ## The response status should always be 200 (OK)
204
- @response.status = 200
209
+ buffer = @value_buffer + @buffer
210
+ if @ses_key
211
+ buffer.unshift( @ses_key )
212
+ end
205
213
 
206
- buffer = @value_buffer + @buffer
207
- if @ses_key
208
- buffer.unshift( @ses_key )
209
214
  end
210
-
211
- end
212
215
 
213
- # flush the output
214
- if @do_gzip
215
- outp = GZString.new('')
216
- gzwriter = Zlib::GzipWriter.new(outp,Zlib::BEST_SPEED)
217
- gzwriter.write( buf_json(buffer) )
218
- gzwriter.close
219
- else
220
- outp = buf_json(buffer)
221
- end
216
+ # flush the output
217
+ if @do_gzip
218
+ outp = GZString.new('')
219
+ gzwriter = Zlib::GzipWriter.new(outp,Zlib::BEST_SPEED)
220
+ gzwriter.write( buf_json(buffer) )
221
+ gzwriter.close
222
+ else
223
+ outp = buf_json(buffer)
224
+ end
222
225
 
223
- @response['content-length'] = outp.size
224
- @response.body = outp
226
+ @response['content-length'] = outp.size
227
+ @response.body = outp
225
228
 
226
- @response_sent = true
227
- end
229
+ @response_sent = true
230
+ end
228
231
 
229
- # Sends data to the client, usually
230
- # javascript, but is valid for any data.
231
- def reply(data,dont_squeeze=false)
232
- data.strip!
233
- data = @plugins[:client_pkg].squeeze( data ) unless dont_squeeze
234
- puts data if @config[:trace]
235
- @buffer.push( data )
236
- end
232
+ # Sends data to the client, usually
233
+ # javascript, but is valid for any data.
234
+ def reply(data,dont_squeeze=false)
235
+ data.strip!
236
+ data = @plugins[:client_pkg].squeeze( data ) unless dont_squeeze
237
+ puts data if @config[:trace]
238
+ @buffer.push( data )
239
+ end
237
240
 
238
- # For value manager; insert changed values BEFORE other js.
239
- def reply_value(data)
240
- puts data if @config[:trace]
241
- @value_buffer.push( data )
242
- end
241
+ # For value manager; insert changed values BEFORE other js.
242
+ def reply_value(data)
243
+ puts data if @config[:trace]
244
+ @value_buffer.push( data )
245
+ end
243
246
 
244
- # Sends data to the client's console.
245
- def console(data)
246
- reply( "console.log(#{data.to_json});" )
247
- end
247
+ # Sends data to the client's console.
248
+ def console(data)
249
+ reply( "console.log(#{data.to_json});" )
250
+ end
248
251
 
249
- # Serves an image object +img_obj+ by returning its disposable URL. The
250
- # second optional parameter +img_format+ defaults to 'PNG' and defines
251
- # the format of served picture.
252
- def serve_img( img_obj, img_format='PNG' )
253
- call(:ticket,:serve_img, self, img_obj, img_format )
254
- end
252
+ # Serves an image object +img_obj+ by returning its disposable URL. The
253
+ # second optional parameter +img_format+ defaults to 'PNG' and defines
254
+ # the format of served picture.
255
+ def serve_img( img_obj, img_format='PNG' )
256
+ call(:ticket,:serve_img, self, img_obj, img_format )
257
+ end
255
258
 
256
- # Sends any binary to be served, returns a disposable uri. First parameter
257
- # defines the file data, second optional defines content type and defaults
258
- # to 'text/plain' and third, also optional defines the filename which
259
- # defaults to 'untitled.txt'.
260
- def serve_file( file_data, content_type='text/plain', filename='untitled.txt' )
261
- call(:ticket,:serve_file, self, file_data, content_type, filename )
262
- end
259
+ # Sends any binary to be served, returns a disposable uri. First parameter
260
+ # defines the file data, second optional defines content type and defaults
261
+ # to 'text/plain' and third, also optional defines the filename which
262
+ # defaults to 'untitled.txt'.
263
+ def serve_file( file_data, content_type='text/plain', filename='untitled.txt' )
264
+ call(:ticket,:serve_file, self, file_data, content_type, filename )
265
+ end
263
266
 
264
- # Sends any binary to be served, returns a static uri.
265
- #
266
- # IMPORTANT: PLEASE call +release_rsrc+ manually, when you
267
- # don't need the resource anymore! Otherwise TicketServe will
268
- # keep on chugging more memory every time you serve something.
269
- #
270
- # HINT: Usually, it's a better idea to use serve_img or
271
- # serve_file instead.
272
- def serve_rsrc( rsrc_data, content_type='text/plain' )
273
- call(:ticket,:serve_rsrc,self, rsrc_data, content_type )
274
- end
267
+ # Sends any binary to be served, returns a static uri.
268
+ #
269
+ # IMPORTANT: PLEASE call +release_rsrc+ manually, when you
270
+ # don't need the resource anymore! Otherwise TicketServe will
271
+ # keep on chugging more memory every time you serve something.
272
+ #
273
+ # HINT: Usually, it's a better idea to use serve_img or
274
+ # serve_file instead.
275
+ def serve_rsrc( rsrc_data, content_type='text/plain' )
276
+ call(:ticket,:serve_rsrc,self, rsrc_data, content_type )
277
+ end
275
278
 
276
- # Removes the uri served, you HAVE TO call this manually when
277
- # you are done serving something! Takes the uri as its only parameter.
278
- def release_rsrc( uri )
279
- run(:ticket,:del_rsrc, uri[3..-1] )
280
- end
281
- alias unserve_rsrc release_rsrc
279
+ # Removes the uri served, you HAVE TO call this manually when
280
+ # you are done serving something! Takes the uri as its only parameter.
281
+ def release_rsrc( uri )
282
+ run(:ticket,:del_rsrc, uri[3..-1] )
283
+ end
284
+ alias unserve_rsrc release_rsrc
282
285
 
283
- # Calls registered plugin +plugin+ method +plugin_method+ with any +args+
284
- def call( plugin_name, plug_method, *args )
285
- @plugins.call( plugin_name, plug_method, *args)
286
+ # Calls registered plugin +plugin+ method +plugin_method+ with any +args+
287
+ def call( plugin_name, plug_method, *args )
288
+ @plugins.call( plugin_name, plug_method, *args)
289
+ end
290
+ alias run call
286
291
  end
287
- alias run call
288
-
289
- end
290
-
291
292
  end