Class: RSence::Plugins::Plugin__
- Inherits:
-
Object
- Object
- RSence::Plugins::Plugin__
- Includes:
- PluginBase
- Defined in:
- lib/rsence/plugins/plugin.rb
Overview
The Plugin__ is actually available as Plugin
from plugin bundles using the Plugin class mimic method.
The Plugin class is the base class for extending server logic. A single Plugin instance serves the requests of all sessions, which makes them very cpu and memory efficient compared to systems, where the server classes are constructed and destructed for each request.
Plugins are designed to be contained in a plugin directory bundle and to be loaded by the main RSence::PluginManager, which is also responsible for delegating the events and other calls throughout the system.
Anatomy of a plugin bundle
First read about the Plugin Bundles.
The plugin bundle contains all data needed to run the plugin. Design your plugin without any hard-coded paths, remember that it’s intended to be installed by moving or copying the whole plugin into one of the “plugins” directories. Use RSence::Plugins::PluginBase#bundle_path to construct paths.
The RSence::PluginManager looks for these bundles and evaluates them into an anonymous Module
namespace. The contents of the ruby source file is then responsible for including its libraries, constructing an instance of itself and registering itself as a part of the system.
Use the GUIPlugin class for plugins that handle user interfaces. Use the Plugin class for plugin bundles that provide supplemental functionality, value responders and other utilities that supplement the user interface.
Extension hooks for server events
These methods are provided as the basic server event hooks:
-
#init
– Use instead ofinitialize
-
#open
– Extend to open objects -
#flush
– Extend to write the state and to flush buffers -
#close
– Extend to close objects
Extension hooks for session events
-
#idle
– Extend to implement logic for each client data synchronization and “idle poll” request. -
#init_ses
– Extend to implement logic when a new session is created. A new session is created, when a user enters accesses the server the first time, or the first time after the previous session expired. -
#restore_ses
– Extend to implement logic when an old session is restored. A session is restored, when the user returns to the page or reloads the page before the session is expired.
Extension hooks for session events, If the server is configured to restore and clone previous sessions (default):
When sessions are cloned, the previous session is not invalidated and exists until timing out as a result of the web browser window being closed or client computer losing network connectivity for a certain (configurable) time frame.
-
#cloned_target
– Extend to implement logic in the request when session is a clone of the original session. -
#cloned_source
– Extend to implement logic in the next request of the original session after it has been cloned.
If the server is configured to not clone sessions:
When the user accesses the same page using the same browser (in different tabs or windows), only the most recently restored one is valid, while the previous ones are immediately invalidated. This is a more secure mode of operation, but has several drawback to usability, so it’s not enabled by default.
Utility methods
These are general utility methods not intended to be extended.
-
#get_ses
Returns the bundle-specific session Hash. -
#file_read
Use to read files -
#yaml_read
Use to read yaml data -
#file_write
Use to write files -
#bundle_path
Use for plugin bundle -specific paths -
#httime
Use for HTTP date/time
Client-support
These methods are intended for server-client interaction. Namely, commanding the client.
-
#read_js
Returns a javascript source file. -
#read_js_once
Returns a javascript source file once per session. -
#values_js
Returns a javascript source snippet containing references to values. -
#include_js
Tells the client to load a library package.
See also
-
Plugin Bundles – General information about the plugin bundle system
-
Values – General information about the value exchange system
-
Plugin Bundle
info.yaml
files – General information about the meta-information files. -
PluginBase – The PluginBase module
-
Servlet – The Servlet base class
-
GUIPlugin – The GUIPlugin base class
Direct Known Subclasses
Instance Attribute Summary collapse
-
#info ⇒ Hash
readonly
The meta-information of the plugin bundle.
-
#name ⇒ Symbol
readonly
The name of the plugin bundle.
-
#path ⇒ String
readonly
The absolute path of the plugin bundle.
Instance Method Summary collapse
-
#cloned_source(msg, target_sessions) ⇒ nil
Extend this method to invoke actions, when the session has been cloned to another session.
-
#cloned_target(msg, source_session) ⇒ nil
Extend this method to invoke actions, when the session is a clone of another session.
-
#get_ses(msg, key = false) ⇒ Hash
This method returns (or creates and returns) the entry in the session based on the name your plugin is registered as.
-
#idle(msg) ⇒ nil
Extend this method to do any tasks every time the client makes a request.
-
#include_js(msg, dependencies = []) ⇒ nil
Tells the js client framework to load a list of pre-packaged client libraries.
-
#init ⇒ nil
Extend this method to do any initial tasks before other methods are called.
-
#init_ses(msg) ⇒ nil
Extend this method to invoke actions, when a new session is created.
- #name_with_manager_s ⇒ Object
-
#read_js(js_name) ⇒ String, false
Returns the source code of the javascript file
js_name
in the ‘js’ subdirectory of the plugin bundle. -
#read_js_once(msg, js_name) ⇒ String, false
Like #read_js, but reads the file only once per session.
-
#restore_ses(msg) ⇒ nil
Extend this method to invoke actions, when a previous session is restored.
- #restore_ses_value(msg, value_name, value_properties) ⇒ Object
-
#values_js(msg, ses = false) ⇒ String
Extracts
HValue
references as javascript from the session Hash.
Methods included from PluginBase
#bundle_path, #close, #file_read, #file_write, #flush, #httime, #method_missing, #open, #yaml_read, #yaml_write
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RSence::Plugins::PluginBase
Instance Attribute Details
#info ⇒ Hash (readonly)
Returns The meta-information of the plugin bundle.
89 90 91 |
# File 'lib/rsence/plugins/plugin.rb', line 89 def info @info end |
#name ⇒ Symbol (readonly)
Returns The name of the plugin bundle.
83 84 85 |
# File 'lib/rsence/plugins/plugin.rb', line 83 def name @name end |
#path ⇒ String (readonly)
Returns The absolute path of the plugin bundle.
86 87 88 |
# File 'lib/rsence/plugins/plugin.rb', line 86 def path @path end |
Instance Method Details
#cloned_source(msg, target_sessions) ⇒ nil
Extend this method to invoke actions, when the session has been cloned to another session. It’s called once, just before #restore_ses is called on the first request after the cloning happened.
A session is cloned, when a user opens a another browser window or tab, while the previous session is still active.
164 165 |
# File 'lib/rsence/plugins/plugin.rb', line 164 def cloned_source( msg, target_sessions ) end |
#cloned_target(msg, source_session) ⇒ nil
Extend this method to invoke actions, when the session is a clone of another session. It’s called once, just before #restore_ses is called.
A session is cloned, when a user opens a another browser window or tab, while the previous session is still active.
153 154 |
# File 'lib/rsence/plugins/plugin.rb', line 153 def cloned_target( msg, source_session ) end |
#get_ses(msg, key = false) ⇒ Hash
This method returns (or creates and returns) the entry in the session based on the name your plugin is registered as. It’s advised to use this call instead of manually managing msg#session in most cases.
Uses the first name registered for the plugin and converts it to a symbol.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/rsence/plugins/plugin.rb', line 198 def get_ses( msg, key=false ) name_sym = name_with_manager_s.to_sym if msg.class == RSence::Message session = msg.session elsif msg.class == Hash session = msg else warn "Invalid class (#{msg.class}) for get_ses in #{name_sym.inspect}!" return nil end unless session.has_key?( name_sym ) session[ name_sym ] = {} end ses = session[ name_sym ] return ses[key] if key return ses end |
#idle(msg) ⇒ nil
Extend this method to do any tasks every time the client makes a request.
120 121 |
# File 'lib/rsence/plugins/plugin.rb', line 120 def idle( msg ) end |
#include_js(msg, dependencies = []) ⇒ nil
Tells the js client framework to load a list of pre-packaged client libraries.
It keeps track of what’s loaded, so nothing library loaded twice.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/rsence/plugins/plugin.rb', line 283 def include_js( msg, dependencies=[] ) ses = msg.session # check, if the session has a dependency array if not ses.has_key?( :deps ) # make an array of dependencies for this session, if not already done ses[:deps] = [] end dependencies = [dependencies] if dependencies.class == String # Check the required dependencies until everything is loaded. dependencies.each do |dependency| unless ses[:deps].include?( dependency ) if RSence.config[:client_pkg][:compound_packages].include?( dependency ) RSence.config[:client_pkg][:compound_packages][dependency].each do |pkg_name| ses[:deps].push( pkg_name ) msg.reply(%{jsLoader.loaded("#{pkg_name}");}) end end ses[:deps].push( dependency ) msg.reply(%{jsLoader.load("#{dependency}");}) end end end |
#init ⇒ nil
Extend this method to do any initial tasks before other methods are called.
By default #init_values is called to load the values.yaml
configuration file.
111 112 113 |
# File 'lib/rsence/plugins/plugin.rb', line 111 def init @values = init_values end |
#init_ses(msg) ⇒ nil
Extend this method to invoke actions, when a new session is created.
By default #init_ses_values is called to initialize values defined in the values.yaml
configuration file.
130 131 132 |
# File 'lib/rsence/plugins/plugin.rb', line 130 def init_ses( msg ) init_ses_values( msg ) end |
#name_with_manager_s ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/rsence/plugins/plugin.rb', line 182 def name_with_manager_s if @info[:manager] return "#{@info[:manager].to_s}:#{@name.to_s}" else return @name.to_s end end |
#read_js(js_name) ⇒ String, false
Returns the source code of the javascript file js_name
in the ‘js’ subdirectory of the plugin bundle. Use it to send raw javascript command code to the client. Use #read_js_once for libraries.
222 223 224 |
# File 'lib/rsence/plugins/plugin.rb', line 222 def read_js( js_name ) file_read( bundle_path( js_name, 'js', '.js' ) ) end |
#read_js_once(msg, js_name) ⇒ String, false
Like #read_js, but reads the file only once per session. Use for inclusion of custom library code.
233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/rsence/plugins/plugin.rb', line 233 def read_js_once( msg, js_name ) ses = msg.session if not ses.has_key?(:deps) ses[:deps] = [] end path = bundle_path( js_name, 'js', '.js' ) unless ses[:deps].include?( path ) ses[:deps].push( path ) return file_read( path ) else return '' end end |
#restore_ses(msg) ⇒ nil
Extend this method to invoke actions, when a previous session is restored.
By default #restore_ses_values
is called to perform actions on values as defined in the values.yaml
configuration file.
141 142 143 |
# File 'lib/rsence/plugins/plugin.rb', line 141 def restore_ses( msg ) restore_ses_values( msg ) end |
#restore_ses_value(msg, value_name, value_properties) ⇒ Object
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
# File 'lib/rsence/plugins/plugin.rb', line 645 def restore_ses_value( msg, value_name, value_properties ) ses = get_ses( msg ) if ses.has_key?( value_name ) and ses[ value_name ].class == HValue if value_properties.has_key?(:responders) init_responders( msg, ses[value_name], value_properties[:responders] ) else release_responders( msg, ses[value_name] ) end unless value_properties[:restore_default] == false if value_properties.has_key?(:value_call) default_value = init_value_call( msg, value_properties[:value_call] ) elsif value_properties.has_key?(:value) default_value = value_properties[:value] else default_value = 0 end ses[value_name].set( msg, default_value ) end else init_ses_value( msg, value_name, value_properties ) end end |
#values_js(msg, ses = false) ⇒ String
Extracts HValue
references as javascript from the session Hash.
256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/rsence/plugins/plugin.rb', line 256 def values_js( msg, ses=false ) # backwards-compatible with pre-1.3 behavior ses = msg if msg.class == Hash # gets the session automatically, if false ses = get_ses( msg ) unless ses js_references = [] ses.each_key do |key_name| if ses[key_name].class == HValue js_references.push( "#{key_name.to_s}:HVM.values['#{ses[key_name].val_id}']" ) end end return "{#{js_references.join(',')}}" end |