예제 #1
0
int gw_em_mad_init( int max_job, char *info )
{
    int rc;

    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);

    if ( rc != GLOBUS_SUCCESS)
    {
        sprintf(info, "GRAM client module activation failed: %s (error %d)",
                globus_gram_client_error_string(rc), rc);
        return 1;
    }

    rc = globus_gram_client_callback_allow(gw_em_mad_state_callback, NULL,
                 &(mad.callback_contact));
                 
    if ( rc != GLOBUS_SUCCESS)
    {
        sprintf(info, "GRAM client callback allow failed: %s (error %d)",
                globus_gram_client_error_string(rc), rc);
        return 1;
    }

    init_job_pool(max_job);

    mad.initialized = 1;
    
    printf("INIT - SUCCESS -\n");

    return 0;
}
예제 #2
0
int gw_em_mad_recover( int jid, char *job_contact, char *info )
{
    int rc;
    int *i;
    
    if (!mad.initialized)
    {
        strcpy(info, "MAD not initialized");
        return 1;
    }

    i  = (int *) malloc( sizeof(int));
    *i = jid;

    rc = globus_gram_client_register_job_callback_registration(job_contact,
            GLOBUS_GRAM_PROTOCOL_JOB_STATE_ALL, mad.callback_contact,
            GLOBUS_GRAM_CLIENT_NO_ATTR, gw_em_mad_recover_callback, i);

    if (rc != GLOBUS_SUCCESS) 
    {
        sprintf(info, "GRAM client job callback register failed: %s (error %d)",
                globus_gram_client_error_string(rc), rc);
        return 1;
    }
        
    add_job(jid, job_contact);

    return 0;
}
예제 #3
0
int gw_em_mad_refresh(gss_cred_id_t creds, char *info)
{
    int jid, max_jobs;
    int rc, status = 0;
    int *i;
    char *job_contact;

    max_jobs = get_max_jobs();
    
    for (jid = 0; jid<max_jobs; jid++)
    {
        job_contact = get_job_contact(jid);
        
        if (job_contact != NULL)
        {
            i  = (int *) malloc( sizeof(int));
            *i = jid;
            
            rc = globus_gram_client_register_job_refresh_credentials(
                    job_contact, creds, GLOBUS_GRAM_CLIENT_NO_ATTR,
                    gw_em_mad_refresh_callback, i);

            if (rc != GLOBUS_SUCCESS) 
            {
                sprintf(info,
                        "GRAM client job refresh credentials failed: %s (%d)",
                        globus_gram_client_error_string(rc), rc);
                status = -1;
            }
        }
    }
    
    return status;
}
예제 #4
0
int gw_em_mad_poll( int jid, char *info )
{
    char *job_contact;
    int rc;
    int  *i;

    if (!mad.initialized)
    {
        strcpy(info, "MAD not initialized");
        return 1;
    }

    job_contact = get_job_contact(jid);

    if (job_contact == NULL)
    {
        strcpy(info, "Job does not exist");
        return 1;
    }
    
    i  = (int *) malloc( sizeof(int));
    *i = jid;
    
    rc = globus_gram_client_register_job_status(job_contact,
            GLOBUS_GRAM_CLIENT_NO_ATTR, gw_em_mad_poll_callback, (void *) i);
            
    if (rc != GLOBUS_SUCCESS)
    {
        sprintf(info, "GRAM client job status failed: %s (error %d)",
                globus_gram_client_error_string(rc), rc);
        return 1;
    }

    return 0;
}
static
void
example_submit_callback(
    void * user_callback_arg,
    globus_gram_protocol_error_t operation_failure_code,
    const char * job_contact,
    globus_gram_protocol_job_state_t job_state,
    globus_gram_protocol_error_t job_failure_code)
{
    struct monitor_t * monitor = user_callback_arg;

    globus_mutex_lock(&monitor->mutex);
    monitor->done = GLOBUS_TRUE;
    globus_cond_signal(&monitor->cond);
    if (operation_failure_code == GLOBUS_SUCCESS)
    {
        printf("Submitted job %s\n",
            job_contact ? job_contact : "UNKNOWN");
    }
    else
    {
        printf("submit failed because %s (Error %d)\n",
                globus_gram_client_error_string(operation_failure_code),
                operation_failure_code);
    }
    globus_mutex_unlock(&monitor->mutex);
}
예제 #6
0
void gw_em_mad_poll_callback(void *arg, const char * job_contact, 
        globus_gram_client_job_info_t *job_info)
{
    globus_gram_protocol_extension_t *entry;
    int *jid;
    
    jid = (int *) arg;

    if (job_info->protocol_error_code == GLOBUS_SUCCESS)
    {
        if (job_info->job_state == GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE)
            if (job_info->extensions)
            {
                entry = globus_hashtable_lookup(&job_info->extensions, "exit-code");

                if (entry != NULL)
                    printf("POLL %d SUCCESS DONE:%s\n", *jid, entry->value);
                else
                    printf("POLL %d SUCCESS DONE\n", *jid);
            }
            else
                printf("POLL %d SUCCESS DONE\n", *jid);
        else
            printf("POLL %d SUCCESS %s\n", *jid, get_job_state_name(job_info->job_state));
    }
    else
        printf("POLL %d FAILURE %s (%i)\n", *jid,
             globus_gram_client_error_string(job_info->protocol_error_code), job_info->protocol_error_code);
    
    free(jid);
}
예제 #7
0
int gw_em_mad_submit( int jid, char *rm_contact, char *rsl_file, char *info )
{
    int  rc;
    int  *i;
    char rsl_string[4096];
    int  fd;
    int  j;
    
    if (!mad.initialized)
    {
        strcpy(info, "MAD not initialized");
        return 1;
    }

    fd = open(rsl_file, O_RDONLY);

    if (fd == -1)
    {
        strcpy(info, "Error opening RSL file");
        return 1;
    }

    j = read(fd, (void *) rsl_string, sizeof(char)*4095);

    if ((j ==-1)||(j==0))
    {
        strcpy(info, "Error reading RSL file");
        close(fd);
        
        return 1;
    }
    else
        rsl_string[j]='\0';

    i  = (int *) malloc( sizeof(int));
    *i = jid;    

    rc = globus_gram_client_register_job_request(rm_contact, rsl_string, 
            GLOBUS_GRAM_PROTOCOL_JOB_STATE_ALL, mad.callback_contact, 
            GLOBUS_GRAM_CLIENT_NO_ATTR, gw_em_mad_submit_callback, (void *) i);
                        
    if (rc != GLOBUS_SUCCESS) 
    {
        sprintf(info, "GRAM client job request failed: %s (error %d)",
                globus_gram_client_error_string(rc), rc);
        close(fd);
        
        return 1;
    }
    
    close(fd);
    return 0;
}
예제 #8
0
void gw_em_mad_cancel_callback(void *arg, globus_gram_protocol_error_t
            failure_code, const char * job_contact, 
            globus_gram_protocol_job_state_t job_state, 
            globus_gram_protocol_error_t error_code)
{
    int *jid;
    
    jid = (int *) arg;

    if ( failure_code == GLOBUS_SUCCESS)
        printf("CANCEL %d SUCCESS -\n", *jid);
    else
        printf("CANCEL %d FAILURE %s (%i)\n", *jid,
             globus_gram_client_error_string(failure_code), error_code);
    
    free(jid);
}
예제 #9
0
void gw_em_mad_recover_callback(void *arg, globus_gram_protocol_error_t
            failure_code, const char * job_contact, 
            globus_gram_protocol_job_state_t job_state, 
            globus_gram_protocol_error_t error_code)
{
    int *jid;
    
    jid = (int *) arg;

    if ( failure_code == GLOBUS_SUCCESS)
    {
        printf("RECOVER %d SUCCESS %s\n", *jid, get_job_state_name(job_state));
    }
    else
    {
        printf("RECOVER %d FAILURE %s (%i)\n", *jid,
                globus_gram_client_error_string(failure_code), error_code);    
        del_job(*jid);
    }
    
    free(jid);
}
예제 #10
0
void gw_em_mad_refresh_callback (void *arg,
        globus_gram_protocol_error_t failure_code, const char * job_contact,
        globus_gram_protocol_job_state_t job_state, 
        globus_gram_protocol_error_t error_code)
{
    int *jid;

    jid = (int *)arg;

    if (failure_code == GLOBUS_SUCCESS)
    {
        printf("REFRESH %d SUCCESS -\n", *jid);
    }
    else
    {
        printf("REFRESH %d FAILURE %s (%d:%d)\n", *jid,
                globus_gram_client_error_string(failure_code), failure_code,
                error_code);
    }
    
    free(jid);
}
예제 #11
0
void gw_em_mad_submit_callback(void *arg, globus_gram_protocol_error_t
            failure_code, const char * job_contact, 
            globus_gram_protocol_job_state_t job_state, 
            globus_gram_protocol_error_t error_code)
{
    int *jid;
    
    jid = (int *) arg;

    if ( failure_code == GLOBUS_SUCCESS)
    {
        add_job(*jid, job_contact);
        printf("SUBMIT %d SUCCESS %s\n", *jid, job_contact);
    }
    else
    {
         printf("SUBMIT %d FAILURE %s (%i)\n", *jid,
             globus_gram_client_error_string(failure_code), error_code);    
    }
    
    free(jid);
}
int main(int argc, char *argv[])
{
    int rc;

    LTDL_SET_PRELOADED_SYMBOLS();
    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);
    if(rc != GLOBUS_SUCCESS)
    {
	fprintf(stderr, "Error activating GRAM Client\n");

	goto error_exit;
    }
    if(argc < 2)
    {
	rc = -1;

	fprintf(stderr, "Usage: %s rm_contact\n", argv[0]);

	goto deactivate_exit;
    }
    rc = globus_gram_client_ping(argv[1]);
    if(rc == GLOBUS_SUCCESS)
    {
	printf("Success pinging %s\n", argv[1]);
    }
    else
    {
	printf("Failed pinging %s because %s\n",
		argv[1],
		globus_gram_client_error_string(rc));
    }

  deactivate_exit:
    globus_module_deactivate_all();
  error_exit:
    return rc;
}
예제 #13
0
void gw_em_mad_state_callback(void *arg, const char *job_contact,
        globus_gram_client_job_info_t *job_info)
{                              
    globus_gram_protocol_extension_t *entry;
    int jid = -1;
    int done = 0;
    int job_state;

    jid = get_jid(job_contact);

    if (jid == -1)
    {
        return;
    }
 
    job_state = job_info->job_state;

    switch (job_state)
    {
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING:    
            printf("CALLBACK %d SUCCESS PENDING\n", jid);
            if (job_pool.cancel_state[jid] == 1)
            {
                globus_gram_client_job_cancel(job_contact);            
                job_pool.cancel_state[jid] = 0;
            }
        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_SUSPENDED:
            printf("CALLBACK %d SUCCESS SUSPENDED\n", jid);
            if (job_pool.cancel_state[jid] == 1)
            {
                globus_gram_client_job_cancel(job_contact);            
                job_pool.cancel_state[jid] = 0;
            }
        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE:
            printf("CALLBACK %d SUCCESS ACTIVE\n", jid);
            if (job_pool.cancel_state[jid] == 1)
            {
                globus_gram_client_job_cancel(job_contact);            
                job_pool.cancel_state[jid] = 0;
            }

        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED:
            if (job_info->protocol_error_code == GLOBUS_GRAM_PROTOCOL_ERROR_USER_CANCELLED )
                printf("CALLBACK %d SUCCESS FAILED\n", jid);
            else
                printf("CALLBACK %d FAILURE %s (error %d)\n", jid,
                        globus_gram_client_error_string(job_info->protocol_error_code),
                        job_info->protocol_error_code);

            done = 1;            
            job_pool.cancel_state[jid] = 0;
                  
        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE:
            if (job_info->extensions)
            {
                entry = globus_hashtable_lookup(&job_info->extensions, "exit-code");

                if (entry != NULL)
                    printf("CALLBACK %d SUCCESS DONE:%s\n", jid, entry->value);
                else
                    printf("CALLBACK %d SUCCESS DONE\n", jid);
            }
            else
                printf("CALLBACK %d SUCCESS DONE\n", jid);

            done = 1;
            job_pool.cancel_state[jid] = 0;            
        break;
    }

    if (done && (jid != -1))
        del_job(jid);
}
int
main(int argc, char *argv[])
{
    int rc;
    int i;
    struct monitor_t monitor;

    if (argc < 3)
    {
        fprintf(stderr, "Usage: %s RESOURCE-MANAGER-CONTACT RSL-SPEC...\n",
                argv[0]);
        rc = 1;

        goto out;
    }

    printf("Submiting %d jobs to %s\n", argc-2, argv[1]);

    /*
     * Always activate the GLOBUS_GRAM_CLIENT_MODULE prior to using any
     * functions from the GRAM Client API or behavior is undefined.
     */
    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error activating %s because %s (Error %d)\n",
                GLOBUS_GRAM_CLIENT_MODULE->module_name,
                globus_gram_client_error_string(rc),
                rc);
        goto out;
    }

    rc = globus_mutex_init(&monitor.mutex, NULL);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error initializing mutex %d\n", rc);

        goto deactivate;
    }

    rc = globus_cond_init(&monitor.cond, NULL);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error initializing condition variable %d\n", rc);

        goto destroy_mutex;
    }
    monitor.submit_pending = 0;

    /* Submits jobs from argv[2] until end of the argv array. At most
     * CONCURRENT_SUBMITS will be pending at any given time.
     */
    globus_mutex_lock(&monitor.mutex);
    for (i = 2; i < argc; i++)
    {
        /* This throttles the number of concurrent job submissions */
        while (monitor.submit_pending >= CONCURRENT_SUBMITS)
        {
            globus_cond_wait(&monitor.cond, &monitor.mutex);
        }

        /* When the job has been submitted, the example_submit_callback
         * will be called, either from another thread or from a 
         * globus_cond_wait in a nonthreaded build
         */
        rc = globus_gram_client_register_job_request(
                argv[1], argv[i], 0, NULL, NULL, example_submit_callback,
                &monitor);
        if (rc != GLOBUS_SUCCESS)
        {
            fprintf(stderr, "Unable to submit job %s because %s (Error %d)\n",
                    argv[i], globus_gram_client_error_string(rc), rc);
        }
        else
        {
            monitor.submit_pending++;
        }
    }

    /* Wait until the example_submit_callback function has been called for
     * each job submission
     */
    while (monitor.submit_pending > 0)
    {
        globus_cond_wait(&monitor.cond, &monitor.mutex);
    }
    globus_mutex_unlock(&monitor.mutex);

    printf("Submitted %d jobs (%d successfully)\n",
            argc-2, monitor.successful_submits);

    globus_cond_destroy(&monitor.cond);
