コード例 #1
0
ファイル: flom_handle.c プロジェクト: tiian/flom
flom_handle_t *flom_handle_new(void)
{
    flom_handle_t *tmp_handle = NULL;

    /* dummy loop */
    while (TRUE) {
        int ret_cod;
        
        /* check flom library is initialized */
        if (FLOM_RC_OK != flom_init_check())
            break;
        FLOM_TRACE(("flom_handle_new\n"));
        /* try to allocate a new handle object */
        if (NULL == (tmp_handle = (flom_handle_t *)g_try_malloc(
                         sizeof(flom_handle_t)))) {
            FLOM_TRACE(("flom_handle_new: g_try_malloc returned NULL\n"));
            break;
        }
        FLOM_TRACE(("flom_handle_new: allocated handle %p\n", tmp_handle));
        /* initialize the new handle */
        if (FLOM_RC_OK != (ret_cod = flom_handle_init(tmp_handle))) {
            FLOM_TRACE(("flom_handle_new: flom_handle_init returned %d\n",
                        ret_cod));
            g_free(tmp_handle);
            FLOM_TRACE(("flom_handle_new: deallocated handle %p\n",
                        tmp_handle));
            tmp_handle = NULL;
            break;
        }
        /* exit the loop after one cycle */
        break;
    } /* while (TRUE) */
    FLOM_TRACE(("flom_handle_new: return %p\n", tmp_handle));
    return tmp_handle;
}
コード例 #2
0
ファイル: advanced_static.c プロジェクト: massdest/flom
int main(int argc, char *argv[]) {
    int ret_cod;
    /* step 1: handle declaration */
    flom_handle_t my_handle;
    
    /* step 2: handle initialization */
    if (FLOM_RC_OK != (ret_cod = flom_handle_init(&my_handle))) {
        fprintf(stderr, "flom_handle_init() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }

    /* step 3a: set a different AF_UNIX/PF_LOCAL socket to connect to FLoM
       daemon */
    if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name(
                           &my_handle, "/tmp/my_socket_name"))) {
        fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* step 3b: set a different (non default) resource name to lock */
    if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name(
                           &my_handle, "Red.Blue.Green"))) {
        fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    
    /* step 4: lock acquisition */
    if (FLOM_RC_OK != (ret_cod = flom_handle_lock(&my_handle))) {
        fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    } else  if (NULL != flom_handle_get_locked_element(&my_handle)) {
        printf("flom_handle_get_locked_element(): '%s'\n",
               flom_handle_get_locked_element(&my_handle));
    }

    /* step 5: execute the code that needs lock protection */
    
    /* step 6: lock release */
    if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(&my_handle))) {
        fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* step 7: handle clean-up (memory release) */
    if (FLOM_RC_OK != (ret_cod = flom_handle_clean(&my_handle))) {
        fprintf(stderr, "flom_handle_clean() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* exit */
    return 0;
}
コード例 #3
0
ファイル: case0002.c プロジェクト: massdest/flom
/*
 * Happy path usage with a static handle
 */
void static_handle_happy_path(void) {
    int ret_cod;
    flom_handle_t my_handle;

    /* initialize a new handle */
    if (FLOM_RC_OK != (ret_cod = flom_handle_init(&my_handle))) {
        fprintf(stderr, "flom_handle_init() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* get current AF_UNIX/PF_LOCAL socket_name */
    printf("flom_handle_get_socket_name() = '%s'\n",
           flom_handle_get_socket_name(&my_handle));
    /* set a new AF_UNIX/PF_LOCAL socket_name */
    if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name(
                           &my_handle, nd_socket_name))) {
        fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* get new AF_UNIX/PF_LOCAL socket_name */
    printf("flom_handle_get_socket_name() = '%s'\n",
           flom_handle_get_socket_name(&my_handle));
    /* check socket name */
    if (strcmp(nd_socket_name, flom_handle_get_socket_name(&my_handle))) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_socket_name\n");
        exit(1);
    }

    /* we don't get current trace filename because it can be altered by a
       global config file */
    /* set a new trace filename */
    flom_handle_set_trace_filename(&my_handle, nd_trace_filename);
    /* get new trace filename */
    printf("flom_handle_get_trace_filename() = '%s'\n",
           flom_handle_get_trace_filename(&my_handle));
    /* check trace filename */
    if (strcmp(nd_trace_filename,
               flom_handle_get_trace_filename(&my_handle))) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_trace_filename\n");
        exit(1);
    }
    
    /* get current resource name */
    printf("flom_handle_get_resource_name() = '%s'\n",
           flom_handle_get_resource_name(&my_handle));
    /* set a new resource name */
    if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name(
                           &my_handle, nd_resource_name))) {
        fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* get new resource name */
    printf("flom_handle_get_resource_name() = '%s'\n",
           flom_handle_get_resource_name(&my_handle));
    /* check resource name */
    if (strcmp(nd_resource_name,
               flom_handle_get_resource_name(&my_handle))) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_resource_name\n");
        exit(1);
    }
    
    /* get current value for resource create property */
    printf("flom_handle_get_resource_create() = %d\n",
           flom_handle_get_resource_create(&my_handle));
    /* set a new value for resource create property */
    flom_handle_set_resource_create(&my_handle, FALSE);
    /* get new value for resource create property */
    printf("flom_handle_get_resource_create() = %d\n",
           flom_handle_get_resource_create(&my_handle));
    /* check resource create 1/2 */
    if (flom_handle_get_resource_create(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_resource_create\n");
        exit(1);
    }
    /* set a new value for resource create property */
    flom_handle_set_resource_create(&my_handle, TRUE);
    /* get new value for resource create property */
    printf("flom_handle_get_resource_create() = %d\n",
           flom_handle_get_resource_create(&my_handle));
    /* check resource create 2/2 */
    if (!flom_handle_get_resource_create(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
		"get_resource_create\n");
        exit(1);
    }
    
    /* get current value for resource timeout property */
    printf("flom_handle_get_resource_timeout() = %d\n",
           flom_handle_get_resource_timeout(&my_handle));
    /* set a new value for resource timeout property */
    flom_handle_set_resource_timeout(&my_handle, -1);
    /* get new value for resource timeout property */
    printf("flom_handle_get_resource_timeout() = %d\n",
           flom_handle_get_resource_timeout(&my_handle));
    /* check resource timeout */
    if (-1 != flom_handle_get_resource_timeout(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
                "get_resource_timeout\n");
        exit(1);
    }
    
    /* get current value for resource quantity property */
    printf("flom_handle_get_resource_quantity() = %d\n",
           flom_handle_get_resource_quantity(&my_handle));
    /* set a new value for resource quantity property */
    flom_handle_set_resource_quantity(&my_handle, 3);
    /* get new value for resource quantity property */
    printf("flom_handle_get_resource_quantity() = %d\n",
           flom_handle_get_resource_quantity(&my_handle));
    /* check resource quantity */
    if (3 != flom_handle_get_resource_quantity(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
                "get_resource_quantity\n");
        exit(1);
    }
    
    /* get current value for resource lock mode property */
    printf("flom_handle_get_lock_mode() = %d\n",
           flom_handle_get_lock_mode(&my_handle));
    /* set a new value for resource lock mode property */
    flom_handle_set_lock_mode(&my_handle, FLOM_LOCK_MODE_PW);
    /* get new value for resource lock mode property */
    printf("flom_handle_get_lock_mode() = %d\n",
           flom_handle_get_lock_mode(&my_handle));
    /* check resource lock mode */
    if (FLOM_LOCK_MODE_PW != flom_handle_get_lock_mode(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_lock_mode\n");
        exit(1);
    }
    
    /* get current value for resource idle lifespan */
    printf("flom_handle_get_resource_idle_lifespan() = %d\n",
           flom_handle_get_resource_idle_lifespan(&my_handle));
    /* set a new value for resource idle lifespan */
    flom_handle_set_resource_idle_lifespan(&my_handle, 10000);
    /* get new value for resource idle lifespan */
    printf("flom_handle_get_resource_idle_lifespan() = %d\n",
           flom_handle_get_resource_idle_lifespan(&my_handle));
    /* check resource idle lifespan */
    if (10000 != flom_handle_get_resource_idle_lifespan(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
                "get_resource_idle_lifespan\n");
        exit(1);
    }
    
    /* get current unicast address */
    printf("flom_handle_get_unicast_address() = '%s'\n",
           flom_handle_get_unicast_address(&my_handle));
    /* set a new unicast_address */
    flom_handle_set_unicast_address(&my_handle, nd_unicast_address);
    /* get new unicast address */
    printf("flom_handle_get_unicast_address() = '%s'\n",
           flom_handle_get_unicast_address(&my_handle));
    /* check unicast address */
    if (strcmp(nd_unicast_address,
               flom_handle_get_unicast_address(&my_handle))) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
                "get_unicast_address\n");
        exit(1);
    }
    
    /* get current multicast address */
    printf("flom_handle_get_multicast_address() = '%s'\n",
           flom_handle_get_multicast_address(&my_handle));
    /* set a new multicast_address */
    flom_handle_set_multicast_address(&my_handle, nd_multicast_address);
    /* get new multicast address */
    printf("flom_handle_get_multicast_address() = '%s'\n",
           flom_handle_get_multicast_address(&my_handle));
    /* check multicast address */
    if (strcmp(nd_multicast_address,
               flom_handle_get_multicast_address(&my_handle))) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
                "get_multicast_address\n");
        exit(1);
    }
    
    /* set AF_UNIX/PF_LOCAL socket_name again */
    if (FLOM_RC_OK != (ret_cod = flom_handle_set_socket_name(
                           &my_handle, nd_socket_name))) {
        fprintf(stderr, "flom_handle_set_socket_name() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    
    /* get current value for unicast port */
    printf("flom_handle_get_unicast_port() = %d\n",
           flom_handle_get_unicast_port(&my_handle));
    /* set a new value for unicast_port */
    flom_handle_set_unicast_port(&my_handle, 7777);
    /* get new value for unicast port */
    printf("flom_handle_get_unicast_port() = %d\n",
           flom_handle_get_unicast_port(&my_handle));
    /* check unicast port */
    if (7777 != flom_handle_get_unicast_port(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_unicast_port\n");
        exit(1);
    }
    
    /* get current value for multicast port */
    printf("flom_handle_get_multicast_port() = %d\n",
           flom_handle_get_multicast_port(&my_handle));
    /* set a new value for multicast_port */
    flom_handle_set_multicast_port(&my_handle, 8888);
    /* get new value for multicast port */
    printf("flom_handle_get_multicast_port() = %d\n",
           flom_handle_get_multicast_port(&my_handle));
    /* check multicast port */
    if (8888 != flom_handle_get_multicast_port(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_multicast_port\n");
        exit(1);
    }
    
    /* get current value for discovery attempts property */
    printf("flom_handle_get_discovery_attempts() = %d\n",
           flom_handle_get_discovery_attempts(&my_handle));
    /* set a new value for discovery attempts property */
    flom_handle_set_discovery_attempts(&my_handle, 5);
    /* get new value for discovery attempts */
    printf("flom_handle_get_discovery_attempts() = %d\n",
           flom_handle_get_discovery_attempts(&my_handle));
    /* check discovery attempts */
    if (5 != flom_handle_get_discovery_attempts(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
                "get_discovery_attempts\n");
        exit(1);
    }
    
    /* get current value for discovery timeout property */
    printf("flom_handle_get_discovery_timeout() = %d\n",
           flom_handle_get_discovery_timeout(&my_handle));
    /* set a new value for discovery timeout property */
    flom_handle_set_discovery_timeout(&my_handle, 750);
    /* get new value for discovery timeout */
    printf("flom_handle_get_discovery_timeout() = %d\n",
           flom_handle_get_discovery_timeout(&my_handle));
    /* check discovery timeout */
    if (750 != flom_handle_get_discovery_timeout(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/"
                "get_discovery_timeout\n");
        exit(1);
    }
    
    /* get current value for discovery ttl property */
    printf("flom_handle_get_discovery_ttl() = %d\n",
           flom_handle_get_discovery_ttl(&my_handle));
    /* set a new value for discovery ttl property */
    flom_handle_set_discovery_ttl(&my_handle, 2);
    /* get new value for discovery ttl */
    printf("flom_handle_get_discovery_ttl() = %d\n",
           flom_handle_get_discovery_ttl(&my_handle));
    /* check discovery ttl */
    if (2 != flom_handle_get_discovery_ttl(&my_handle)) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_discovery_ttl\n");
        exit(1);
    }
    
    /* lock acquisition */
    if (FLOM_RC_OK != (ret_cod = flom_handle_lock(&my_handle))) {
        fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    } else if (NULL != flom_handle_get_locked_element(&my_handle)) {
        printf("staticHandleHappyPath locked element is %s\n",
               flom_handle_get_locked_element(&my_handle));
    }
    /* lock release */
    if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(&my_handle))) {
        fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* handle clean-up (memory release) */
    if (FLOM_RC_OK != (ret_cod = flom_handle_clean(&my_handle))) {
        fprintf(stderr, "flom_handle_clean() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
}
コード例 #4
0
ファイル: case0003.c プロジェクト: massdest/flom
/*
 * Happy path usage with a static handle
 */
void unlock_different_resource(void) {
    int ret_cod;
    flom_handle_t my_handle;

    /* initialize a new handle */
    if (FLOM_RC_OK != (ret_cod = flom_handle_init(&my_handle))) {
        fprintf(stderr, "flom_handle_init() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* get current resource name */
    printf("flom_handle_get_resource_name() = '%s'\n",
           flom_handle_get_resource_name(&my_handle));
    /* set a new resource name */
    if (FLOM_RC_OK != (ret_cod = flom_handle_set_resource_name(
                           &my_handle, nd_resource_name1))) {
        fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* get new resource name */
    printf("flom_handle_get_resource_name() = '%s'\n",
           flom_handle_get_resource_name(&my_handle));
    /* check resource name */
    if (strcmp(nd_resource_name1,
               flom_handle_get_resource_name(&my_handle))) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_resource_name\n");
        exit(1);
    }
        
    /* lock acquisition */
    if (FLOM_RC_OK != (ret_cod = flom_handle_lock(&my_handle))) {
        fprintf(stderr, "flom_handle_lock() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    } else if (NULL != flom_handle_get_locked_element(&my_handle)) {
        printf("staticHandleHappyPath locked element is %s\n",
               flom_handle_get_locked_element(&my_handle));
    }
    
    /* set a different resource name */
    if (FLOM_RC_API_IMMUTABLE_HANDLE != (
            ret_cod = flom_handle_set_resource_name(
                &my_handle, nd_resource_name2))) {
        fprintf(stderr, "flom_handle_set_resource_name() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* get new resource name */
    printf("flom_handle_get_resource_name() = '%s'\n",
           flom_handle_get_resource_name(&my_handle));
    /* check resource name: it should not be changed because the resource
     is already locked */
    if (strcmp(nd_resource_name1,
               flom_handle_get_resource_name(&my_handle))) {
        fprintf(stderr,
                "Unexpected result from flom_handle_set/get_resource_name\n");
        exit(1);
    }
        
    /* lock release */
    if (FLOM_RC_OK != (ret_cod = flom_handle_unlock(&my_handle))) {
        fprintf(stderr, "flom_handle_unlock() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
    /* handle clean-up (memory release) */
    if (FLOM_RC_OK != (ret_cod = flom_handle_clean(&my_handle))) {
        fprintf(stderr, "flom_handle_clean() returned %d, '%s'\n",
                ret_cod, flom_strerror(ret_cod));
        exit(1);
    }
}