Technical (Java Script)

Talkative

A framework to let diverse web applications talk to each other through a common protocol.

 

INTRODUCTION

Consider a typical intranet situation. Let's say you have 5 intranet web applications. Out of them 2 are Java applets, 2 more are ActiveX objects and the remaining one is an application written using JavaScript. Some of them are standard stuff and though they publish APIs to drive them from other applications, you can not modify the application. How do you connect them together? If you double click on an address book entry, it should open the mail application with the selected address. How do you do that?

Well, here's the answer. Talkative is a set of Java, COM and JavaScript libraries which together can provide connectivity between different kinds of applications. It provides the basic messaging infrastructure. Applications can either be written to adhere to this protocol or wrappers can be written over existing applications to be Talkative.

 

ARCHITECTURE

Talkative is based on a publish - subscribe model. The is a central messaging engine which collects messages from all publishers and delivers them to the subscribers at regular intervals. Publishers and subscribers must adhere to a published interface to enable themselves being called by the messaging engine.

Components

The core delivery system is developed using Javascript. Javascript is theuniversal glue - available in most of the browsers and has access to to the completeDOM (Document Object Model), including any Java and ActiveX objects. An application has to register with the delivery system supplying a class and whether it wants to publish or subscribe.

Helper libraries have been provided for certain common development languages to ease the development of applications that adhere to this protocol. However, it is not mandatory to use these libraries. It is enough as long as the classes implement the required interfaces. A single class can both be a publisher as well as a subscriber.

A Message is characterized by a message type and the content string. Bothmessage type and content are strings, though typically the message type will be a shortidentifier string. A subscriber subscribes for one or more message types. A publisher canpublish one or more message types.

Message Flow

The infrastructure starts with publishers and subscribers registering themselved with the messaging engine (using the register methods). The engine thread is then started by the containing HTML page using the startTalk method. After this the infrastructure is active and running. Publishers may start publishing messages and the messaging engine will deliver them to the subscribers. Publishers and subscribers may also register while the system is active and running.

The messaging engine keeps a list of publishers and subscribers. At a regular (configurable) interval, it checks with each publisher for new messages, retrieves them and stores them internally. Then it goes through the list of subscribers and delivers the revevant messages to them. Once a message is delivered, it is marked as used. After one round of operation, all used messages are cleaned up from the system and the message store goes on for the next round.

 

HELPER (SAMPLE) LIBRARIES

The following libraries are provided. Some of them can be used directly for any purpose. Some need to be modified to suit the exact purpose of usage.

Java Library

    A class that can be used to subscribe or publish messages from Java applets. This provides the interface required by the delivery system. An instance of this class needs to be registered with the delivery system. Save the code below as jtalk.java and compile it to create a class file. The source code and the classes are also included in the downloadable zip file provided.
    
    import java.util.Vector;
    
    public abstract class jtalk
    {
        Vector vectMsgType = new Vector();
        Vector vectMsg = new Vector();
        
        public void acceptMsg(String sMsgType, String sMsg)
        {
            // override this method to handle messages
        }
        
        public int getNumMessages()
        {
            return vectMsgType.size();
        }
        
        public String getMsgType(int iMsgIndex)
        {
            return (String)vectMsgType.elementAt(iMsgIndex);
        }
        
        public String getMsg(int iMsgIndex)
        {
            return (String)vectMsg.elementAt(iMsgIndex);
        }
        
        public void flushMsg(int iMsgIndex)
        {
            vectMsg.removeElementAt(iMsgIndex);
            vectMsgType.removeElementAt(iMsgIndex);
        }
        
        // call this function to store messages that will be retrieved by the 
        // talkative routines
        void publishMessage(String sMsgType, String sMsg)
        {
            vectMsgType.addElement(sMsgType);
            vectMsg.addElement(sMsg);
        }
    }
    

The JavaScript Library

    A class that can be used to subscribe or publish messages from JavaScript soutines. Save the code below as jtalk.js. Modify the acceptMsg method as per the requirement. The source code is also included in the downloadable zip file provided.
    <!--
    var msgTypeArr = new Array();
    var msgArr = new Array();
    
    // modify this function to handle messages the way you want.
    function acceptMsg(sMsgType, sMsg)
    {
        document.forms[0].recvdString.value = "Message Type: " + sMsgType + ", " + "Message: " + sMsg;
    }
    
    function getNumMessages()
    {
        return msgTypeArr.length;
    }
    
    function getMsgType(iMsgIndex)
    {
        return msgTypeArr[iMsgIndex];
    }
    
    function getMsg(iMsgIndex)
    {
        return msgArr[iMsgIndex];
    } 
    
    function  flushMsg(iMsgIndex) 
    {
        var newMsgTypeArr =  new Array();
        var newMsgArr = new Array();
        var iSize =  msgTypeArr.length; 
        var  iIndex; 
        var iNewIndex; 
        
        for(iIndex=iNewIndex=0; iIndex < iSize; iIndex++)
        {
            if(iMsgIndex == iIndex) continue;
            newMsgTypeArr[iNewIndex] = msgTypeArr[iIndex];
            newMsgArr[iNewIndex] = msgArr[iIndex];
            iNewIndex++;
        }
        msgTypeArr = newMsgTypeArr;
        msgArr = newMsgArr;
    }
    
    // call this function to store messages that will be retrieved by the talkative routines
    function publishMessage(sMsgType, sMsg)
    {
        var iSize = msgTypeArr.length;
        msgTypeArr[iSize] = sMsgType;
        msgArr[iSize] = sMsg;
    }
    
    // this function retruns a class that can act as a listener as well as
    // publisher. Call methods in this class to publish your messages
    function getClass()
    {
        this.acceptMsg = acceptMsg;
        this.getNumMessages = getNumMessages;
        this.getMsgType = getMsgType;
        this.getMsg = getMsg;
        this.flushMsg = flushMsg;
        return this;
    }
    
    //-->
    

The Visual Basic Library

    A set of functions that can be included in a ocx to subscribe or publish messages from Visual Basic ActiveX components. A sample Visual Basic ActiveX control named Sample.ctl has been provided in the downloadable zip file for implementation and demonstration of Talkative. Create a ActiveX control project in Visual Basic and add this source to the project.

 

Examples and Sources

Click here to download the sources for the delivery system, libraries and the sample implementations along with a readme file. Unzip the file into a directory and open Sample.html with Internet Explorer browser to see the readymade sample application. Alternatively, you can click here to open Sample.html.

List of files:

    Sample.html - The html file to test Talkative
    talkative.js - The talkative engine written in Javascript
    jtalk.java/jtalk.class - Java implementation of Talkative
    Sample.java/Sample.class/jtalkimpl.class - Sample java applet for demonstration
    jstalk.js - Sample Javascript implementation of Talkative demonstration
    Sample.ctl - Sample Visual Basic ActiveX control for implementation and demonstration of Talkative. Create a ActiveX control project in Visual Basic and add this source to the project.