示例#1
0
int main(int argc, char **argv)
{
	lbm_context_t *ctx;  /* Context object: container for UM "instance". */
	lbm_src_t *src;      /* Source object: for sending messages. */
	int err;

#if defined(_MSC_VER)
	/* Windows-specific startup overhead */
	WSADATA wsadata;
	int wsa_err = WSAStartup(MAKEWORD(2,2), &wsadata);
	if (wsa_err != 0) {
		printf("line %d: wsStat=%d\n",__LINE__,wsStat);
		fprintf(stderr, "%s:%d, WSAStartup error: %d\n",
			__FILE__, __LINE__, wsa_err);
		exit(1);
	}
#endif

	/*** Initialization: create necessary UM objects. ***/

	err = lbm_context_create(&ctx, NULL, NULL, NULL);
	EX_LBM_CHK(err);

	{
		lbm_topic_t *topic;    /* Topic object: only needed temporarily. */

		err = lbm_src_topic_alloc(&topic, ctx, "Greeting", NULL);
		EX_LBM_CHK(err);

		err = lbm_src_create(&src, ctx, topic, NULL, NULL, NULL);
		EX_LBM_CHK(err);
	}

	SLEEP(1);  /* Let topic resolution execute. */


	/*** Send a message. ***/

	err = lbm_src_send(src, "Hello!", 6, LBM_MSG_FLUSH | LBM_SRC_BLOCK);
	EX_LBM_CHK(err);


	/*** Cleanup: delete UM objects. ***/

	SLEEP(3);  /* Linger a bit to allow retransmissions. */

	err = lbm_src_delete(src);
	EX_LBM_CHK(err);

	err = lbm_context_delete(ctx);
	EX_LBM_CHK(err);


#if defined(_MSC_VER)
	/* Windows-specific cleanup overhead */
	WSACleanup();
#endif

	return 0;
}  /* main */
示例#2
0
main()
{
	lbm_context_t *ctx;             /* pointer to context object */
	lbm_context_attr_t * cattr;     /* pointer to context attribute object */
	int err;                        /* return status of lbm functions (true=error) */
#if defined(_WIN32)
        HANDLE wthrdh;
#else
        pthread_t pthids;
#endif /* _WIN32 */


#if defined(_MSC_VER)
        /* windows-specific code */
        WSADATA wsadata;
        int wsStat = WSAStartup(MAKEWORD(2,2), &wsadata);
        if (wsStat != 0)
        {
                printf("line %d: wsStat=%d\n",__LINE__,wsStat);
                exit(1);
        }
#endif

	if (lbm_context_attr_create(&cattr) != 0)
	{
		fprintf(stderr, "lbm_context_attr_create: %s\n", lbm_errmsg());
		exit(1);
	}

	/* Setting the resolver address using the string method */
	if (lbm_context_attr_str_setopt(cattr, "operational_mode", "sequential") != 0) 
	{
		fprintf(stderr, "lbm_context_attr_str_setopt:operational_mode: %s\n", lbm_errmsg());
		exit(1);
	}

        err = lbm_context_create(&ctx, cattr, NULL, NULL);
        if (err)
        {
                printf("line %d: %s\n", __LINE__, lbm_errmsg());
                exit(1);
        }

#if defined(_WIN32)
	if ((wthrdh = CreateThread(NULL, 0, seq_thread, ctx, 0, NULL)) == NULL) {
		fprintf(stderr, "could not create thread\n");
		exit(1);
	}
#else
	if (pthread_create(&pthids, NULL, seq_thread, ctx) != 0) {
		fprintf(stderr, "could not spawn thread\n");
		exit(1);
	}
#endif /* _WIN32 */

	/* Wait forever */
	while (1) { }
}
示例#3
0
main()
{
	lbm_context_t *ctx;                     /* Context object */
	lbm_context_attr_t * cattr;             /* Context attribute object */
	lbm_rcv_t *rcv;                         /* Receive object: for subscribing to messages. */
	lbm_topic_t *rtopic;                    /* Receiver Topic object */
	lbm_rcv_topic_attr_t * rattr;		/* Receiver attribute object */
	lbm_ume_rcv_recovery_info_ex_func_t cb; /* Sequence number info callback function */
	int err;                                /* Used for checking API return codes */

#if defined(_WIN32)
	/* windows-specific code */
	WSADATA wsadata;
	int wsStat = WSAStartup(MAKEWORD(2,2), &wsadata);
	if (wsStat != 0)
	{
		printf("line %d: wsStat=%d\n",__LINE__,wsStat);
		exit(1);
	}
#endif

	/* Initialize context atrributes and create context */
	err = lbm_context_create(&ctx, NULL, NULL, NULL);
	EX_LBM_CHK(err);

	/* Create receiver for receiving request and sending response */
	err = lbm_rcv_topic_attr_create(&rattr);
	EX_LBM_CHK(err);

	cb.func = ume_rcv_seqnum_ex;
	cb.clientd = NULL;
	err = lbm_rcv_topic_attr_setopt(rattr, "ume_recovery_sequence_number_info_function", &cb, sizeof(cb));
	EX_LBM_CHK(err);

	err = lbm_rcv_topic_lookup(&rtopic, ctx, "test.topic", rattr);
        EX_LBM_CHK(err);

	err = lbm_rcv_create(&rcv, ctx, rtopic, rcv_handle_msg, NULL, NULL);
	EX_LBM_CHK(err);

	while (1) { }

	err = lbm_rcv_delete(rcv);
	EX_LBM_CHK(err);

	err = lbm_context_delete(ctx);
	EX_LBM_CHK(err);

#if defined(_MSC_VER)
	/* Windows-specific cleanup overhead */
	WSACleanup();
#endif
} /* main */
int main(int argc, char **argv)
{
	lbm_context_t *ctx;      /* Context object */
	lbm_rcv_t *rcv;          /* Receive object: for subscribing to messages. */
	lbm_topic_t *rtopic;     /* Receiver Topic object */
	int err;

#if defined(_WIN32)
	/* windows-specific code */
	WSADATA wsadata;
	int wsStat = WSAStartup(MAKEWORD(2,2), &wsadata);
	if (wsStat != 0)
	{
		printf("line %d: wsStat=%d\n",__LINE__,wsStat);
		exit(1);
	}
#endif
	
	/* Initialize context atrributes and create context */
	err = lbm_context_create(&ctx, NULL, NULL, NULL);
	EX_LBM_CHK(err);

	/* Create receiver for receiving request and sending response */
	err = lbm_rcv_topic_lookup(&rtopic, ctx, "test.topic", NULL);
	EX_LBM_CHK(err);

	err = lbm_rcv_create(&rcv, ctx, rtopic, rcv_handle_msg, NULL, NULL);
	EX_LBM_CHK(err);

	/* Wait forever (or until control-c). */
	while (1) { }

	err = lbm_rcv_delete(rcv);
	EX_LBM_CHK(err);

	err = lbm_context_delete(ctx);
	EX_LBM_CHK(err);

#if defined(_MSC_VER)
	/* Windows-specific cleanup overhead */
	WSACleanup();
#endif

	return 0;
}  /* main */
示例#5
0
main()
{
	lbm_context_t *ctx;							/* pointer to context object */
	lbm_topic_t *topic_1;						/* pointer to topic object */
	lbm_src_t *src;								/* pointer to source object */
	lbm_src_topic_attr_t *tattr;				/* pointer to source attribute object */
	lbm_context_attr_t * cattr;					/* pointer to context attribute object */
	int err;									/* return status of lbm functions (true=error) */
	lbm_event_queue_t *evq = NULL;				/* pointer to eventQ handle */

#if defined(_MSC_VER)
	/* windows-specific code */
	WSADATA wsadata;
	int wsStat = WSAStartup(MAKEWORD(2, 2), &wsadata);
	if (wsStat != 0)
	{
		printf("line %d: wsStat=%d\n", __LINE__, wsStat);
		exit(1);
	}
#endif

	/* Initialize the defaults for the context attribute object */
	if (lbm_context_attr_create(&cattr) != 0)
	{
		fprintf(stderr, "lbm_context_attr_create: %s\n", lbm_errmsg());
		exit(1);
	}

	/* Creating the context */
	err = lbm_context_create(&ctx, cattr, NULL, NULL);
	if (err)
	{
		printf("line %d: %s\n", __LINE__, lbm_errmsg());
		exit(1);
	}

	/* Delete the context attribute object */
	lbm_context_attr_delete(cattr);

	/* Initializing the source attribute object */
	if (lbm_src_topic_attr_create(&tattr) != 0)
	{
		fprintf(stderr, "lbm_src_topic_attr_create: %s\n", lbm_errmsg());
		exit(1);
	}

	/* Allocating the topic */
	err = lbm_src_topic_alloc(&topic_1, ctx, "test.topic", tattr);
	if (err)
	{
		printf("line %d: %s\n", __LINE__, lbm_errmsg());
		exit(1);
	}

	/* Create an event queue and associate it with a callback */
	if (lbm_event_queue_create(&evq, NULL, NULL, NULL) == LBM_FAILURE) {
		fprintf(stderr, "lbm_event_queue_create: %s\n", lbm_errmsg());
		exit(1);
	}
	
	/* Creating the source */
	err = lbm_src_create(&src, ctx, topic_1, handle_src_event, NULL, evq);
	if (err)
	{
		printf("line %d: %s\n", __LINE__, lbm_errmsg());
		exit(1);
	}

	/* This runs the eventQ for 10 seconds.  This means for the next 10 seconds */
	/* all of the sources events will be processed on this thread.				*/
	if(lbm_event_dispatch(evq, 10000) == LBM_FAILURE) {
		fprintf(stderr, "lbm_event_dispatch returned error: %s\n", lbm_errmsg());
	}

	/* Delete the first and second source topic attribute objects */
	lbm_src_topic_attr_delete(tattr);

	/* Finished with all LBM functions, delete the source and context object. */
	lbm_src_delete(src);
	lbm_context_delete(ctx);
	lbm_event_queue_delete(evq);
#if defined(_MSC_VER)
	WSACleanup();
#endif
}
示例#6
0
int main(int argc, char **argv)
{
	lbm_context_t *ctx;
	server_t server;  /* State information for the server. */
	char response_topic_name[256];
	int err;

	/* Get (pretty much) unique client name. */
	sprintf(response_topic_name, "Client.%lx.Response", (long)getpid());

#if defined(_MSC_VER)
	/* windows-specific code */
	WSADATA wsadata;
	int wsStat = WSAStartup(MAKEWORD(2,2), &wsadata);
	if (wsStat != 0) {
		printf("line %d: wsStat=%d\n",__LINE__,wsStat); exit(1);
	}
#endif

	server.ctx = NULL;

	server.topic_name = "Server1.Request";
	server.src = NULL;   /* Source to send requests to server. */
	server.rcv = NULL;   /* Receiver for responses from server. */
	server.state = 0;    /* Waiting for registration. */

	err = lbm_config("client.cfg");
	EX_LBM_CHK(err);

	err = lbm_context_create(&ctx, NULL, NULL, NULL);
	EX_LBM_CHK(err);
	server.ctx = ctx;

	/* Create source to send requests to server. */
	{
		lbm_topic_t *topic;
		err = lbm_src_topic_alloc(&topic, ctx, server.topic_name, NULL);
		EX_LBM_CHK(err);
		err = lbm_src_create(&server.src, ctx, topic, NULL, NULL, NULL);
		EX_LBM_CHK(err);
	}

	/* Create receiver for responses from server. */
	{
		lbm_topic_t *topic;
		err = lbm_rcv_topic_lookup(&topic, ctx, response_topic_name, NULL);
		EX_LBM_CHK(err);
		err = lbm_rcv_create(&server.rcv, ctx, topic, response_rcv_cb, &server, NULL);
		EX_LBM_CHK(err);
	}

	/* Register with the server.  May need multiple tries. */
	{
		int try_cnt = 0;
		int backoff_delay;
		char register_msg[257];
		sprintf(register_msg, "r%s", response_topic_name);

		backoff_delay = 1;  /* In milliseconds. */
		MSLEEP(backoff_delay);  /* Let TR complete. */
		while (server.state == 0) {
			try_cnt ++;
			err = lbm_src_send(server.src, register_msg,
				strlen(register_msg) + 1, LBM_MSG_FLUSH | LBM_SRC_BLOCK);
			EX_LBM_CHK(err);
			printf("Sent '%s' to %s\n", register_msg, server.topic_name);

			/* Exponential backoff, to max of 1 sec. */
			backoff_delay *= 2;  /* Exponential backoff to max of 1 sec. */
			if (backoff_delay > 1000) {
				backoff_delay = 1000;
			}
			MSLEEP(backoff_delay);  /* Wait for server response. */
		}
		printf("Took %d tries to register with server.\n", try_cnt);
	}

	/* Main work of the program, which includes sending 5 requests. */
	{
		int i;
		char send_buf[500];
		req_hdr_t *req_hdr = (req_hdr_t *)send_buf;

		memset((char *)req_hdr, '0', sizeof(req_hdr_t));
		req_hdr->data[0] = 'R';

		for (i = 0; i < 5; i++) {
			/* The application builds a request into <tt>request_msg</tt>. */
			char request_msg[257];
			sprintf(request_msg, "%s.%d", response_topic_name, i);

			/* The application message is copied in after the header. */
			strcpy(&send_buf[sizeof(req_hdr_t)], request_msg);
			err = lbm_src_send(server.src, send_buf, strlen(send_buf) + 1,
				LBM_MSG_FLUSH | LBM_SRC_NONBLOCK);
			EX_LBM_CHK(err);
			MSLEEP(1000);
		}
	}

	printf("Client exiting.\n");
	err = lbm_rcv_delete(server.rcv);
	EX_LBM_CHK(err);
	err = lbm_src_delete(server.src);
	EX_LBM_CHK(err);

	err = lbm_context_delete(ctx);
	EX_LBM_CHK(err);

#if defined(_MSC_VER)
	WSACleanup();
#endif

	return 0;
}  /* main */
main()
{
    lbm_context_t *ctx;             /* pointer to context object */
    lbm_topic_t *topic_1;           /* pointer to topic object */
    lbm_topic_t *topic_2;           /* pointer to topic object */
    lbm_src_t *src_1;               /* pointer to source object */
    lbm_src_t *src_2;               /* pointer to source object */
    lbm_src_topic_attr_t *tattr_1;  /* pointer to source attribute object */
    lbm_src_topic_attr_t *tattr_2;  /* pointer to source attribute object */
    lbm_context_attr_t * cattr;     /* pointer to context attribute object */
    lbm_uint16_t res_port;          /* Int to set resolver port */
    lbm_uint16_t des_port;          /* Int to set lbtrm destination port */
    int err;                        /* return status of lbm functions (true=error) */

#if defined(_MSC_VER)
    /* windows-specific code */
    WSADATA wsadata;
    int wsStat = WSAStartup(MAKEWORD(2,2), &wsadata);
    if (wsStat != 0)
    {
        printf("line %d: wsStat=%d\n",__LINE__,wsStat);
        exit(1);
    }
#endif

    /* Initialize the defaults for the context attribute object */
    if (lbm_context_attr_create(&cattr) != 0)
    {
    	fprintf(stderr, "lbm_context_attr_create: %s\n", lbm_errmsg());
	exit(1);
    }

    /* Setting the resolver address using the string method */
    if (lbm_context_attr_str_setopt(cattr, "resolver_multicast_address", "224.10.11.12") != 0) 
    {
     	fprintf(stderr, "lbm_context_attr_str_setopt:resolver_multicast_address: %s\n", lbm_errmsg());
     	exit(1);
    }

    /* Setting the resolver port using the data-type value method */
    res_port = 12345;
    if (lbm_context_attr_setopt(cattr, "resolver_multicast_port", &res_port, sizeof(lbm_uint16_t)) != 0 )
    {
	fprintf(stderr, "lbm_context_attr_setopt:resolver_mutlicast_port: %s\n", lbm_errmsg());
        exit(1);
    }

    /* Creating the context */
    err = lbm_context_create(&ctx, cattr, NULL, NULL);
    if (err)
    {
        printf("line %d: %s\n", __LINE__, lbm_errmsg());
        exit(1);
    }

    /* Delete the context attribute object */
    lbm_context_attr_delete(cattr);

    /* Initializing the source attribute object */
    if (lbm_src_topic_attr_create(&tattr_1) != 0)
    {
    	fprintf(stderr, "lbm_src_topic_attr_create: %s\n", lbm_errmsg());
        exit(1);
    }

    /* Setting the transport via the source topic string method */
    if (lbm_src_topic_attr_str_setopt(tattr_1, "transport", "lbtrm") != 0 )
    {
    	fprintf(stderr, "lbm_src_topic_attr_str_setopt:transport: %s\n", lbm_errmsg());
        exit(1);
    }

    /* Setting the lbtrm destination port via the direct value set method */
    des_port = 14001;
    if (lbm_src_topic_attr_setopt(tattr_1, "transport_lbtrm_destination_port", &des_port, sizeof(lbm_uint16_t)) != 0 )
    {
    	fprintf(stderr, "lbm_src_topic_attr_setopt:transport_lbtrm_destination_port: %s\n", lbm_errmsg());
        exit(1);
    }

    /* Allocating the topic */
    err = lbm_src_topic_alloc(&topic_1, ctx, "test.topic.1", tattr_1);
    if (err)
    {
        printf("line %d: %s\n", __LINE__, lbm_errmsg());
        exit(1);
    }

    /* Creating the source */
    err = lbm_src_create(&src_1, ctx, topic_1, NULL, NULL, NULL);
    if (err)
    {
        printf("line %d: %s\n", __LINE__, lbm_errmsg());
        exit(1);
    }

    /* Initialized the second source attributes as a copy of the first */
    if (lbm_src_topic_attr_dup(&tattr_2, tattr_1) != 0 )
    {
        fprintf(stderr, "lbm_src_topic_attr_dup: %s\n", lbm_errmsg());
        exit(1);
    }

    /* Now modify the destination port for this second source to put the publisher on a different transport */
    des_port = 14002;
    if (lbm_src_topic_attr_setopt(tattr_2, "transport_lbtrm_destination_port", &des_port, sizeof(lbm_uint16_t)) != 0 )
    {
        fprintf(stderr, "lbm_src_topic_attr_setopt:transport_lbtrm_destination_port: %s\n", lbm_errmsg());
        exit(1);
    }

    /* Allocating the second topic */
    err = lbm_src_topic_alloc(&topic_2, ctx, "test.topic.2", tattr_2);
    if (err)
    {
        printf("line %d: %s\n", __LINE__, lbm_errmsg());
        exit(1);
    }

    /* Creating the source */
    err = lbm_src_create(&src_2, ctx, topic_2, NULL, NULL, NULL);
    if (err)
    {
        printf("line %d: %s\n", __LINE__, lbm_errmsg());
        exit(1);
    }

    /* Delete the first and second source topic attribute objects */
    lbm_src_topic_attr_delete(tattr_1);
    lbm_src_topic_attr_delete(tattr_2);

    /* Finished with all LBM functions, delete the source and context object. */
    lbm_src_delete(src_1);
    lbm_src_delete(src_2);
    lbm_context_delete(ctx);

#if defined(_MSC_VER)
    WSACleanup();
#endif
}