destroy_mutex:
    globus_mutex_destroy(&monitor.mutex);
deactivate:
    /*
     * Deactivating the module allows it to free memory and close network
     * connections.
     */
    rc = globus_module_deactivate(GLOBUS_GRAM_CLIENT_MODULE);
out:
    return rc;
}
예제 #15
0
void gw_em_mad_state_callback(void *arg, char *job_contact, int job_state,
            int error_code )
{                              
    int jid = -1;
    int done = 0;

    jid = get_jid(job_contact);

    if (jid == -1)
    {
        return;
    }
 
    switch (job_state)
    {
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING:    
            printf("CALLBACK %d SUCCESS PENDING\n", jid);
            if (job_pool.cancel_state[jid] == 1)
            {
                globus_gram_client_job_cancel(job_contact);            
                job_pool.cancel_state[jid] = 0;
            }
        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_SUSPENDED:
            printf("CALLBACK %d SUCCESS SUSPENDED\n", jid);
            if (job_pool.cancel_state[jid] == 1)
            {
                globus_gram_client_job_cancel(job_contact);            
                job_pool.cancel_state[jid] = 0;
            }
        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE:
            printf("CALLBACK %d SUCCESS ACTIVE\n", jid);
            if (job_pool.cancel_state[jid] == 1)
            {
                globus_gram_client_job_cancel(job_contact);            
                job_pool.cancel_state[jid] = 0;
            }

        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED:
            if (error_code == GLOBUS_GRAM_PROTOCOL_ERROR_USER_CANCELLED )
                printf("CALLBACK %d SUCCESS FAILED\n", jid);
            else
                printf("CALLBACK %d FAILURE %s (error %d)\n", jid,
                        globus_gram_client_error_string(error_code),
                        error_code);
            done = 1;            
            job_pool.cancel_state[jid] = 0;
                  
        break;
        
        case GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE:
            printf("CALLBACK %d SUCCESS DONE\n", jid);
            done = 1;
            job_pool.cancel_state[jid] = 0;            
        break;
    }

    if (done && (jid != -1))
        del_job(jid);
}
int
main(int argc, char *argv[])
{
    int rc;
    globus_gram_client_attr_t attr;
    struct monitor_t monitor;

    if (argc < 3)
    {
        fprintf(stderr, "Usage: %s RESOURCE-MANAGER-CONTACT RSL-SPEC...\n",
                argv[0]);
        rc = 1;

        goto out;
    }

    printf("Submiting job to %s with full proxy\n", argv[1]);

    /*
     * Always activate the GLOBUS_GRAM_CLIENT_MODULE prior to using any
     * functions from the GRAM Client API or behavior is undefined.
     */
    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error activating %s because %s (Error %d)\n",
                GLOBUS_GRAM_CLIENT_MODULE->module_name,
                globus_gram_client_error_string(rc),
                rc);
        goto out;
    }

    rc = globus_mutex_init(&monitor.mutex, NULL);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error initializing mutex %d\n", rc);

        goto deactivate;
    }

    rc = globus_cond_init(&monitor.cond, NULL);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error initializing condition variable %d\n", rc);

        goto destroy_mutex;
    }
    monitor.done = GLOBUS_FALSE;

    /* Initialize attribute so that we can set the delegation attribute */
    rc = globus_gram_client_attr_init(&attr);

    /* Set the proxy attribute */
    rc = globus_gram_client_attr_set_delegation_mode(
        attr,
        GLOBUS_IO_SECURE_DELEGATION_MODE_FULL_PROXY);

    /* Submit the job rsl from argv[2]
     */
    globus_mutex_lock(&monitor.mutex);
    /* When the job has been submitted, the example_submit_callback
     * will be called, either from another thread or from a 
     * globus_cond_wait in a nonthreaded build
     */
    rc = globus_gram_client_register_job_request(
            argv[1], argv[2], 0, NULL, attr, example_submit_callback,
            &monitor);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Unable to submit job %s because %s (Error %d)\n",
                argv[2], globus_gram_client_error_string(rc), rc);
    }

    /* Wait until the example_submit_callback function has been called for
     * the job submission
     */
    while (!monitor.done)
    {
        globus_cond_wait(&monitor.cond, &monitor.mutex);
    }
    globus_mutex_unlock(&monitor.mutex);

    globus_cond_destroy(&monitor.cond);
