demo/c++/ExternOrb.cpp

Go to the documentation of this file.
00001 #ifdef COMPILE_CORBA_PLUGIN
00002 
00003 #include <client/XmlBlasterAccess.h>
00004 #include <util/XmlBlasterException.h>
00005 #include <util/Global.h>
00006 #include <util/Timestamp.h>
00007 #include <client/protocol/corba/CorbaDriverFactory.h>
00008 #include <iostream>
00009 
00027 using namespace std;
00028 using namespace org::xmlBlaster::util;
00029 using namespace org::xmlBlaster::util::qos;
00030 using namespace org::xmlBlaster::util::thread;
00031 using namespace org::xmlBlaster::util::dispatch;
00032 using namespace org::xmlBlaster::client;
00033 using namespace org::xmlBlaster::client::protocol::corba;
00034 using namespace org::xmlBlaster::client::qos;
00035 using namespace org::xmlBlaster::client::key;
00036 
00037 namespace org { namespace xmlBlaster { namespace demo {
00038 
00039 class ExternOrb : public I_Callback,           // for the asynchroneous updates
00040                   public I_ConnectionProblems, // for notification of connection problems when failsafe
00041                   public Thread                // the thread to perform the orb work
00042 {
00043 private:
00044    string              ME;                     // the string identifying this class when logging
00045    Global&             global_;
00046    I_Log&              log_;                                                                                                                                // the reference to the log object for this instance
00047    bool                doRun_;
00048    CORBA::ORB_ptr      orb_;
00049    CorbaDriverFactory& factory_;
00050 public:
00051    ExternOrb(Global& glob, CORBA::ORB_ptr orb)
00052    : Thread(),
00053      ME("ExternOrb"),
00054      global_(glob), 
00055      log_(glob.getLog("demo")),
00056      factory_(CorbaDriverFactory::getFactory(glob, orb))
00057    {                  
00058       doRun_ = true;
00059       orb_   = orb;
00060    } 
00061 
00062 
00063    virtual ~ExternOrb()                                                                                               // the constructor does nothing for the moment
00064    {
00065       doRun_ = false;
00066       this->join();
00067    }
00068 
00069 
00070    void run()
00071    {
00072       log_.info(ME, "the corba loop starts now");
00073       while (doRun_) {
00074          while (orb_->work_pending()) orb_->perform_work();
00075          sleep(20); // sleep 20 milliseconds
00076       }
00077       log_.info(ME, "the corba loop has ended now");
00078    }
00079 
00080 
00081    bool reachedAlive(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/)
00082    {
00083       log_.info(ME, "reconnected");
00084       return true;
00085    }
00086 
00087    void reachedDead(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/)
00088    {
00089       log_.info(ME, "lost connection");
00090    }
00091 
00092    void reachedPolling(StatesEnum /*oldState*/, I_ConnectionsHandler* /*connectionsHandler*/)
00093    {
00094       log_.info(ME, "going to poll modus");
00095    }
00096 
00097    void execute()
00098    {
00099       start(false); // to start the orb worker ...
00100       try {
00101       // CorbaDriver driver = 
00102          XmlBlasterAccess con(global_);
00103          con.initFailsafe(this);
00104 
00105          // Creates a connect qos with the user 'joe' and the password 'secret'
00106          ConnectQos qos(global_, "joe", "secret");
00107          log_.info(ME, string("connecting to xmlBlaster. Connect qos: ") + qos.toXml());
00108 
00109          // connects to xmlBlaster and gives a pointer to this class to tell which update method to invoke
00110          // when callbacks come from the server.
00111          ConnectReturnQos retQos = con.connect(qos, this);  // Login to xmlBlaster, register for updates
00112          log_.info(ME, "successfully connected to xmlBlaster. Return qos: " + retQos.toXml());
00113 
00114          // subscribe key. By invoking setOid you implicitly choose the 'EXACT' mode. If you want to 
00115          // subscribe with XPATH use setQueryString instead.
00116          SubscribeKey subKey(global_);
00117          subKey.setOid("ExternOrb");
00118          SubscribeQos subQos(global_);
00119          log_.info(ME, string("subscribing to xmlBlaster with key: ") + subKey.toXml() + " and qos: " + subQos.toXml());
00120 
00121          SubscribeReturnQos subRetQos = con.subscribe(subKey, subQos);
00122          log_.info(ME, string("successfully subscribed to xmlBlaster. Return qos: ") + subRetQos.toXml());
00123 
00124          // publish a message with the oid 'ExternOrb'
00125          PublishQos publishQos(global_);
00126          PublishKey publishKey(global_);
00127          publishKey.setOid("ExternOrb");
00128          MessageUnit msgUnit(publishKey, string("Hi"), publishQos);
00129          log_.info(ME, string("publishing to xmlBlaster with message: ") + msgUnit.toXml());
00130          PublishReturnQos pubRetQos = con.publish(msgUnit);
00131          log_.info(ME, "successfully published to xmlBlaster. Return qos: " + pubRetQos.toXml());
00132          try {
00133             Thread::sleepSecs(1);
00134          }
00135          catch(XmlBlasterException e) {
00136             cout << e.toXml() << endl;
00137          }
00138 
00139          // now an update should have come. Its time to erase the message (otherwise you would get directly
00140          // an update the next time you connect to the same xmlBlaster server. Specify which messages you
00141          // want to erase. Note that you will get an update with the status of the UpdateQos set to 'ERASED'.
00142          EraseKey eraseKey(global_);
00143          eraseKey.setOid("ExternOrb");
00144          EraseQos eraseQos(global_);
00145          log_.info(ME, string("erasing the published message. Key: ") + eraseKey.toXml() + " qos: " + eraseQos.toXml());
00146          vector<EraseReturnQos> eraseRetQos = con.erase(eraseKey, eraseQos);
00147          for (size_t i=0; i < eraseRetQos.size(); i++ ) {
00148             log_.info(ME, string("successfully erased the message. return qos: ") + eraseRetQos[i].toXml());
00149          }
00150 
00151          log_.info(ME, "going to sleep for one minute");
00152          org::xmlBlaster::util::thread::Thread::sleep(60000);
00153 
00154          DisconnectQos disconnectQos(global_);
00155          con.disconnect(disconnectQos);
00156       }
00157       catch (XmlBlasterException e) {
00158          cout << e.toXml() << endl;
00159       }
00160    }
00161 
00162    string update(const string& /*sessionId*/, 
00163                  UpdateKey& updateKey, 
00164                  const unsigned char* /*content*/, 
00165                  long /*contentSize*/, 
00166                  UpdateQos& updateQos)
00167    {
00168       log_.info(ME, "update: key: " + updateKey.toXml());
00169       log_.info(ME, "update: qos: " + updateQos.toXml());
00170       return "";
00171    }
00172 
00173 };
00174 
00175 }}} // namespace
00183 int main(int args, char ** argv)
00184 {
00185    org::xmlBlaster::util::Object_Lifetime_Manager::init();
00186    // suppose you have already initialized an orb
00187    CORBA::ORB_ptr orb = CORBA::ORB_init(args, argv);
00188 
00189    Global& glob = Global::getInstance();
00190    glob.initialize(args, argv);
00191 
00192    org::xmlBlaster::demo::ExternOrb hello(glob, orb);
00193    hello.execute();
00194    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
00195    return 0;
00196 }
00197 
00198 #else // COMPILE_CORBA_PLUGIN
00199 #include <iostream>
00200 int main(int args, char ** argv)
00201 {
00202    ::std::cout << "ExternOrb: COMPILE_CORBA_PLUGIN is not defined, nothing to do" << ::std::endl;
00203    return 0;
00204 }
00205 #endif // COMPILE_CORBA_PLUGIN
00206