/** "Hello, world." program for HyperCast 2.0
*
* @author Multimedia Networks Group
* @author HyperCast Team 
* @version Hello World (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 implements I_CallBack 


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

public HelloWorld ()
{


/*****************************************************
This the callback function of the I_Callback interface. 
The callback is invoked each time a message arrives. 
This callback merely displays the received message 
******************************************************/


public void messageArrived(I_OverlayMessage RecvdMessage)
{
     /* 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);
}

/******************************************************
   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)
{
/******************************************************
                                       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; 


     /* A HelloWorld Instance to recive messages from I_CallBack */
     HelloWorld hw = new HelloWorld(); 


     /* 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 
    "hw" is the object that implements the callback 
*/
MySocket=ConfObj.createOverlaySocket(hw);

/* 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) {}

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

/* We 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 coming message 
*/ 
for (;;);
    
}

};