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

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

6.3.3.1 非同期型のエントリ検索

 非同期型のエントリ検索において、検索結果を1個ずつ受信する場合のサンプルプログラムを以下に示します。

[asearch.c]

/*
 * All Rights Reserved, Copyright (C) FUJITSU LIMITED 2006
 *
 * すべてのエントリを検索する。(非同期)
 *
 */

#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    *s_base  = SAMPLE_BASEDN;
int     scope    = LDAP_SCOPE_SUBTREE;
char    *filter  = "(objectclass=*)";


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

    global_counter++;

}


/*-------------------------------------------------------------*/
/* メインプログラム                                            */
int    main(
    int     argc,
    char    *argv[] )
{
    LDAP        *ld;
    LDAPMessage *result;
    LDAPMessage *entry;
    int         msgid;
    int         rtn;
    int         optdata;
    struct timeval  timeout;
    int         finished;
    int         errcode;
    char        *errmsg;
    char        *matched;
    int         num_entries = 0;

    extern    char    *host;
    extern    int     port;
    extern    char    *s_base;
    extern    int     scope;
    extern    char    *filter;
/*----------------------------------------------------------*/

    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( anonymous )\n", argv[0] );
    rtn = ldap_simple_bind_s( ld, NULL, NULL );
    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_search( \"%s\", %d, \"%s\" )\n", argv[0], s_base, scope, filter );
    msgid = ldap_search( ld, s_base, scope, filter, NULL, 0 );
    if ( msgid < 0 ) {
        do_get_ldaperror( ld, &errcode, &errmsg );
        do_error_msg( "ldap_search", 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_ONE, &timeout, &result );
        switch ( rtn ) {
        case -1:
            /* エラー発生時 */
            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 search error.\n", argv[0] );
            do_unbind( ld, argv );
            printf( "%s is abnormal end\n", argv[0] );
            return( 1 );
        case 0:
            /* タイムアウト発生時、または
               検索結果がないとき */
            if ( result != NULL ) {
                ldap_msgfree( result );
            }
#if defined(unix)
            sleep(1);
#elif defined(_WIN32)
            Sleep(1*1000);
#endif
            break;
        default:
            /*
             * 検索結果があるとき、または
             * すべての検索結果を受信したとき
             */
            entry = ldap_first_entry( ld, result );
            if ( entry == NULL ) {
                /* すべての検索結果を受信 */
                finished = 1;
                if ( result != NULL ) {
                    ldap_msgfree( result );
                }
                continue;
            }
            num_entries += print_entry( ld, result, 1 );
        }
        /* 検索結果を待ち合わせている間、他の作業をする。 */
        do_other_work();
    }

    /* 終了処理 */
    printf( "%s: %d entries retrieved.  I counted to %ld while I was waiting.\n", argv[0], num_entries, global_counter );

    do_unbind( ld, argv );
    printf( "%s is normal end\n", argv[0] );
    return( 0 );
}

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

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