Browse Source

Filled in ScannerPool (Completed?)

git-svn-id: https://svn.microneil.com/svn/SNF4CGP/trunk@19 59e8e3e7-56fa-483b-b4b4-fa6ab0af3dfc
master
madscientist 15 years ago
parent
commit
2005147208
2 changed files with 50 additions and 8 deletions
  1. 46
    7
      SNF4CGP/ScannerPool.cpp
  2. 4
    1
      SNF4CGP/ScannerPool.hpp

+ 46
- 7
SNF4CGP/ScannerPool.cpp View File

try { // Destructors don't throw. try { // Destructors don't throw.
if(Engine_) { // Safely destroy the engine. if(Engine_) { // Safely destroy the engine.
Engine_->close(); Engine_->close();
delete Engine_;
Engine_=0; Engine_=0;
} }
} }
//// ScannerPool /////////////////////////////////////////////////////////////// //// ScannerPool ///////////////////////////////////////////////////////////////
ScannerPool::ScannerPool() : // Constructed simply. ScannerPool::ScannerPool() : // Constructed simply.
Rulebase_(0),
ScannerCount(0) {
Rulebase_(new snf_RulebaseHandler()),
ScannerCount(0),
Started(false) {
} }
ScannerPool::~ScannerPool() { // Cleanup on the way out. ScannerPool::~ScannerPool() { // Cleanup on the way out.
try { stop(); }
try {
stop();
delete Rulebase_;
Rulebase_ = 0;
}
catch(...) {} catch(...) {}
} }
void ScannerPool::init(string Configuration) { // Get ready to provide scanners.
RuntimeCheck CheckRulebaseExists("ScannerPool::Rulebase() Check(0 != Rulebase_)");
snf_RulebaseHandler& ScannerPool::Rulebase() { // Safe access to the rulebase.
CheckRulebaseExists(0 != Rulebase_);
return (*Rulebase_);
}
void ScannerPool::init(const string Configuration) { // Get ready to provide scanners.
Rulebase().open(Configuration.c_str(), "", ""); // Light up the rulebase manager.
Scanner* FirstScanner = new Scanner(Rulebase()); // Create our first scanner.
PooledScanners.give(FirstScanner); // Drop it into the pool.
Started = true;
}
void ScannerPool::killScannerFromPool() { // Destroy and count a pooled scanner.
Scanner* ScannerToKill = 0;
ScannerToKill = PooledScanners.take();
delete ScannerToKill;
--ScannerCount;
} }
void ScannerPool::stop() { // Shutdown and clean up. void ScannerPool::stop() { // Shutdown and clean up.
ScopeMutex Busy(AllocationControl);
while(0 < ScannerCount) killScannerFromPool();
Rulebase().close();
Started = false;
} }
Scanner* ScannerPool::makeScanner() { // Create and count a scanner. Scanner* ScannerPool::makeScanner() { // Create and count a scanner.
Scanner* NewScanner = new Scanner(Rulebase());
++ScannerCount;
return NewScanner;
} }
Scanner& ScannerPool::grab() { // Provides a Scanner from the pool.
}
LogicCheck CheckGrabScannersOnlyWhenStarted("ScannerPool::grab() Check(Started)");
RuntimeCheck CheckForValidGrabbedScanner("ScannerPool::grab() Check(0 != GrabbedScanner)");
void ScannerPool::killScannerFromPool() { // Destroy and count a pooled scanner.
Scanner& ScannerPool::grab() { // Provides a Scanner from the pool.
ScopeMutex Busy(AllocationControl);
CheckGrabScannersOnlyWhenStarted(Started);
Scanner* GrabbedScanner = 0;
if(0 < PooledScanners.size()) GrabbedScanner = PooledScanners.take(); // Prefer scanners from the pool.
else GrabbedScanner = makeScanner(); // Make new ones if needed.
CheckForValidGrabbedScanner(0 != GrabbedScanner);
return (*GrabbedScanner);
} }
void ScannerPool::drop(Scanner& S) { // Returns a Scanner to the pool. void ScannerPool::drop(Scanner& S) { // Returns a Scanner to the pool.
PooledScanners.give(&S);
} }
//// ScopeScanner ////////////////////////////////////////////////////////////// //// ScopeScanner //////////////////////////////////////////////////////////////

+ 4
- 1
SNF4CGP/ScannerPool.hpp View File

unsigned int ScannerCount; // Allocates and counts scanners. unsigned int ScannerCount; // Allocates and counts scanners.
ProductionQueue<Scanner*> PooledScanners; // Pool of ready-to-go scanners. ProductionQueue<Scanner*> PooledScanners; // Pool of ready-to-go scanners.
Mutex AllocationControl; // Allocation syncrhonizer.
bool Started; // True when we're ready to operate.
Scanner* makeScanner(); // Make and count a scanner. Scanner* makeScanner(); // Make and count a scanner.
void killScannerFromPool(); // Kill and count a pooled scanner. void killScannerFromPool(); // Kill and count a pooled scanner.
public: public:
ScannerPool(); // Constructed simply. ScannerPool(); // Constructed simply.
~ScannerPool(); // Cleans up when destroyed. ~ScannerPool(); // Cleans up when destroyed.
void init(string Configuration); // Get ready to provide scanners.
void init(const string Configuration); // Get ready to provide scanners.
void stop(); // Shutdown and clean up. void stop(); // Shutdown and clean up.
Scanner& grab(); // Provides a Scanner from the pool. Scanner& grab(); // Provides a Scanner from the pool.
void drop(Scanner& S); // Returns a Scanner to the pool. void drop(Scanner& S); // Returns a Scanner to the pool.

Loading…
Cancel
Save