/*file: minsrc.c - minimal source (sender) program. * * Copyright (c) 2005-2007 29West, Inc. 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 29West for educational - and evaluation purposes only. - - THE SOFTWARE IS PROVIDED "AS IS" AND 29WEST DISCLAIMS ALL WARRANTIES - EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF - NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR - PURPOSE. 29WEST DOES NOT WARRANT THAT USE OF THE SOFTWARE WILL BE - UNINTERRUPTED OR ERROR-FREE. 29WEST 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 29WEST HAS BEEN APPRISED OF - THE LIKELIHOOD OF SUCH DAMAGES. - - As you will see, error handling in this program is primitive. A production - program would want to have better error handling, but for the purposes of - a minimal example, it would just be a distraction. Also, a production - program would want to support a configuration file to override default - values on options. - - Build/run notes - WINDOWS - 1. Make sure the preprocessor variable "WIN32" is defined. From VC6: - Project->Settings...; C/C++; Category:General; - Preprocessor definitions: - 2. Add C:\Program Files\29West\LBM_\Win2k-i386\include\lbm to - additional include directories (where is the appropriate - version identifier). From VC6: - Project->Settings...; C/C++; Category:Preprocessor; - Additional include directories: - 3. Add lbm.lib to Object/library modules. From VC6: - Project->Settings...; Link; Category:General; Object/library modules: - 4. Add C:\Program Files\29West\LBM_\Win2k-i386\lib to - additional library path (where is the appropriate - version identifier). From VC6: - Project->Settings...; Link; Category:Input; Additional library path: - 5. The install procedure should already have added the LBM "bin" directory - to the Windows PATH. This is necessary so that "lbm.dll" can be found - when a program is run. To modify the Windows PATH from Win-XP: - right-click "My Computer"->properties; Advanced; - Environment variables; System variables:Path; Edit - - Build/run notes - UNIX - 1. Here is a sample build command: - cc -I$HOME/lbm/LBM_//include/lbm - -L$HOME/lbm/LBM_//lib -llbm -o min_src min_src.c - 2. The appropriate library search path should be updated to include the - LBM "bin" directory. For example, on Solaris: - LD_LIBRARY_PATH="$HOME/lbm/LBM_//bin:$LD_LIBRARY_PATH" - export LD_LIBRARY_PATH - Alternatively, the shared library can be copied from the LBM "bin" - directory to a directory which is already in the library search path. */ #include #if defined(_MSC_VER) /* Windows-only includes */ #include #define SLEEP(s) Sleep((s)*1000) #else /* Unix-only includes */ #include #include #define SLEEP(s) sleep(s) #endif #include main() { lbm_context_t *ctx; /* pointer to context object */ lbm_topic_t *topic; /* pointer to topic object */ lbm_src_t *src; /* pointer to source (sender) object */ int lbm_failed; /* return status of lbm functions */ #if defined(_MSC_VER) /* windows-specific code */ WSADATA wsadata; int wsStat = WSAStartup(MAKEWORD(2,2), &wsadata); if (wsStat != 0) {printf("line %s: %d\n", __LINE__, wsStat); exit(1);} #endif /*callout: context * Create a context object. A context is an environment in which LBM * functions. Note that the first parameter is a pointer to a pointer * variable; lbm_context_create writes the pointer to the context * object into "ctx". Also, by passing NULL to the context attribute * parameter, the default option values are used. */ lbm_failed = lbm_context_create(&ctx, NULL, NULL, NULL); if (lbm_failed) {printf("line %s: %s\n", __LINE__, lbm_errmsg()); exit(1);} /*callout: topic * Allocate a topic object. A topic object is little more than a string * (the topic name). During operation, LBM keeps some state information * in the topic object as well. The topic is bound to the containing * context, and will also be bound to a source object. Note that the * first parameter is a pointer to a pointer variable; lbm_src_topic_alloc * writes the pointer to the topic object into "topic". Also, by passing * NULL to the source topic attribute, the default option values are used. * The string "Greeting" is the topic string. */ lbm_failed = lbm_src_topic_alloc(&topic, ctx, "Greeting", NULL); if (lbm_failed) {printf("line %s: %s\n", __LINE__, lbm_errmsg()); exit(1);} /*callout: src * Create the source object. A source object is used to send messages. * It must be bound to a topic. Note that the first parameter is a pointer * to a pointer variable; lbm_src_create writes the pointer to the source * object to into "src". Use of the third and fourth parameters is * optional but recommended in a production program - some source events * can be important to the application. The last parameter is an optional * event queue (not used in this example). */ lbm_failed = lbm_src_create(&src, ctx, topic, NULL, NULL, NULL); if (lbm_failed) {printf("line %s: %s\n", __LINE__, lbm_errmsg()); exit(1);} /*callout: sleep1 * Need to wait for receivers to find us. See LBM application note: * doc/AppNotes/delay-before-sending.html * for details. */ SLEEP(3); /*callout: send * Send a message to the "Greeting" topic. The flags make sure the * call to lbm_src_send doesn't return until the message is sent. */ lbm_failed = lbm_src_send(src, "Hello!", 6, LBM_MSG_FLUSH | LBM_SRC_BLOCK); if (lbm_failed) {printf("line %s: %s\n", __LINE__, lbm_errmsg()); exit(1);} /*callout: sleep2 * Even though the message is sent, some transports may need a bit of * time to request re-transmission. If the above lbm_src_send call * didn't include the flags, some time might also be needed to empty * the batching buffer. */ SLEEP(2); /* Finished all sending to this topic, delete the source object. */ lbm_src_delete(src); /* Do not need to delete the topic object - LBM keeps track of topic * objects and deletes them as-needed. */ /* Finished with all LBM functions, delete the context object. */ lbm_context_delete(ctx); #if defined(_MSC_VER) WSACleanup(); #endif } /* main */