/*******************************************************************************
**
** Source File Name = nodecat.c
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 2000 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
**    PURPOSE :
**       an example showing how to use NODE CATALOG APIs in order to:
**          - create/catalog a node
**          - list a directory of nodes (showing what was created)
**          - uncatalog the node
**
**    APIs USED :
**       CATALOG NODE                           sqlectnd()
**       OPEN NODE DIRECTORY SCAN               sqlenops()
**       GET NEXT NODE DIRECTORY ENTRY          sqlengne()
**       CLOSE NODE DIRECTORY SCAN              sqlencls()
**       UNCATALOG NODE                         sqleuncn()
**
**    STRUCTURES USED :
**       sqle_node_struct
**       sqle_node_tcpip
**       sqleninfo
**       sqlca
**
**    OTHER FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**
**       internal :
**          list_node :       Displays a directory of catalogued nodes
**
**       external :
**          check_error :     Checks for SQLCODE error, and prints out any
**          [in UTIL.C]          related information available.
**                               This procedure is located in the UTIL.C file.
**
**    EXTERNAL DEPENDENCIES :
**       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
**         or the Microsoft Visual C++ compiler (Windows) 
**         or the compiler supported on your platform.
**
** For more information about these samples see the README file.
**
** For more information on programming in C, see the:
**   -  "Programming in C and C++" section of the Application Development Guide
** For more information on Building C Applications, see the:
**   -  "Building C Applications" section of the Application Building Guide.
**
** For more information on the SQL language see the SQL Reference.
**
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef DB2MAC
#include <malloc.h>
#endif
#include <sqlenv.h>
#include "utilapi.h"



int list_node (struct sqlca);

int main (void) {
   struct sqle_node_struct newNode;
   struct sqlca sqlca;
   struct sqle_node_tcpip protocolTCPIP;


   printf ("this is sample program 'nodecat.c'\n");
   strncpy (newNode.nodename, "newnode", SQL_NNAME_SZ);
   strncpy (newNode.comment, "this is a test node : newnode", SQL_CMT_SZ);
   newNode.struct_id = SQL_NODE_STR_ID;

   /* for TCP/IP connections, additional information on host and server
      needs to be entered */
   newNode.protocol = SQL_PROTOCOL_TCPIP;

   strncpy (protocolTCPIP.hostname, "hostname", SQL_HOSTNAME_SZ + 1);
   strncpy (protocolTCPIP.service_name, "servicename",
      SQL_SERVICE_NAME_SZ + 1);

   printf("cataloging the node : '%s'...\n", newNode.nodename);
   /*************************\
   * CATALOG NODE API called *
   \*************************/
   sqlectnd (&newNode, &protocolTCPIP, &sqlca);
   API_SQL_CHECK("cataloging the node");
   printf ("node '%s' has been created\n",newNode.nodename);

   printf ("now listing all nodes...\n");
   list_node (sqlca);

   printf ("now uncataloging the node that was created : '%s'\n",
      newNode.nodename);
   /***************************\
   * UNCATALOG NODE API called *
   \***************************/
   sqleuncn (newNode.nodename, &sqlca);
   API_SQL_CHECK("uncataloging node");

   printf ("list all nodes [after uncatalogued node]\n");
   list_node (sqlca);
   return 0;
}


/*******************************************************************************
** procedure : list_node
**
** This procedure OPENs a NODE DIRECTORY SCAN, GETs a NODE DIRECTORY ENTRY,
** displays the content of the entry, and then CLOSEs the NODE DIRECTORY SCAN
** once all entries have been displayed.
**
*******************************************************************************/
int list_node (struct sqlca sqlca) {
   unsigned short idx;
   unsigned short nodeHandle, nodeCount;
   struct sqleninfo *nodeBuffer;

   /*************************************\
   * OPEN NODE DIRECTORY SCAN API called *
   \*************************************/
   sqlenops (&nodeHandle, &nodeCount, &sqlca);
   if (sqlca.sqlcode == 1037) {
      printf ("--- Node directory is empty ---\n");
      return 1;
   } else {
      API_SQL_CHECK("opening the node directory scan API");
   } /* endif */

   for (idx = 0; idx < nodeCount; idx++) {
      /******************************************\
      * GET NEXT NODE DIRECTORY ENTRY API called *
      \******************************************/
      sqlengne (nodeHandle, &nodeBuffer, &sqlca);
      API_SQL_CHECK("getting next node entry");

      /* printing out the node information on to the screen */
      printf ("node name         : %.8s\n", nodeBuffer->nodename);
      printf ("node comment      : %.30s\n", nodeBuffer->comment);
      printf ("node host name    : %.55s\n", nodeBuffer->hostname);
      printf ("node service name : %.14s\n", nodeBuffer->service_name);

      switch (nodeBuffer->protocol) {
      case SQL_PROTOCOL_APPC:
         printf ("node protocol     : APPC\n");
         break;
      case SQL_PROTOCOL_NETB:
         printf ("node protocol     : NetBios\n");
         break;
      case SQL_PROTOCOL_APPN:
         printf ("node protocol     : APPN\n");
         break;
      case SQL_PROTOCOL_TCPIP:
         printf ("node protocol     : TCP/IP\n");
         break;
      case SQL_PROTOCOL_CPIC:
         printf ("node protocol     : CPIC\n");
         break;
/*      case SQL_PROTOCOL_IPXSPX:
         printf ("node protocol     : IPXSPX\n");
         break; */
      default:
         printf ("node protocol     : \n");
         break;
      } /* endswitch */
      printf ("\n");
   } /* endfor */
   /**************************************\
   * CLOSE NODE DIRECTORY SCAN API called *
   \**************************************/
   sqlencls (nodeHandle, &sqlca);
   API_SQL_CHECK("closing node directory scan");
   return 0;
}