REQUIREMENT client.cpp |
Type | NEW |
Priority | HIGH |
Status | CLOSED |
Topic | XmlBlaster provides a complete client library written in c++. |
Des cription |
For c++ client applications you can use the class XmlBlasterAccess which provides access to xmlBlaster in an easy way. All invocations to xmlBlaster are done by passing objects to the dispatcher.
Below you find an example for a typical C++ client connecting to xmlBlaster. In the configuration section there is a list of operating systems and CORBA libraries which are tested with xmlBlaster. Other combinations should work out of the box or with minor changes. Optionally you can use our native SOCKET protocol instead of CORBA to access the server. |
Example CPP |
/*------------------------------------------------------------------------------ Name: xmlBlaster/demo/c++/HelloWorld2.cpp Project: xmlBlaster.org Comment: C++ client example Author: Michele Laghi ------------------------------------------------------------------------------*/ #include <client/XmlBlasterAccess.h> #include <util/XmlBlasterException.h> #include <util/Global.h> #include <util/Log.h> #include <util/PlatformUtils.hpp> #include <util/Timestamp.h> using namespace std; using namespace org::xmlBlaster::util; using namespace org::xmlBlaster::client; using namespace org::xmlBlaster::client::qos; using namespace org::xmlBlaster::client::key; using namespace org::xmlBlaster; /** * This client connects to xmlBlaster and subscribes to a message. * <p> * We then publish the message and receive it asynchronous in the update() method. * </p> * <p> * Note that the CORBA layer is transparently hidden, * and all code conforms to STD C++ (with STL). * </p> * <pre> * Invoke: HelloWorld2 * </pre> * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" * target="others">xmlBlaster interface</a> */ class HelloWorld2 : public I_Callback, // for the asynchroneous updates public I_ConnectionProblems // notification of connection problems when failsafe { private: string ME; // the string identifying this class when logging Global& global_; Log& log_; // the reference to the log object for this instance public: HelloWorld2(Global& glob) : ME("HelloWorld2"), global_(glob), log_(glob.getLog("demo")) // all logs written in this class are written to the { // log channel called 'demo'. To see the traces of this } // channel invoke -trace[demo] true on the command line, // then it will only switch on the traces for the demo channel virtual ~HelloWorld2() // the constructor does nothing for the moment { } bool reachedAlive(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/) { log_.info(ME, "reconnected"); return true; } void reachedDead(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/) { log_.info(ME, "lost connection"); } void reachedPolling(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/) { log_.info(ME, "going to poll modus"); } void execute() { try { XmlBlasterAccess con(global_); con.initFailsafe(this); // Creates a connect qos with the user 'joe' and the password 'secret' ConnectQos qos(global_, "joe", "secret"); log_.info(ME, string("connecting to xmlBlaster. Connect qos: ") + qos.toXml()); // connects to xmlBlaster and gives a pointer to this class to tell // which update method to invoke when callbacks come from the server. ConnectReturnQos retQos = con.connect(qos, this); // Login and register for updates log_.info(ME, "successfully connected to xmlBlaster. Return qos: " + retQos.toXml()); // subscribe key. By invoking setOid you implicitly choose the 'EXACT' mode. // If you want to subscribe with XPATH use setQueryString instead. SubscribeKey subKey(global_); subKey.setOid("HelloWorld2"); SubscribeQos subQos(global_); log_.info(ME, string("subscribing to xmlBlaster with key: ") + subKey.toXml() + " and qos: " + subQos.toXml()); SubscribeReturnQos subRetQos = con.subscribe(subKey, subQos); log_.info(ME, string("successfully subscribed to xmlBlaster. Return qos: ") + subRetQos.toXml()); // publish a message with the oid 'HelloWorld2' PublishQos publishQos(global_); PublishKey publishKey(global_); publishKey.setOid("HelloWorld2"); MessageUnit msgUnit(publishKey, string("Hi"), publishQos); log_.info(ME, string("publishing to xmlBlaster with message: ") + msgUnit.toXml()); PublishReturnQos pubRetQos = con.publish(msgUnit); log_.info(ME, "successfully published to xmlBlaster. Return qos: " + pubRetQos.toXml()); try { Thread::sleepSecs(1); } catch(XmlBlasterException e) { cout << e.toXml() << endl; } // now an update should have come. Its time to erase the message, // otherwise you would get directly an update the next time you connect // to the same xmlBlaster server. // Specify which messages you want to erase. Note that you will get an // update with the status of the UpdateQos set to 'ERASED'. EraseKey eraseKey(global_); eraseKey.setOid("HelloWorld2"); EraseQos eraseQos(global_); log_.info(ME, string("erasing the published message. Key: ") + eraseKey.toXml() + " qos: " + eraseQos.toXml()); vector<EraseReturnQos> eraseRetQos = con.erase(eraseKey, eraseQos); for (size_t i=0; i < eraseRetQos.size(); i++ ) { log_.info(ME, string("successfully erased the message. return qos: ") + eraseRetQos[i].toXml()); } log_.info(ME, "going to sleep for 2 sec and disconnect"); org::xmlBlaster::util::thread::Thread::sleep(2000); DisconnectQos disconnectQos(global_); con.disconnect(disconnectQos); } catch (XmlBlasterException e) { cout << e.toXml() << endl; } } /** * Callbacks from xmlBlaster arrive here. */ string update(const string& /*sessionId*/, UpdateKey& updateKey, void* /*content*/, long /*contentSize*/, UpdateQos& updateQos) { log_.info(ME, "update: key: " + updateKey.toXml()); log_.info(ME, "update: qos: " + updateQos.toXml()); return ""; } }; /** * Try * <pre> * HelloWorld2 -help * </pre> * for usage help */ int main(int args, char ** argv) { XMLPlatformUtils::Initialize(); Global& glob = Global::getInstance(); glob.initialize(args, argv); // XmlBlasterAccess::usage(); // glob.getLog().info("HelloWorld2", "Example: HelloWorld2\n"); HelloWorld2 hello(glob); hello.execute(); return 0; } |
Configure |
These configurations are tested: For compilation options please use build cpp compiles client lib and testsuite on UNIX or Windows build usage show other compile options Compilation with VC 6 will fail (broken namespace support leads to internal compiler error). Besides the orbs mentioned in the table the source code is already prepared for other corba vendors such as
but the current status of the library has not been tested against these orbs. See CompatibleCorba.h for a current list. Please let us know if you successfully compiled and ran the testsuite with other combinations. Particularly interesting it would be the INTEL compiler on Linux and Windows and Solaris with the GCC Compiler. How to check to the version of the libraryTo check which version your shared library is (UNIX only) try one of the following: cd xmlBlaster/lib # Searching manually the lib: strings libxmlBlasterClient.so | grep Global.cpp # Or using the what command: what libxmlBlasterClient.so # Or (ident is part of the RCS package): ident libxmlBlasterClient.so # The result is something like: Global.cpp,v 1.29 2003/03/02 19:53:42 ruff Exp # You can now lookup Global.cpp 1.29 with cd xmlBlaster/src/c++/util svn log Global.cpp How to create the Doxygen documentationThe C++ code is commented to suit the Doxygen tool from http://www.doxygen.org and the generated C++ API documentation is available online. If you want to generate the documentation yourself you need to install Doxygen and GraphViz as described in their manuals. Than create the documentation like that: cd $XMLBLASTER_HOME/src/c++/doc doxygen Doxyfile Now you can point your browser on $XMLBLASTER_HOME/doc/doxygen/cpp/html/index.html to view the documentation. After setting export MANPATH=:$XMLBLASTER_HOME/doc/doxygen/c/man:$XMLBLASTER_HOME/doc/doxygen/cpp/man you are ready to read the manual pages with for example 'man XmlBlasterAccess' or 'man 3 Global'.
NOTE: Configuration parameters are specified on command line (-someValue 17) or in the
xmlBlaster.properties file (someValue=17). See requirement "util.property" for details. |
See API | HelloWorld2 |
See API | XmlBlasterAccess |
See | http://www.xmlBlaster.org/xmlBlaster/doc/doxygen/cpp/html/namespaces.html |
See REQ | C++ compilation hints |
See REQ | client.c.socket |
See REQ | protocol.socket |
See REQ | client.cpp.mico |
See REQ | client.cpp.tao |
See REQ | client.cpp.orbix |
See REQ | client.cpp.failsafe |
See REQ | client.cpp.protocol |
This page is generated from the requirement XML file xmlBlaster/doc/requirements/client.cpp.xml