00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 ************************************************************************/ 00021 00022 00023 // CommunicationRecorder.h: interface for the CommunicationRecorder class. 00024 // 00025 ////////////////////////////////////////////////////////////////////// 00026 00027 #if !defined(COMMUNICATIONRECORDER_H) 00028 #define COMMUNICATIONRECORDER_H 00029 00030 #if _MSC_VER > 1000 00031 #pragma once 00032 #endif // _MSC_VER > 1000 00033 00034 #include "JThread.h" 00035 #include "JFile.h" 00036 #include "Collections.h" 00037 #include "Message.h" 00038 #include "DataSample.h" 00039 #include "InfoItem.h" 00040 00041 namespace cmlabs { 00042 00043 #define COMRECNOINIT 0 00044 #define COMRECWRITER 1 00045 #define COMRECREADER 2 00046 00047 class CommunicationRecorder : public Object, public JThread 00048 { 00049 public: 00050 00051 static InfoItem* getRecordingInfo(const JString& storage); 00052 static InfoItem* getIndexFileInfo(const JString& location, const JString& indexfile); 00053 00054 CommunicationRecorder(const JString& storage, bool overwrite = false); 00055 virtual ~CommunicationRecorder(); 00056 Object* clone() const; 00057 00058 bool unitTest(); 00059 00060 bool initReading(); 00061 bool initWriting(); 00062 void run(); 00063 00064 // Operation 00065 bool setNewStorageLocation(const JString& storage); 00066 bool startRecording(); 00067 bool pauseRecording(); 00068 bool restartRecording(); 00069 00070 bool startPlayback(); 00071 bool pausePlayback(); 00072 bool restartPlayback(); 00073 00074 // General Queries 00075 long getCurrentOffset(); 00076 JTime getStartTime(); 00077 JTime calcOriginalTime(const JTime& time); 00078 JTime calcCurrentTime(const JTime& originalTime); 00079 00080 int getMessageCount(); 00081 int getSampleCount(); 00082 JTime getFirstMessageTime(); 00083 JTime getLastMessageTime(); 00084 JTime getFirstSampleTime(); 00085 JTime getLastSampleTime(); 00086 00087 long getFirstMessageOffset(); 00088 long getLastMessageOffset(); 00089 long getFirstSampleOffset(); 00090 long getLastSampleOffset(); 00091 00092 // Adding Data 00093 bool addNewMessage(Message* msg); 00094 bool addNewSample(DataSample* sample); 00095 00096 // Retrieving Data 00097 Message* retrieveMessage(int n); 00098 DataSample* retrieveSample(int n); 00099 Message* retrieveNextMessage(const JTime& time); 00100 DataSample* retrieveNextSample(const JTime& time); 00101 00102 bool finishWriting(int timeout); 00103 00104 bool isInitialised(); 00105 bool isWriter(); 00106 bool isReader(); 00107 bool isPaused(); 00108 bool wasReset(); 00109 00110 private: 00111 00112 bool saveSegment(int start, int end); 00113 bool loadSegment(int start, int end); 00114 00115 Message* loadMessage(const JString& filename); 00116 DataSample* loadSample(const JString& filename); 00117 00118 bool saveMessage(const JString& filename, Message* msg); 00119 bool saveSample(const JString& filename, DataSample* sample); 00120 00121 bool saveIndexFile(); 00122 bool loadIndexFile(); 00123 00124 bool resetFlag; 00125 bool paused; 00126 bool shouldContinue; 00127 bool overwriteRecording; 00128 JString storage; 00129 JString indexFilename; 00130 int accessType; 00131 int masterOffset; 00132 00133 JTime originalStartTime; 00134 JTime currentStartTime; 00135 00136 ObjectQueue msgQueue; 00137 ObjectQueue sampleQueue; 00138 00139 ObjectTable storedMessageIndex; 00140 ObjectTable storedSampleIndex; 00141 int preloadStartIndex; 00142 int preloadEndIndex; 00143 int lastMsgIndex; 00144 int lastSampleIndex; 00145 }; 00146 00147 } // namespace cmlabs 00148 00149 #endif // !defined(COMMUNICATIONRECORDER_H)