/* @(#)93    1.19  src/examples/type_mgr/tm_admin_calls.c, examples.src, os2dce21.dss, 960602a.1 1/29/96 12:54:35 */
/*
 * COMPONENT_NAME:  examples.src
 *
 * FUNCTIONS:
 *
 * ORIGINS: 27
 *
 * (C) COPYRIGHT International Business Machines Corp. 1992, 1994
 * All Rights Reserved
 * Licensed Materials - Property of IBM
 *
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 */

/*********************************************************************
 *  File      :  tm_admin_calls.c                                    *
 *********************************************************************
 *                                                                   *
 *  Functions :  admin_calls()                                       *
 *                                                                   *
 *  Comments  :  Contains the RPC management calls that the          *
 *               management application will make.                   *
 *                                                                   *
 *********************************************************************/
#ifdef IBMOS2
#include <os2def.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>
#include <string.h>
#include <dce/pthread.h>
#include <dce/dce_error.h>
#include <dce/rpcbase.h>
#include <dce/dcefree.h>
#include <dce/rpc.h>
#include "tm_admin_if.h"
#include "type_mgr.h"
#include <util.h>
#include "tm_admin.h"

/*
 *  Macro to check the status of an RPC call.  The status is a parameter
 *  to the RPC, not the RPC's result.
 */

#define TM_ADMIN_CHECK_STATUS( _str, _val ) \
{ \
    char _error_text[80]; \
    int  _rc; \
    if ((_val)) { \
        PRINT(_str); \
        dce_error_inq_text( (_val), (_error_text), &(_rc) ); \
        if ( (_rc) != -1 ) { \
            PRINT("Error Code: %d\n", (_val)); \
            PRINT("Error String:  %s\n", (_error_text)); \
        } else { \
            PRINT( error_message[ _val - 1 ]); \
        } \
    } \
}

/* Error message table of the tm_admin application */

char *error_message[] = {
"The specified object already exists in the namespace.\n",
"The specified object already exists in the server database.\n",
"Something is wrong in the server database.\n",
"Something is wrong with the server database.\n",
"The specified object already exists.\n",
"Could not add the object to the server database.\n",
"Could not add the object to the server database.\n",
"Could not export the object to the namespace.\n",
"The specified object does not exist\n",
"Could not remove the object from the server database.\n",
"Could not get the object uuid from the namespace.\n",
"The specified UUID type is not valid.\n"
};


/*********************************************************************
 *   Function    :  admin_calls()                                    *
 *********************************************************************
 *                                                                   *
 *   Description :  Routine where RPC management runtime calls are   *
 *                  made.  DCE exception handling is implemented to  *
 *                  catch any exceptions that the RPC runtime        *
 *                  or pthread raise.The exception handling routine  *
 *                  print_exception() examines the exception         *
 *                  structure and prints a text string indicating    *
 *                  what the exception was.                          *
 *                                                                   *
 *   Returns     :  SUCCESS(0)  /  other value                       *
 *                                                                   *
 *********************************************************************/

static unsigned32 admin_print_list( obj_list *object_list );
static unsigned32 admin_server_list(void);

