Database Group, School of Computing, National University of Singapore

Project Introduction
Team Members
Open Source

Developing a new function into BestPeer++ open source

In the following example code, we are going to publish the table names that a participating peer want to share over the corporate network. To implement this new function based on the BestPeer++ open source, we follow three steps:

  • Create a function to publish this table index
  • Implement a listener class for table index action
  • Register the implemented listener to the event dispatcher

The inline comments might help to describe the meaning of the code.

Step 1: Create a function to publish the index

public void createTableIndex()


    //create message head

    Head head = new Head();


    //create message body

    ERPInsertTableIndexBody body = new ERPInsertTableIndexBody(physicalInfo, listOfTables, logicalInfo);

    //create a message from head and body

    Message message = new Message(head, body);

    //forward message into corporate network

    serverPeer.sendMessage(this.physicalInfo, message);


Step 2: Implement a listener class for table index action

public class ERPInsertTableIndexListener extends ActionAdapter


    public void actionPerformed(ObjectOutputStream oos, Message msg) throws EventHandleException


        /* get the handler of the ServerPeer */

        ServerPeer serverpeer = (ServerPeer) gui.peer();

        /* get the message body */

        ERPInsertTableIndexBody body = (ERPInsertTableIndexBody) msg.getBody();

        PhysicalInfo physicalOwner = body.getPhysicalOwner();

        Vector<LocalTableIndex> listOfTables = body.getListOfTables();

        /* get the BATON tree node of this peer */

        TreeNode treeNode = serverpeer.getTreeNode(body.getLogicalDestination());

        BoundaryValue minValue = treeNode.getContent().getMinValue();

        BoundaryValue maxValue = treeNode.getContent().getMaxValue();


        /* insert index value */

        for (int i = 0; i < listOfTables.size(); i++)


            LocalTableIndex tableIndex = listOfTables.get(i);

            String tableName = tableIndex.getTableName();

            if (tableName.compareTo(minValue.getStringValue()) < 0)


            else if (tableName.compareTo(maxValue.getStringValue()) >= 0)



                //insert index to this peer

                this.insertTableIndex(tableIndex, physicalOwner.toString());



        /* forward remaining indices using BATON protocol*/



             // routing index values to the left of BATON tree


        while(rightIndices.size() > 0)


            // routing index values to the right of BATON tree



Step 3: Register the implemented listener to the event dispatcher

Having implemented the listener for table index action, we now need to register it to the event dispatcher of the participating peer. Add the following code to class ServerEventDispatcher in  package

public void registerActionListeners()


    //register table index listener

    this.addActionListener(new ERPInsertTableIndexListener(gui));

    // other existing listeners in the open source for other functions....

    // ...