|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// File: WebNotify.cc |
| 3 | +// Author: Andreas-Joachim Peters - CERN |
| 4 | +//------------------------------------------------------------------------------ |
| 5 | + |
| 6 | +/************************************************************************ |
| 7 | + * EOS - the CERN Disk Storage System * |
| 8 | + * Copyright (C) 2025 CERN/Switzerland * |
| 9 | + * * |
| 10 | + * This program is free software: you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation, either version 3 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + * This program is distributed in the hope that it will be useful, * |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 18 | + * GNU General Public License for more details. * |
| 19 | + * * |
| 20 | + * You should have received a copy of the GNU General Public License * |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>.* |
| 22 | + ************************************************************************/ |
| 23 | + |
| 24 | +#include "common/Logging.hh" |
| 25 | +#include "common/Namespace.hh" |
| 26 | +#include "common/StringConversion.hh" |
| 27 | +#include "common/WebNotify.hh" |
| 28 | +#include <iostream> |
| 29 | +#include <memory> |
| 30 | + |
| 31 | +// curl |
| 32 | +#include <curl/curl.h> |
| 33 | +#include <curl/easy.h> |
| 34 | +#include <json/json.h> |
| 35 | + |
| 36 | +// active MQ |
| 37 | +#include <cms/Connection.h> |
| 38 | +#include <cms/ConnectionFactory.h> |
| 39 | +#include <cms/Session.h> |
| 40 | +#include <cms/TextMessage.h> |
| 41 | +#include <cms/MessageProducer.h> |
| 42 | +#include <decaf/lang/Thread.h> |
| 43 | +#include <decaf/util/UUID.h> |
| 44 | +#include <decaf/internal/util/concurrent/Threading.h> |
| 45 | +#include <activemq/library/ActiveMQCPP.h> |
| 46 | + |
| 47 | +// grpc |
| 48 | +#ifdef EOS_GRPC |
| 49 | +#include <grpc++/grpc++.h> |
| 50 | +#include "proto/Rpc.grpc.pb.h" |
| 51 | +using eos::rpc::Eos; |
| 52 | +using eos::rpc::NotificationRequest; |
| 53 | +using eos::rpc::NotificationResponse; |
| 54 | +/*#include <grpc/grpc.h> |
| 55 | +#include <grpc/grpc_security.h> |
| 56 | +#include <grpcpp/grpcpp.h> |
| 57 | +#include <grpcpp/channel.h> |
| 58 | +#include <grpcpp/client_context.h> |
| 59 | +#include <grpcpp/create_channel.h> |
| 60 | +#include <grpcpp/support/channel_arguments.h> |
| 61 | +#include <grpc/impl/codegen/grpc_types.h> |
| 62 | +*/ |
| 63 | +#endif |
| 64 | + |
| 65 | +// QClient |
| 66 | +#include <qclient/QClient.hh> |
| 67 | + |
| 68 | +using namespace cms; |
| 69 | +using namespace decaf::lang; |
| 70 | +using namespace std; |
| 71 | + |
| 72 | +EOSCOMMONNAMESPACE_BEGIN; |
| 73 | + |
| 74 | + |
| 75 | +bool WebNotify::Notify(const std::string& protocol, |
| 76 | + const std::string& uri, |
| 77 | + const std::string& sport, |
| 78 | + const std::string& channel, |
| 79 | + const std::string& message, |
| 80 | + const std::string& stimeout) |
| 81 | +{ |
| 82 | + WebNotify notify; |
| 83 | + try { |
| 84 | + int timeoutMs = stimeout.empty() ? 0 : std::stoi(stimeout); |
| 85 | + int port = sport.empty() ? 0 : std::stoi(sport); |
| 86 | + eos_static_debug("protocol='%s'", protocol.c_str()); |
| 87 | + if (protocol == "http") |
| 88 | + return notify.sendHttpPostNotification(uri, message, timeoutMs); |
| 89 | + if (protocol == "grpc") |
| 90 | + return notify.sendGrpcNotification(uri, message, timeoutMs); |
| 91 | + if (protocol == "redis") |
| 92 | + return notify.sendQClientNotification(uri, port, channel, message, timeoutMs, true); |
| 93 | + if (protocol == "qclient") |
| 94 | + return notify.sendQClientNotification(uri, port, channel, message, timeoutMs, false); |
| 95 | + if (protocol == "amq") |
| 96 | + return notify.sendActiveMQNotification(uri, channel, message, timeoutMs); |
| 97 | + eos_static_err("msg=\"unsupported notification protocol specified\" protocol=\"%s\"", protocol.c_str()); |
| 98 | + } catch (const std::exception& e) { |
| 99 | + eos_static_err("msg=\"invalid numeric input\" error=\"%s\"", e.what()); |
| 100 | + } |
| 101 | + |
| 102 | + return false; |
| 103 | +} |
| 104 | + |
| 105 | +bool WebNotify::sendHttpPostNotification(const std::string& url, const std::string& message, long timeoutMs) { |
| 106 | + CURL* curl = curl_easy_init(); |
| 107 | + if (!curl) return false; |
| 108 | + |
| 109 | + struct curl_slist* headers = nullptr; |
| 110 | + headers = curl_slist_append(headers, "Content-Type: application/json"); |
| 111 | + |
| 112 | + std::string jsonPayload; |
| 113 | + if (!message.empty() && message.front() == '{') { |
| 114 | + jsonPayload = message; |
| 115 | + } else { |
| 116 | + jsonPayload = "{\"message\": \"" + message + "\"}"; |
| 117 | + } |
| 118 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 119 | + curl_easy_setopt(curl, CURLOPT_POST, 1L); |
| 120 | + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonPayload.c_str()); |
| 121 | + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); |
| 122 | + curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeoutMs); |
| 123 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NoOpCallback); |
| 124 | + |
| 125 | + CURLcode res = curl_easy_perform(curl); |
| 126 | + curl_slist_free_all(headers); |
| 127 | + curl_easy_cleanup(curl); |
| 128 | + |
| 129 | + return (res == CURLE_OK); |
| 130 | +} |
| 131 | + |
| 132 | +bool WebNotify::sendActiveMQNotification(const std::string& brokerURI, const std::string& queueName, const std::string& messageText, int timeoutMs) { |
| 133 | + static std::once_flag initFlag; |
| 134 | + |
| 135 | + std::call_once(initFlag, [] { |
| 136 | + activemq::library::ActiveMQCPP::initializeLibrary(); |
| 137 | + }); |
| 138 | + |
| 139 | + try { |
| 140 | + // Construct broker URI with connection timeout |
| 141 | + std::ostringstream fullBrokerURI; |
| 142 | + fullBrokerURI << brokerURI; |
| 143 | + if (brokerURI.find('?') == std::string::npos) { |
| 144 | + fullBrokerURI << "?"; |
| 145 | + } else { |
| 146 | + fullBrokerURI << "&"; |
| 147 | + } |
| 148 | + fullBrokerURI |
| 149 | + << "connection.requestTimeout=" << timeoutMs |
| 150 | + << "&wireFormat.maxInactivityDuration=" << timeoutMs |
| 151 | + << "&wireFormat.maxInactivityDurationInitialDelay=" << timeoutMs |
| 152 | + << "&transport.maxReconnectAttempts=0"; |
| 153 | + // Create a ConnectionFactory |
| 154 | + std::unique_ptr<ConnectionFactory> connectionFactory(ConnectionFactory::createCMSConnectionFactory(fullBrokerURI.str())); |
| 155 | + |
| 156 | + // Create a Connection |
| 157 | + std::unique_ptr<Connection> connection(connectionFactory->createConnection()); |
| 158 | + connection->start(); |
| 159 | + |
| 160 | + // Create a Session |
| 161 | + std::unique_ptr<Session> session(connection->createSession(Session::AUTO_ACKNOWLEDGE)); |
| 162 | + |
| 163 | + // Create the Destination (queue) |
| 164 | + std::unique_ptr<Destination> destination(session->createQueue(queueName)); |
| 165 | + |
| 166 | + // Create a MessageProducer from the Session to the Topic or Queue |
| 167 | + std::unique_ptr<MessageProducer> producer(session->createProducer(destination.get())); |
| 168 | + producer->setDeliveryMode(DeliveryMode::NON_PERSISTENT); |
| 169 | + |
| 170 | + // Create the message and send it |
| 171 | + std::unique_ptr<TextMessage> message(session->createTextMessage(messageText)); |
| 172 | + producer->send(message.get()); |
| 173 | + return true; |
| 174 | + } catch (const CMSException& e) { |
| 175 | + std::cerr << "CMSException: "; |
| 176 | + e.printStackTrace(); |
| 177 | + return false; |
| 178 | + } catch (const std::exception& e) { |
| 179 | + eos_static_err("exception='%s'", e.what()); |
| 180 | + return false; |
| 181 | + } catch (...) { |
| 182 | + eos_static_err("Unknown exception occurred while sending ActiveMQ notification."); |
| 183 | + return false; |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +bool WebNotify::sendGrpcNotification(const std::string& target, const std::string& message, int timeoutMs) |
| 188 | +{ |
| 189 | +#ifdef EOS_GRPC |
| 190 | + grpc::ChannelArguments ch_args; |
| 191 | + // This is the key: set connection timeout (in milliseconds) |
| 192 | + ch_args.SetInt("grpc.client_channel_backup_poll_interval_ms", timeoutMs); |
| 193 | + ch_args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, -1); // Unlimited if needed |
| 194 | + |
| 195 | + auto deadline = std::chrono::system_clock::now() + std::chrono::milliseconds(timeoutMs); |
| 196 | + std::shared_ptr<grpc::Channel> channel = grpc::CreateCustomChannel( |
| 197 | + target, |
| 198 | + grpc::InsecureChannelCredentials(), |
| 199 | + ch_args |
| 200 | + ); |
| 201 | + |
| 202 | + auto stub = eos::rpc::Eos::NewStub(channel); |
| 203 | + |
| 204 | + NotificationRequest request; |
| 205 | + request.set_message(message); |
| 206 | + |
| 207 | + grpc::ClientContext context; |
| 208 | + context.set_deadline(deadline); |
| 209 | + |
| 210 | + NotificationResponse response; |
| 211 | + grpc::Status status = stub->Notify(&context, request, &response); |
| 212 | + if (status.ok()) { |
| 213 | + eos_static_debug("gRPC call succeeded"); |
| 214 | + return response.success(); |
| 215 | + } else { |
| 216 | + eos_static_err("msg=\"gRPC call failed\" errc=%d errmsg='%s'", status.error_code(), status.error_message().c_str()); |
| 217 | + return false; |
| 218 | + } |
| 219 | +#else |
| 220 | + return false; |
| 221 | +#endif |
| 222 | +} |
| 223 | + |
| 224 | +bool WebNotify::sendQClientNotification(const std::string& hostname, int port, |
| 225 | + const std::string& channel, |
| 226 | + const std::string& message, |
| 227 | + int timeoutMs, |
| 228 | + bool push) { |
| 229 | + using namespace qclient; |
| 230 | + |
| 231 | + try { |
| 232 | + // Connect with socket timeout |
| 233 | + QClient client{hostname, port, {}}; |
| 234 | + |
| 235 | + // Send PUBLISH command |
| 236 | + std::string method = push ?"RPUSH":"PUBLISH"; |
| 237 | + auto publish = client.exec(method, channel, message); |
| 238 | + qclient::redisReplyPtr reply = publish.get(); |
| 239 | + if (reply && reply->type == REDIS_REPLY_INTEGER && reply->integer != 0) { |
| 240 | + eos_static_debug("msg=\"published\" subscribers=%d", reply->integer); |
| 241 | + return true; |
| 242 | + } else { |
| 243 | + eos_static_err("msg=\"unexpected or null reply from QuarkDB/REDIS") |
| 244 | + return false; |
| 245 | + } |
| 246 | + } catch (const std::exception& ex) { |
| 247 | + eos_static_err("msg=\"QuarkDB/REDIS connection or command error\" msg='%s'", ex.what()); |
| 248 | + return false; |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +EOSCOMMONNAMESPACE_END; |
0 commit comments