00001 /*----------------------------------------------------------------------------- 00002 Name: TestLogin.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Login/logout test for xmlBlaster 00006 Version: $Id: TestLogin.cpp 14955 2006-03-20 12:40:31Z goetzger $ 00007 -----------------------------------------------------------------------------*/ 00008 00024 #include <string> 00025 #include <client/protocol/corba/CorbaConnection.h> 00026 #include <client/LoginQosWrapper.h> 00027 #include <util/StopWatch.h> 00028 #include <util/Global.h> 00029 #include <client/I_Callback.h> 00030 #include <iostream> 00031 00032 using namespace std; 00033 using namespace org::xmlBlaster::util; 00034 using namespace org::xmlBlaster::client::protocol::corba; 00035 00036 namespace org { namespace xmlBlaster { 00037 00038 class TestLogin : public virtual client::I_Callback { 00039 00040 private: 00041 string me() { 00042 return "Tim"; 00043 } 00044 00045 string publishReturnQos, secondOid_; 00046 string oid_; 00047 string senderName_, secondName_; 00048 string senderContent_; 00049 CorbaConnection *senderConnection_, *secondConnection_; 00050 serverIdl::MessageUnit* msgUnit_; // a message to play with 00051 00052 int numReceived_; // error checking 00053 string contentMime_; 00054 string contentMimeExtended_; 00055 util::StopWatch stopWatch_; 00056 Global& global_; 00057 util::I_Log& log_; 00058 00059 public: 00067 TestLogin(Global& global, const string &senderName, 00068 const string &secondName) : stopWatch_(), global_(global), 00069 log_(global.getLog("test")) 00070 { 00071 senderName_ = senderName; 00072 secondName_ = secondName; 00073 publishReturnQos = ""; 00074 secondOid_ = "SecondOid"; 00075 oid_ = "TestLogin"; 00076 numReceived_ = 0; 00077 contentMime_ = "text/plain"; 00078 contentMimeExtended_ = "1.0"; 00079 msgUnit_ = 0; 00080 senderConnection_ = secondConnection_ = 0; 00081 } 00082 00083 ~TestLogin() { 00084 delete senderConnection_; 00085 delete secondConnection_; 00086 delete msgUnit_; 00087 } 00088 00089 00105 string update(const string &/*sessionId*/, 00106 UpdateKey &/*updateKey*/, 00107 const unsigned char * /*content*/, long /*contentSize*/, 00108 UpdateQos &/*updateQos*/) { 00109 if (log_.call()) log_.call(me(), "Receiving update of a message ..."); 00110 numReceived_++; 00111 return "<qos><state id='OK'/></qos>"; 00112 } 00113 00119 void setUp(int args, char *argc[]) { 00120 for (int ii=0; ii<args; ii++) { 00121 if (strcmp(argc[ii], "-?")==0 || strcmp(argc[ii], "-h")==0 || strcmp(argc[ii], "-help")==0) { 00122 usage(); 00123 exit(0); 00124 } 00125 } 00126 try { 00127 if (senderConnection_) delete senderConnection_; 00128 senderConnection_ = new CorbaConnection(global_); // Find orb 00129 string passwd = "secret"; 00130 00131 LoginQosWrapper qosWrapper; 00132 senderConnection_->login(senderName_, passwd, qosWrapper, this); 00133 00134 // Login to xmlBlaster 00135 if (secondConnection_) delete secondConnection_; 00136 secondConnection_ = new CorbaConnection(global_); // Find orb 00137 secondConnection_->login(secondName_, passwd, qosWrapper, this); 00138 00139 // a sample message unit 00140 string xmlKey = "<key oid='" + oid_ + "' contentMime='" + 00141 contentMime_ + "' contentMimeExtended='" + 00142 contentMimeExtended_ + "'>\n" + 00143 " <TestLogin-AGENT> </TestLogin-AGENT> </key>"; 00144 senderContent_ = "Some content"; 00145 if (msgUnit_) delete msgUnit_; 00146 msgUnit_ = new serverIdl::MessageUnit(); 00147 msgUnit_->xmlKey = xmlKey.c_str(); 00148 msgUnit_->content = 00149 serverIdl::ContentType(senderContent_.length(), 00150 senderContent_.length(), 00151 (CORBA::Octet*)senderContent_.c_str()); 00152 } 00153 catch (CORBA::Exception &e) { 00154 log_.error(me(), to_string(e)); 00155 cerr << to_string(e); 00156 usage(); 00157 } 00158 } 00159 00160 00166 void tearDown() { 00167 string xmlKey = "<key oid='" + oid_ + "' queryType='EXACT'>\n</key>"; 00168 string qos = "<qos></qos>"; 00169 vector<string> strArr; 00170 try { 00171 strArr = senderConnection_->erase(xmlKey.c_str(), qos.c_str()); 00172 } 00173 catch(serverIdl::XmlBlasterException &e) { 00174 string msg = me() + "-tearDown()"; 00175 string txt = string("XmlBlasterException in erase(): ") + string(e.errorCodeStr) + string(": ") + string(e.message); 00176 log_.error(msg, txt); 00177 } 00178 if (strArr.size() != 1) { 00179 string txt = "Erased "; 00180 txt += strArr.size() + " messages:"; 00181 log_.error(me(), txt); 00182 } 00183 00184 xmlKey = "<key oid='" + secondOid_ + "' queryType='EXACT'>\n</key>"; 00185 qos = "<qos></qos>"; 00186 try { 00187 strArr = senderConnection_->erase(xmlKey.c_str(), qos.c_str()); 00188 } 00189 catch(serverIdl::XmlBlasterException &e) { 00190 00191 string msg = me() + "-tearDown()"; 00192 string txt = string("XmlBlasterException in erase(): ") + string(e.errorCodeStr) + string(": ") + string(e.message); 00193 log_.error(msg, txt); 00194 } 00195 if (strArr.size() != 1) { 00196 string txt = "Erased "; 00197 txt += "many messages"; // change many with the number!!!! 00198 log_.error(me(), txt); 00199 } 00200 senderConnection_->logout(); 00201 secondConnection_->logout(); 00202 } 00203 00204 00205 public: 00211 void testSubscribeXPath() { 00212 if (log_.trace()) log_.trace(me(),"Subscribing using XPath syntax ..."); 00213 00214 string xmlKey = "<key oid='' queryType='XPATH'>\n"; 00215 xmlKey += " //TestLogin-AGENT </key>"; 00216 string qos = "<qos></qos>"; 00217 numReceived_ = 0; 00218 string subscribeOid = ""; 00219 try { 00220 subscribeOid = 00221 senderConnection_->subscribe(xmlKey.c_str(), qos.c_str()); 00222 string txt = "Success: Subscribe on "; 00223 txt += subscribeOid + " done"; 00224 log_.info(me(), txt); 00225 } 00226 catch(serverIdl::XmlBlasterException &e) { 00227 log_.warn(me() + "-testSubscribeXPath", string("XmlBlasterException: ") + string(e.errorCodeStr) + ": " + string(e.message)); 00228 string txt = string("subscribe - XmlBlasterException: ") + string(e.errorCodeStr) + ": " + string(e.message); 00229 cerr << txt << endl; 00230 assert(0); 00231 } 00232 if (subscribeOid == "") { 00233 cerr << "returned null subscribeOid" << endl; 00234 assert(0); 00235 } 00236 if (subscribeOid.length() < 1) { 00237 cerr << "returned subscribeOid is empty" << endl; 00238 assert(0); 00239 } 00240 } 00241 00242 00249 void testPublish(bool ptp) { 00250 if (log_.trace()) log_.trace(me(), "Publishing a message ..."); 00251 00252 numReceived_ = 0; 00253 string qos = "<qos></qos>"; 00254 if (ptp) 00255 qos = "<qos>\n<destination>\n" + secondName_ + 00256 "\n</destination>\n</qos>"; 00257 try { 00258 msgUnit_->qos = qos.c_str(); 00259 publishReturnQos = senderConnection_->publish(*msgUnit_); 00260 if (publishReturnQos.find(oid_) == string::npos) { 00261 cerr << "oid is different" << endl; 00262 assert(0); 00263 } 00264 00265 } 00266 catch(serverIdl::XmlBlasterException &e) { 00267 log_.warn(me()+"-testPublish", 00268 string("XmlBlasterException: ") + string(e.errorCodeStr) + ": " + string(e.message)); 00269 string msg = string("publish - XmlBlasterException: ") + string(e.errorCodeStr) + ": " + string(e.message); 00270 cerr << msg << endl; 00271 assert(0); 00272 } 00273 00274 if (publishReturnQos == "") { 00275 cerr << "returned publishOid == null" << endl; 00276 assert(0); 00277 } 00278 if (publishReturnQos.length() < 1) { 00279 cerr << "returned publishOid is zero in length"; 00280 assert(0); 00281 } 00282 } 00283 00284 00289 void testLoginLogout(int args=0, char *argc[]=0) { 00290 // test ordinary login 00291 numReceived_ = 0; 00292 testSubscribeXPath(); 00293 testPublish(false); 00294 waitOnUpdate(1000L, 1); // message arrived? 00295 00296 // login again, without logout 00297 setUp(args, argc); 00298 testPublish(true); // sending directly PtP to 'receiver' 00299 waitOnUpdate(1000L, 1); // message arrived? 00300 00301 // login again, without logout 00302 setUp(args, argc); 00303 testPublish(false); 00304 stopWatch_.wait(1000L); 00305 numReceived_ = 0; 00306 testSubscribeXPath(); 00307 waitOnUpdate(1000L, 1); 00308 // test publish from other user 00309 numReceived_ = 0; 00310 try { 00311 // a sample message unit 00312 string xmlKey = "<key oid='" + secondOid_ + "' contentMime='" + 00313 contentMime_ + "' contentMimeExtended='" + contentMimeExtended_ 00314 + "'>\n" + " <TestLogin-AGENT>" + " </TestLogin-AGENT>" 00315 + "</key>"; 00316 string content = "Some content"; 00317 00318 serverIdl::MessageUnit secondMsg; 00319 secondMsg.xmlKey = xmlKey.c_str(); 00320 secondMsg.content = 00321 serverIdl::ContentType(content.length(), 00322 content.length(), 00323 (CORBA::Octet*)content.c_str()); 00324 00325 secondMsg.qos = "<qos></qos>"; 00326 publishReturnQos = /*secondBlaster_*/ 00327 /*second*/ senderConnection_->publish(secondMsg); 00328 } 00329 catch(serverIdl::XmlBlasterException &e) { 00330 log_.warn(me()+"-secondPublish", string("XmlBlasterException: ") + string(e.errorCodeStr) + ": " + string(e.message)); 00331 string msg = string("second - publish - XmlBlasterException: ") + string(e.errorCodeStr) + ": " + string(e.message); 00332 cerr << msg << endl; 00333 assert(0); 00334 } 00335 waitOnUpdate(1000L, 1); // message arrived? 00336 00337 if (publishReturnQos == "") { 00338 cerr << "returned publishOid == null" << endl; 00339 assert(0); 00340 } 00341 if (publishReturnQos.length() == 0) { 00342 cerr << "returned publishOid" << endl; 00343 assert(0); 00344 } 00345 // test logout with following subscribe() 00346 senderConnection_->logout(); 00347 try { 00348 msgUnit_->qos = "<qos></qos>"; 00349 publishReturnQos = /*xmlBlaster_*/ 00350 senderConnection_->publish(*msgUnit_); 00351 cerr << "Didn't expect successful subscribe after logout"; 00352 assert(0); 00353 } 00354 catch(serverIdl::XmlBlasterException &e) { 00355 log_.info(me(), string("Success: ") + string(e.errorCodeStr) + ": " + string(e.message)); 00356 } 00357 00358 stopWatch_.wait(1000L); // wait a second 00359 if (numReceived_ != 0) { 00360 cerr << "Didn't expect an update" << endl; 00361 assert(0); 00362 } 00363 00364 // login again 00365 setUp(args, argc); 00366 00367 } 00368 00369 00377 private: 00378 void waitOnUpdate(long timeout, int numWait) { 00379 long pollingInterval = 50L; // check every 0.05 seconds 00380 if (timeout < 50L) pollingInterval = timeout / 10L; 00381 long sum = 0L; 00382 // check if too few are arriving 00383 while (numReceived_ < numWait) { 00384 stopWatch_.wait(pollingInterval); 00385 sum += pollingInterval; 00386 senderConnection_->orbPerformWork(); 00387 secondConnection_->orbPerformWork(); 00388 if (sum >= timeout) { 00389 cerr << "Timeout of " << timeout << " occured without updatetimeout: " << timeout << " " << numWait << endl; 00390 assert(0); 00391 } 00392 } 00393 00394 // check if too many are arriving 00395 stopWatch_.wait(timeout); 00396 if (numWait != numReceived_) { 00397 cerr << "Wrong number of messages arrived "; 00398 cerr << "expected: " << numWait << " received: "; 00399 cerr << numReceived_ << endl; 00400 assert(0); 00401 } 00402 numReceived_ = 0; 00403 } 00404 void usage() 00405 { 00406 Global& glob = Global::getInstance(); 00407 glob.initialize(); 00408 util::I_Log& log_ = glob.getLog("test"); 00409 log_.plain(me(), "----------------------------------------------------------"); 00410 log_.plain(me(), "Testing C++/CORBA access to xmlBlaster"); 00411 log_.plain(me(), "Usage:"); 00412 CorbaConnection::usage(); 00413 log_.usage(); 00414 log_.plain(me(), "Example:"); 00415 log_.plain(me(), " TestLogin -dispatch/connection/plugin/ior/iorFile /tmp/ior.dat -trace true"); 00416 log_.plain(me(), "----------------------------------------------------------"); 00417 } 00418 }; 00419 00420 }} // namespace 00421 00422 00423 00424 00425 00426 int main(int args, char *argc[]) { 00427 // Init the XML platform 00428 org::xmlBlaster::util::Object_Lifetime_Manager::init(); 00429 Global& glob = Global::getInstance(); 00430 glob.initialize(args, argc); 00431 org::xmlBlaster::TestLogin *testSub = new org::xmlBlaster::TestLogin(glob, "Tim", "Joe"); 00432 testSub->setUp(args, argc); 00433 testSub->testLoginLogout(); 00434 testSub->tearDown(); 00435 delete testSub; 00436 // .exit(TestLogin.ME, "Good bye"); 00437 org::xmlBlaster::util::Object_Lifetime_Manager::fini(); 00438 return 0; 00439 } 00440