unsigned32 admin_calls( rpc_binding_handle_t *binding_h )
{
    error_status_t  rc = 0;
    unsigned32      status = 0;
    obj_list        *object_list;

    TRY
    {
        switch ( g_operation )
        {
         case ADD_OBJECT_MODE: /* add object */

            rc = tm_admin_add_object( *binding_h,
                       g_type_entry,
                       g_object_ns_entry,
                       &status );
            if ( rc )
            {
                CHECK_STATUS("tm_admin_add_object()\n",
                              rc,
                              RETURN );
            }

            if ( status )
            {
                TM_ADMIN_CHECK_STATUS( "Request to add an object failed.\n",
                                        status );
            }
            else
            {
                PRINT("Request to add an object was successful.\n");
            }
            break;

         case DEL_OBJECT_MODE: /* delete an object */

            rc = tm_admin_del_object( *binding_h,
                                      g_object_ns_entry,
                                      &status );
            if ( rc )
            {
                CHECK_STATUS("tm_admin_del_object()\n",
                              rc,
                              RETURN );
            }

            if ( status )
            {
                TM_ADMIN_CHECK_STATUS(
                           "Request to delete an object failed.\n",
                               status );
            }
            else
            {
                PRINT("Request to delete an object was successful.\n");
            }
            break;

         case LIST_OBJECT_MODE: /* print the object list */
            rc = tm_admin_list_object( *binding_h,
                      &object_list,
                      &status);
            if ( rc )
            {
                CHECK_STATUS("tm_admin_del_object()\n",
                              rc,
                              RETURN );
            }

            if ( status )
            {
                TM_ADMIN_CHECK_STATUS(
                          "Request to list the objects failed.\n",
                    status );
            }
            else
            {
                status = admin_print_list( object_list );
                if ( status )
                {
                    PRINT("Could not print the objects list.\n");
                }
                else
                {
                    PRINT("Request to print the objects was successful.\n");
                }
            }
            break;

         case STOP_SERVER_MODE: /* stop server */

            rpc_mgmt_stop_server_listening( *binding_h, &status );

            CHECK_STATUS( "rpc_mgmt_stop_server_listening()\n",
                          status,
                          CONTINUE );
            if ( status )
            {
                PRINT("Request to stop the server failed.\n");
                rc++;
            }
            else
            {
                PRINT("Request to stop the server was successful.\n");
            }
            break;

         case LIST_SERVER_MODE: /* print the server list */

            status = admin_server_list();

            if ( status )
            {
                PRINT("Could not list the servers.\n");
            }
            else
            {
                PRINT("Request to print the servers was successful.\n");
            }
            break;

     default:
                PRINT("Invalid operation!\n");

        } /* end switch */
    } /* end TRY */

    CATCH_ALL
    {
        PRINT("Caught an unexpected exception.\n");
        print_exception(THIS_CATCH);
        rc++;
    }

    ENDTRY

    return(0);

} /* end tm_admin_calls() */


/*********************************************************************
 *   Function    :  admin_print_list()                               *
 *********************************************************************
 *                                                                   *
 *   Description :  print the object list returned from the RPC.     *
 *                                                                   *
 *   Returns     :                                                   *
 *                                                                   *
 *********************************************************************/

static unsigned32 admin_print_list( obj_list *object_list )

{
   short    i;

   PRINT("*************************************************\n");
   PRINT("*   The object list:\n");

   for ( i = 0 ; i < object_list->size; i++)
   {
        PRINT("*      %s\n", object_list->objects[i]);
   }
   PRINT("*************************************************\n");

   return( SUCCESS );

} /* end admin_print_list() */


/*********************************************************************
 *   Function    :  admin_server_list ()                             *
 *********************************************************************
 *                                                                   *
 *   Description : Print the servers that exist.                     *
 *                 Since there is no available API to get the        *
 *                 information, from the name space, we use the      *
 *                 group attribute of the RPC entry.                 *
 *                 The servers register to the type_mgr/server-group *
 *                as a member.                                       *
 *                                                                   *
 *   Returns     :                                                   *
 *                                                                   *
 *********************************************************************/

static unsigned32 admin_server_list(void)

{
    rpc_ns_handle_t      inquiry_context;
    unsigned32           status;
    char                 group_name[ENTRY_LEN];
    char                 *member_name;
    char                 *tmp;

    sprintf( group_name, "%s%s", NAME_SERVICE_PATH,
                                 NAME_SERVICE_TM_SERVER_GROUP);

    rpc_ns_group_mbr_inq_begin( rpc_c_ns_syntax_default,
                                group_name,
                                rpc_c_ns_syntax_default,
                                &inquiry_context,
                                &status);

    PRINT("*************************************************\n");
    PRINT("*   List of Type Manager Servers:\n");

    while ( TRUE )
    {

       rpc_ns_group_mbr_inq_next( inquiry_context,
                      &member_name,
                  &status);

       if ( status == rpc_s_no_more_members )
       {
          break;
       }

       if ( status == rpc_s_entry_not_found )
       {
            PRINT("*      No servers exist yet.\n");
            break;
       }

       CHECK_STATUS( "rpc_ns_group_mbr_inq_next()\n",
                      status,
                      RETURN );

       PRINT("*    %s\n",(tmp=strrchr(member_name,'/'),++tmp));
       dce_free((char *)member_name);
    }
    rpc_ns_group_mbr_inq_done( &inquiry_context,
                          &status);

    CHECK_STATUS( "rpc_ns_group_mbr_inq_done()\n",
                   status,
                   RETURN );

    PRINT("*************************************************\n");

    return( SUCCESS );

} /* end admin_list_server() */

/* EOF tm_admin_calls.c */


