1 /*-----------------------------------------------------------------------------
  2 Name:      TestCommand.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Demo code for a client using xmlBlaster
  6 Version:   $Id: TestCommand.cpp 12915 2004-11-18 14:55:44Z ruff $
  7 -----------------------------------------------------------------------------*/
  8 #include "TestSuite.h"
  9 #include <iostream>
 10 
 11 using namespace std;
 12 using namespace org::xmlBlaster::util;
 13 using namespace org::xmlBlaster::util::qos;
 14 using namespace org::xmlBlaster::util::thread;
 15 using namespace org::xmlBlaster::client;
 16 using namespace org::xmlBlaster::client::qos;
 17 using namespace org::xmlBlaster::client::key;
 18 using namespace org::xmlBlaster::authentication;
 19 
 20 namespace org { namespace xmlBlaster { namespace test {
 21 
 22 /**
 23  * This client tests the method sendAdministrativeCommand(). 
 24  * This client may be invoked multiple time on the same xmlBlaster server,
 25  * as it cleans up everything after his tests are done.
 26  */
 27 class SpecificCallback : public I_Callback {
 28 private:
 29    int numReceived_;
 30    string name_;
 31    I_Log& log_;
 32 
 33 public:
 34    SpecificCallback(I_Log& log, const string& name) : log_(log) {
 35       name_ = name;
 36       numReceived_ = 0;
 37    }
 38 
 39    int getCount() {
 40       return numReceived_;
 41    }
 42 
 43 
 44    string update(const string &sessionId,
 45                UpdateKey &updateKey,
 46                const unsigned char * /*content*/, long /*contentSize*/,
 47                UpdateQos &updateQos) 
 48    {
 49       log_.info("update", string("Receiving update on callback '") + name_ + "' of message oid=" +
 50                 updateKey.getOid() + " state=" + updateQos.getState() +
 51                 " authentication sessionId=" + sessionId + " ...");
 52       numReceived_++;
 53       return "<qos><state id='OK'/></qos>";
 54    }
 55 
 56 
 57 };
 58 
 59 
 60 class TestCommand: public TestSuite, public virtual I_Callback 
 61 {
 62 private:
 63    int    numReceived_;         //  = 0;         // error checking
 64    string subscribeOid_;
 65    string publishOid_;          // = "dummy";
 66    string senderName_;
 67    string senderContent_;
 68    string receiverName_;        // sender/receiver is here the same client
 69    string contentMime_;         // = "text/xml";
 70    string contentMimeExtended_; //  = "1.0";
 71    ConnectReturnQos returnQos_;
 72    SpecificCallback *cb1_;
 73    SpecificCallback *cb2_;
 74    SpecificCallback *cb3_;
 75 
 76    /** Publish tests */
 77    enum TestType {
 78       TEST_ONEWAY, TEST_PUBLISH, TEST_ARRAY
 79    };
 80 
 81    /**
 82     * Constructs the TestCommand object.
 83     * <p />
 84     * @param testName  The name used in the test suite
 85     * @param loginName The name to login to the xmlBlaster
 86     */
 87  public:
 88    TestCommand(int args, char *argc[], const string &loginName)
 89       : TestSuite(args, argc, "TestCommand"), returnQos_(global_)
 90    {
 91       senderName_          = loginName;
 92       receiverName_        = loginName;
 93       numReceived_         = 0;
 94       publishOid_          = "dummy";
 95       contentMime_         = "text/xml";
 96       contentMimeExtended_ = "1.0";
 97       senderContent_       = "Yeahh, i'm the new content";
 98       cb1_ = new SpecificCallback(log_, "callback1");
 99    }
100 
101    virtual ~TestCommand() 
102    {
103       delete cb1_;
104    }
105 
106    /**
107     * Sets up the fixture. <p />
108     * Connect to xmlBlaster and login
109     */
110    void setUp() 
111    {
112       log_.info(ME, "Trying to connect to xmlBlaster with C++ client lib " + Global::getVersion() + " from " + Global::getBuildTimestamp());
113       TestSuite::setUp();
114       try {
115          string passwd = "secret";
116          SecurityQos secQos(global_, senderName_, passwd);
117          ConnectQos connQos(global_);
118          connQos.getSessionQosRef()->setPubSessionId(3L);
119          returnQos_ = connection_.connect(connQos, this);
120          string name = returnQos_.getSessionQos().getAbsoluteName();
121          string name1 = returnQos_.getSessionQosRef()->getAbsoluteName();
122          assertEquals(log_, ME, name, name1, string("name comparison for reference"));
123 
124          log_.info(ME, string("connection setup: the session name is '") + name + "'");
125          // Login to xmlBlaster
126       }
127       catch (XmlBlasterException &e) {
128          log_.error(ME, string("Login failed: ") + e.toXml());
129          usage();
130          assert(0);
131       }
132    }
133 
134 
135    /**
136     * Tears down the fixture. <p />
137     * cleaning up .... erase() the previous message OID and logout
138     */
139    void tearDown() 
140    {
141       log_.info(ME, "Cleaning up test - erasing message.");
142       /*
143       EraseKey eraseKey(global_);
144       eraseKey.setOid(publishOid_);
145       EraseQos eraseQos(global_);
146 
147       vector<EraseReturnQos> retArr;
148       try {
149          retArr = connection_.erase(eraseKey, eraseQos);
150       }
151       catch(XmlBlasterException &e) {
152          log_.error(ME, string("XmlBlasterException: ") + e.toXml());
153       }
154       if (retArr.size() != 1) {
155          log_.error(ME, "Erased " + lexical_cast<string>(retArr.size()) + " messages");
156       }
157       */
158       connection_.disconnect(DisconnectQos(global_));
159       TestSuite::tearDown();
160    }
161 
162 
163    void testSetCallbackDispatcherActive() 
164    {
165       if (log_.trace()) log_.trace(ME, "setCallbackDispatcherActive() ...");
166       try {
167          connection_.setCallbackDispatcherActive(false);
168          log_.info(ME, string("Success: setCallbackDispatcherActive(false)"));
169 
170          //string ret = org::xmlBlaster::util::waitOnKeyboardHit("Hit a key to activate again ...");
171 
172          connection_.setCallbackDispatcherActive(true);
173          log_.info(ME, string("Success: setCallbackDispatcherActive(true)"));
174 
175          //org::xmlBlaster::util::waitOnKeyboardHit("Hit a key to finish test ...");
176       }
177       catch(XmlBlasterException &e) {
178          log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
179          assert(0);
180       }
181    }
182 
183 
184    void testSendAdministrativeCommand() 
185    {
186       if (log_.trace()) log_.trace(ME, "sendAdministrativeCommand() ...");
187       //string command = "?clientList";
188       {
189          string command = global_.getId()+"/?dispatcherActive";
190          log_.info(ME, string("Trying command '" + command + "'"));
191          try {
192             string ret = connection_.sendAdministrativeCommand(command);
193             log_.info(ME, string("Success: " + command + " returned '" + ret + "'"));
194             assert(true == lexical_cast<bool>(ret));
195          }
196          catch(XmlBlasterException &e) {
197             log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
198             assert(0);
199          }
200       }
201       {
202          string command = global_.getId()+"/?dispatcherActive=false";
203          log_.info(ME, string("Trying command '" + command + "'"));
204          try {
205             string ret = connection_.sendAdministrativeCommand(command);
206             log_.info(ME, string("Success: " + command + " returned '" + ret + "'"));
207             assert(ret == "OK");
208          }
209          catch(XmlBlasterException &e) {
210             log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
211             assert(0);
212          }
213       }
214       {
215          string command = global_.getId()+"/?dispatcherActive";
216          log_.info(ME, string("Trying command '" + command + "'"));
217          try {
218             string ret = connection_.sendAdministrativeCommand(command);
219             log_.info(ME, string("Success: " + command + " returned '" + ret + "'"));
220             assert(false == lexical_cast<bool>(ret));
221          }
222          catch(XmlBlasterException &e) {
223             log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
224             assert(0);
225          }
226       }
227       {
228          string command = "set " + global_.getId()+"/?dispatcherActive=true";
229          log_.info(ME, string("Trying command '" + command + "'"));
230          try {
231             string ret = connection_.sendAdministrativeCommand(command);
232             log_.info(ME, string("Success: " + command + " returned '" + ret + "'"));
233             assert(ret == "OK");
234          }
235          catch(XmlBlasterException &e) {
236             log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
237             assert(0);
238          }
239       }
240       {
241          string command = "get " + global_.getId()+"/?dispatcherActive";
242          log_.info(ME, string("Trying command '" + command + "'"));
243          try {
244             string ret = connection_.sendAdministrativeCommand(command);
245             log_.info(ME, string("Success: " + command + " returned '" + ret + "'"));
246             assert(true == lexical_cast<bool>(ret));
247          }
248          catch(XmlBlasterException &e) {
249             log_.warn(ME, string("XmlBlasterException: ") + e.toXml());
250             assert(0);
251          }
252       }
253    }
254 
255    string update(const string &sessionId,
256                UpdateKey &updateKey,
257                const unsigned char * /*content*/, long /*contentSize*/,
258                UpdateQos &updateQos) 
259    {
260       log_.info(ME, string("Receiving update of message oid=") +
261                 updateKey.getOid() + " state=" + updateQos.getState() +
262                 " authentication sessionId=" + sessionId + " ...");
263       numReceived_ ++;
264       return "<qos><state id='OK'/></qos>";
265    }
266 };
267 
268 }}} // namespace
269 
270 using namespace org::xmlBlaster::test;
271 
272 int main(int args, char *argc[]) 
273 {
274    try {
275       org::xmlBlaster::util::Object_Lifetime_Manager::init();
276       TestCommand testSub(args, argc, "Tim");
277  
278       testSub.setUp();
279       testSub.testSetCallbackDispatcherActive();
280       testSub.testSendAdministrativeCommand();
281       testSub.tearDown();
282 
283       Thread::sleepSecs(1);
284    }
285    catch (XmlBlasterException& ex) {
286       std::cout << ex.toXml() << std::endl;
287    }
288    catch (...) {
289       cout << "unknown exception occured" << endl;
290       XmlBlasterException e(INTERNAL_UNKNOWN, "main", "main thread");
291       cout << e.toXml() << endl;
292    }
293    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
294    return 0;
295 }


syntax highlighted by Code2HTML, v. 0.9.1