00001 /*----------------------------------------------------------------------------- 00002 Name: TestCommand.cpp 00003 Project: xmlBlaster.org 00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file 00005 Comment: Demo code for a client using xmlBlaster 00006 Version: $Id: TestCommand.cpp 12915 2004-11-18 14:55:44Z ruff $ 00007 -----------------------------------------------------------------------------*/ 00008 #include "TestSuite.h" 00009 #include <iostream> 00010 00011 using namespace std; 00012 using namespace org::xmlBlaster::util; 00013 using namespace org::xmlBlaster::util::qos; 00014 using namespace org::xmlBlaster::util::thread; 00015 using namespace org::xmlBlaster::client; 00016 using namespace org::xmlBlaster::client::qos; 00017 using namespace org::xmlBlaster::client::key; 00018 using namespace org::xmlBlaster::authentication; 00019 00020 namespace org { namespace xmlBlaster { namespace test { 00021 00027 class SpecificCallback : public I_Callback { 00028 private: 00029 int numReceived_; 00030 string name_; 00031 I_Log& log_; 00032 00033 public: 00034 SpecificCallback(I_Log& log, const string& name) : log_(log) { 00035 name_ = name; 00036 numReceived_ = 0; 00037 } 00038 00039 int getCount() { 00040 return numReceived_; 00041 } 00042 00043 00044 string update(const string &sessionId, 00045 UpdateKey &updateKey, 00046 const unsigned char * /*content*/, long /*contentSize*/, 00047 UpdateQos &updateQos) 00048 { 00049 log_.info("update", string("Receiving update on callback '") + name_ + "' of message oid=" + 00050 updateKey.getOid() + " state=" + updateQos.getState() + 00051 " authentication sessionId=" + sessionId + " ..."); 00052 numReceived_++; 00053 return "<qos><state id='OK'/></qos>"; 00054 } 00055 00056 00057 }; 00058 00059 00060 class TestCommand: public TestSuite, public virtual I_Callback 00061 { 00062 private: 00063 int numReceived_; // = 0; // error checking 00064 string subscribeOid_; 00065 string publishOid_; // = "dummy"; 00066 string senderName_; 00067 string senderContent_; 00068 string receiverName_; // sender/receiver is here the same client 00069 string contentMime_; // = "text/xml"; 00070 string contentMimeExtended_; // = "1.0"; 00071 ConnectReturnQos returnQos_; 00072 SpecificCallback *cb1_; 00073 SpecificCallback *cb2_; 00074 SpecificCallback *cb3_; 00075 00077 enum TestType { 00078 TEST_ONEWAY, TEST_PUBLISH, TEST_ARRAY 00079 }; 00080 00087 public: 00088 TestCommand(int args, char *argc[], const string &loginName) 00089 : TestSuite(args, argc, "TestCommand"), returnQos_(global_) 00090 { 00091 senderName_ = loginName; 00092 receiverName_ = loginName; 00093 numReceived_ = 0; 00094 publishOid_ = "dummy"; 00095 contentMime_ = "text/xml"; 00096 contentMimeExtended_ = "1.0"; 00097 senderContent_ = "Yeahh, i'm the new content"; 00098 cb1_ = new SpecificCallback(log_, "callback1"); 00099 } 00100 00101 virtual ~TestCommand() 00102 { 00103 delete cb1_; 00104 } 00105 00110 void setUp() 00111 { 00112 log_.info(ME, "Trying to connect to xmlBlaster with C++ client lib " + Global::getVersion() + " from " + Global::getBuildTimestamp()); 00113 TestSuite::setUp(); 00114 try { 00115 string passwd = "secret"; 00116 SecurityQos secQos(global_, senderName_, passwd); 00117 ConnectQos connQos(global_); 00118 connQos.getSessionQosRef()->setPubSessionId(3L); 00119 returnQos_ = connection_.connect(connQos, this); 00120 string name = returnQos_.getSessionQos().getAbsoluteName(); 00121 string name1 = returnQos_.getSessionQosRef()->getAbsoluteName(); 00122 assertEquals(log_, ME, name, name1, string("name comparison for reference")); 00123 00124 log_.info(ME, string("connection setup: the session name is '") + name + "'"); 00125 // Login to xmlBlaster 00126 } 00127 catch (XmlBlasterException &e) { 00128 log_.error(ME, string("Login failed: ") + e.toXml()); 00129 usage(); 00130 assert(0); 00131 } 00132 } 00133 00134 00139 void tearDown() 00140 { 00141 log_.info(ME, "Cleaning up test - erasing message."); 00142 /* 00143 EraseKey eraseKey(global_); 00144 eraseKey.setOid(publishOid_); 00145 EraseQos eraseQos(global_); 00146 00147 vector<EraseReturnQos> retArr; 00148 try { 00149 retArr = connection_.erase(eraseKey, eraseQos); 00150 } 00151 catch(XmlBlasterException &e) { 00152 log_.error(ME, string("XmlBlasterException: ") + e.toXml()); 00153 } 00154 if (retArr.size() != 1) { 00155 log_.error(ME, "Erased " + lexical_cast<string>(retArr.size()) + " messages"); 00156 } 00157 */ 00158 connection_.disconnect(DisconnectQos(global_)); 00159 TestSuite::tearDown(); 00160 } 00161 00162 00163 void testSetCallbackDispatcherActive() 00164 { 00165 if (log_.trace()) log_.trace(ME, "setCallbackDispatcherActive() ..."); 00166 try { 00167 connection_.setCallbackDispatcherActive(false); 00168 log_.info(ME, string("Success: setCallbackDispatcherActive(false)")); 00169 00170 //string ret = org::xmlBlaster::util::waitOnKeyboardHit("Hit a key to activate again ..."); 00171 00172 connection_.setCallbackDispatcherActive(true); 00173 log_.info(ME, string("Success: setCallbackDispatcherActive(true)")); 00174 00175 //org::xmlBlaster::util::waitOnKeyboardHit("Hit a key to finish test ..."); 00176 } 00177 catch(XmlBlasterException &e) { 00178 log_.warn(ME, string("XmlBlasterException: ") + e.toXml()); 00179 assert(0); 00180 } 00181 } 00182 00183 00184 void testSendAdministrativeCommand() 00185 { 00186 if (log_.trace()) log_.trace(ME, "sendAdministrativeCommand() ..."); 00187 //string command = "?clientList"; 00188 { 00189 string command = global_.getId()+"/?dispatcherActive"; 00190 log_.info(ME, string("Trying command '" + command + "'")); 00191 try { 00192 string ret = connection_.sendAdministrativeCommand(command); 00193 log_.info(ME, string("Success: " + command + " returned '" + ret + "'")); 00194 assert(true == lexical_cast<bool>(ret)); 00195 } 00196 catch(XmlBlasterException &e) { 00197 log_.warn(ME, string("XmlBlasterException: ") + e.toXml()); 00198 assert(0); 00199 } 00200 } 00201 { 00202 string command = global_.getId()+"/?dispatcherActive=false"; 00203 log_.info(ME, string("Trying command '" + command + "'")); 00204 try { 00205 string ret = connection_.sendAdministrativeCommand(command); 00206 log_.info(ME, string("Success: " + command + " returned '" + ret + "'")); 00207 assert(ret == "OK"); 00208 } 00209 catch(XmlBlasterException &e) { 00210 log_.warn(ME, string("XmlBlasterException: ") + e.toXml()); 00211 assert(0); 00212 } 00213 } 00214 { 00215 string command = global_.getId()+"/?dispatcherActive"; 00216 log_.info(ME, string("Trying command '" + command + "'")); 00217 try { 00218 string ret = connection_.sendAdministrativeCommand(command); 00219 log_.info(ME, string("Success: " + command + " returned '" + ret + "'")); 00220 assert(false == lexical_cast<bool>(ret)); 00221 } 00222 catch(XmlBlasterException &e) { 00223 log_.warn(ME, string("XmlBlasterException: ") + e.toXml()); 00224 assert(0); 00225 } 00226 } 00227 { 00228 string command = "set " + global_.getId()+"/?dispatcherActive=true"; 00229 log_.info(ME, string("Trying command '" + command + "'")); 00230 try { 00231 string ret = connection_.sendAdministrativeCommand(command); 00232 log_.info(ME, string("Success: " + command + " returned '" + ret + "'")); 00233 assert(ret == "OK"); 00234 } 00235 catch(XmlBlasterException &e) { 00236 log_.warn(ME, string("XmlBlasterException: ") + e.toXml()); 00237 assert(0); 00238 } 00239 } 00240 { 00241 string command = "get " + global_.getId()+"/?dispatcherActive"; 00242 log_.info(ME, string("Trying command '" + command + "'")); 00243 try { 00244 string ret = connection_.sendAdministrativeCommand(command); 00245 log_.info(ME, string("Success: " + command + " returned '" + ret + "'")); 00246 assert(true == lexical_cast<bool>(ret)); 00247 } 00248 catch(XmlBlasterException &e) { 00249 log_.warn(ME, string("XmlBlasterException: ") + e.toXml()); 00250 assert(0); 00251 } 00252 } 00253 } 00254 00255 string update(const string &sessionId, 00256 UpdateKey &updateKey, 00257 const unsigned char * /*content*/, long /*contentSize*/, 00258 UpdateQos &updateQos) 00259 { 00260 log_.info(ME, string("Receiving update of message oid=") + 00261 updateKey.getOid() + " state=" + updateQos.getState() + 00262 " authentication sessionId=" + sessionId + " ..."); 00263 numReceived_ ++; 00264 return "<qos><state id='OK'/></qos>"; 00265 } 00266 }; 00267 00268 }}} // namespace 00269 00270 using namespace org::xmlBlaster::test; 00271 00272 int main(int args, char *argc[]) 00273 { 00274 try { 00275 org::xmlBlaster::util::Object_Lifetime_Manager::init(); 00276 TestCommand testSub(args, argc, "Tim"); 00277 00278 testSub.setUp(); 00279 testSub.testSetCallbackDispatcherActive(); 00280 testSub.testSendAdministrativeCommand(); 00281 testSub.tearDown(); 00282 00283 Thread::sleepSecs(1); 00284 } 00285 catch (XmlBlasterException& ex) { 00286 std::cout << ex.toXml() << std::endl; 00287 } 00288 catch (...) { 00289 cout << "unknown exception occured" << endl; 00290 XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread"); 00291 cout << e.toXml() << endl; 00292 } 00293 org::xmlBlaster::util::Object_Lifetime_Manager::fini(); 00294 return 0; 00295 } 00296 00297