24
24
#include " registries.hpp"
25
25
#include " registries/base_message_registry.hpp"
26
26
#include " registries/openbmc_message_registry.hpp"
27
+ #include " registries/privilege_registry.hpp"
27
28
#include " registries/task_event_message_registry.hpp"
28
29
#include " server_sent_events.hpp"
29
30
#include " str_utility.hpp"
38
39
#include < boost/url/format.hpp>
39
40
#include < sdbusplus/bus/match.hpp>
40
41
42
+ #include < algorithm>
41
43
#include < cstdlib>
42
44
#include < ctime>
43
45
#include < fstream>
@@ -53,9 +55,13 @@ using ReadingsObjType =
53
55
static constexpr const char * eventFormatType = " Event" ;
54
56
static constexpr const char * metricReportFormatType = " MetricReport" ;
55
57
58
+ static constexpr const char * subscriptionTypeSSE = " SSE" ;
56
59
static constexpr const char * eventServiceFile =
57
60
" /var/lib/bmcweb/eventservice_config.json" ;
58
61
62
+ static constexpr const uint8_t maxNoOfSubscriptions = 20 ;
63
+ static constexpr const uint8_t maxNoOfSSESubscriptions = 10 ;
64
+
59
65
namespace registries
60
66
{
61
67
inline std::span<const MessageEntry>
@@ -382,15 +388,20 @@ class Subscription : public persistent_data::UserSubscription
382
388
boost::asio::io_context& ioc) :
383
389
host (inHost),
384
390
port (inPort), policy(std::make_shared<crow::ConnectionPolicy>()),
385
- client (ioc, policy), path(inPath), uriProto(inUriProto)
391
+ path (inPath), uriProto(inUriProto)
386
392
{
393
+ client.emplace (ioc, policy);
387
394
// Subscription constructor
388
395
policy->invalidResp = retryRespHandler;
389
396
}
390
397
398
+ explicit Subscription (crow::sse_socket::Connection& connIn) :
399
+ sseConn(&connIn)
400
+ {}
401
+
391
402
~Subscription () = default ;
392
403
393
- bool sendEvent (std::string& msg)
404
+ bool sendEvent (std::string&& msg)
394
405
{
395
406
persistent_data::EventServiceConfig eventServiceConfig =
396
407
persistent_data::EventServiceStore::getInstance ()
@@ -402,13 +413,17 @@ class Subscription : public persistent_data::UserSubscription
402
413
403
414
bool useSSL = (uriProto == " https" );
404
415
// A connection pool will be created if one does not already exist
405
- client.sendData (msg, host, port, path, useSSL, httpHeaders,
406
- boost::beast::http::verb::post);
407
- eventSeqNum++;
416
+ if (client)
417
+ {
418
+ client->sendData (std::move (msg), host, port, path, useSSL,
419
+ httpHeaders, boost::beast::http::verb::post);
420
+ return true ;
421
+ }
408
422
409
423
if (sseConn != nullptr )
410
424
{
411
- sseConn->sendData (eventSeqNum, msg);
425
+ eventSeqNum++;
426
+ sseConn->sendEvent (std::to_string (eventSeqNum), msg);
412
427
}
413
428
return true ;
414
429
}
@@ -437,7 +452,7 @@ class Subscription : public persistent_data::UserSubscription
437
452
438
453
std::string strMsg = msg.dump (2 , ' ' , true ,
439
454
nlohmann::json::error_handler_t ::replace);
440
- return this -> sendEvent (strMsg);
455
+ return sendEvent (std::move ( strMsg) );
441
456
}
442
457
443
458
#ifndef BMCWEB_ENABLE_REDFISH_DBUS_LOG_ENTRIES
@@ -503,10 +518,10 @@ class Subscription : public persistent_data::UserSubscription
503
518
msg[" Id" ] = std::to_string (eventSeqNum);
504
519
msg[" Name" ] = " Event Log" ;
505
520
msg[" Events" ] = logEntryArray;
506
-
507
521
std::string strMsg = msg.dump (2 , ' ' , true ,
508
522
nlohmann::json::error_handler_t ::replace);
509
- this ->sendEvent (strMsg);
523
+ sendEvent (std::move (strMsg));
524
+ eventSeqNum++;
510
525
}
511
526
#endif
512
527
@@ -546,7 +561,7 @@ class Subscription : public persistent_data::UserSubscription
546
561
547
562
std::string strMsg = msg.dump (2 , ' ' , true ,
548
563
nlohmann::json::error_handler_t ::replace);
549
- this -> sendEvent (strMsg);
564
+ sendEvent (std::move ( strMsg) );
550
565
}
551
566
552
567
void updateRetryConfig (uint32_t retryAttempts,
@@ -561,15 +576,32 @@ class Subscription : public persistent_data::UserSubscription
561
576
return eventSeqNum;
562
577
}
563
578
579
+ void setSubscriptionId (const std::string& id2)
580
+ {
581
+ BMCWEB_LOG_DEBUG << " Subscription ID: " << id2;
582
+ subId = id2;
583
+ }
584
+
585
+ std::string getSubscriptionId ()
586
+ {
587
+ return subId;
588
+ }
589
+
590
+ bool matchSseId (const crow::sse_socket::Connection& thisConn)
591
+ {
592
+ return &thisConn == sseConn;
593
+ }
594
+
564
595
private:
596
+ std::string subId;
565
597
uint64_t eventSeqNum = 1 ;
566
598
std::string host;
567
599
uint16_t port = 0 ;
568
600
std::shared_ptr<crow::ConnectionPolicy> policy;
569
- crow::HttpClient client;
601
+ crow::sse_socket::Connection* sseConn = nullptr ;
602
+ std::optional<crow::HttpClient> client;
570
603
std::string path;
571
604
std::string uriProto;
572
- std::shared_ptr<crow::ServerSentEvents> sseConn = nullptr ;
573
605
574
606
// Check used to indicate what response codes are valid as part of our retry
575
607
// policy. 2XX is considered acceptable
@@ -828,8 +860,8 @@ class EventServiceManager
828
860
for (const auto & it :
829
861
EventServiceManager::getInstance ().subscriptionsMap )
830
862
{
831
- std::shared_ptr< Subscription> entry = it.second ;
832
- entry-> updateRetryConfig (retryAttempts, retryTimeoutInterval);
863
+ Subscription& entry = * it.second ;
864
+ entry. updateRetryConfig (retryAttempts, retryTimeoutInterval);
833
865
}
834
866
}
835
867
}
@@ -942,6 +974,8 @@ class EventServiceManager
942
974
// Update retry configuration.
943
975
subValue->updateRetryConfig (retryAttempts, retryTimeoutInterval);
944
976
977
+ // Set Subscription ID for back trace
978
+ subValue->setSubscriptionId (id);
945
979
return id;
946
980
}
947
981
@@ -966,11 +1000,38 @@ class EventServiceManager
966
1000
}
967
1001
}
968
1002
969
- size_t getNumberOfSubscriptions ()
1003
+ void deleteSseSubscription (const crow::sse_socket::Connection& thisConn)
1004
+ {
1005
+ for (const auto & it : subscriptionsMap)
1006
+ {
1007
+ std::shared_ptr<Subscription> entry = it.second ;
1008
+ bool entryIsThisConn = entry->matchSseId (thisConn);
1009
+ if (entryIsThisConn)
1010
+ {
1011
+ persistent_data::EventServiceStore::getInstance ()
1012
+ .subscriptionsConfigMap .erase (
1013
+ it.second ->getSubscriptionId ());
1014
+ return ;
1015
+ }
1016
+ }
1017
+ }
1018
+
1019
+ size_t getNumberOfSubscriptions () const
970
1020
{
971
1021
return subscriptionsMap.size ();
972
1022
}
973
1023
1024
+ size_t getNumberOfSSESubscriptions () const
1025
+ {
1026
+ auto size = std::count_if (
1027
+ subscriptionsMap.begin (), subscriptionsMap.end (),
1028
+ [](const std::pair<std::string, std::shared_ptr<Subscription>>&
1029
+ entry) {
1030
+ return (entry.second ->subscriptionType == subscriptionTypeSSE);
1031
+ });
1032
+ return static_cast <size_t >(size);
1033
+ }
1034
+
974
1035
std::vector<std::string> getAllIDs ()
975
1036
{
976
1037
std::vector<std::string> idList;
@@ -981,7 +1042,7 @@ class EventServiceManager
981
1042
return idList;
982
1043
}
983
1044
984
- bool isDestinationExist (const std::string& destUrl)
1045
+ bool isDestinationExist (const std::string& destUrl) const
985
1046
{
986
1047
for (const auto & it : subscriptionsMap)
987
1048
{
@@ -997,7 +1058,7 @@ class EventServiceManager
997
1058
998
1059
bool sendTestEventLog ()
999
1060
{
1000
- for (const auto & it : this -> subscriptionsMap )
1061
+ for (const auto & it : subscriptionsMap)
1001
1062
{
1002
1063
std::shared_ptr<Subscription> entry = it.second ;
1003
1064
if (!entry->sendTestEventLog ())
@@ -1027,7 +1088,7 @@ class EventServiceManager
1027
1088
1028
1089
eventRecord.emplace_back (std::move (eventMessage));
1029
1090
1030
- for (const auto & it : this -> subscriptionsMap )
1091
+ for (const auto & it : subscriptionsMap)
1031
1092
{
1032
1093
std::shared_ptr<Subscription> entry = it.second ;
1033
1094
bool isSubscribed = false ;
@@ -1062,7 +1123,7 @@ class EventServiceManager
1062
1123
1063
1124
std::string strMsg = msgJson.dump (
1064
1125
2 , ' ' , true , nlohmann::json::error_handler_t ::replace);
1065
- entry->sendEvent (strMsg);
1126
+ entry->sendEvent (std::move ( strMsg) );
1066
1127
eventId++; // increament the eventId
1067
1128
}
1068
1129
else
@@ -1073,7 +1134,7 @@ class EventServiceManager
1073
1134
}
1074
1135
void sendBroadcastMsg (const std::string& broadcastMsg)
1075
1136
{
1076
- for (const auto & it : this -> subscriptionsMap )
1137
+ for (const auto & it : subscriptionsMap)
1077
1138
{
1078
1139
std::shared_ptr<Subscription> entry = it.second ;
1079
1140
nlohmann::json msgJson;
@@ -1085,7 +1146,7 @@ class EventServiceManager
1085
1146
1086
1147
std::string strMsg = msgJson.dump (
1087
1148
2 , ' ' , true , nlohmann::json::error_handler_t ::replace);
1088
- entry->sendEvent (strMsg);
1149
+ entry->sendEvent (std::move ( strMsg) );
1089
1150
}
1090
1151
}
1091
1152
@@ -1188,7 +1249,7 @@ class EventServiceManager
1188
1249
return ;
1189
1250
}
1190
1251
1191
- for (const auto & it : this -> subscriptionsMap )
1252
+ for (const auto & it : subscriptionsMap)
1192
1253
{
1193
1254
std::shared_ptr<Subscription> entry = it.second ;
1194
1255
if (entry->eventFormatType == " Event" )
0 commit comments