/***************************************************************************************** * * * OpenSpace * * * * Copyright (c) 2014-2017 * * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * * software and associated documentation files (the "Software"), to deal in the Software * * without restriction, including without limitation the rights to use, copy, modify, * * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * * permit persons to whom the Software is furnished to do so, subject to the following * * conditions: * * * * The above copyright notice and this permission notice shall be included in all copies * * or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ #include #include #include #include #include namespace { const std::string _loggerCat = "SequenceParser"; const char* keyTranslation = "DataInputTranslation"; const char* PlaybookIdentifierName = "Playbook"; } namespace openspace { std::map SequenceParser::getSubsetMap(){ return _subsetMap; } std::vector> SequenceParser::getInstrumentTimes(){ return _instrumentTimes; } std::vector> SequenceParser::getTargetTimes(){ return _targetTimes; } std::vector SequenceParser::getCaptureProgression(){ return _captureProgression; }; std::map>& SequenceParser::getTranslation() { return _fileTranslation; } template void writeToBuffer(std::vector& buffer, size_t& currentWriteLocation, T value) { if ((currentWriteLocation + sizeof(T)) > buffer.size()) buffer.resize(2 * buffer.size()); std::memmove(buffer.data() + currentWriteLocation, reinterpret_cast(&value), sizeof(T)); currentWriteLocation += sizeof(T); } template <> void writeToBuffer(std::vector& buffer, size_t& currentWriteLocation, std::string value) { if ((currentWriteLocation + sizeof(uint8_t) + value.size()) > buffer.size()) buffer.resize(2 * buffer.size()); uint8_t length = static_cast(value.size()); std::memcpy(buffer.data() + currentWriteLocation, &length, sizeof(uint8_t)); currentWriteLocation += sizeof(uint8_t); std::memmove(buffer.data() + currentWriteLocation, value.data(), length); currentWriteLocation += length; } void SequenceParser::sendPlaybookInformation(const std::string& name) { std::string fullName = std::string(PlaybookIdentifierName) + "_" + name; _messageIdentifier = OsEng.networkEngine().identifier(fullName); std::vector buffer(1024); size_t currentWriteLocation = 0; // Protocol: // 4 bytes: Total number of bytes sent // 1 byte : Number of Targets (i) // i times: 1 byte (id), 1 byte (length j of name), j bytes (name) // 1 byte : Number of Instruments (i) // i times: 1 byte (id), 1 byte (length j of name), j bytes (name) // 4 byte: Number (n) of images // n times: 8 byte (beginning time), 8 byte (ending time), 1 byte (target id), 2 byte (instrument id) std::map targetMap; uint8_t currentTargetId = 0; for (auto target : _subsetMap) { if (targetMap.find(target.first) == targetMap.end()) targetMap[target.first] = currentTargetId++; } std::map instrumentMap; uint16_t currentInstrumentId = 1; for (auto target : _subsetMap) { for (auto image : target.second._subset) { for (auto instrument : image.activeInstruments) { if (instrumentMap.find(instrument) == instrumentMap.end()) { instrumentMap[instrument] = currentInstrumentId; currentInstrumentId = currentInstrumentId << 1; } } } } writeToBuffer(buffer, currentWriteLocation, uint8_t(targetMap.size())); for (const std::pair& p : targetMap) { writeToBuffer(buffer, currentWriteLocation, p.second); writeToBuffer(buffer, currentWriteLocation, p.first); } writeToBuffer(buffer, currentWriteLocation, uint8_t(instrumentMap.size())); for (const std::pair& p : instrumentMap) { writeToBuffer(buffer, currentWriteLocation, p.second); writeToBuffer(buffer, currentWriteLocation, p.first); } uint32_t allImages = 0; for (auto target : _subsetMap) allImages += static_cast(target.second._subset.size()); writeToBuffer(buffer, currentWriteLocation, allImages); for (auto target : _subsetMap){ for (auto image : target.second._subset){ writeToBuffer(buffer, currentWriteLocation, image.timeRange.start); writeToBuffer(buffer, currentWriteLocation, image.timeRange.end); std::string timeBegin = SpiceManager::ref().dateFromEphemerisTime(image.timeRange.start); std::string timeEnd = SpiceManager::ref().dateFromEphemerisTime(image.timeRange.end); writeToBuffer(buffer, currentWriteLocation, timeBegin); writeToBuffer(buffer, currentWriteLocation, timeEnd); uint8_t targetId = targetMap[target.first]; writeToBuffer(buffer, currentWriteLocation, targetId); uint16_t totalInstrumentId = 0; if (image.activeInstruments.empty()) { LERROR("Image had no active instruments"); } for (auto instrument : image.activeInstruments) { uint16_t thisInstrumentId = instrumentMap[instrument]; totalInstrumentId |= thisInstrumentId; } writeToBuffer(buffer, currentWriteLocation, totalInstrumentId); } } union { uint32_t value; std::array data; } sizeBuffer; sizeBuffer.value = static_cast(currentWriteLocation); buffer.insert(buffer.begin(), sizeBuffer.data.begin(), sizeBuffer.data.end()); currentWriteLocation += sizeof(uint32_t); buffer.resize(currentWriteLocation); //OsEng.networkEngine()->publishMessage(PlaybookIdentifier, buffer); OsEng.networkEngine().setInitialConnectionMessage(_messageIdentifier, buffer); } }