powered by Jive Software

Smack with event loop

Hi all,

I am a newbie to Smack, and am trying to write a jabber client which can take commands over telnet. So, I would like to have an event based system which can listen on sockets for incoming connections (and commands over those connections) and also use Smack API to talk to the server. How do I use a common event loop (so that I can use java.nio as well as Smack)?

Thanks,

Praveen

I found how to do this on the internet, but I didn’t bookmark it apparently. Anyway, here’s my skeleton:

public static void main(String[] args) throws Throwable {
        BotMain bot = new BotMain();         try {             addShutdownHook(); // important to disconnect!             // sanity checks and startup actions
                        // database setup             // XMPP setup
                    } catch (XMPPException ex) {
            final XMPPError error = ex.getXMPPError();
            int errorCode = 0;
            String errorMessage = "";
            if (error != null) {
                errorCode = error.getCode();
              // output error
            }
            throw new Throwable(errorMessage, ex);
                } catch (final Throwable e) {
            shutdown();
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    logger.fatal("Startup failed", e);
                }
            });
            System.exit(1);
            return;
        }                       mainloop = new Thread(bot);
        if (mainloop != null)  {
            mainloop.start();
        }
        mainloop = null;
    }

Note that I don’t know how much of a hack this is, but with the Tanuki service/daemon wrapper our bot works well.

Edit: here’s a NIO tutorial http://rox-xmlrpc.sourceforge.net/niotut/index.html

Thanks for the reply Sabine. My level of Java is somewhat novice. Could you just tell me in words what you’re trying to do. I’m not clear on how you are getting NIO into the picture. From the tutorial, it is clear that I need a selector thread to run the ‘select loop’ and read/write/accept on channels. And there are worker threads which interpret packets and construct responses (which is what I had in my mind earlier).

My idea was to be able to get hold of the socket underlying an XMPPConnection and register it with the selector. The first suggestion in the following link (regarding I/O abstraction) comes close to what I’m thinking:

http://mailman.jabber.org/pipermail/jdev/2005-February/020106.html

Hope thats clear.

-Praveen

No, I am not using NIO at all - I just noticed you mention it after posting. But the tutorial I posted does create an event loop.

My example creates a thread called mainloop, so the program doesn’t exit when it reaches the end of main().

Can’t help you with NIO specifically. If you have questions and issues with Java programming, go to the newsgroups comp.lang.java.help (for beginners) and comp.lang.java.programmer (more advanced).