| @@ -21,23 +21,15 @@ · 20 unmodified lines |
| 21 | // | 21 | // |
| 22 | //****************************************************************************************************** | 22 | //****************************************************************************************************** |
| 23 | | 23 | |
| 24 | // ReSharper disable once CppUnusedIncludeDirective | | |
| 25 | #include "../FilterExpressions/FilterExpressions.h" | | |
| 26 | #include <boost/bind.hpp> | | |
| 27 | #include <boost/locale.hpp> | | |
| 28 | | | |
| 29 | #include "DataPublisher.h" | 24 | #include "DataPublisher.h" |
| 30 | #include "Constants.h" | 25 | #include "Constants.h" |
| 31 | #include "../Common/Convert.h" | 26 | #include "../Common/Convert.h" |
| 32 | #include "../Common/EndianConverter.h" | 27 | #include "../Common/EndianConverter.h" |
| 33 | #include "../FilterExpressions/FilterExpressionParser.h" | | |
| 34 | #include "MetadataSchema.h" | 28 | #include "MetadataSchema.h" |
| 35 | #include "ActiveMeasurementsSchema.h" | 29 | #include "ActiveMeasurementsSchema.h" |
| 36 | | 30 | |
| 37 | using namespace std; | 31 | using namespace std; |
| 38 | using namespace pugi; | | |
| 39 | using namespace boost; | 32 | using namespace boost; |
| 40 | using namespace boost::locale::conv; | | |
| 41 | using namespace boost::asio; | 33 | using namespace boost::asio; |
| 42 | using namespace boost::asio::ip; | 34 | using namespace boost::asio::ip; |
| 43 | using namespace GSF; | 35 | using namespace GSF; |
| @@ -46,6 +38,16 @@ using namespace GSF::FilterExpressions; · 2 unmodified lines |
| 46 | using namespace GSF::TimeSeries; | 38 | using namespace GSF::TimeSeries; |
| 47 | using namespace GSF::TimeSeries::Transport; | 39 | using namespace GSF::TimeSeries::Transport; |
| 48 | | 40 | |
| | 41 | inline int32_t GetColumnIndex(const DataTablePtr& table, const string& columnName) |
| | 42 | { |
| | 43 | const DataColumnPtr& column = table->Column(columnName); |
| | 44 | |
| | 45 | if (column == nullptr) |
| | 46 | throw PublisherException("Column name \"" + columnName + "\" was not found in table \"" + table->Name() + "\""); |
| | 47 | |
| | 48 | return column->Index(); |
| | 49 | } |
| | 50 | |
| 49 | // --- ClientConnectedInfo --- | 51 | // --- ClientConnectedInfo --- |
| 50 | | 52 | |
| 51 | struct ClientConnectionInfo | 53 | struct ClientConnectionInfo |
| @@ -67,6 +69,7 @@ ClientConnection::ClientConnection(DataPublisherPtr parent, IOContext& commandCh · 15 unmodified lines |
| 67 | m_commandChannelService(commandChannelService), | 69 | m_commandChannelService(commandChannelService), |
| 68 | m_subscriberID(NewGuid()), | 70 | m_subscriberID(NewGuid()), |
| 69 | m_operationalModes(OperationalModes::NoFlags), | 71 | m_operationalModes(OperationalModes::NoFlags), |
| | 72 | m_compressionModes(CompressionModes::None), |
| 70 | m_encoding(OperationalEncoding::UTF8), | 73 | m_encoding(OperationalEncoding::UTF8), |
| 71 | m_usePayloadCompression(false), | 74 | m_usePayloadCompression(false), |
| 72 | m_useCompactMeasurementFormat(true), | 75 | m_useCompactMeasurementFormat(true), |
| @@ -129,6 +132,16 @@ void ClientConnection::SetOperationalModes(uint32_t value) · 56 unmodified lines |
| 129 | m_encoding = m_operationalModes & OperationalModes::EncodingMask; | 132 | m_encoding = m_operationalModes & OperationalModes::EncodingMask; |
| 130 | } | 133 | } |
| 131 | | 134 | |
| | 135 | uint32_t ClientConnection::GetCompressionModes() const |
| | 136 | { |
| | 137 | return m_compressionModes; |
| | 138 | } |
| | 139 | |
| | 140 | void ClientConnection::SetCompressionModes(uint32_t value) |
| | 141 | { |
| | 142 | m_compressionModes = value; |
| | 143 | } |
| | 144 | |
| 132 | uint32_t ClientConnection::GetEncoding() const | 145 | uint32_t ClientConnection::GetEncoding() const |
| 133 | { | 146 | { |
| 134 | return m_encoding; | 147 | return m_encoding; |
| @@ -196,11 +209,16 @@ void ClientConnection::SetSubscriptionInfo(const string& value) · 61 unmodified lines |
| 196 | m_subscriptionInfo = source + " version " + version + " built on " + buildDate; | 209 | m_subscriptionInfo = source + " version " + version + " built on " + buildDate; |
| 197 | } | 210 | } |
| 198 | | 211 | |
| 199 | SignalIndexCache& ClientConnection::GetSignalIndexCache() | 212 | const SignalIndexCachePtr& ClientConnection::GetSignalIndexCache() const |
| 200 | { | 213 | { |
| 201 | return m_signalIndexCache; | 214 | return m_signalIndexCache; |
| 202 | } | 215 | } |
| 203 | | 216 | |
| | 217 | void ClientConnection::SetSignalIndexCache(SignalIndexCachePtr signalIndexCache) |
| | 218 | { |
| | 219 | m_signalIndexCache = std::move(signalIndexCache); |
| | 220 | } |
| | 221 | |
| 204 | bool ClientConnection::CipherKeysDefined() const | 222 | bool ClientConnection::CipherKeysDefined() const |
| 205 | { | 223 | { |
| 206 | return !m_keys[0].empty(); | 224 | return !m_keys[0].empty(); |
| @@ -496,6 +514,11 @@ DataPublisher::DataPublisher(const TcpEndPoint& endpoint) : · 289 unmodified lines |
| 496 | m_clientConnectedCallback(nullptr), | 514 | m_clientConnectedCallback(nullptr), |
| 497 | m_clientDisconnectedCallback(nullptr) | 515 | m_clientDisconnectedCallback(nullptr) |
| 498 | { | 516 | { |
| | 517 | m_tableIDFields = NewSharedPtr<TableIDFields>(); |
| | 518 | m_tableIDFields->SignalIDFieldName = "SignalID"; |
| | 519 | m_tableIDFields->MeasurementKeyFieldName = "ID"; |
| | 520 | m_tableIDFields->PointTagFieldName = "PointTag"; |
| | 521 | |
| 499 | m_callbackThread = Thread(bind(&DataPublisher::RunCallbackThread, this)); | 522 | m_callbackThread = Thread(bind(&DataPublisher::RunCallbackThread, this)); |
| 500 | m_commandChannelAcceptThread = Thread(bind(&DataPublisher::RunCommandChannelAcceptThread, this)); | 523 | m_commandChannelAcceptThread = Thread(bind(&DataPublisher::RunCommandChannelAcceptThread, this)); |
| 501 | } | 524 | } |
| @@ -602,28 +625,75 @@ void DataPublisher::HandleSubscribe(const ClientConnectionPtr& connection, uint8 · 100 unmodified lines |
| 602 | const StringMap<string> settings = ParseKeyValuePairs(connectionString); | 625 | const StringMap<string> settings = ParseKeyValuePairs(connectionString); |
| 603 | string setting; | 626 | string setting; |
| 604 | | 627 | |
| 605 | // TODO: Explore re-subscription details | | |
| 606 | // if (connection->GetIsSubscribed()) ... | | |
| 607 | | | |
| 608 | connection->SetUsePayloadCompression(usePayloadCompression); | 628 | connection->SetUsePayloadCompression(usePayloadCompression); |
| 609 | connection->SetUseCompactMeasurementFormat(useCompactMeasurementFormat); | 629 | connection->SetUseCompactMeasurementFormat(useCompactMeasurementFormat); |
| 610 | | 630 | |
| 611 | // Apply client filter expression and build signal index cache | 631 | SignalIndexCachePtr signalIndexCache = nullptr; |
| | 632 | |
| | 633 | // Apply subscriber filter expression and build signal index cache |
| 612 | if (TryGetValue(settings, "inputMeasurementKeys", setting)) | 634 | if (TryGetValue(settings, "inputMeasurementKeys", setting)) |
| 613 | { | 635 | { |
| 614 | SignalIndexCache& signalIndexCache = connection->GetSignalIndexCache(); | 636 | signalIndexCache = NewSharedPtr<SignalIndexCache>(); |
| 615 | const DataTablePtr& filterTable = m_activeMetadata->Table("ActiveMeasurements"); | 637 | string exceptionMessage, parsingException; |
| 616 | vector<DataRowPtr> rows = FilterExpressionParser::Select(filterTable, setting); //?? | 638 | FilterExpressionParser parser(setting); |
| 617 | const int32_t signalIDColumn = filterTable->Column("SignalID")->Index(); | 639 | |
| | 640 | parser.SetDataSet(m_activeMetadata); |
| | 641 | parser.SetTableIDFields("ActiveMeasurements", m_tableIDFields); |
| | 642 | parser.RegisterParsingExceptionCallback([&parsingException](FilterExpressionParserPtr, const string& exception) { parsingException = exception; }); |
| | 643 | |
| | 644 | try |
| | 645 | { |
| | 646 | parser.Evaluate(); |
| | 647 | } |
| | 648 | catch (const FilterExpressionParserException& ex) |
| | 649 | { |
| | 650 | exceptionMessage = "FilterExpressionParser exception: " + string(ex.what()); |
| | 651 | } |
| | 652 | catch (const ExpressionTreeException& ex) |
| | 653 | { |
| | 654 | exceptionMessage = "ExpressionTree exception: " + string(ex.what()); |
| | 655 | } |
| | 656 | catch (...) |
| | 657 | { |
| | 658 | exceptionMessage = current_exception_diagnostic_information(true); |
| | 659 | } |
| | 660 | |
| | 661 | if (!exceptionMessage.empty()) |
| | 662 | { |
| | 663 | if (!parsingException.empty()) |
| | 664 | exceptionMessage += "\n" + parsingException; |
| | 665 | |
| | 666 | SendClientResponse(connection, ServerResponse::Failed, ServerCommand::Subscribe, exceptionMessage); |
| | 667 | DispatchErrorMessage(exceptionMessage); |
| | 668 | return; |
| | 669 | } |
| | 670 | |
| | 671 | const DataTablePtr& activeMeasurements = m_activeMetadata->Table("ActiveMeasurements"); |
| | 672 | const vector<DataRowPtr>& rows = parser.FilteredRows(); |
| | 673 | const int32_t idColumn = GetColumnIndex(activeMeasurements, "ID"); |
| | 674 | const int32_t signalIDColumn = GetColumnIndex(activeMeasurements, "SignalID"); |
| 618 | | 675 | |
| 619 | for (size_t i = 0; i < rows.size(); i++) | 676 | for (size_t i = 0; i < rows.size(); i++) |
| 620 | { | 677 | { |
| 621 | const DataRowPtr& row = rows[i]; | 678 | const DataRowPtr& row = rows[i]; |
| 622 | const uint16_t signalIndex = 0; | | |
| 623 | const Guid& signalID = row->ValueAsGuid(signalIDColumn).GetValueOrDefault(); | 679 | const Guid& signalID = row->ValueAsGuid(signalIDColumn).GetValueOrDefault(); |
| 624 | signalIndexCache.AddMeasurementKey(signalIndex, signalID, "", 0); | 680 | const vector<string> parts = Split(row->ValueAsString(idColumn).GetValueOrDefault(), ":"); |
| | 681 | uint16_t signalIndex = 0; |
| | 682 | string source; |
| | 683 | int32_t id = 0; |
| | 684 | |
| | 685 | if (parts.size() == 2) |
| | 686 | { |
| | 687 | source = parts[0]; |
| | 688 | id = stoi(parts[1]); |
| | 689 | } |
| | 690 | else |
| | 691 | { |
| | 692 | source = parts[0]; |
| | 693 | } |
| | 694 | |
| | 695 | signalIndexCache->AddMeasurementKey(signalIndex++, signalID, source, id); |
| 625 | } | 696 | } |
| 626 | | | |
| 627 | } | 697 | } |
| 628 | | 698 | |
| 629 | // Pass subscriber assembly information to connection, if defined | 699 | // Pass subscriber assembly information to connection, if defined |
| @@ -674,7 +744,18 @@ void DataPublisher::HandleSubscribe(const ClientConnectionPtr& connection, uint8 · 44 unmodified lines |
| 674 | */ | 744 | */ |
| 675 | } | 745 | } |
| 676 | | 746 | |
| 677 | const string message = "Client subscribed as " + string(useCompactMeasurementFormat ? "" : "non-") + "compact unsynchronized with " + ToString(connection->GetSignalIndexCache().Count()) + " signals."; | 747 | // Send updated signal index cache to client with validated rights of the selected input measurement keys |
| | 748 | vector<uint8_t> serializedSignalIndexCache; |
| | 749 | SerializeSignalIndexCache(connection, signalIndexCache, serializedSignalIndexCache); |
| | 750 | SendClientResponse(connection, ServerResponse::UpdateSignalIndexCache, ServerCommand::Subscribe, serializedSignalIndexCache); |
| | 751 | connection->SetSignalIndexCache(signalIndexCache); |
| | 752 | |
| | 753 | int32_t signalCount = 0; |
| | 754 | |
| | 755 | if (signalIndexCache != nullptr) |
| | 756 | signalCount = signalIndexCache->Count(); |
| | 757 | |
| | 758 | const string message = "Client subscribed as " + string(useCompactMeasurementFormat ? "" : "non-") + "compact unsynchronized with " + ToString(signalCount) + " signals."; |
| 678 | | 759 | |
| 679 | connection->SetIsSubscribed(true); | 760 | connection->SetIsSubscribed(true); |
| 680 | SendClientResponse(connection, ServerResponse::Succeeded, ServerCommand::Subscribe, message); | 761 | SendClientResponse(connection, ServerResponse::Succeeded, ServerCommand::Subscribe, message); |
| @@ -919,11 +1000,12 @@ void DataPublisher::ClientDisconnectedDispatcher(DataPublisher* source, const st · 238 unmodified lines |
| 919 | delete data; | 1000 | delete data; |
| 920 | } | 1001 | } |
| 921 | | 1002 | |
| 922 | void DataPublisher::SerializeSignalIndexCache(const GSF::Guid& clientID, const SignalIndexCache& signalIndexCache, vector<uint8_t>& buffer) | 1003 | void DataPublisher::SerializeSignalIndexCache(const ClientConnectionPtr& connection, const SignalIndexCachePtr& signalIndexCache, vector<uint8_t>& buffer) |
| 923 | { | 1004 | { |
| | 1005 | // TODO: Serialize signal index cache |
| 924 | } | 1006 | } |
| 925 | | 1007 | |
| 926 | std::string DataPublisher::DecodeClientString(const ClientConnectionPtr& connection, const uint8_t* data, uint32_t offset, uint32_t length) const | 1008 | string DataPublisher::DecodeClientString(const ClientConnectionPtr& connection, const uint8_t* data, uint32_t offset, uint32_t length) const |
| 927 | { | 1009 | { |
| 928 | uint32_t encoding = OperationalEncoding::UTF8; | 1010 | uint32_t encoding = OperationalEncoding::UTF8; |
| 929 | bool swapBytes = EndianConverter::IsLittleEndian(); | 1011 | bool swapBytes = EndianConverter::IsLittleEndian(); |
| @@ -934,7 +1016,7 @@ std::string DataPublisher::DecodeClientString(const ClientConnectionPtr& connect · 4 unmodified lines |
| 934 | switch (encoding) | 1016 | switch (encoding) |
| 935 | { | 1017 | { |
| 936 | case OperationalEncoding::UTF8: | 1018 | case OperationalEncoding::UTF8: |
| 937 | return string(reinterpret_cast<const char_t*>(data + offset), length / sizeof(char_t)); | 1019 | return string(reinterpret_cast<const char*>(data + offset), length / sizeof(char)); |
| 938 | case OperationalEncoding::Unicode: | 1020 | case OperationalEncoding::Unicode: |
| 939 | case OperationalEncoding::ANSI: | 1021 | case OperationalEncoding::ANSI: |
| 940 | swapBytes = !swapBytes; | 1022 | swapBytes = !swapBytes; |
| @@ -958,7 +1040,7 @@ std::string DataPublisher::DecodeClientString(const ClientConnectionPtr& connect · 17 unmodified lines |
| 958 | } | 1040 | } |
| 959 | } | 1041 | } |
| 960 | | 1042 | |
| 961 | std::vector<uint8_t> DataPublisher::EncodeClientString(const ClientConnectionPtr& connection, const std::string& value) const | 1043 | vector<uint8_t> DataPublisher::EncodeClientString(const ClientConnectionPtr& connection, const std::string& value) const |
| 962 | { | 1044 | { |
| 963 | uint32_t encoding = OperationalEncoding::UTF8; | 1045 | uint32_t encoding = OperationalEncoding::UTF8; |
| 964 | bool swapBytes = EndianConverter::IsLittleEndian(); | 1046 | bool swapBytes = EndianConverter::IsLittleEndian(); |
| @@ -971,7 +1053,7 @@ std::vector<uint8_t> DataPublisher::EncodeClientString(const ClientConnectionPtr · 6 unmodified lines |
| 971 | switch (encoding) | 1053 | switch (encoding) |
| 972 | { | 1054 | { |
| 973 | case OperationalEncoding::UTF8: | 1055 | case OperationalEncoding::UTF8: |
| 974 | result.reserve(value.size() * sizeof(char_t)); | 1056 | result.reserve(value.size() * sizeof(char)); |
| 975 | result.assign(value.begin(), value.end()); | 1057 | result.assign(value.begin(), value.end()); |
| 976 | break; | 1058 | break; |
| 977 | case OperationalEncoding::Unicode: | 1059 | case OperationalEncoding::Unicode: |
| @@ -1195,16 +1277,6 @@ bool DataPublisher::SendClientResponse(const ClientConnectionPtr& connection, ui · 217 unmodified lines |
| 1195 | return success; | 1277 | return success; |
| 1196 | } | 1278 | } |
| 1197 | | 1279 | |
| 1198 | inline int32_t GetColumnIndex(const DataTablePtr& table, const string& columnName) | | |
| 1199 | { | | |
| 1200 | const DataColumnPtr& column = table->Column(columnName); | | |
| 1201 | | | |
| 1202 | if (column == nullptr) | | |
| 1203 | throw PublisherException("Column name \"" + columnName + "\" was not found in table \"" + table->Name() + "\""); | | |
| 1204 | | | |
| 1205 | return column->Index(); | | |
| 1206 | } | | |
| 1207 | | | |
| 1208 | void DataPublisher::DefineMetadata(const vector<DeviceMetadataPtr>& deviceMetadata, const vector<MeasurementMetadataPtr>& measurementMetadata, const vector<PhasorMetadataPtr>& phasorMetadata, const int32_t versionNumber) | 1280 | void DataPublisher::DefineMetadata(const vector<DeviceMetadataPtr>& deviceMetadata, const vector<MeasurementMetadataPtr>& measurementMetadata, const vector<PhasorMetadataPtr>& phasorMetadata, const int32_t versionNumber) |
| 1209 | { | 1281 | { |
| 1210 | typedef unordered_map<uint16_t, char> PhasorTypeMap; | 1282 | typedef unordered_map<uint16_t, char> PhasorTypeMap; |
| @@ -1589,6 +1661,7 @@ void DataPublisher::DefineMetadata(const DataSetPtr& metadata) · 378 unmodified lines |
| 1589 | | 1661 | |
| 1590 | void DataPublisher::PublishMeasurements(const vector<Measurement>& measurements) | 1662 | void DataPublisher::PublishMeasurements(const vector<Measurement>& measurements) |
| 1591 | { | 1663 | { |
| | 1664 | // TODO: Publish measurements to subscribed connections - routing? |
| 1592 | } | 1665 | } |
| 1593 | | 1666 | |
| 1594 | void DataPublisher::PublishMeasurements(const vector<MeasurementPtr>& measurements) | 1667 | void DataPublisher::PublishMeasurements(const vector<MeasurementPtr>& measurements) |