Tuesday, October 21, 2014

Plugin and object oriented programming

Object oriented programming has the features like class, object, abstraction, encapsulation, polymorphism, inheritance. Interestingly plugin has some of those features. This is a continuous effort to investigate plugins.

Object

A plugin is simply a singleton object. It can be initiated and destroyed. And the registered extensions are it’s fields in analogy to an object.

Abstraction

Plugin can use any named extensions even they are not registered by any actions. In this way they are less specific and abstract. For example, an application lets plugin to do certain things before exit. And it executes the actions under the onQuit extension point. Initially it can be empty.

On some point of execution a new plugin may be loaded that registers action to onQuit. Suppose the action is just to print Good Bye on the screen. So before the application exists it executes the action registered at onQuit and eventually it says Good Bye to the user.

Encapsulation

Encapsulation makes your code more flexible, because the developer is not concerned about who is using the inner code. And the inner code can be changed anytime without modifying the other plugins. It is like advertising less makes things flexible(less is beautiful). Plugin just hides its internal code blocks as they are not registered to any extension point.

Inheritance

Plugin has the basic feature of overriding. It lets a code block to register to an extension point. In this way it overrides the behavior of that extension point.

Suppose in the earlier example, we load a new plugin which overrides the onQuit with action to print See You. In that case the application may execute both the actions before exit resulting it to say Good Bye See You .

Polymorphism

Polymorphism is the idea of abstraction. It is the idea that the behavior of the code may be changed. And Plugin has this idea too. Different plugins make the application behave in different way. In the earlier example of inheritance, the behavior is changed from doing nothing to saying Good Bye and to saying Good Bye See You before exit.

Advantages of plugins over object oriented design

  • Object oriented programming is sometimes a mess. Plugins are good point of decoupling. The figure below shows the idea.

Readings

SOLID
Philosophies

Monday, October 20, 2014

Ejabberd plugin

Ejabberd is very popular scalable XMPP server. This server is an instant messaging or chatting server. It is written in erlang. It implements it’s features in forms of modules. These modules can be seen as plugins. Here is how it works. This is our continuous effort to investigate plugins in different projects.

Here the mod_last is taken as example. Most of the modules here in ejabberd are small and less than 500 lines. Here is a small window of the output of find -iname "mod_*.erl" | xargs wc in linux shell. The values in left column are the number of lines.

....
    198     825    8244 ./src/mod_echo.erl
    173     566    5827 ./src/mod_proxy65_sm.erl
    682    1991   22312 ./src/mod_register.erl
    351    1082   11792 ./src/mod_last.erl
   1463    4336   48959 ./src/mod_shared_roster.erl
   1347    3897   45857 ./src/mod_irc.erl
    270    1087   10304 ./src/mod_carboncopy.erl
    109     380    3650 ./src/mod_time.erl
     97     330    3437 ./src/mod_service_log.erl
....

It is interesting that mod_last contains only 351 lines of code including the code comments. So this file will be easy to follow.

The purpose of this module

This module keeps a record of the time when a person is last seen online.

Loading and unloading

The modules here are loadable. They have a start and stop functions to initiate and deinitialize them. The start function takes two arguments and stop takes one argument.

Extension points

The start function here registers hook for following events.

  • unset_presence_hook
    The on_presence_update function is called when user is logged out of server. It just stores the time entry in the database.
    ejabberd_hooks:add(unset_presence_hook, Host, ?MODULE,
                       on_presence_update, 50).
on_presence_update(User, Server, _Resource, Status) ->
    TimeStamp = now_to_seconds(now()),
    store_last_info(User, Server, TimeStamp, Status).
  • remove_user
    When the user is removed permanently from server he is deleted from last_activity database.
    ejabberd_hooks:add(remove_user, Host, ?MODULE,
                       remove_user, 50),

There are other hooks that answer the user activity information and do other things.

Summary

This is very interesting to see how small the code is. And it is very precise too. Programmers will love to write modules here. It can be given 500 points for the mechanism (there will be more points coming for other projects). Thanks to erlang, nice language.