The TIBCO SmartSockets C++ class library organizes the C language API into an object-oriented C++ class library. If you are already working in C++ and with object-oriented software, in general, you reap the greatest benefits in using the TIBCO SmartSockets C++ class library.
Whether to use the TIBCO SmartSockets C++ class library or the C API is up to you. The benefits of the TIBCO SmartSockets C++ class library are those that software developers have experienced when transitioning software design from purely procedural languages such as C to more object-oriented ones such as C++.
Some of the key advantages of the TIBCO SmartSockets C++ class library are:
By grouping together related methods into a particular C++ class, the C++ class library reduces the name length and the number of arguments of many of the corresponding C API functions, leading to shorter, more readable code.
In C++, the allocation, initialization, and deallocation of objects is often localized to class constructors and destructors. Where possible, the classes included in the C++ class library use this technique to assume responsibility for the memory allocation and destruction of TIBCO SmartSockets system objects on behalf of developers. You are then freed from having to explicitly allocate objects using functions such as TipcMsgCreate, and from having to explicitly deallocate objects using functions such as TipcMsgDestroy.
Several of the C++ classes in the class library are available to you for reuse with inheritance and reimplementation of their virtual member functions.
The TIBCO SmartSockets TipcMsg class, described in the later lessons of this tutorial, describes how C++ insertion and extraction operator overloading is used in the TipcMsg class to unify much of the process of constructing, reading, and writing the messages inside a TIBCO SmartSockets application program.
To illustrate some of the similarities and advantages of using the C++ class library, let’s look at our earlier sending and receiving programs written in C++.
// send.cxx - send an INFO message to lesson1 subject
// $RTHOME/examples/smrtsock/tutorial/lesson1/send.cxx
#include <rtworks/sscpp.h> using namespace SmartSockets; int main(int argc, char **argv) { try { TipcSrv srv("", NULL, NULL, NULL); srv.open(); TipcMt mt(T_MT_INFO); srv.write("/tutorial/lesson1", mt, TRUE, T_IPC_FT_STR, "Hello World!", NULL); srv.flush(); srv.close(); } catch (TipcMtException mte) { Utilities::out("Error in TipcMt: %s\n", mte.what()); } catch (TipcSrvException srve) { Utilities::out("Error in TipcMsg: %s\n", srve.what()); } return 0; }// main
Here is the C++ version of the receiving program:
// receive.cxx - read and print an INFO message
// $RTHOME/examples/smrtsock/tutorial/lesson1/receive.cxx
#include <rtworks/sscpp.h> using namespace SmartSockets; int main(int argc, char **argv) { try { TipcSrv srv("", NULL, NULL, NULL); srv.open(); char *msg_text; TipcMsg msg(T_MT_INFO); msg.setNumFields(0); srv.setSubscribe((const char*)"tutorial/lesson1"); srv.nextEx(msg, T_TIMEOUT_FOREVER); msg.setCurrent(0); msg >> msg_text; Utilities::out("Text from INFO message = %s\n", msg_text); srv.close(); } catch (TipcMsgException msge) { Utilities::out("Error in TipcMsg: %s\n", msge.what()); } catch (TipcSrvException srve) { Utilities::out("Error in TipcMsg: %s\n", srve.what()); } return 0; }// main
C++ programs always have the file extension .cxx
. Although this tutorial shows all the examples in C, there is also a C++ version of every program in the TIBCO SmartSockets installation directories. The C++ version has the same name as the C version except it has a .cxx
extension instead of .c
extension. For Windows, the C++ programs are in the directory %RTHOME%\examples\sscpp
.
For more details on the C++ class library, see the TIBCO SmartSockets C++ User’s Guide.
TIBCO SmartSockets™ Tutorial Software Release 6.8, July 2006 Copyright © TIBCO Software Inc. All rights reserved www.tibco.com |