File indexing completed on 2025-01-19 06:54:47
0001 // 0002 // C++ Implementation: cCmdQueues 0003 // 0004 // Description: list of all command queues 0005 // 0006 /* 0007 Copyright 2005-2011 Tomas Mecir <kmuddy@kmuddy.com> 0008 0009 This program is free software; you can redistribute it and/or 0010 modify it under the terms of the GNU General Public License as 0011 published by the Free Software Foundation; either version 2 of 0012 the License, or (at your option) any later version. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #include "ccmdqueues.h" 0024 0025 #include "ccmdqueue.h" 0026 #include "ctextchunk.h" 0027 0028 #include <qtimer.h> 0029 0030 cCmdQueues::cCmdQueues (int sess) 0031 : QObject (), cActionBase ("cmdqueues", sess) 0032 { 0033 waitTimer = new QTimer; 0034 connect (waitTimer, &QTimer::timeout, this, &cCmdQueues::timeout); 0035 0036 // this must react BEFORE the triggers ! Other wise we'll get weird results with queues that 0037 // are invoked from a trigger and want to wait for the same pattern that invoked the trigger 0038 // those would get invoked immediately - not what we want 0039 addEventHandler ("got-line", 10, PT_TEXTCHUNK); 0040 0041 // create a new command queue containing this one command ... 0042 addEventHandler ("command", 50, PT_STRING); 0043 } 0044 0045 cCmdQueues::~cCmdQueues() 0046 { 0047 removeEventHandler ("got-line"); 0048 removeEventHandler ("command"); 0049 0050 delete waitTimer; 0051 } 0052 0053 void cCmdQueues::eventChunkHandler (QString event, int, cTextChunk *chunk) 0054 { 0055 if (event == "got-line") 0056 gotLine (chunk->plainText()); 0057 } 0058 0059 void cCmdQueues::eventStringHandler (QString event, int, QString &par1, const QString &) 0060 { 0061 if (event == "command") 0062 { 0063 cCmdQueue *queue = new cCmdQueue (sess()); 0064 queue->addCommand (par1); 0065 addQueue (queue); 0066 } 0067 } 0068 0069 void cCmdQueues::addQueue (cCmdQueue *queue) 0070 { 0071 // process the queue first 0072 processQueue (queue); 0073 if (queue->finished()) 0074 delete queue; 0075 else 0076 queues.push_back (queue); 0077 } 0078 0079 void cCmdQueues::removeAll () 0080 { 0081 std::list<cCmdQueue *>::iterator it; 0082 for (it = queues.begin(); it != queues.end(); ++it) 0083 delete *it; 0084 queues.clear (); 0085 } 0086 0087 void cCmdQueues::timeout () 0088 { 0089 // TODO: handle the queues waiting for some time here ... 0090 0091 } 0092 0093 void cCmdQueues::gotLine (const QString &/*line*/) 0094 { 0095 // TODO: handle the queues waiting for a line here ... 0096 0097 } 0098 0099 void cCmdQueues::processQueues () 0100 { 0101 std::list<cCmdQueue *>::iterator it; 0102 for (it = queues.begin(); it != queues.end(); ) 0103 { 0104 processQueue (*it); 0105 if ((*it)->finished()) { 0106 // queue finished - delete it 0107 delete *it; 0108 it = queues.erase (it); 0109 } 0110 else 0111 // queue not finished - just continue with the next one 0112 ++it; 0113 } 0114 } 0115 0116 void cCmdQueues::processQueue (cCmdQueue *queue) 0117 { 0118 bool canExec = true; 0119 if (queue->waiting ()) { 0120 // if the queue is waiting for something, check if conditions are met 0121 // TODO: check conditions 0122 } 0123 0124 if (canExec && (!queue->finished ())) { 0125 // block commands - so that all commands from a queue are sent in one packet, if possible 0126 invokeEvent ("command-block", sess()); 0127 0128 // conditions are met - keep executing commands while we can 0129 while (canExec) 0130 { 0131 // execute next command in the queue 0132 queue->executeNext (); 0133 0134 // if the queue waits or has finished, no more execution 0135 if (queue->waiting() || (queue->finished())) 0136 canExec = false; 0137 } 0138 0139 // send all queued commands 0140 invokeEvent ("send-commands", sess()); 0141 } 0142 } 0143 0144 #include "moc_ccmdqueues.cpp"