Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * qa_interface_msgq.cpp - QA for interface message queue 00004 * 00005 * Created: Tue Oct 24 14:34:40 2006 00006 * Copyright 2006 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU Library General Public License for more details. 00019 * 00020 * Read the full text in the LICENSE.GPL file in the doc directory. 00021 */ 00022 00023 /// @cond QA 00024 00025 #include <interface/message_queue.h> 00026 #include <iostream> 00027 00028 #include <interfaces/test.h> 00029 #include <typeinfo> 00030 00031 using namespace std; 00032 using namespace fawkes; 00033 00034 void 00035 printMessages(MessageQueue *mq) 00036 { 00037 cout << "Iterating through messages:" << endl; 00038 MessageQueue::MessageIterator i; 00039 mq->lock(); 00040 for ( i = mq->begin(); i != mq->end(); ++i) { 00041 if ( i.is<TestInterface::SetTestIntMessage>() ) { 00042 cout << "Message " << i.id() << " int: " 00043 << i.get<TestInterface::SetTestIntMessage>()->test_int() 00044 << " type: " << typeid((*i)).name() 00045 << endl; 00046 } else if ( i.is<Message>() ) { 00047 cout << "It's just a message" << endl; 00048 } else { 00049 cout << "Message " << i.id() << " is not of correct type, it is " << typeid((*i)).name() << endl; 00050 } 00051 } 00052 mq->unlock(); 00053 } 00054 00055 int 00056 main(int argc, char **argv) 00057 { 00058 00059 unsigned int id; 00060 MessageQueue *mq = new MessageQueue(); 00061 00062 cout << "Message queue initialized, now appending three test messages" << endl; 00063 00064 TestInterface::SetTestIntMessage *m1 = new TestInterface::SetTestIntMessage(1); 00065 TestInterface::SetTestIntMessage *m2 = new TestInterface::SetTestIntMessage(2); 00066 TestInterface::SetTestIntMessage *m3 = new TestInterface::SetTestIntMessage(3); 00067 TestInterface::SetTestStringMessage *m4 = new TestInterface::SetTestStringMessage(); 00068 id = mq->append(m1); 00069 cout << "m1 added with id " << id << endl; 00070 id = mq->append(m1); 00071 cout << "m1 added with id " << id << endl; 00072 id = mq->append(m2); 00073 cout << "m2 added with id " << id << endl; 00074 id = mq->append(m3); 00075 cout << "m3 added with id " << id << endl; 00076 id = mq->append(m4); 00077 cout << "m4 (of different type!) added with id " << id << endl; 00078 00079 cout << "Size is now " << mq->size() << endl; 00080 00081 cout << "Unreferencing messages" << endl; 00082 m1->unref(); 00083 m2->unref(); 00084 m3->unref(); 00085 m4->unref(); 00086 00087 cout << "Appending m1 again" << endl; 00088 id = mq->append(m1); 00089 cout << "m1 added with id " << id << endl; 00090 cout << "Size is now " << mq->size() << endl; 00091 cout << "m1 refcount is now " << m1->refcount() << endl; 00092 00093 printMessages(mq); 00094 00095 cout << "Now removing message m1 once" << endl; 00096 mq->remove(m1); 00097 printMessages(mq); 00098 00099 00100 cout << "m1 has refcount " << m1->refcount() << endl; 00101 00102 cout << "Now removing message m2" << endl; 00103 mq->remove(m2); 00104 printMessages(mq); 00105 00106 cout << "Now removing message m4" << endl; 00107 mq->remove(m4); 00108 printMessages(mq); 00109 00110 cout << "Size is now " << mq->size() << endl; 00111 00112 printMessages(mq); 00113 00114 delete mq; 00115 // messages will be erased when enqueued in mq! 00116 00117 } 00118 00119 /// @endcond