arpadboda commented on a change in pull request #573: minificpp-873 Implmented.
URL: https://github.com/apache/nifi-minifi-cpp/pull/573#discussion_r288524559
 
 

 ##########
 File path: extensions/windows-event-log/CollectorInitiatedSubscription.cpp
 ##########
 @@ -0,0 +1,777 @@
+/**
+ * @file CollectorInitiatedSubscription.cpp
+ CollectorInitiatedSubscription class implementation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "CollectorInitiatedSubscription.h"
+#include <vector>
+#include <queue>
+#include <map>
+#include <vector>
+#include <set>
+#include <sstream>
+#include <stdio.h>
+#include <string>
+#include <iostream>
+#include <memory>
+#include <codecvt>
+
+#include "io/DataStream.h"
+#include "core/ProcessContext.h"
+#include "core/ProcessSession.h"
+
+#pragma comment(lib, "wevtapi.lib")
+#pragma comment(lib, "Wecapi.lib")
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace processors {
+
+struct OnScopeExit
+{
+  std::function<void(void)> functor_;
+public:
+  OnScopeExit(const decltype(functor_)& functor) : functor_(functor) {}
+  ~OnScopeExit() { functor_(); }
+};
+
+#define SCOPE_EXIT_CONCAT(x, y) x##y
+#define SCOPE_EXIT_UNIQUE_NAME(name, counter) SCOPE_EXIT_CONCAT(name, counter)
+#define ON_SCOPE_EXIT(f) OnScopeExit SCOPE_EXIT_UNIQUE_NAME(onScopeExit, 
__COUNTER__)([&]{f;})
+
+static std::set<core::Property> s_supportedProperties;
+
+struct SupportedProperty: public core::Property
+{
+  template <typename ...Args>
+  SupportedProperty(const Args& ...args): core::Property(args...) {
+    s_supportedProperties.insert(*this);
+  }
+};
+
+const std::string ProcessorName("CollectorInitiatedSubscription");
+
+//! Supported Properties
+static SupportedProperty s_subscriptionName(
+  core::PropertyBuilder::createProperty("Subscription Name")->
+  isRequired(true)->
+  withDescription("The name of the subscription. The value provided for this 
parameter should be unique within the computer's scope.")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_subscriptionDescription(
+  core::PropertyBuilder::createProperty("Subscription Description")->
+  isRequired(true)->
+  withDescription("A description of the subscription.")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_sourceAddress(
+  core::PropertyBuilder::createProperty("Source Address")->
+  isRequired(true)->
+  withDescription("The IP address or fully qualified domain name (FQDN) of the 
local or remote computer (event source) from which the events are collected.")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_sourceUserName(
+  core::PropertyBuilder::createProperty("Source User Name")->
+  isRequired(true)->
+  withDescription("The user name, which is used by the remote computer (event 
source) to authenticate the user.")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_sourcePassword(
+  core::PropertyBuilder::createProperty("Source Password")->
+  isRequired(true)->
+  withDescription("The password, which is used by the remote computer (event 
source) to authenticate the user.")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_sourceChannels(
+  core::PropertyBuilder::createProperty("Source Channels")->
+  isRequired(true)->
+  withDescription("The Windows Event Log Channels (on domain computer(s)) from 
which events are transferred.")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_maxDeliveryItems(
+  core::PropertyBuilder::createProperty("Max Delivery Items")->
+  isRequired(true)->
+  withDefaultValue<core::DataSizeValue>("1000")->
+  withDescription("Determines the maximum number of items that will forwarded 
from an event source for each request.")->
+  build());
+
+static SupportedProperty s_deliveryMaxLatencyTime(
+  core::PropertyBuilder::createProperty("Delivery MaxLatency Time")->
+  isRequired(true)->
+  withDefaultValue<core::DataSizeValue>("10 min")->
+  withDescription("How long, in milliseconds, the event source should wait 
before sending events.")->
+  build());
+
+static SupportedProperty s_heartbeatInterval(
+  core::PropertyBuilder::createProperty("Heartbeat Interval")->
+  isRequired(true)->
+  withDefaultValue<core::DataSizeValue>("10 min")->
+  withDescription(
+    "Time interval, in milliseconds, which is observed between the sent 
heartbeat messages."
+    " The event collector uses this property to determine the interval between 
queries to the event source.")->
+  build());
+
+static SupportedProperty s_channel(
+  core::PropertyBuilder::createProperty("Channel")->
+  isRequired(true)->
+  withDefaultValue("ForwardedEvents")->
+  withDescription("The Windows Event Log Channel (on local machine) to which 
events are transferred.")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_query(
+  core::PropertyBuilder::createProperty("Query")->
+  isRequired(true)->
+  withDefaultValue("*")->
+  withDescription("XPath Query to filter events. (See 
https://msdn.microsoft.com/en-us/library/windows/desktop/dd996910(v=vs.85).aspx 
for examples.)")->
+  supportsExpressionLanguage(true)->
+  build());
+
+static SupportedProperty s_maxBufferSize(
+  core::PropertyBuilder::createProperty("Max Buffer Size")->
+  isRequired(true)->
+  withDefaultValue<core::DataSizeValue>("1 MB")->
+  withDescription(
+    "The individual Event Log XMLs are rendered to a buffer."
+    " This specifies the maximum size in bytes that the buffer will be allowed 
to grow to. (Limiting the maximum size of an individual Event XML.)")->
+  build());
+
+static SupportedProperty s_inactiveDurationToReconnect(
+  core::PropertyBuilder::createProperty("Inactive Duration To Reconnect")->
+  isRequired(true)->
+  withDefaultValue<core::TimePeriodValue>("10 min")->
+  withDescription(
+    "If no new event logs are processed for the specified time period,"
+    " this processor will try reconnecting to recover from a state where any 
further messages cannot be consumed."
+    " Such situation can happen if Windows Event Log service is restarted, or 
ERROR_EVT_QUERY_RESULT_STALE (15011) is returned."
+    " Setting no duration, e.g. '0 ms' disables auto-reconnection.")->
+  build());
+
+//! Supported Relationships
+static core::Relationship s_success("success", "Relationship for successfully 
consumed events.");
+
+
+CollectorInitiatedSubscription::CollectorInitiatedSubscription(const 
std::string& name, utils::Identifier uuid)
+  : core::Processor(name, uuid), 
logger_(logging::LoggerFactory<CollectorInitiatedSubscription>::getLogger()) {
+  char buff[MAX_COMPUTERNAME_LENGTH + 1];
+  DWORD size = sizeof(buff);
+  if (GetComputerName(buff, &size)) {
+    computerName_ = buff;
+  } else {
+    logWindowsError(__LINE__, "GetComputerName");
+  }
+}
+
+void CollectorInitiatedSubscription::initialize() {
+  //! Set the supported properties
+  setSupportedProperties(s_supportedProperties);
+
+  //! Set the supported relationships
+  setSupportedRelationships({s_success});
+}
+
+void CollectorInitiatedSubscription::onSchedule(const 
std::shared_ptr<core::ProcessContext> &context, const 
std::shared_ptr<core::ProcessSessionFactory> &sessionFactory) {
+  if (subscriptionHandle_) {
+    logger_->log_error("Processor already subscribed to Event Log, expected 
cleanup to unsubscribe.");
+  } else {
+    sessionFactory_ = sessionFactory;
+
+    if (!createSubscription(context))
+      return;
+    if (!checkSubscriptionRuntimeStatus())
+      return;
+
+    subscribe(context);
+  }
+}
+
+void CollectorInitiatedSubscription::onTrigger(const 
std::shared_ptr<core::ProcessContext> &context, const 
std::shared_ptr<core::ProcessSession> &session) {
+  if (!subscriptionHandle_) {
+    if (!subscribe(context)) {
+      context->yield();
+      return;
+    }
+  }
+
+  // Check subscription runtime status.
+  checkSubscriptionRuntimeStatus();
+
+  const auto flowFileCount = processQueue(session);
+
+  const auto now = GetTickCount();
 
 Review comment:
   As far as I remember gettickkount _overflows_ after a while (at about 50 
days). Running on Windows servers that might be exceeded causing issues here. 
   I don't care about being platform-independent here given this code is only 
useful on Win, but std::chrono might be a safer choice for timing. 
   If you are aware of Windows-specific cons of chrono, please don't hesitate 
to share!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to