destroy_mutex:
    globus_mutex_destroy(&monitor.mutex);
deactivate:
    /*
     * Deactivating the module allows it to free memory and close network
     * connections.
     */
    rc = globus_module_deactivate(GLOBUS_GRAM_CLIENT_MODULE);
out:
    return rc;
}
int
main(int argc, char *argv[])
{
    int rc;
    globus_hashtable_t extensions = NULL;
    globus_gram_protocol_extension_t * extension_value;

    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s RESOURCE-MANAGER-CONTACT\n", argv[0]);
        rc = 1;

        goto out;
    }

    printf("Checking version of GRAM resource: %s\n", argv[1]);

    /*
     * Always activate the GLOBUS_GRAM_CLIENT_MODULE prior to using any
     * functions from the GRAM Client API or behavior is undefined.
     */
    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error activating %s because %s (Error %d)\n",
                GLOBUS_GRAM_CLIENT_MODULE->module_name,
                globus_gram_client_error_string(rc),
                rc);
        goto out;
    }
    /*
     * Contact the service passed as our first command-line option and perform
     * a version check. If successful,
     * this function will return GLOBUS_SUCCESS, otherwise an integer
     * error code. Old versions of the job manager will return 
     * GLOBUS_GRAM_PROTOCOL_ERROR_HTTP_UNPACK_FAILED as they do not support
     * the version operation.
     */
    rc = globus_gram_client_get_jobmanager_version(argv[1], &extensions);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Unable to get service version from %s because %s "
                "(Error %d)\n",
                argv[1], globus_gram_client_error_string(rc), rc);
    }
    else
    {
        /* The version information is returned in the extensions hash table */
        extension_value = globus_hashtable_lookup(
                &extensions,
                "toolkit-version");

        if (extension_value == NULL)
        {
            printf("Unknown toolkit version\n");
        }
        else
        {
            printf("Toolkit Version: %s\n", extension_value->value);
        }

        extension_value = globus_hashtable_lookup(
                &extensions,
                "version");
        if (extension_value == NULL)
        {
            printf("Unknown package version\n");
        }
        else
        {
            printf("Package Version: %s\n", extension_value->value);
        }
        /* Free the extensions hash and its values */
        globus_gram_protocol_hash_destroy(&extensions);
    }

    /*
     * Deactivating the module allows it to free memory and close network
     * connections.
     */
    rc = globus_module_deactivate(GLOBUS_GRAM_CLIENT_MODULE);
