/** HelloWorld_NoCallBack 
*
*	The HyperCast HelloWorld programs demonstrate various aspects of
*	the HyperCast API.  The programs create an overlay socket that 
* 	sends and receives messages. 
*
*       In HelloWorld_NoCallBack, all receive operations are blocking.
*	This file corresponds to an example program in the HyperCast API description 
*	(see Chapter:  "API - The Basics", Section: "The HelloWorld program"). 
*
* @author Multimedia Networks Group
* @author HyperCast Team 
* @version 2005 (version 3.0)
* 
*/


import java.io.*;
import hypercast.*;

public class HelloWorld_NoCallBack {

public static void main(String[] args)throws InterruptedIOException
{

     	// The string we send 
     	String MyString = new String("Hello World");

     	// The overlay socket 
     	I_OverlaySocket MySocket = null;

     	// The configuration object
     	HyperCastConfig ConfObj = null;

	// Create a configuration object
	ConfObj = HyperCastConfig.createConfig("hypercast.xml");

	// Create an overlay socket with the configuration object
	MySocket=ConfObj.createOverlaySocket(null);

	// Join the overlay network 
	MySocket.joinOverlay();

	// Print the logical address */ 
	String MyLogicalAddress = MySocket.getLogicalAddress().toString();
	System.out.println("Logical address is " + MyLogicalAddress + ".");
	
	// Repeat 10 times 
	for(int i = 0; i < 10; i++){

		// wait 2 seconds 	
		try {
	        Thread.sleep(2000);
		} catch (InterruptedException e) {}	
		
		// Convert the string to a byte array and create a message
		byte[] MyData = MyString.getBytes(); 
		I_OverlayMessage MyMessage = MySocket.createMessage(MyData);

		// The message is sent to all members of the overlay //
		MySocket.sendToAll(MyMessage);
		System.out.println(" message sent to other members ...");
	 }
	
	// Repeat forever 
	for(;;){

		// Blocking eceived message 
		I_OverlayMessage msg = MySocket.receive(); 

             	// Extract the payload and the logical address of the source
		byte[] data = msg.getPayload();
        	String Src = msg.getSourceAddress().toString();

        	// Print the payload (Skip messages sent by this program) 
        	if(!msg.getSourceAddress().equals(MySocket.getLogicalAddress()))
            		System.out.println("Received <"+ new String(data) + "> from logical address: " + Src + ".");
	 }
}
}