/* @(#)89    1.16  src/examples/type_mgr/server_util.c, examples.src, os2dce21.dss, 960602a.1 1/11/96 10:21:54 */
/*
 * 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      :  server_util.c                                       *
 *********************************************************************
 *                                                                   *
 *  Functions :  utilities to access namespace object entries.
 *                                                                   *
 *  Comments  :  These routines are called by the server at startup  *
 *               and by the tm_admin manager.                        *
 *                                                                   *
 *********************************************************************/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <dce/pthread.h>
#include <dce/dce_error.h>
#include <dce/rpc.h>
#include "cust_if.h"
#include "type_mgr.h"
#include <util.h>
#include "server.h"
#include "server_util.h"

/*
 *  Global structure containing the defined types the server supports.
 */

type_table_t  g_type_table[2];


/*********************************************************************
 *   Function    :  ns_compose_object_entry_name
 *********************************************************************
 *
 *   Description :  Given an object entry name, return the object's
 *            name service entry name.
 *
 *   Returns     :  SUCCESS/FAILURE
 *
 *   Comments     :  The ns directory is specified in type_mgr.h

 *********************************************************************/

unsigned32
ns_compose_object_entry_name( char  *object_base_name,
                  char  **object_ns_entry_name_p )
{
    char *str;

    /* create the full path of the object entry in the namespace. */

    str = (char *) malloc( strlen( NAME_SERVICE_PATH )
               + strlen( object_base_name )
               + 1 );

    if( str == NULL )
    return( FAILURE );

    strcpy( str, NAME_SERVICE_PATH );
    strcat( str, object_base_name );

    *object_ns_entry_name_p = str;

    return( SUCCESS );
}


/*********************************************************************
 *   Function    :  ns_get_object_uuid                              *
 *********************************************************************
 *                                                                   *
 *   Description :  Given an object entry name, return the object    *
 *                  UUID from the namespace.                         *
 *                                                                   *
 *   Returns     :  SUCCESS/FAILURE
 *                                                                   *
 *********************************************************************/

unsigned32
ns_get_object_uuid( char    *obj_base_name,
            uuid_t  *object_uuid_p )
{
    unsigned32           status;
    char                 *object_ns_entry_name;
    rpc_ns_handle_t      inquiry_context;

    status = ns_compose_object_entry_name( obj_base_name,
                       &object_ns_entry_name );

    if( status != SUCCESS )
    return( status );

    rpc_ns_entry_object_inq_begin( rpc_c_ns_syntax_default,
                                   object_ns_entry_name,
                                   &inquiry_context,
                                   &status);

    free( object_ns_entry_name );

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

    rpc_ns_entry_object_inq_next( inquiry_context,
                                  object_uuid_p,
                                  &status);

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

    rpc_ns_entry_object_inq_done( &inquiry_context,
                                  &status);

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

    return( SUCCESS );

} /* ns_get_object_uuid() */


/*********************************************************************
 *   Function    : ns_export_object
 *********************************************************************
 *
 *   Description :  Export the binding for the specified object and
 *            object_uuid to namespace.
 *
 *   Returns     :  SUCCESS/FAILURE
 *
 *********************************************************************/

unsigned32
ns_export_object( rpc_binding_vector_t *binding_vector,
          char               *object_base_name,
          uuid_t           *object_uuid_p )
{
    unsigned32     status;
    uuid_vector_t  uuid_vec;
    char           *object_ns_entry_name;

    status = ns_compose_object_entry_name( object_base_name,
                       &object_ns_entry_name );

    if( status != SUCCESS )
    return( status );


    uuid_vec.count = 1;
    uuid_vec.uuid[0] = object_uuid_p;


    rpc_ns_binding_export( rpc_c_ns_syntax_default,
               (unsigned_char_p_t) object_ns_entry_name,
               type_mgr_v1_0_s_ifspec,
               binding_vector,
               &uuid_vec,
               &status);

    free( object_ns_entry_name );


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

    return( SUCCESS );

} /* ns_export_object() */


unsigned32
type_uuid_from_type_name( char *type_name,
              uuid_t  *uuid_p )
{
    int i;
    unsigned32  status;

    for( i = 0; i < NUM_TYPE_MGRS; i++ )
    {
    if( strcmp( type_name, g_type_table[i].name ) == 0 )
    {
        /*
         *  In case you were wondering why not use the
         *  uuid already in the type table:
         *  There is no uuid_copy routine.  Even though
         *  we suspect uuid_t is a struct, and it is
         *  ANSI-legal to assign structs, it's just
         *  not the right thing to do.  So, we use
         *  uuid_from_string, which converts and copies the
         *  string uuid into the address passed in.
         */

        uuid_from_string( g_type_table[i].string_uuid,
                  uuid_p,
                  &status );

        CHECK_STATUS( "uuid_from_string() [g_type_table]\n",
              status,
              RETURN );

        break;
    }
    }
    return (0);
}  /* type_uuid_from_type_name */

/* EOF server_util.c */