out:
    return rc;
}
int main(int argc, char *argv[])
{
    char *                              callback_contact;
    char *                              job_contact;
    char *                              rm_contact;
    monitor_t                           monitor;
    int                                 rc = 0;

    LTDL_SET_PRELOADED_SYMBOLS();

    printf("1..1\n");
    rm_contact = getenv("CONTACT_STRING");

    if (argc == 2)
    {
        rm_contact = argv[1];
    }

    if (rm_contact == NULL)
    {
        fprintf(stderr, "Usage: %s resource-manager-contact\n", argv[0]);
        exit(1);
    }

    rc = globus_module_activate(GLOBUS_COMMON_MODULE);
    if(rc)
    {
        goto end;
    }
    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);
    if(rc)
    {
        goto disable_modules;
    }

    globus_mutex_init(&monitor.mutex ,GLOBUS_NULL);
    globus_cond_init(&monitor.cond, GLOBUS_NULL);
    monitor.state = GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING;

    rc = globus_gram_client_callback_allow(gram_state_callback,
                                           &monitor,
                                           &callback_contact);
    if(rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr,
                "Error creating callback contact %s.\n",
                globus_gram_client_error_string(rc));

        goto error_exit;
    }

    globus_mutex_lock(&monitor.mutex);
    rc = globus_gram_client_job_request(
            rm_contact,
            "&(executable=/bin/sleep)(arguments=300)",
            GLOBUS_GRAM_PROTOCOL_JOB_STATE_ALL,
            callback_contact,
            &job_contact);

    if(rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr,
                "Error submitting job request %s.\n",
                globus_gram_client_error_string(rc));

        goto destroy_callback_contact;
    }

    while(monitor.state != GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE &&
          monitor.state != GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED &&
          monitor.state != GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE)
    {
        globus_cond_wait(&monitor.cond, &monitor.mutex);
    }

    if(monitor.state == GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE)
    {
        rc = globus_gram_client_job_cancel(job_contact);
        if(rc != GLOBUS_SUCCESS)
        {
            fprintf(stderr,
                    "Error cancelling job %s.\n",
                    globus_gram_client_error_string(rc));

            goto destroy_callback_contact;
        }
    }

    while(monitor.state != GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE &&
          monitor.state != GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED)
    {
        globus_cond_wait(&monitor.cond, &monitor.mutex);
    }
    rc = monitor.errorcode;
    if(rc == GLOBUS_GRAM_PROTOCOL_ERROR_USER_CANCELLED)
    {
        rc = GLOBUS_SUCCESS;
    }
