next up previous contents index
Next: ir2classes.pl Up: Code Previous: Code

ccsmread.C

  
/*
 * Sample Program to read an object-tree
 * Bernhard Fischer, 23.08.96
 */
 
#include <stdio.h>
#include <iostream.h>         // for cout/cerr
#include <somd.xh>            // DSOM header file
#include "bhcursor.xh"        // BHGCursor class
#include "bhfreem.xh"         // BHGFreeMem class
#include "bhfree.hh"          // BHGFree convenience functions
#include "bherror.xh"         // BHGErr module
#include "bhany.xh"           // BHGAny class
#include "ccsmread.xh"         // ccsmread header file

/*
 * FUNCTION:     nice
 * DESCRIPTION:  Format an Tree-Strukture of found objects.
 */
void
nice(int depth)
{
  for (int i=0; i<depth;i++)
    { 
      cout << "  |";
    }
}

/*
 * CLASS:        CDMCursor
 * DESCRIPTION:  Privat cursor-definition for CICSDomainManager
 */ 
class CDMCursor 
{
public:
  Environment &theEnv;
  BHGCursor * theCursor;
  int depth;  

  CDMCursor(Environment &env, int i=0)
    : theEnv(env),
      depth(i)
  {
    theCursor = new BHGCursor;                  
  }

  CDMCursor(const CDMCursor &aCursor)
    : theCursor(new BHGCursor(aCursor.theCursor)),
      theEnv(aCursor.theEnv),
      depth (aCursor.depth+1)
  {
  }

  ~CDMCursor()
  {
    delete theCursor;
  }
  
  operator string ()
  {
    return theCursor->getName(&theEnv);
  }

  /*
   * METHOD:      Standard-Methods of BHGCursor for CDMCursor
   * DESCRIPTION:  
   */
  int
  getContents(string filter,
              _IDL_SEQUENCE_BHGCursor_ObjectDescription *od)
  {
    return theCursor->getContents(&theEnv, filter, od);
  }

  int navigate(string object)
  {
    return theCursor->navigate(&theEnv, object);
  }

  BHGErr_ErrorInfo 
  getLastError()
  {
    return theCursor->getLastError(&theEnv);
  }


  /*
   * Check that the attribute value (argv[4]) can be parsed 
   * as a value of the correct type
  */
  long setAttributeValue( Environment ev,
                          any *       default_value,
                          any         attribute_value,
                          char *      any_value)
  {
    
    TCKind attribute_type;
    long   att_rc = 0;

    BHGAny     *any_obj =
      (BHGAny*) _BHGAny->sommGetSingleInstance(&ev);
     cerr << "***got a hold of single instance BHGAny" << endl;

    attribute_value._type  = NULL;
    attribute_value._value = NULL;
    // (the default value contains the attribute type information)
    
    attribute_type = TypeCode_kind(default_value->_type,&ev);
    
    switch (attribute_type)
      {
      case tk_short: 
        {
          short att_val;
          sscanf(any_value,"%d",&att_val);
          att_rc = any_obj->any_from_short(&ev,att_val,
                                           &attribute_value);
          break;
        }

      case tk_long: 
        {
          long att_val;
          sscanf(any_value,"%d",&att_val);
          att_rc = any_obj->any_from_long(&ev,att_val,
                                          &attribute_value);
          break;
        }
        
      case tk_ushort:  
        {
          unsigned short att_val;
          sscanf(any_value,"%d",&att_val);
          att_rc = any_obj->any_from_unsigned_short(&ev,att_val,
                                                    &attribute_value);
          break;
        }
        
      case tk_ulong:
      case tk_enum:        // enums can be set as unsigned longs
        {
          unsigned long att_val;
          sscanf(any_value,"%d",&att_val);
          att_rc = any_obj->any_from_unsigned_long(&ev,att_val, 
                                                   &attribute_value);
          break;
        }

      case tk_double: 
        {
          double att_val;
          sscanf(any_value,"%f",&att_val);
          att_rc = any_obj->any_from_double(&ev,att_val,
                                            &attribute_value);
          break;
        }
        
      case tk_boolean: 
        {
          boolean att_val;
          att_val = ((any_value[0] == 'T')  | (any_value[0]=='t'));
          att_rc = any_obj->any_from_boolean(&ev,att_val,
                                             &attribute_value);
          break;
        }

      case tk_string: 
        {
          att_rc = any_obj->any_from_string(&ev,any_value,
                                            &attribute_value);
          break;
        }

      case tk_float: 
        {
          float att_val;
          sscanf(any_value,"%f",&att_val);
          att_rc = any_obj->any_from_float(&ev,att_val,
                                           &attribute_value);
          break;
        }

      default:
        // the other type code kinds are not supported...
        cerr << "Unsupported Attribute Type" << endl;
        delete any_obj;       
        return (7);
      }  // end of switch
    return att_rc;
  }

