/* * Paglo Crawler * Copyright (C) 2006-2008 Paglo Labs Inc. All rights reserved. * www.paglo.com * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ //--------------------------------------------------------------------------- #ifndef EventQueueH #define EventQueueH //--------------------------------------------------------------------------- #include #include #include "Condition.h" #include "Event.h" class TEventQueue { /* * Let StopScans() have direct access to the EventQueue vector, so that it can * remove scans that are not currently permitted according to the subnet schedules. */ friend void StopScans(void *Arg); public: TEventQueue(void); ~TEventQueue(void); /* * Insert an event at the back of the queue. The queue is not responsible for freeing * the event and the caller should not access it anymore */ void Put(TPEvent *Evt); /* * Insert an event at the front of the queue. The queue is not responsible for * freeing the event and the caller should not access it anymore. */ void PutFront(TPEvent *Evt); /* * Remove an event from queue and return a pointer to it to the caller. * The caller is responsible for freeing the event object */ TPEvent *Get(void); /* * Get the specified item from the queue. This must only be done while the * queue is locked and the event must not be freed. */ TPEvent *Get(int Index) { return EventQueue[Index]; } /* * Remove the specified element. */ void Remove(int Index) { EventQueue.erase(EventQueue.begin() + Index); } /* * Wrappers for std::queue methods */ int Size(void) { return EventQueue.size(); }; bool IsEmpty(void) { return EventQueue.empty(); }; /* * Allows callers to get/set condition variable associated with queue inserts * so that they can wait on it */ void SetCond(TCondition *); TCondition *GetCond(void){ return QueueCond; }; /* * Lock the entire queue. Useful when iterative through all events in the * queue. */ void Lock() { pthread_mutex_lock(&QueueMutex); } void Unlock() { pthread_mutex_unlock(&QueueMutex); } private: std::vector EventQueue; pthread_mutex_t QueueMutex; TCondition *QueueCond; bool ExternalCond; // was the QueueCond set by something else }; //--------------------------------------------------------------------------- #endif