样本 Sync Client C/C++ 应用程序

下列示例举例说明如何使用 DB2 Everyplace Sync Client API 函数的选择号来构建应用程序。可以在 \DB2e\Clients\clientapisample\C_API 中找到更多源代码示例。

/******************************************************************/
/**
 * This function defines the sync listener. See isyncore.h for more
 * information.
 * param: listenerData, your personal data.
 * param: event, event object
 * param: pExtraInfo (reserved)
 * return: integer, when event type is ISCEVTTYPE_Retry:
 *   . ISCRTNCB_ReplyYes    : retry less than 3 times
 *   . ISCRTNCB_ReplyNo     : retry more than or equal to 3 times
 *     when event type is ISCEVTTYPE_Info:
 *   . ISCRTNCB_Done
 *     when event type is ISCEVTTYPE_Query and its event code is ISCEVT_QueLogin:
 *   . ISCRTNCB_Done        : username and password are entered correctly
 *   . ISCRTNCB_Default     : username and password are not entered
 *     others (ISCEVTTYPE_Fatal, ISCEVTTYPE_Error, ISCEVTTYPE_Query,
 *             and ISCEVTTYPE_Conflict)
 *   . ISCRTNCB_Default     : take default action
 **/
static isy_INT32 syncListener(
                 isy_UINT32            listenerData,
       ISCEVT    *event,
       isy_VOID  *pExtraInfo)
{
    // appEventCodeToMessage is some user function to map an event code to
    // some descriptive event message
    char *statusMsg = appEventCodeToMessage(event);
    int   timesRetried;
 
    switch (event->type) {
        case ISCEVTTYPE_Fatal:
        case ISCEVTTYPE_Error:
                  printf("Error: %s\n", statusMsg);
            return ISCRTNCB_Default ;
 
        case ISCEVTTYPE_Retry:
            timesRetried = event->retry;
            if (timesRetried >= 3)
                        return ISCRTNCB_ReplyNo;
            else {
                char ans;
                printf("%s [Y/N] ", statusMsg);
                ans = getchar();        
                getchar();
                if(tolower(ans) == 'y')
                        return ISCRTNCB_ReplyYes;
                else
                        return ISCRTNCB_ReplyNo;
            }
 
        case ISCEVTTYPE_Info:
            switch (event->code) {
                case ISCEVT_InfSucceeded:
                case ISCEVT_InfFailed:
                case ISCEVT_InfCanceled:
                    printf("Conclusion: %s\n", statusMsg);
                     break;
                case ISCEVT_InfGeneral:
                case ISCEVT_InfCancelingSync:
                case ISCEVT_InfPrepMsg:
                case ISCEVT_InfSendMsg:
                case ISCEVT_InfWaitMsg:
                case ISCEVT_InfApplyMsg:
                    printf("Status: %s\n", statusMsg);
                     break;
                default: // ignore other event code
                     break;
            } // switch (event->code)
                return ISCRTNCB_Done;
 
        case ISCEVTTYPE_Query:
            if (event->code == ISCEVT_QueLogin) {
                ISCLISTENARG *args = event->info;
                isy_TCHAR *target = args->argv[0];
                // Just an example, not intended to be free of memory leaks.
                  isy_TCHAR *username = (isy_TCHAR *) calloc(18, sizeof(isy_TCHAR));
                  isy_TCHAR *password = (isy_TCHAR *) calloc(254, sizeof(isy_TCHAR));
                char c;
                int  i;
 
                printf("Query on target data(%s): %s ...\n", target, statusMsg);
                // Ask for the username
                printf("Username: ");
                for(i = 0; (c = getchar()) != '\n'; i++) username[i] = c;
                username[i] = '\0';
                if (i == 0) return ISCRTNCB_Default; // username not entered
                // Ask for the password
                printf("Password: ");
                for(i = 0; (c = getchar()) != '\n'; i++) password[i] = c;
                password[i] = '\0';
						args->argv[1] = username;
                  args->argv[2] = password;
                return ISCRTNCB_Done;
            }
            return ISCRTNCB_Default;
 
        // all other event types, don't care
        default:
            return ISCRTNCB_Default;
    } // switch (event->type)
}
 
// Sample SyncClient
main()
{
    isy_TCHAR user[]     = isy_T("user1");
    isy_TCHAR password[] = isy_T("password");
    HISCSERV  hServ;
    HISCCONF  hConf;
    HISCENG   hEngine;
    isy_INT32 rc;
 
    rc = iscConfigOpen(hServ, isy_T(".\isyncPath"), &hConf;); 
    rc = iscEngineOpen(hConf, &hEngine;);
    iscEngineSetListener(hEngine, syncListener, NULL);
 
    iscEngineSyncConfig(hEngine); // get the configuration first
    iscConfigEnableSubsSet(hConf, NULL); // enable all subscription sets
        rc = iscEngineSync(hEngine); // sync config + subscription sets
 
    if (rc == ISCRTN_Failed) {
        HISCCSR   hCursor;
        isy_TCHAR  id[ISCLEN_SubsSetID];
        isy_TCHAR  name[ISCLEN_SubsSetName];
        isy_INT32  enabled;
 
        iscConfigOpenCursor(hConf, &hCursor;);
        while (iscConfigGetNextSubsSet(hConf, hCursor, id, name)
               == ISCRTN_Succeeded) {
            enabled = iscConfigSubsSetIsEnable(hConf, id);
            if (enabled != ISCRTN_True) continue; // forget about those which have 
                                                  // been disabled
            rc = iscConfigGetSubsSetStatus(hConf, id);
            if (rc != ISCRTN_Succeeded)
// Then, the application can have some code
// processing the failing subscription sets here.
// To disable the subscription set, call:
 
                iscConfigDisableSubsSet(hConf, id);
        }
        iscConfigCloseCursor(hConf, hCursor);
        rc = iscEngineSync(hEngine); // sync config + subscription sets
    }
    // close all handles
    iscEngineClose(hEngine);
    iscConfigClose(hConf);
    iscServiceClose(hServ);
} // main
 

相关任务

相关参考