/** "Hello, world." program for HyperCast 2.0
*
* @author Multimedia Networks Group
* @author HyperCast Team 
* @version Hello World (No Callback Version) 1.0, Nov 2001
*/


import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import edu.virginia.cs.mng.hypercast.*;

public class HelloWorld 


/******************************************************
                                           Constructor 
******************************************************/

public HelloWorld ()
{



/******************************************************
   This is the main function:
        1. Creates an overlay socket
        2. Sends a "Hello World" to all members
        3. Receives and prints "Hello World"s received 
        from other members in the overlay 
******************************************************/

public static void main(String[] args) throws InterruptedIOException
{
/******************************************************
                                       Data Members 
******************************************************/

     /* This is the string we will sent */ 
    
String MyString = new String("Hello World.");

     /* MySocket is the name of the overlay socket */
    
I_OverlaySocket MySocket = null;

     /* The configuration object contains configuration  parameters */
     OverlaySocketConfig ConfObj = null;

     /*  reads the configuration file and creates the configuration object */ 
     OverlayManager OvManager = null; 


     /* Create the overlay manager using the configuration file */
     OvManager =new OverlayManager("hypercast.prop");

try {
     /* Obtain the OverlayID from teh configuration file */
    
String overlayID = OvManager.getDefaultProperty("OverlayID");

      /* Next, a configuration object is created */ 

     /* if no overlayID is provided or does not exist 
          create a configuration objec t with a new overlayID */ 

    if (overlayID==null || overlayID.equals("") ||          !OvManager.doesOverlayExist(overlayID))     
   
ConfObj=OvManager.createOverlay(overlayID);
    

    if (ConfObj != null)
    /* Creation of new overlayID is successful */ 
      System.out.println("Created overlay:  "+ConfObj.getStringProperty("Overlay", null));
      else
     /* if overlayID exists create a configuration object */ 
     ConfObj=OvManager.getOverlaySocketConfig(overlayID);

} catch (Exception e) {
     System.err.println("Exception when creating overlay.");
     System.err.println("Exception: " + e.getMessage());
     System.err.println("Exiting.");
     e.printStackTrace(System.err);
     System.exit(1);
}
/* Finally, the overlay socket is created with the configuration object 
    
*/
MySocket=ConfObj.createOverlaySocket(null);

/* Once the overlay socket is create, it can join the overlay */ 
MySocket.joinGroup();

/* Now we wait for 2 seconds. This is enough 
    time to have the overlay stabilize 
*/
try {
        Thread.sleep(2000);
} catch (InterruptedException e) {}

/* Print the logical address */ 
System.out.println("Logical Address: +MySocket.getLogicalAddress().toString()+"_");

/* Convert the string to a byte array and create a message */ 

byte[] MyData = MyString.getBytes(); 
I_OverlayMessage MyMessage = MySocket.createMessage(MyData,    MyData.length);

/* The message is sent to all members of the overlay */
MySocket.sendToAll(MyMessage);

/* Then, wait for the messages from other sockets; 
*/ 

while(true){
      try {
            /* get the received message */ 
         I_OverlayMessage RecvdMessage = MySocket.receive(); 

          /* get the logical address of the sender */ 
         String SenderSrc = RecvdMessage.getSrcLogicalAddress().toString();

        /* Next we transfor the received message into a string */
       String sMsg=new String(RecvdMessage.getPayload(), 0, 
       RecvdMessage.getPayloadLength());

       /* ... and print the payload */ 
      
System.out.println(SenderSrc+" sent: "+sMsg);
    } 

     catch (InterruptedException e) {}

    }
}

};