openmind ☃   May 11, 2007  ☃  SpreadHandle.java  (, )

The Spread Toolkit is one of those things that I probably would never have encountered if I were a non-programmer. As a long-time Linux user, however, I recognize a good piece of (free) software when I see it.

Due to the complexity it addresses, a multicast library is not something that most people—let alone most programmers—require. The only thing less rare rarer than actually needing a multicast library is writing a multicast library. Luckily for programmers everywhere, there is Spread—a fast, stable, fairly well-documented multicast library that completely abstracts away the complexity of multicast messaging.

Using Spread is so easy to use that even an English major can do it™. All Spread is really lacking are some published examples of how to implement the Java API—i.e., some code that people can “borrow” when they are starting out. So with no further ado, here’s the general connection handler I wrote. The key is the call to Thread.sleep(). I think I read about that trick on the Spread mailing list, which is probably the best place to find usage examples other than the documentation itself. The following code, along with all of the other code on this website, is released under the GNU General Public License.

package at.downbe;

import spread.*;
import java.net.*;
import org.apache.log4j.Logger;

public class SpreadHandle
{

    Logger logger = Logger.getLogger(SpreadHandle.class);
    final String group = "mygroup";
    String id = new String();
    SpreadConnection connection;
    String privateGroup = new String();
    public SpreadConnection sc;
    
    public SpreadHandle()
    {
            this.connection = getHandle();
    }

    private String getRandStr()
    {
            // Make unique connection string.
            return (Long.toString(System.currentTimeMillis()));
    }
    
    public boolean disconnected()
    {
            return (this.sc.getPrivateGroup() == null);
    }

    public SpreadConnection getHandle()
    {
            int retryMs = 5000;
            int port = 4803;
            this.sc = new SpreadConnection();
            try
            {
                // Connect.
                logger.debug("Connect id: " + id);
                sc.connect(InetAddress.getByName("localhost"),port,this.getRandStr(),true,false);
                this.privateGroup = sc.getPrivateGroup().toString();

                // Join.
                SpreadGroup sgroup = new SpreadGroup();
                sgroup.join(sc, group);
                logger.debug("Joined group " + group + " as " + this.privateGroup);
            }
            catch(Exception e)
            {
                logger.info(e.toString());
                logger.info("Problem connecting to spread. Retrying in " + (retryMs/1000) + " seconds...");
                try
                {
                    Thread.sleep(retryMs);
                }
                catch(Exception e2)
                {
                    logger.info("Thread interrputed, can't continue"  + e2.toString());
                }
            }
            return sc;
    }
    
    /*public static void main(String args[]){
        org.apache.log4j.BasicConfigurator.configure();
        new SpreadHandle();
    }*/
}

I use it in Swing like this:


public void go()
{
new Thread()
{
public void run()
{
do
{
if(sh == null || sh.disconnected())
{
logger.debug(“Not connected. Getting handle…”);
sh = new SpreadHandle();
}
else
{
setStatus(2);
logger.debug("Connected to spread. " + sh.id);
processMessages();
}
}
while(true);
}
}.start();
}

blog comments powered by Disqus