Commit 681eeb8288ffa50ca3d2bcd5b8ecbcc5c251f89b

Authored by Wiebe Cazemier
1 parent 3f3708fd

Rename ThreadAuth to ThreadGlobals

Because that's what's it is now.

A lot of code can be refactored to get the settings from this now, but
I'm not going to do that yet.
CMakeLists.txt
... ... @@ -56,7 +56,7 @@ add_executable(FlashMQ
56 56 persistencefile.h
57 57 sessionsandsubscriptionsdb.h
58 58 qospacketqueue.h
59   - threadauth.h
  59 + threadglobals.h
60 60 threadloop.h
61 61  
62 62 mainapp.cpp
... ... @@ -93,7 +93,7 @@ add_executable(FlashMQ
93 93 persistencefile.cpp
94 94 sessionsandsubscriptionsdb.cpp
95 95 qospacketqueue.cpp
96   - threadauth.cpp
  96 + threadglobals.cpp
97 97 threadloop.cpp
98 98  
99 99 )
... ...
FlashMQTests/FlashMQTests.pro
... ... @@ -47,7 +47,7 @@ SOURCES += tst_maintests.cpp \
47 47 ../persistencefile.cpp \
48 48 ../sessionsandsubscriptionsdb.cpp \
49 49 ../qospacketqueue.cpp \
50   - ../threadauth.cpp \
  50 + ../threadglobals.cpp \
51 51 ../threadloop.cpp \
52 52 mainappthread.cpp \
53 53 twoclienttestcontext.cpp
... ... @@ -88,7 +88,7 @@ HEADERS += \
88 88 ../persistencefile.h \
89 89 ../sessionsandsubscriptionsdb.h \
90 90 ../qospacketqueue.h \
91   - ../threadauth.h \
  91 + ../threadglobals.h \
92 92 ../threadloop.h \
93 93 mainappthread.h \
94 94 twoclienttestcontext.h
... ...
FlashMQTests/tst_maintests.cpp
... ... @@ -33,7 +33,7 @@ License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
33 33 #include "sessionsandsubscriptionsdb.h"
34 34 #include "session.h"
35 35 #include "threaddata.h"
36   -#include "threadauth.h"
  36 +#include "threadglobals.h"
37 37  
38 38 // Dumb Qt version gives warnings when comparing uint with number literal.
39 39 template <typename T1, typename T2>
... ... @@ -996,7 +996,7 @@ void MainTests::testSavingSessions()
996 996  
997 997 // Kind of a hack...
998 998 Authentication auth(*settings.get());
999   - ThreadAuth::assign(&auth);
  999 + ThreadGlobals::assign(&auth);
1000 1000  
1001 1001 std::shared_ptr<Client> c1(new Client(0, t, nullptr, false, nullptr, settings, false));
1002 1002 c1->setClientProperties(ProtocolVersion::Mqtt311, "c1", "user1", true, 60, false);
... ... @@ -1115,7 +1115,7 @@ void testCopyPacketHelper(const std::string &amp;topic, char from_qos, char to_qos,
1115 1115  
1116 1116 // Kind of a hack...
1117 1117 Authentication auth(*settings.get());
1118   - ThreadAuth::assign(&auth);
  1118 + ThreadGlobals::assign(&auth);
1119 1119  
1120 1120 std::shared_ptr<Client> dummyClient(new Client(0, t, nullptr, false, nullptr, settings, false));
1121 1121 dummyClient->setClientProperties(ProtocolVersion::Mqtt311, "qostestclient", "user1", true, 60, false);
... ...
mainapp.cpp
... ... @@ -28,7 +28,7 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
28 28 #include <openssl/err.h>
29 29  
30 30 #include "logger.h"
31   -#include "threadauth.h"
  31 +#include "threadglobals.h"
32 32 #include "threadloop.h"
33 33  
34 34 MainApp *MainApp::instance = nullptr;
... ... @@ -645,7 +645,7 @@ void MainApp::loadConfig()
645 645 confFileParser->loadFile(false);
646 646 settings = confFileParser->moveSettings();
647 647 settingsLocalCopy = *settings.get();
648   - ThreadAuth::assignSettings(&settingsLocalCopy);
  648 + ThreadGlobals::assignSettings(&settingsLocalCopy);
649 649  
650 650 if (settings->listeners.empty())
651 651 {
... ...
mqttpacket.cpp
... ... @@ -22,7 +22,7 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
22 22 #include <cassert>
23 23  
24 24 #include "utils.h"
25   -#include "threadauth.h"
  25 +#include "threadglobals.h"
26 26  
27 27 RemainingLength::RemainingLength()
28 28 {
... ... @@ -460,7 +460,7 @@ void MqttPacket::handleConnect()
460 460 bool accessGranted = false;
461 461 std::string denyLogMsg;
462 462  
463   - Authentication &authentication = *ThreadAuth::getAuth();
  463 + Authentication &authentication = *ThreadGlobals::getAuth();
464 464  
465 465 if (!user_name_flag && settings.allowAnonymous)
466 466 {
... ... @@ -531,7 +531,7 @@ void MqttPacket::handleSubscribe()
531 531 throw ProtocolError("Packet ID 0 when subscribing is invalid."); // [MQTT-2.3.1-1]
532 532 }
533 533  
534   - Authentication &authentication = *ThreadAuth::getAuth();
  534 + Authentication &authentication = *ThreadGlobals::getAuth();
535 535  
536 536 std::list<char> subs_reponse_codes;
537 537 while (remainingAfterPos() > 0)
... ... @@ -691,7 +691,7 @@ void MqttPacket::handlePublish()
691 691 payloadLen = remainingAfterPos();
692 692 payloadStart = pos;
693 693  
694   - Authentication &authentication = *ThreadAuth::getAuth();
  694 + Authentication &authentication = *ThreadGlobals::getAuth();
695 695 if (authentication.aclCheck(sender->getClientId(), sender->getUsername(), topic, subtopics, AclAccess::write, qos, retain) == AuthResult::success)
696 696 {
697 697 if (retain)
... ...
session.cpp
... ... @@ -19,13 +19,13 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
19 19  
20 20 #include "session.h"
21 21 #include "client.h"
22   -#include "threadauth.h"
  22 +#include "threadglobals.h"
23 23  
24 24 std::chrono::time_point<std::chrono::steady_clock> appStartTime = std::chrono::steady_clock::now();
25 25  
26 26 Session::Session() :
27   - maxQosMsgPending(ThreadAuth::getSettings()->maxQosMsgPendingPerClient),
28   - maxQosBytesPending(ThreadAuth::getSettings()->maxQosBytesPendingPerClient)
  27 + maxQosMsgPending(ThreadGlobals::getSettings()->maxQosMsgPendingPerClient),
  28 + maxQosBytesPending(ThreadGlobals::getSettings()->maxQosBytesPendingPerClient)
29 29 {
30 30  
31 31 }
... ... @@ -152,7 +152,7 @@ void Session::writePacket(MqttPacket &amp;packet, char max_qos, std::shared_ptr&lt;Mqtt
152 152 assert(max_qos <= 2);
153 153 const char effectiveQos = std::min<char>(packet.getQos(), max_qos);
154 154  
155   - Authentication *_auth = ThreadAuth::getAuth();
  155 + Authentication *_auth = ThreadGlobals::getAuth();
156 156 assert(_auth);
157 157 Authentication &auth = *_auth;
158 158 if (auth.aclCheck(client_id, username, packet.getTopic(), packet.getSubtopics(), AclAccess::read, effectiveQos, packet.getRetain()) == AuthResult::success)
... ...
threadauth.cpp deleted
1   -#include "threadauth.h"
2   -
3   -thread_local Authentication *ThreadAuth::auth = nullptr;
4   -thread_local ThreadData *ThreadAuth::threadData = nullptr;
5   -thread_local Settings *ThreadAuth::settings = nullptr;
6   -
7   -void ThreadAuth::assign(Authentication *auth)
8   -{
9   - ThreadAuth::auth = auth;
10   -}
11   -
12   -Authentication *ThreadAuth::getAuth()
13   -{
14   - return auth;
15   -}
16   -
17   -void ThreadAuth::assignThreadData(ThreadData *threadData)
18   -{
19   - ThreadAuth::threadData = threadData;
20   -}
21   -
22   -ThreadData *ThreadAuth::getThreadData()
23   -{
24   - return threadData;
25   -}
26   -
27   -void ThreadAuth::assignSettings(Settings *settings)
28   -{
29   - ThreadAuth::settings = settings;
30   -}
31   -
32   -Settings *ThreadAuth::getSettings()
33   -{
34   - return settings;
35   -}
threadglobals.cpp 0 → 100644
  1 +#include "threadglobals.h"
  2 +#include "cassert"
  3 +
  4 +thread_local Authentication *ThreadGlobals::auth = nullptr;
  5 +thread_local ThreadData *ThreadGlobals::threadData = nullptr;
  6 +thread_local Settings *ThreadGlobals::settings = nullptr;
  7 +
  8 +void ThreadGlobals::assign(Authentication *auth)
  9 +{
  10 +#ifndef TESTING
  11 + assert(ThreadGlobals::auth == nullptr);
  12 +#endif
  13 + ThreadGlobals::auth = auth;
  14 +}
  15 +
  16 +Authentication *ThreadGlobals::getAuth()
  17 +{
  18 + return auth;
  19 +}
  20 +
  21 +void ThreadGlobals::assignThreadData(ThreadData *threadData)
  22 +{
  23 +#ifndef TESTING
  24 + assert(ThreadGlobals::threadData == nullptr);
  25 +#endif
  26 + ThreadGlobals::threadData = threadData;
  27 +}
  28 +
  29 +ThreadData *ThreadGlobals::getThreadData()
  30 +{
  31 + return threadData;
  32 +}
  33 +
  34 +void ThreadGlobals::assignSettings(Settings *settings)
  35 +{
  36 +#ifndef TESTING
  37 + assert(ThreadGlobals::settings == nullptr || ThreadGlobals::settings == settings);
  38 +#endif
  39 + ThreadGlobals::settings = settings;
  40 +}
  41 +
  42 +Settings *ThreadGlobals::getSettings()
  43 +{
  44 + return settings;
  45 +}
... ...
threadauth.h renamed to threadglobals.h
1   -#ifndef THREADAUTH_H
2   -#define THREADAUTH_H
  1 +#ifndef THREADGLOBALS_H
  2 +#define THREADGLOBALS_H
3 3  
4 4 #include "forward_declarations.h"
5 5  
6 6 class Authentication;
7 7  
8   -// TODO: rename, this is no longer just auth, but thread local globals.
9   -class ThreadAuth
  8 +class ThreadGlobals
10 9 {
11 10 static thread_local Authentication *auth;
12 11 static thread_local ThreadData *threadData;
... ... @@ -22,4 +21,4 @@ public:
22 21 static Settings *getSettings();
23 22 };
24 23  
25   -#endif // THREADAUTH_H
  24 +#endif // THREADGLOBALS_H
... ...
threadloop.cpp
... ... @@ -20,9 +20,9 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
20 20 void do_thread_work(ThreadData *threadData)
21 21 {
22 22 int epoll_fd = threadData->epollfd;
23   - ThreadAuth::assign(&threadData->authentication);
24   - ThreadAuth::assignThreadData(threadData);
25   - ThreadAuth::assignSettings(&threadData->settingsLocalCopy);
  23 + ThreadGlobals::assign(&threadData->authentication);
  24 + ThreadGlobals::assignThreadData(threadData);
  25 + ThreadGlobals::assignSettings(&threadData->settingsLocalCopy);
26 26  
27 27 struct epoll_event events[MAX_EVENTS];
28 28 memset(&events, 0, sizeof (struct epoll_event)*MAX_EVENTS);
... ...
threadloop.h
... ... @@ -19,7 +19,7 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
19 19 #define THREADLOOP_H
20 20  
21 21 #include "threaddata.h"
22   -#include "threadauth.h"
  22 +#include "threadglobals.h"
23 23  
24 24 #define MAX_EVENTS 65536
25 25  
... ...