Class: RSence::Plugins::Plugin__

Inherits:
Object
  • Object
show all
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 of initialize

  • #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.

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

Direct Known Subclasses

GUIPlugin__

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#infoHash (readonly)

Returns The meta-information of the plugin bundle.

Returns:



89
90
91
# File 'lib/rsence/plugins/plugin.rb', line 89

def info
  @info
end

#nameSymbol (readonly)

Returns The name of the plugin bundle.

Returns:

  • (Symbol)

    The name of the plugin bundle



83
84
85
# File 'lib/rsence/plugins/plugin.rb', line 83

def name
  @name
end

#pathString (readonly)

Returns The absolute path of the plugin bundle.

Returns:

  • (String)

    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.

Parameters:

  • msg (Message)

    The message is supplied by the system.

  • target_session (Hash)

    The actual cloned session object, which is a copy of the current session.

Returns:

  • (nil)


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.

Parameters:

  • msg (Message)

    The message is supplied by the system.

  • source_session (Hash)

    The actual previous session object, which was used as the source of the clone.

Returns:

  • (nil)


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.

Parameters:

  • msg (Message)

    The message is supplied by the system.

  • key (Symbol) (defaults to: false)

    (optional) returns a ses key, if defined.

Returns:

  • (Hash)

    Plugin-specific session hash



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.

Parameters:

  • msg (Message)

    The message is supplied by the system.

Returns:

  • (nil)


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.

Examples:

include_js( msg, [ 'default_theme', 'controls' ] )

Parameters:

  • msg (Message)

    The message is supplied by the system.

  • dependencies (Array) (defaults to: [])

    A list of package names.

Returns:

  • (nil)


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

#initnil

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.

Returns:

  • (nil)

See Also:



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.

Parameters:

  • msg (Message)

    The message is supplied by the system.

Returns:

  • (nil)


130
131
132
# File 'lib/rsence/plugins/plugin.rb', line 130

def init_ses( msg )
  init_ses_values( msg )
end

#name_with_manager_sObject



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.

Parameters:

  • js_name (String)

    Javascript source file name without the ‘.js’ suffix.

Returns:

  • (String)

    The source of the file.

  • (false)

    Returns false, when file was not found.



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.

Parameters:

  • msg (Message)

    The message is supplied by the system.

  • js_name (String)

    Javascript source file name without the ‘.js’ suffix.

Returns:

  • (String)

    The source of the file on the first call in a session, or an empty string on the subsequent calls.

  • (false)

    Returns false, when file was not found.



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.

Parameters:

  • msg (Message)

    The message is supplied by the system.

Returns:

  • (nil)


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.

Examples:

values_js( msg, get_ses(msg) )

Parameters:

  • msg (Message)

    The message is supplied by the system.

  • ses (Hash) (defaults to: false)

    Used for supplying a Hash with the HValue instances. It’s optional and defaults to the current plugin node in the active session.

Returns:

  • (String)

    A string representing a javascript object similar to the ruby Hash ses.



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