  /*
   * METHOD:      dumpAttributes
   * DESCRIPTION: Prints the attribute-name of an object
   */
  int dumpAttributes(boolean bVal)
  {
    _IDL_SEQUENCE_BHGCursor_AttributeDescription ad;

    BHGAny     *any_obj =
      (BHGAny*) _BHGAny->sommGetSingleInstance(&theEnv);

    any    av;
    long   rc;
    string output;

    theCursor->describeAttributes(&theEnv, &ad);

    if( !ad._length == 0 ) 
      {
        nice(depth);
        cout << "  |-- ATTRIBUTES" << endl;
        for (int loop=0; loop<ad._length; loop++)
          {
            nice(depth);
            cout << "  |-- " << ad._buffer[loop].name;

            if (bVal)    // dump Values
              { 
                 
                theCursor->getAttribute(&theEnv, 
                                        ad._buffer[loop].name, 
                                        &av);
                any_obj->string_from_any(&theEnv, &av, &output );
                
                cout << ": " << output ;
                BHGFree(av);
              }
            cout << endl;
          }
        nice(depth);
        cout << endl;
      }
    BHGFree(ad);
    return 0;
  }

  /*
   * METHOD:      dumpActions
   * DESCRIPTION: Prints the actions-name of an object
   */
  int dumpActions()
  {
    _IDL_SEQUENCE_BHGCursor_ActionDescription actd;
    theCursor->describeActions(&theEnv, &actd);
    if( !actd._length == 0 ) 
      {
        nice(depth);
        cout << "  |-- ACTIONS" << endl;
        for (int loop=0; loop<actd._length; loop++)
          {
            nice(depth);
            cout << "  |-- " << actd._buffer[loop].name << endl;
          }
        nice(depth);
        cout << endl;
      }
    BHGFree(actd);
    return 0;
  }

  /*
   * METHOD:      dumpRelations
   * DESCRIPTION: Prints the relations-name of an object
   */
  int dumpRelations()
  {
    _IDL_SEQUENCE_BHGCursor_RelationshipDescription reld;
    theCursor->getRelations(&theEnv, "",&reld);
    if( !reld._length == 0 ) 
      {
        nice(depth);
        cout << "  |-- RELATIONS" << endl;
        for (int loop=0; loop<reld._length; loop++)
          {
            nice(depth);
            cout << "  |-- " << reld._buffer[loop].rel_name 
                 << " -- Class:" 
                 << reld._buffer[loop].class_name
                 << endl;
          }
        nice(depth);
        cout << endl;
      }
    BHGFree(reld);
    return 0;
  }

  /*
   * METHOD:      
   * DESCRIPTION: Prints the name and classname of an object
   */
  void dump(boolean bAttr, 
            boolean bVal, 
            boolean bOper, 
            boolean bRel)
  {
//    nice(depth);
//    cout << endl;

    /* Nur bis zur Tiefe der Ressource Definition (7) */
    /* Auch einzelne Ressource Definition (8) */
    if (depth < DEPTH)
      {
        nice(depth);
        cout << "--- " << theCursor->getName(&theEnv);
        cout << " -- ClassName: " 
             << theCursor->getClassName(&theEnv) 
             << endl;
        if (bAttr) dumpAttributes(bVal);
        if (bOper) dumpActions();
        if (bRel) dumpRelations();
      }
  }    

