source_notify_callbacks.c

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 /* source_notify_callbacks.c - see http://ultramessaging.github.io/UMExamples/source_notify_callbacks/c/index.html * * Copyright (c) 2005-2017 Informatica Corporation. All Rights Reserved. * Permission is granted to licensees to use * or alter this software for any purpose, including commercial applications, * according to the terms laid out in the Software License Agreement. * * This source code example is provided by Informatica for educational * and evaluation purposes only. * * THE SOFTWARE IS PROVIDED "AS IS" AND INFORMATICA DISCLAIMS ALL WARRANTIES * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF * NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR * PURPOSE. INFORMATICA DOES NOT WARRANT THAT USE OF THE SOFTWARE WILL BE * UNINTERRUPTED OR ERROR-FREE. INFORMATICA SHALL NOT, UNDER ANY CIRCUMSTANCES, BE * LIABLE TO LICENSEE FOR LOST PROFITS, CONSEQUENTIAL, INCIDENTAL, SPECIAL OR * INDIRECT DAMAGES ARISING OUT OF OR RELATED TO THIS AGREEMENT OR THE * TRANSACTIONS CONTEMPLATED HEREUNDER, EVEN IF INFORMATICA HAS BEEN APPRISED OF * THE LIKELIHOOD OF SUCH DAMAGES. */ #include <stdio.h> #if defined(_MSC_VER) /* Windows-only includes */ #include <winsock2.h> #define SLEEP(s) Sleep((s)*1000) #else /* Unix-only includes */ #include <stdlib.h> #include <unistd.h> #define SLEEP(s) sleep(s) #endif #include <lbm/lbm.h> /* State structure associated with each source the receiver is joined to. */ typedef struct source_state_s { int msgs_rcvd; /* Track message count from each source. */ } source_state_t; /* Example error checking macro. Include after each UM call. */ #define EX_LBM_CHK(err) do { \ if ((err) < 0) { \ fprintf(stderr, "%s:%d, lbm error: '%s'\n", \ __FILE__, __LINE__, lbm_errmsg()); \ exit(1); \ } \ } while (0) void *new_src_notification_callback(const char *source_name, void *clientd) { source_state_t *source_state = (source_state_t *)malloc(sizeof(source_state_t)); source_state->msgs_rcvd = 0; printf("Delivery Controller Created: %s\n", source_name); return source_state; /* This will be available in the receive callback as msg->source_clientd. */ } /* new_src_notification_callback */ int src_delete_notification_callback(const char *source_name, void *clientd, void* src_clientd ) { free(src_clientd); /* This was created by new_src_notification_callback() */ printf("Delivery Controller Deleted: %s\n", source_name); return 0; } /* src_delete_notification_callback */ /* Callback used to handle request message for receiver */ int rcv_handle_msg(lbm_rcv_t *rcv, lbm_msg_t *msg, void *clientd) { source_state_t *source_state = (source_state_t *)msg->source_clientd; switch (msg->type) { case LBM_MSG_DATA: source_state->msgs_rcvd ++; printf("[%s][%s], Received message %d\n", msg->topic_name, msg->source, source_state->msgs_rcvd); break; case LBM_MSG_BOS: printf("[%s][%s], Beginning of Transport Session\n", msg->topic_name, msg->source); break; case LBM_MSG_EOS: printf("[%s][%s], End of Transport Session\n", msg->topic_name, msg->source); break; default: printf("Other event, type=%x\n", msg->type); break; } return 0; } /* rcv_handle_msg */ 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 */ lbm_rcv_topic_attr_t * rattr; /* Receiver attribute object */ lbm_rcv_src_notification_func_t srccb; /* Source notify callback structure */ 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_attr_create(&rattr); EX_LBM_CHK(err); srccb.create_func = new_src_notification_callback; srccb.delete_func = src_delete_notification_callback; err = lbm_rcv_topic_attr_setopt(rattr, "source_notification_function", &srccb, sizeof(srccb)); 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); /* 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 */