/** HelloWorld_Security
 *
 *       The HyperCast HelloWorld programs demonstrate various aspects of
 *       the HyperCast API.  The programs create an overlay socket that 
 *       sends and receives messages. 
 *
 *       The HelloWorld_Security program is derived from Helloworld_CallBack, i.e., 
 *       all receive operations are nonblocking.
 *       This file corresponds to an example program in the HyperCast API description 
 *       (see Chapter:  "Overlay Socket API -Advanced Topics", Section: "Secure Overlay Sockets"). 
 * 
 *       The program works with all  security settings:
 *           - NodeAdapter and/or SocketAdapter set to SSL
 *           or
 *           - Security Level set to integrity, privacy, protocolintegrity
 *        Further:
 *        - The security algorithms use the default configuration.  
 *        - The .keystore files and the testcert.cer files are from the software distiribution, 
 *          and must be copied to the working directory.
 *           
 *       The file is a bit more complex than the example in the chapter.
 *
 * @author HyperCast Team
 * @version 2006 
 *
 */


import hypercast.*;
import hypercast.util.XmlUtil;

public class HelloWorld_Security implements I_ReceiveCallback {
	
// The string we send 
String MyString = new String("Hello World");

// The overlay socket 
I_OverlaySocket MySocket = null;

// The configuration object
HyperCastConfig ConfObj = null;
    
public static void main(String[] args) {
    	
   	// Create a new HelloWorld_CallBack instance 
	HelloWorld_Security hw = new HelloWorld_Security();
}
    
public void ReceiveCallback (I_OverlayMessage msg) {
		
   	// 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 + ".");
	  }
		
public  HelloWorld_Security () {
	
	//	Create a configuration object
	
	ConfObj = HyperCastConfig.createConfig("hypercast.xml");
   //  The keystore password must be set for all security settings 
	ConfObj.setPrivateTextAttribute(XmlUtil.createXPath("/Private/KeyStorePassword"), "password");
   
    // The private key alias and password are needed only for all Neighborhood key methods
    ConfObj.setPrivateTextAttribute (XmlUtil.createXPath("/Private/PrivateKeyAlias"), "testpair");
    ConfObj.setPrivateTextAttribute (XmlUtil.createXPath("/Private/PrivateKeyPassword"), "password");
   
   /*   The group key must be set for A byte array (obtained by other way) is used to generate the group key. 
    *   In this default configuration, the cypher algorithm specified in the configuration file by the attribute
    *	/Public/Security/cryptAlgorithm is AES. AES needs a 16-byte byte array tos create the
    *	 secret key.
    *
    *    The group key is only needed when the key mode method is set to GroupKeys or Neighborhoodkey1
    */
    ConfObj.setPrivateTextAttribute (XmlUtil.createXPath("/Private/GroupKey"), "1234567812345678");


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

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

	// Print the logical address */ 
	String MyLogicalAddress = MySocket.getLogicalAddress().toString();
	System.out.println("Logical address is " + MyLogicalAddress + ".");
	
	// Repeat forever
	for(;;) {

		// 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 ...");
	 }
}
}