#include #pragma hdrstop #if !defined(SPOOLER_HPP) # include #endif #if !defined(FILESTRM_HPP) # include #endif //############################################################################# //############################## SpoolFile ################################ //############################################################################# //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // SpoolFile::SpoolFile( void *stream_start, size_t stream_size, size_t initial_offset ): MemoryStream(stream_start, stream_size, initial_offset) { spoolState = Empty; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // SpoolFile::SpoolFile(SpoolFile& spool): MemoryStream( spool.streamStart, spool.streamSize, spool.GetBytesUsed() ) { spoolState = spool.spoolState; Verify(spoolState == Playing); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // SpoolFile::~SpoolFile() { } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // void SpoolFile::SaveAs(const char *file_name) { FileStream output(file_name, True); size_t length = GetBytesUsed(); output << length; Rewind(); output.WriteBytes(GetPointer(), length); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // void SpoolFile::Read(const char *file_name) { FileStream input(file_name); if (!input.IsFileOpened()) { spoolState = Empty; return; } // //------------------------------- // Figure out how big the file is //------------------------------- // size_t spool_size = 0; input >> spool_size; if (spool_size > GetBytesRemaining() || !spool_size) { spoolState = Empty; } // //----------- // Read it in //----------- // else { input.ReadBytes(streamStart, spool_size); streamSize = spool_size; Rewind(); spoolState = Stored; } } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // void SpoolFile::SpoolPacket(NetworkPacket *packet) { Check(this); Check_Pointer(packet); // //------------------------------------------------------------------------- // Check to see if we are out of spool space. For now, if it happens, just // die //------------------------------------------------------------------------- // int length = packet->messageData.messageLength + sizeof(NetworkPacketHeader); int remaining = GetBytesRemaining(); if (remaining < length) { DEBUG_STREAM << "Error: Spool file ran out of memory!\n"; Exit(AbortExitCodeID); } // //----------------------------------------------------- // Copy into the memory stream, and advance the pointer //----------------------------------------------------- // Mem_Copy(GetPointer(), packet, length, remaining); AdvancePointer(length); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // NetworkPacket* SpoolFile::NextPacket() { Check(this); // //-------------------------------------------------------------------------- // Check to see if anything is left in the spool. If only a partial message // remains, die from an error //------------------------------------------------------------------------- // if (!GetBytesRemaining()) { return NULL; } NetworkPacket *packet = (NetworkPacket*)GetPointer(); int length = packet->messageData.messageLength + sizeof(NetworkPacketHeader); if (GetBytesRemaining() < length) { DEBUG_STREAM << "Error: Partial packet in spool file!\n"; Exit(AbortExitCodeID); return NULL; } // //----------------------------------------------------- // Copy into the memory stream, and advance the pointer //----------------------------------------------------- // AdvancePointer(length); return (NetworkPacket*)GetPointer(); } //############################################################################# //################# MissionReviewApplicationManager ##################### //############################################################################# //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // MissionReviewApplicationManager::MissionReviewApplicationManager( Scalar frame_rate, int spool_count, size_t spool_size ): ApplicationManager(frame_rate) { // //-------------------------------------------------------- // Allocate the space for the spool files and buffer table //-------------------------------------------------------- // spoolCount = spool_count; spoolSize = spool_size; spoolBuffers = new char* [spoolCount]; Register_Pointer(spoolBuffers); spoolFiles = new SpoolFile* [spoolCount]; Register_Pointer(spoolFiles); // //-------------------------------------------------------- // Allocate each spool buffer, and initial the spool files to none //-------------------------------------------------------- // for (int i=0; ispoolState == SpoolFile::Empty) { spoolFiles[i]->spoolState = SpoolFile::Spooling; return spoolFiles[i]; } } Fail("No spool files available!"); return NULL; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // SpoolFile* MissionReviewApplicationManager::GetStoredSpoolFile() { for (int i=0; ispoolState == SpoolFile::Empty) { spoolFiles[i]->Read("last.spl"); if (spoolFiles[i]->spoolState == SpoolFile::Stored) { spoolFiles[i]->spoolState = SpoolFile::Playing; return spoolFiles[i]; } return NULL; } } return NULL; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // void MissionReviewApplicationManager::StoreSpoolFile(SpoolFile *spool_file) { for (int i=0; ispoolState == SpoolFile::Spooling); spoolFiles[i]->spoolState = SpoolFile::Stored; spoolFiles[i]->SaveAs("last.spl"); spoolFiles[i]->Rewind(); return; } } } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // void MissionReviewApplicationManager::ReleaseSpoolFile(SpoolFile *spool_file) { for (int i=0; ispoolState != SpoolFile::Spooling); spoolFiles[i]->spoolState = SpoolFile::Empty; spoolFiles[i]->Rewind(); return; } } }