| 135 | } |
| 136 | } |
| 137 | void C2Agent::configure(const std::shared_ptr<Configure> &configure, bool reconfigure) { |
| 138 | std::string clazz, heartbeat_period, device; |
| 139 | |
| 140 | if (!reconfigure) { |
| 141 | if (!configure->get("nifi.c2.agent.protocol.class", "c2.agent.protocol.class", clazz)) { |
| 142 | clazz = "CoapProtocol"; |
| 143 | } |
| 144 | logger_->log_info("Class is %s", clazz); |
| 145 | |
| 146 | auto protocol = core::ClassLoader::getDefaultClassLoader().instantiateRaw(clazz, clazz); |
| 147 | if (protocol == nullptr) { |
| 148 | logger_->log_warn("Class %s not found", clazz); |
| 149 | protocol = core::ClassLoader::getDefaultClassLoader().instantiateRaw("CoapProtocol", "CoapProtocol"); |
| 150 | if (!protocol) { |
| 151 | const char* errmsg = "Attempted to load CoapProtocol. To enable C2, please specify an active protocol for this agent."; |
| 152 | logger_->log_error(errmsg); |
| 153 | throw minifi::Exception{ minifi::GENERAL_EXCEPTION, errmsg }; |
| 154 | } |
| 155 | |
| 156 | logger_->log_info("Class is CoapProtocol"); |
| 157 | } |
| 158 | |
| 159 | // Since !reconfigure, the call comes from the ctor and protocol_ is null, therefore no delete is necessary |
| 160 | protocol_.exchange(dynamic_cast<C2Protocol *>(protocol)); |
| 161 | |
| 162 | protocol_.load()->initialize(controller_, configuration_); |
| 163 | } else { |
| 164 | protocol_.load()->update(configure); |
| 165 | } |
| 166 | |
| 167 | if (configure->get("nifi.c2.agent.heartbeat.period", "c2.agent.heartbeat.period", heartbeat_period)) { |
| 168 | core::TimeUnit unit; |
| 169 | |
| 170 | try { |
| 171 | int64_t schedulingPeriod = 0; |
| 172 | if (core::Property::StringToTime(heartbeat_period, schedulingPeriod, unit) && core::Property::ConvertTimeUnitToMS(schedulingPeriod, unit, schedulingPeriod)) { |
| 173 | heart_beat_period_ = schedulingPeriod; |
| 174 | logger_->log_debug("Using %u ms as the heartbeat period", heart_beat_period_); |
| 175 | } else { |
| 176 | heart_beat_period_ = std::stoi(heartbeat_period); |
| 177 | } |
| 178 | } catch (const std::invalid_argument &) { |
| 179 | heart_beat_period_ = 3000; |
| 180 | } |
| 181 | } else { |
| 182 | if (!reconfigure) |
| 183 | heart_beat_period_ = 3000; |
| 184 | } |
| 185 | |
| 186 | std::string update_settings; |
| 187 | if (configure->get("nifi.c2.agent.update.allow", "c2.agent.update.allow", update_settings) && utils::StringUtils::StringToBool(update_settings, allow_updates_)) { |
| 188 | // allow the agent to be updated. we then need to get an update command to execute after |
| 189 | } |
| 190 | |
| 191 | if (allow_updates_) { |
| 192 | if (!configure->get("nifi.c2.agent.update.command", "c2.agent.update.command", update_command_)) { |
| 193 | std::string cwd = utils::Environment::getCurrentWorkingDirectory(); |
| 194 | if (cwd.empty()) { |
no test coverage detected