📄 devicerecoverylogic.cpp
字号:
/* -*- c++ -*- --------------------------------------------------------------- Copyright (C) 2005, SWECO, All Rights Reserved. Implementation of DeviceRecoveryLogic class Author: Zsolt Molnar (Zsolt.Molnar@ieee.org) ---------------------------------------------------------------------------*/#include <algorithm>#include <assert.h>#include <iostream>#include "base.hh"#include "FileReader.hh"#include "AppFactory.hh"#include "INetHandler.hh"#include "ICommInitiator.hh"#include "DeviceRecoveryLogic.hh"namespace { const char rcs_id[] = "$Id$"; }DeviceRecoveryLogic::DeviceRecoveryLogic(IDeviceManager& aDevManager) : _deviceManager(aDevManager), _isLooping(true){ // EMPTY}void DeviceRecoveryLogic::start(){ int scanWait = 0; while (_isLooping) { // Commit the changes to the network handler AppFactory::getNetHandler().commit(); // Sleep a while... sleep(CFG_WAIT_PERIOD); if (scanWait > 0) { --scanWait; continue; } scanWait = CFG_NEXT_SCAN_WAIT; cout << endl << "DeviceRecovery: new turn..." << endl; _tDeviceSet actualDevices(_inquiryForDevices()); _tDeviceSet toReset(_sync()); // Remove the banned devices _tDeviceSet tmpBanDevset; set_difference(actualDevices.begin(), actualDevices.end(), _bannedDevices.begin(), _bannedDevices.end(), inserter(tmpBanDevset, tmpBanDevset.begin())); actualDevices = tmpBanDevset; // Perform reset _tDeviceSet tmpResetDevset; set_difference(_presentLastTime.begin(), _presentLastTime.end(), toReset.begin(), toReset.end(), inserter(tmpResetDevset, tmpResetDevset.begin())); _presentLastTime = tmpResetDevset; AppFactory::getCommInitiator().reset(toReset); // Get the devices left from the last scan _tDeviceSet leftDevices; set_difference(_presentLastTime.begin(), _presentLastTime.end(), actualDevices.begin(), actualDevices.end(), inserter(leftDevices, leftDevices.begin())); // Remove them from the system for (_tDeviceSet::iterator itr = leftDevices.begin(); itr != leftDevices.end(); ++itr) { _deviceManager.removeDevice(*itr); } // Get the devices appeared from the last scan _tDeviceSet newDevices; set_difference(actualDevices.begin(), actualDevices.end(), _presentLastTime.begin(), _presentLastTime.end(), inserter(newDevices, newDevices.begin())); // Attach them to the system for (_tDeviceSet::iterator itr = newDevices.begin(); itr != newDevices.end(); ++itr) { _deviceManager.addDevice(*itr); } // Record the new set of present devices _presentLastTime = actualDevices; }}DeviceRecoveryLogic::_tDeviceSetDeviceRecoveryLogic::_sync(){ _tDeviceSet ret; // handle resets FileReader reset(CFG_RESET_FILENAME); while(!reset.isEnd()) { string line = reset.nextLine(); if (line.empty()) { continue; } Device dev(line); ret.insert(dev); _bannedDevices.erase(dev); cout << "DeviceRecoveryLogic: device reset for " << dev.devID << " is recognized." << endl; } reset.remove(); // handle bans FileReader ban(CFG_BAN_FILENAME); while(!ban.isEnd()) { string line = ban.nextLine(); if (line.empty()) { continue; } Device dev(line); _bannedDevices.insert(dev); cout << "DeviceRecoveryLogic: device ban for " << dev.devID << " is recognized." << endl; } cout << flush; ban.remove(); return ret;}// Implementation only for test purposesDeviceRecoveryLogic::_tDeviceSet DeviceRecoveryLogic::_inquiryForDevices(){ _tDeviceSet ret;#ifdef __TEST__ static int loopCount = 1; Device dev_1("ID_1"); Device dev_2("ID_2"); switch(loopCount) { case 1: // 1 new device appears ret.push_back(dev_1); break; case 2: // no new device assert(_presentLastTime.size() == 1); assert(_presentLastTime[0] == dev_1); ret.push_back(dev_1); break; case 3: // Device goes away break; case 4: // No devices present assert(_presentLastTime.size() == 0); break; case 5: // 2 new devices appear ret.push_back(dev_1); ret.push_back(dev_2); break; case 6: // 2 should be present assert(_presentLastTime.size() == 2); assert(_presentLastTime[0] == dev_1); assert(_presentLastTime[1] == dev_2); ret.push_back(dev_1); ret.push_back(dev_2); break; case 7: // 1 device leaves ret.push_back(dev_1); break; case 8: // 1 should be present assert(_presentLastTime.size() == 1); assert(_presentLastTime[0] == dev_1); break; case 9: // 1 more device appears ret.push_back(dev_1); ret.push_back(dev_2); break; case 10: assert(_presentLastTime.size() == 2); assert(_presentLastTime[0] == dev_1); assert(_presentLastTime[1] == dev_2); // Everybody goes away break; case 11: assert(_presentLastTime.size() == 0); break; default: cout << "DeviceRecoveryLogic test passed" << endl; _isLooping = false; } ++loopCount;#endif // #ifdef __TEST__ return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -