/**
***************************************************************************
 IndConsumer.java
 This is a test program to receive Indications sent by Indication clients.
 This program start the CIM Indication Listener on port 8189.

 The Initial Developer of the Original Code :
     Bapu Patil ( bapu_patil@hp.com)
     Hewlett-Packard Company.

 Modified by:

***************************************************************************
*/
package tests.IndTests;

import org.snia.wbem.client.*;
import org.snia.wbem.cim.*;
import org.snia.wbem.listener.*;
import org.snia.wbemcmd.xml.*;
import java.net.*;
import java.util.*;

public class IndConsumer 
{
    int proto=CIMClient.XML;
    String hostName="sequoia";
    String nameSpace="root/cimv2";
    int portNo=5988;
    int listenerPortNo=8189;
    String classText="";
    String username="guest";
    String password="guest";
    String attachName=hostName;

    boolean cleanupOnly=false;
    boolean delete=true;
    CIMClient cc=null;
    CIMNameSpace clientNameSpace=null;
    CIMListener crl=null;
    boolean monitorMode=false;

    String localHostName=null;
    String sysCreationName="primary";

    static final String FCCN="CIM_IndicationFilter";
    static final String SCCN="CIM_IndicationSubscription";

    IndConsumer() { }

    /**
     This method processes the CIM Indication which is passed
     to this method by indicationOccurred method as a CIM Instance. 
     indicationOccurred method also give indURL which holds the
     name of the Indication URL specified in indications' request method
     For example : POST /cimom/myIndication1 HTTP/1.1, in here
        /cimom/myIndication1 is an indication URL.
     */
    void handleIndication(CIMInstance ind, String indURL) 
    {
       System.out.println("---------- Begin of event data ----------");
       System.out.println("--- Indication URL : "+indURL);
       System.out.println("--- Indication : "+ind.toString());
       try 
       {
          Object o=null;
          if (ind.getClassName().startsWith("CIM_Class"))
             o=(Object)ind.getProperty("classdefinition").getValue().getValue();
          if (ind.getClassName().startsWith("CIM_Inst"))
            o=(Object) ind.getProperty("sourceinstance").getValue().getValue();

          if (o instanceof CIMInstance) 
	  {
             CIMInstance eo=(CIMInstance)o;
             System.out.println("--- Embedded Instance: "+eo);
          }
          else if (o instanceof CIMClass) {
             CIMClass eo=(CIMClass)o;
             System.out.println("--- Embedded Class: "+eo);
          }
          System.out.println("----------- End of event data -----------\n");
       }
       catch (Exception ee) 
       {
          ee.printStackTrace();
       }
    }


    void receiveIt(String name, boolean isSSL)
    {
       CIMHTTPListener myCIML=null;
       try 
       {
	  //
	  // This extends CIMHTTPListener class and overrides the
	  // methood called indicationOccured. This as caller
	  // should know how to process an Indication which is
	  // passed back as a CIM Instance to it.
	  //
          CIMHTTPListener ccl = new CIMHTTPListener(listenerPortNo, isSSL) 
	        { 
                  public void indicationOccured(CIMIndication e, String indURL) 
                  {
                      handleIndication(e.getIndication(), indURL);
                   }
                };
          ccl.startCIMHTTPListener();

       }
       catch (Exception e) {
          System.out.println(e);
          e.printStackTrace();
       }

       System.out.println("Ok -  waiting for events "+sysCreationName);
    }

    /**
      Simple old main function
      It takes an argument '-ssl' for enabling SSL communication
     */
    public static void main(String args[]) throws CIMException 
    {
      boolean enableSSL=false;

      if ( args.length != 0 )
      {
        if ( args[0].equals("-ssl") )
        {
          System.out.println("SSL Enabled Indication Listener ");
          enableSSL = true;
        }

      }

       IndConsumer ti=new IndConsumer();
       ti.receiveIt("WBEMIndication", enableSSL);
    }
}
