Hello World Example

The following is an example application using Jockey that sends a request "Hello" and replies with "World". It was written as a complete working example using only 23 statements.

public class HelloWorldExample implements PeerMessageListener
{
    public void HelloWorldExample()
    {
	//
	// -- 1. Register App
	//
	HelloWorldDescriptor descriptor = new HelloWorldDescriptor();
	HelloWorldAuthority authority = new HelloWorldAuthority();

	sessionToken_ = PeerPlatform.getInstance().startPeerSession(descriptor);
	PeerPlatform.getInstance().addAuthority(authority);

	//
	// -- 2. Find peers for up to 5 seconds
	//
	GroupToken appGroup = PeerPlatform.getInstance().getDefaultGroup();
	PeerNetworkBrowser browser = PeerNetworkBrowser.getInstance(appGroup);
	MessageBroker broker = MessageBroker.getInstance(appGroup);

	Vector peerTokens = browser.findPeers(5000); 

	//
	// -- 3. Send message to each peer found
	//
	PeerMessage message = new PeerMessage();
	message.setContent("Hello");

	for(int i=0;i<peerTokens.size();i++)
	{
	    PeerToken token = (PeerToken)peerTokens.get(i);
	    broker.sendMessage(token, message);
	}
    }

    //
    // 4. Implement PeerMessageListener Methods...
    //

    public void onRequest(PeerMessage message)
    {
	//
	// -- 5. Reply to a "Hello" message
	//
	PeerMessage replyMessage = new PeerMessage();
	replyMessage.setContent("World");

	// API: add getPeerToken() to PeerMessage
	PeerToken source = message.getPeerToken();
	SendMarker sendMarker = broker.sendReply(source, message, replyMessage);
    }

    public void onReply(PeerMessage reply)
    {
	String content = reply.getContent();
	//
	// -- 6. Print a statement to System.out when we receive the "World" reply
	//
	System.out.println("Received the following expected message:"+content);
	shutDown();
    }

    // ... other PeerMessageListener methods here ...

    private void shutDown()
    {
	//
	// -- 7. End session
	//
	PeerPlatform.getInstance().endSession(sessionToken_);
    }

    public static void main(String args[])
    {
	new HelloWorldExample();
    }

    private PeerToken sessionToken_;
}
	

$Id: HelloWorld.html,v 1.3 2001/07/27 17:20:59 james Exp $