/*  $Id: test_ncbi_disp.c,v 6.9 2001/11/29 22:20:52 lavr Exp $
 * ===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 * Author:  Anton Lavrentiev
 *
 * File Description:
 *   Standard test for named service resolution facility
 *
 * --------------------------------------------------------------------------
 * $Log: test_ncbi_disp.c,v $
 * Revision 6.9  2001/11/29 22:20:52  lavr
 * Flow control trace messages added
 *
 * Revision 6.8  2001/09/24 20:35:34  lavr
 * +Test for SERV_Reset()
 *
 * Revision 6.7  2001/07/18 17:44:18  lavr
 * Added parameter to switch to local test
 *
 * Revision 6.6  2001/03/20 22:14:08  lavr
 * Second test added to list service by server type (yet #if 0'ed out)
 *
 * Revision 6.5  2001/03/09 04:58:26  lavr
 * Typo (made of pretty styling by vakatov) corrected in comparison
 *
 * Revision 6.4  2001/03/08 17:56:25  lavr
 * Redesigned to show that SERV_Open can return SERV_ITER, that
 * in turn returns 0 even if used for the very first time.
 *
 * Revision 6.3  2001/03/05 23:21:11  lavr
 * SERV_WriteInfo take only one argument now
 *
 * Revision 6.2  2001/03/02 20:01:38  lavr
 * SERV_Close() shown; "../ncbi_priv.h" explained
 *
 * Revision 6.1  2001/03/01 00:33:59  lavr
 * Initial revision
 *
 * ==========================================================================
 */

#include "../ncbi_priv.h"               /* CORE logging facilities */
#include <connect/ncbi_service.h>
#include <stdio.h>

/* One can define env.var. 'service'_CONN_HOST to reroute dispatching
 * information to particular dispatching host (instead of default).
 */
int main(int argc, const char* argv[])
{
    const char* service = argc > 1 ? argv[1] : "io_bounce";
    int/*bool*/ local = argc > 2;
    const SSERV_Info* info;
    int n_found = 0;
    SERV_ITER iter;

    CORE_SetLOGFILE(stderr, 0/*false*/);
    CORE_LOGF(eLOG_Note, ("Looking for service `%s' (%s)", service,
                          local ? "locally" : "randomly"));
    CORE_LOG(eLOG_Trace, "Opening service mapper");
    if ((local &&
         (iter = SERV_Open(service, fSERV_Any, SERV_LOCALHOST, 0)) != 0) ||
        (!local && (iter = SERV_OpenSimple(service)) != 0)) {
        CORE_LOG(eLOG_Trace, "Service mapper has been successfully opened");
        while ((info = SERV_GetNextInfo(iter)) != 0) {
            char* info_str = SERV_WriteInfo(info);
            CORE_LOGF(eLOG_Note, ("Service `%s' = %s", service, info_str));
            n_found++;
        }
        CORE_LOG(eLOG_Trace, "Resetting service mapper");
        SERV_Reset(iter);
        CORE_LOG(eLOG_Trace, "Service mapper has been reset");
        if (n_found && !(info = SERV_GetNextInfo(iter)))
            CORE_LOG(eLOG_Fatal, "Service not found after reset");
        CORE_LOG(eLOG_Trace, "Closing service mapper");
        SERV_Close(iter);
    }

    if (n_found != 0)
        CORE_LOGF(eLOG_Note, ("Test complete: %d server(s) found", n_found));
    else
        CORE_LOG(eLOG_Fatal, "Requested service not found");

#if 0
    {{
        SConnNetInfo* net_info;
        net_info = ConnNetInfo_Create(service);
        iter = SERV_Open(service, fSERV_Http, SERV_LOCALHOST, net_info);
        ConnNetInfo_Destroy(net_info);
    }}

    if (iter != 0) {
        while ((info = SERV_GetNextInfo(iter)) != 0) {
            char* info_str = SERV_WriteInfo(info);
            CORE_LOGF(eLOG_Note, ("Service `%s' = %s", service, info_str));
            n_found++;
        }
        SERV_Close(iter);
    }
#endif

    return 0;
}