destroy_callback_contact:
    globus_mutex_unlock(&monitor.mutex);
    globus_gram_client_callback_disallow(callback_contact);
    free(callback_contact);
    free(job_contact);
error_exit:
    globus_mutex_destroy(&monitor.mutex);
    globus_cond_destroy(&monitor.cond);
disable_modules:
    globus_module_deactivate_all();
end:
    printf("%s - cancel-test\n", rc == 0 ? "ok" : "not ok");
    return rc;
}
int
main(int argc, char *argv[])
{
    int rc;
    int status = 0;
    int failure_code = 0;

    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s JOB-CONTACT\n", argv[0]);
        rc = 1;

        goto out;
    }

    /*
     * Always activate the GLOBUS_GRAM_CLIENT_MODULE prior to using any
     * functions from the GRAM Client API or behavior is undefined.
     */
    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error activating %s because %s (Error %d)\n",
                GLOBUS_GRAM_CLIENT_MODULE->module_name,
                globus_gram_client_error_string(rc),
                rc);
        goto out;
    }
    /*
     * Check the job status of the job named by the first argument to
     * this program.
     */
    rc = globus_gram_client_job_status(argv[1], &status, &failure_code);
    if (rc != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Unable to check job status because %s (Error %d)\n",
                globus_gram_client_error_string(rc), rc);
    }
    else
    {
        switch (status)
        {
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_UNSUBMITTED:
                printf("Unsubmitted\n");
                break;
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_STAGE_IN:
                printf("StageIn\n");
                break;
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING:
                printf("Pending\n");
                break;
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_ACTIVE:
                printf("Active\n");
                break;
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_SUSPENDED:
                printf("Suspended\n");
                break;
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_STAGE_OUT:
                printf("StageOut\n");
                break;
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE:
                printf("Done\n");
                break;
            case GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED:
                printf("Failed (%d)\n", failure_code);
                break;
            default:
                printf("Unknown job state\n");
                break;
        }
    }
    /*
     * Deactivating the module allows it to free memory and close network
     * connections.
     */
    rc = globus_module_deactivate(GLOBUS_GRAM_CLIENT_MODULE);
