Interstage Application Server Smart Repository運用ガイド
目次 索引 前ページ次ページ

第6章 アプリケーションの作成(C API)> 6.3 サンプルプログラム> 6.3.3 サンプルプログラムの紹介

6.3.3.5 非同期型のエントリ削除

 非同期型でエントリを削除する場合のサンプルプログラムを以下に示します。

[del.c]

/*
 * All Rights Reserved, Copyright (C) FUJITSU LIMITED 2006
 *
 * エントリを1つ削除する。
 *
 */

#include    <sys/types.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <time.h>
#if defined(unix)
#include    <unistd.h>
#elif defined(_WIN32)
#include    <winsock.h>
#endif
#include    "examples.h"
#include    "def.h"

unsigned long    global_counter = 0;

char    *host   = SAMPLE_HOST;
int     port    = SAMPLE_PORT;
char    *binddn = SAMPLE_BINDDN;
char    *passwd = SAMPLE_PASSWORD;
char    *del_dn = "uid=100001," SAMPLE_BASEDN;

/*-------------------------------------------------------------*/
/*
 * 削除結果を待ち合わせている間、他の作業をすることができます。
 * 他の作業をするプログラムをここに記述します。
 */
static void do_other_work()
{
    extern  unsigned long   global_counter;

    global_counter++;
}


/*-------------------------------------------------------------*/
int main(
    int     argc,
    char    *argv[] )
{
    LDAP        *ld;
    LDAPMessage *result;
    int         rtn;
    int         msgid;
    int         finished;
    struct timeval  timeout;
    int         optdata;
    int         errcode;
    char        *errmsg;
    char        *matched;

    extern  char    *host;
    extern  int     port;
    extern  char    *binddn;
    extern  char    *passwd;
    extern  char    *del_dn;

/*----------------------------------------------------------*/

    printf( "%s is start\n", argv[0] );

    /* セションのオープン */
    printf( "%s: ldap_init( \"%s\", %d )\n", argv[0], host, port );
    ld = ldap_init( host, port );
    if ( ld == NULL ) {
        perror( "ldap_init" );
        printf( "%s is abnormal end\n", argv[0] );
        return( 1 );
    }

    /* オプションの設定 ( LDAP_VERSION3 ) */
    optdata = LDAP_VERSION3;
    printf( "%s: ldap_set_option( LDAP_OPT_PROTOCOL_VERSION, %d )\n", argv[0], optdata );
    rtn = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *)(&optdata));
    if ( rtn != 0 ) {
        do_error_msg( "ldap_set_option", rtn, NULL, NULL );
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 1 );
    }

    /* 簡易認証*/
    printf( "%s: ldap_simple_bind_s( \"%s\", \"******\" )\n", argv[0], binddn );
    rtn = ldap_simple_bind_s( ld, binddn, passwd );
    if ( rtn != LDAP_SUCCESS ) {
        do_error_msg( "ldap_simple_bind_s", rtn, NULL, NULL );
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 1 );
    }

    /* エントリの削除 */
    printf( "%s: ldap_delete( \"%s\" )\n", argv[0], del_dn );
    msgid = ldap_delete( ld, del_dn );
    if ( msgid < 0 ) {
        do_get_ldaperror( ld, &errcode, &errmsg );
        do_error_msg( "ldap_delete", errcode, NULL, errmsg );
        ldap_memfree( errmsg );
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 1 );
    }

    /* 結果の待ち合わせ処理 */
    finished = 0;
    while ( finished == 0 ) {
        printf( "%s: ldap_result( )\n", argv[0] );
        timeout.tv_sec  = 30L;
        timeout.tv_usec = 0L;
        rtn = ldap_result( ld, msgid, LDAP_MSG_ALL, &timeout, &result );
        switch ( rtn ) {
        case -1:
            /* some error occurred */
            if ( result != NULL ) {
                ldap_parse_result( ld, result, &errcode, &matched, &errmsg, 0, 0, 0 );
                do_error_msg( "ldap_result", errcode, matched, errmsg );
                ldap_msgfree( result );
                ldap_memfree( matched );
                ldap_memfree( errmsg );
            }
            printf( "%s: Entry delete error.\n", argv[0] );
            do_unbind( ld, argv );
            printf( "%s is abnormal end\n", argv[0] );
            return( 1 );
        case 0:
            /* タイムアウト発生時 */
#if defined(unix)
            sleep(1);
#elif defined(_WIN32)
            Sleep(1*1000);
#endif
            break;
        default:
            /* 処理完了 */
            finished = 1;
            ldap_parse_result( ld, result, &errcode, &matched, &errmsg, 0 , 0, 0 );
            if ( errcode == LDAP_SUCCESS ) {
                printf( "%s: Entry deleted successfully.  I counted to %ld while waiting.\n", argv[0], global_counter );
            } else {
                printf( "%s: Error while deleting entry.\n", argv[0] );
                do_error_msg( "ldap_result", errcode, matched, errmsg );
            }
            ldap_msgfree( result );
            ldap_memfree( matched );
            ldap_memfree( errmsg );
        }
        do_other_work();
    }
    /* 終了処理 */
    do_unbind( ld, argv );
    printf( "%s is normal end\n", argv[0] );
    return( 0 );
}

目次 索引 前ページ次ページ

All Rights Reserved, Copyright(C) 富士通株式会社 2007