CARMA C++
SystemStateManager.h
1 #ifndef CARMA_CONTROL_SYSTEMSTATEMANAGER_H
2 #define CARMA_CONTROL_SYSTEMSTATEMANAGER_H
3 
4 #include "carma/corba/corba.h"
5 #include "carma/control/SubarrayControl.h"
6 
7 #include "carma/monitor/MonitorSystemSelector.h"
8 #include "carma/monitor/types.h"
9 #include "carma/util/types.h"
10 #include "carma/util/PthreadMutex.h"
11 #include "carma/util/PthreadRWLock.h"
12 
13 #include <deque>
14 #include <map>
15 #include <set>
16 #include <string>
17 #include <vector>
18 
19 namespace carma {
20 
21 namespace monitor {
22  class MonitorContainer;
23 } // namespace monitor
24 
25 namespace control {
26 
27 typedef std::set< ::carma::util::frameType > FrameSet;
28 typedef FrameSet::iterator FrameSetIter;
29 typedef FrameSet::const_iterator FrameSetConstIter;
30 
31 typedef std::map< ::carma::monitor::tagIDType, FrameSet > TagIndex;
32 typedef TagIndex::iterator TagIndexIter;
33 typedef TagIndex::const_iterator TagIndexConstIter;
34 
35 typedef std::set< ::carma::monitor::tagIDType > TagSet;
36 typedef TagSet::iterator TagSetIter;
37 typedef TagSet::const_iterator TagSetConstIter;
38 
39 typedef std::map< ::carma::util::frameType, TagSet > ReverseTagIndex;
40 typedef ReverseTagIndex::iterator ReverseTagIndexIter;
41 typedef ReverseTagIndex::const_iterator ReverseTagIndexConstIter;
42 typedef ReverseTagIndex::const_reverse_iterator ReverseTagIndexConstRevIter;
43 
44 
45 class StateManager {
46 public:
47 
48  explicit StateManager ( const ::carma::monitor::CmsSelector cmsType,
49  const ::std::string & stateDirName,
50  const bool throwOnStateDirErrors );
51 
52  ~StateManager( ); // Will attempt to save state on exit
53 
58  bool update( );
59 
60  // Big performance ouch, don't do this unless absolutely necessary.
61  void buildIndexFromScratch( const std::vector< std::string > & directories,
62  int numThreads );
63 
64  void restoreIndexFromFile( );
65 
66  void removeFrameFromIndex( ::carma::util::frameType frame );
67 
68  FrameSet getStateChangeFrames( carma::monitor::tagIDType tag ) const;
69 
70  bool tagChangedForFrame( carma::monitor::tagIDType tag,
71  carma::util::frameType frame ) const;
72 
73  carma::util::frameType getMostRecentSavedState( ) const;
74 
75  carma::util::frameType getOldestSavedState( ) const;
76 
77  carma::util::frameType getClosestSavedState(
78  carma::util::frameType frame ) const;
79 
80  bool verifyIndexIntegrity( );
81 
82 private:
83 
84  bool saveStateIfChanged( );
85 
86  void restoreStateWhenUninitialized( );
87 
88  ::carma::util::frameType retrieveLastStateSaveFrame( ) const;
89 
90  ::std::string retrieveMostRecentFilename( ) const;
91 
92 
93  TagIndex::value_type parseIndexFileLine( const ::std::string & line ) const;
94 
95  void saveIndexFilesHoldingWriteLock( ) const;
96 
97  void removeFrameFromIndexHoldingWriteLock( ::carma::util::frameType frame );
98 
99  void initializeIndexTablesHoldingWriteLock(::carma::util::frameType frame);
100 
101  void indexDifferences( ::carma::util::frameType stateChangeFrame );
102 
103  void indexDifferencesHoldingWriteLock (
104  ::carma::monitor::MonitorContainer & container, // Most recent container
105  ::std::string filename, // Previous saved state filename
107 
108  static void addDiffSetToTagIndices( ::carma::util::frameType frame,
109  const TagSet & tagIdDiffs,
110  TagIndex & tagIndex,
111  ReverseTagIndex & reverseTagIndex );
112 
113  void cancelPendingSave( bool deleteFile );
114 
115  void finalizeSave( );
116 
117  ::carma::util::frameType lastStateSaveFrame_;
118  ::carma::util::frameType unsettleStartFrame_;
119  ::carma::util::frameType lastStateRestoreFrame_;
120  ::carma::util::frameType noFileFrameCount_;
121  ::carma::monitor::CmsAP inputCms_;
122 
123  bool savePending_;
124  ::carma::util::frameType savePendingFrame_;
125  ::std::string savePendingFilename_;
126 
127  const ::std::string stateDir_;
128  const ::std::string filePrefix_;
129  const ::std::string fileSuffix_;
130  ::carma::control::SubarrayControl_var subarrayControl_;
131 
132  struct IndexDiffRequest {
133  ::carma::util::frameType newFrame;
134  ::std::string newFilename;
135  ::carma::util::frameType oldFrame;
136  ::std::string oldFilename;
137  };
138 
139  struct IndexDiffThreadArgs {
140  StateManager * This;
141  ::std::deque< IndexDiffRequest > indexDiffRequestDeque;
142  TagIndex temporaryTagIndex;
143  ReverseTagIndex temporaryReverseTagIndex;
144  carma::util::PthreadMutex indexDiffMutex; // Big mutex for whole struct
145  };
146 
147  static void indexDiffThread( IndexDiffThreadArgs & args );
148 
149  // Our index maps for keeping track of what changed when.
150  // The reverse index is kept for the purpose of easily removing frames but
151  // can also be used for determining which monitor points changed over
152  // specific periods of time.
153  mutable carma::util::PthreadRWLock indexRWLock_;
154  TagIndex tagIndex_;
155  ReverseTagIndex reverseTagIndex_;
156 
157  const ::std::string indexFilename_;
158  const bool throwOnStateDirError_;
159 
160 }; // class StateManager
161 
162 } // namespace control
163 } // namespace carma
164 #endif
A simple wrapper class that makes use of ::pthread_rwlock_t easier in a C++ world.
Definition: PthreadRWLock.h:46
unsigned int frameType
Half second frames since Jan 1, 2000.
Definition: types.h:29
A generic monitor system container base class.
type definitions for monitor system
A simple wrapper class that makes use of ::pthread_mutex_t easier in a C++ world. ...
Definition: PthreadMutex.h:41
Various type definitions for util classes.