  /*
   * METHOD:      dumpContents
   * DESCRIPTION: Reads the contents of an object
   */
  _IDL_SEQUENCE_BHGCursor_ObjectDescription
  dumpContents(boolean bCont)
  {
    _IDL_SEQUENCE_BHGCursor_ObjectDescription od;
    int rc, loop;
    
  //  cerr << "--- Entering dumpContents() " << endl;
    rc =  theCursor->getContents(&theEnv,"",&od);
    if (rc != BHGErr_Success) 
      {
        cerr << "!!! ERROR getContents !!!" << endl;
        BHGErr_ErrorInfo err_info;
        err_info = getLastError();
        cerr << err_info.error_text << endl;
        od._length = 0;
      }
    if (bCont) 
      {
        if( !od._length == 0 ) 
          {
            for (loop = 0; loop < od._length; loop++)
              {
                nice(depth);
                cout << "| | |-- " 
                     << loop 
                     << ": " 
                     << od._buffer[loop].name 
                     << endl;
              }
            nice(depth);
            cout << "| |" << endl;
          }
      }
    return od;
  }
};

/*
 * FUNCTION:     allAboutAnObject
 * DESCRIPTION:  Prints many information about an object 
 *               and navigates through the object-tree
 */
int allAboutAnObject(Environment *global_ev, 
                     CDMCursor cursor,
                     string nextObject,
                     boolean bAttr,
                     boolean bVal,
                     boolean bOper,
                     boolean bRel,
                     boolean bCont)
{
  int rc, objectCounter;
  _IDL_SEQUENCE_BHGCursor_ObjectDescription od;

  if(nextObject)
    cursor.navigate(nextObject);
//  cerr << "*** Entering allAboutAnObject" << endl;

//  cerr << "=========================================" << endl;
  cursor.dump( bAttr, bVal, bOper, bRel);
//  cerr << "-----------------------------------------" << endl;

  od = cursor.dumpContents(bCont);
    
  for(int i=0; i<od._length; i++)
    {
      allAboutAnObject(global_ev,
                       cursor,
                       od._buffer[i].name,
                       bAttr,bVal,bOper,
                       bRel,bCont);
    }
  return 1;
}

int main(int argc, char * argv[])
  {
    boolean bAttr, bVal, bOper, bRel, bCont;

    /*
     * ------------- Application Initialization ---------------
     */

    if (argc < 2)
    {
      cerr << "USAGE: ccsmread [-a|-v|-o|-r|-c] serveralias" 
           << endl;
      cerr << "Required parameter is : "                    
           << endl;
      cerr << " 1. The alias of the server owning the object" 
           << endl;
      cerr << "Optional parameters are : "                    
           << endl;
      cerr << "   -a    Print Attributes" << endl;
      cerr << "   -v    Print Values of Attributes" 
           << endl;
      cerr << "   -o    Print Operations" << endl;
      cerr << "   -r    Print Relations" << endl;
      cerr << "   -c    Print Contents" << endl;
      return(1);
    }

    bAttr = FALSE;
    bVal  = FALSE;
    bOper = FALSE;
    bRel  = FALSE;
    bCont = FALSE;

    for (int i=1;i<argc;i++)
      {
        cerr << i << ": " << argv[i] << endl;
        (strcmp("-a",argv[i]) == 0) ? bAttr = TRUE : bAttr = bAttr;
        (strcmp("-v",argv[i]) == 0) ? bVal  = TRUE : bVal  = bVal  ;
        (strcmp("-o",argv[i]) == 0) ? bOper = TRUE : bOper = bOper;
        (strcmp("-r",argv[i]) == 0) ? bRel  = TRUE : bRel  = bRel;
        (strcmp("-c",argv[i]) == 0) ? bCont = TRUE : bCont = bCont;
      }


    /*
     * ------------- API Initialization ------------------------
     */
 
    // Declare an environment structure to be used by SOM
    Environment ev;
    
    int loop;
    cerr << " Declared an environment structure to be used by SOM" 
         << endl;
 
    // Initialize the SOM and DSOM environment
    SOM_InitEnvironment(&ev);
    SOMD_Init(&ev);

    cerr << "*** Initialized the SOM and DSOM environment" << endl;
 
    // Initialization of DSOM may set an exception 
    // in the environment
    if (ev._major != NO_EXCEPTION)
    {
      cerr << "*** EXCEPTION !!!" << endl;
      cerr << ev._major << endl;
      cerr << somExceptionId(&ev);
      cerr << endl;

      // Can't start the program - 
      // perhaps the SOM daemon is not running?
      somdExceptionFree(&ev);
      cerr <<
        "DSOM initialization failed - 
         perhaps the SOM daemon is not started?";
      cerr << endl;
      return (2);
    }

    cerr << "*** About to Initialize the API classes" << endl;
 
    // Initialize the API classes
    BHGCursorNewClass(0,0);
    BHGFreeMemNewClass(0,0);
    BHGAnyNewClass(0,0);
 
    cerr << "*** Initialized the API classes "                    
         << endl;

    // BHGFreeMem and BHGAny are "single instance" classes 
    // - lets get a hold of their single instances now
    BHGFreeMem *mem_free_obj =
      (BHGFreeMem*) _BHGFreeMem->sommGetSingleInstance(&ev);
    cerr << "***got a hold of single instance BHGAny" << endl;
 
    // BHGAny     *any_obj =
    //   (BHGAny*) _BHGAny->sommGetSingleInstance(&ev);
 
    // cerr << "***got a hold of single instance BHGAny" << endl;

    string temp;
    temp = _BHGCursor->somGetName() ;
    cerr << temp << endl;
    SOMFree (temp);

    /*
     * ------------------ Connection to Server -----------------
     */

    // The next thing I have to do is establish a connection
    //  to an SM Application or Resource Controller
 
    long rc;
    CDMCursor *my_cursor;

    cerr << "*** Connecting to Server:  " ;
    cerr << argv[argc-1] << endl;
    //cerr << argv[1] << endl;

    rc = _BHGCursor->connect(&ev,argv[argc-1]);
    //rc = _BHGCursor->connect(&ev,argv[1]);

    // did it work?
    if (rc != BHGErr_Success)
    {
      cerr << "*** ERROR occured!" << endl;
      BHGErr_ErrorInfo err_info;
      err_info = _BHGCursor->getLastError(&ev);
      cerr << err_info.error_text << endl;
 
      delete mem_free_obj;
 
      // Uninitialize SOM environment
      SOMD_Uninit(&ev);
      SOM_UninitEnvironment(&ev);
      return(3);
    }
    else
    {
      // it worked!
      // create a cursor located at the root of the network 
      // within component

      cerr << "*** Connected !!!" << endl ;

      my_cursor = new CDMCursor(ev);
    
      if (my_cursor) cerr << "*** New Cursor!" << endl;
      rc = my_cursor->theCursor->setAtLocation(&ev,argv[argc-1]);
      //rc = my_cursor->theCursor->setAtLocation(&ev,argv[1]);

      allAboutAnObject(&ev, *my_cursor, 
                       NULL,bAttr,bVal,bOper,bRel,bCont);
      cerr << "*** Exited allAboutAnObject!" << endl;

      BHGErr_ErrorInfo err_info;
      err_info = my_cursor->getLastError();
      cerr << err_info.error_text << endl;
                
      delete mem_free_obj;
     // delete any_obj;
      delete my_cursor;
                
      // Uninitialize SOM environment
      SOMD_Uninit(&ev);
      SOM_UninitEnvironment(&ev);
 
      SOMFree (&ev);

    } /* else */

 
    /*
     * ----------------------- Termination ---------------
     */

    // Free up memory used by all objects
    delete my_cursor;
    delete mem_free_obj;
    //delete any_obj;
 
    // Uninitialize SOM environment
    SOMD_Uninit(&ev);
    SOM_UninitEnvironment(&ev);
 
    return 0;
  } // end of main program...
 
 
/*
 * END OF CODE --------------------------------------------
 */
 



Copyright Munich Network Management Team