out:
    return rc;
}
/* submit a job without a callback contact, register a callback
 * contact, wait for job to terminate
 */
int
test1()
{
    char *				callback_contact;
    char *				job_contact;
    int					rc;
    monitor_t				monitor;

    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);

    if(rc)
    {
	goto out;
    }

    globus_mutex_init(&monitor.mutex ,GLOBUS_NULL);
    globus_cond_init(&monitor.cond, GLOBUS_NULL);
    monitor.state = GLOBUS_GRAM_PROTOCOL_JOB_STATE_PENDING;

    rc = globus_gram_client_callback_allow(gram_state_callback,
	                                   &monitor,
					   &callback_contact);
    if(rc != GLOBUS_SUCCESS)
    {
	fprintf(stderr,
		"Error creating callback contact %s.\n",
		globus_gram_client_error_string(rc));

	goto error_exit;
    }

    globus_mutex_lock(&monitor.mutex);
    rc = globus_gram_client_job_request(
	    resource_manager_contact,
	    "&(executable=/bin/sleep)(arguments=10)",
	    GLOBUS_GRAM_PROTOCOL_JOB_STATE_ALL,
	    GLOBUS_NULL,
	    &job_contact);

    if(rc != GLOBUS_SUCCESS)
    {
	fprintf(stderr,
		"Failed submitting job request because %s.\n",
		globus_gram_client_error_string(rc));

	goto destroy_callback_contact;
    }
    
    rc = globus_gram_client_job_callback_register(
	    job_contact,
	    GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED|
	    GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE,
	    callback_contact,
	    &monitor.state,
	    &monitor.errorcode);

    if(rc != GLOBUS_SUCCESS)
    {
	fprintf(stderr,
		"Error registering callback contact because %s.\n",
		globus_gram_client_error_string(rc));

	goto destroy_callback_contact;
    }

    while(monitor.state != GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED &&
	  monitor.state != GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE)
    {
	globus_cond_wait(&monitor.cond, &monitor.mutex);
    }

    rc = monitor.errorcode;

destroy_callback_contact:
    globus_gram_client_callback_disallow(callback_contact);
    globus_libc_free(callback_contact);
    globus_libc_free(job_contact);
    globus_mutex_unlock(&monitor.mutex);
error_exit:
    globus_mutex_destroy(&monitor.mutex);
    globus_cond_destroy(&monitor.cond);
    globus_module_deactivate(GLOBUS_GRAM_CLIENT_MODULE);

out:
    return rc;
}
int main(int argc, char *argv[])
{
    int rc = 1;
    monitor_t monitor;
    char * rsl;

    if (argc < 4 || (!globus_l_get_mode(&monitor, argv[2]))
            || ((monitor.timeout = atoi(argv[3])) < 0))
    {
        globus_libc_fprintf(stderr,
                "Usage: %s RM-CONTACT MODE SAVE_STATE TIMEOUT\n"
                "    RM-CONTACT: resource manager contact\n"
                "    MODE: no-commit|no-commit-end|commit|late-commit-end\n"
                "    TIMEOUT: two-phase timeout in seconds\n",
                argv[0]);
        goto error_exit;
    }

    rc = globus_module_activate(GLOBUS_GRAM_CLIENT_MODULE);

    if (rc != GLOBUS_SUCCESS)
    {
        globus_libc_fprintf(stderr,
                "failure activating GLOBUS_GRAM_CLIENT_MODULE: %s\n",
                globus_gram_client_error_string(rc));
        goto error_exit;
    }

    rsl = globus_common_create_string(format, monitor.timeout);
    if (rsl == NULL)
    {
        globus_libc_fprintf(stderr, "failure allocating rsl string\n");
        goto deactivate_exit;
    }

    globus_mutex_init(&monitor.mutex, NULL);
    globus_cond_init(&monitor.cond, NULL);
    monitor.job_contact = NULL;
    monitor.callback_contact = NULL;

    rc = globus_gram_client_callback_allow(
            globus_l_state_callback,
            &monitor,
            &monitor.callback_contact);

    if (rc != GLOBUS_SUCCESS || monitor.callback_contact == NULL)
    {
        globus_libc_fprintf(stderr,
                "failure allowing callbacks\n");
        rc = -1;
        goto destroy_monitor_exit;
    }

    globus_mutex_lock(&monitor.mutex);
    rc = globus_gram_client_job_request(
            argv[1],
            rsl,
            GLOBUS_GRAM_PROTOCOL_JOB_STATE_ALL,
            monitor.callback_contact,
            &monitor.job_contact);
    if (monitor.job_contact != NULL)
    {
        globus_libc_printf("%s\n", monitor.job_contact);
    }

    if (rc != GLOBUS_GRAM_PROTOCOL_ERROR_WAITING_FOR_COMMIT)
    {
        if (rc == GLOBUS_SUCCESS)
        {
            globus_libc_fprintf(stderr,
                    "job manager did not return "
                    "GLOBUS_GRAM_PROTOCOL_ERROR_COMMIT_TIMED_OUT\n");
            rc = -1;
        }
        else
        {
            globus_libc_fprintf(stderr,
                    "failure submitting job request [%d]: %s\n",
                    rc,
                    globus_gram_client_error_string(rc));
        }

        goto disallow_exit;
    }
    rc = 0;

    if (monitor.mode == no_commit)
    {
        goto disallow_exit;
    }

    rc = globus_gram_client_job_signal(
            monitor.job_contact,
            GLOBUS_GRAM_PROTOCOL_JOB_SIGNAL_COMMIT_REQUEST,
            NULL,
            &monitor.job_status,
            &monitor.failure_code);

    if (rc != GLOBUS_SUCCESS)
    {
        globus_libc_fprintf(stderr,
                "failure sending commit signal: %s\n",
                globus_gram_client_error_string(rc));
        goto disallow_exit;
    }

    while (monitor.job_status != GLOBUS_GRAM_PROTOCOL_JOB_STATE_DONE &&
           monitor.job_status != GLOBUS_GRAM_PROTOCOL_JOB_STATE_FAILED)
    {
        globus_cond_wait(&monitor.cond, &monitor.mutex);
    }

    if (monitor.mode == no_commit_end)
    {
        rc = 0;
        goto disallow_exit;
    }
    else if (monitor.mode == late_commit_end)
    {
        rc = 0;
        sleep(monitor.timeout + 1);
    }

    rc = globus_gram_client_job_signal(
            monitor.job_contact,
            GLOBUS_GRAM_PROTOCOL_JOB_SIGNAL_COMMIT_END,
            NULL,
            &monitor.job_status,
            &monitor.failure_code);

    if (rc != GLOBUS_SUCCESS)
    {
        globus_libc_fprintf(stderr,
                "failure sending commit end signal: %s\n",
                globus_gram_client_error_string(rc));
        goto disallow_exit;
    }

disallow_exit:
    if (monitor.job_contact != NULL)
    {
        globus_gram_client_job_contact_free(monitor.job_contact);
    }
    globus_mutex_unlock(&monitor.mutex);
    globus_gram_client_callback_disallow(monitor.callback_contact);
destroy_monitor_exit:
    if (monitor.callback_contact != NULL)
    {
        free(monitor.callback_contact);
    }
    globus_mutex_destroy(&monitor.mutex);
    globus_cond_destroy(&monitor.cond);
    free(rsl);
deactivate_exit:
    globus_module_deactivate_all();
error_exit:
    return rc;
}