Skip to content

Commit c21c5dd

Browse files
code refactor
1 parent 1cf094a commit c21c5dd

12 files changed

Lines changed: 111 additions & 184 deletions

File tree

include/bitchat/core/bitchat_manager.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,16 @@ namespace bitchat
1717
// Forward declarations
1818
class BluetoothAnnounceRunner;
1919
class CleanupRunner;
20+
class IUserInterface;
21+
class MessageService;
2022

2123
// BitchatManager: Main orchestrator that coordinates all components
22-
class BitchatManager
24+
class BitchatManager : public std::enable_shared_from_this<BitchatManager>
2325
{
2426
public:
25-
// Singleton access
26-
static std::shared_ptr<BitchatManager> shared();
27-
2827
BitchatManager();
2928
~BitchatManager();
3029

31-
// Copy constructor and assignment operator disabled for thread safety
32-
BitchatManager(const BitchatManager &) = delete;
33-
BitchatManager &operator=(const BitchatManager &) = delete;
34-
3530
// Initialize the manager
3631
bool initialize(
3732
std::shared_ptr<IUserInterface> userInterface,
@@ -67,13 +62,7 @@ class BitchatManager
6762
std::shared_ptr<CryptoService> getCryptoService() const;
6863
std::shared_ptr<NoiseService> getNoiseService() const;
6964

70-
#ifdef UNIT_TEST
71-
static void resetInstance();
72-
#endif
7365
private:
74-
// Static instance
75-
static std::shared_ptr<BitchatManager> instance;
76-
7766
// Bluetooth interface
7867
std::shared_ptr<IBluetoothNetwork> bluetoothNetworkInterface;
7968

include/bitchat/helpers/user_interface_helper.h

Lines changed: 0 additions & 69 deletions
This file was deleted.

include/bitchat/ui/console_ui.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ConsoleUserInterface : public IUserInterface
1919
~ConsoleUserInterface() = default;
2020

2121
// Initialize the UI with message service
22-
bool initialize(std::shared_ptr<MessageService> messageService) override;
22+
bool initialize(std::shared_ptr<BitchatManager> manager, std::shared_ptr<MessageService> messageService) override;
2323

2424
// Message event callbacks
2525
void onMessageReceived(const BitchatMessage &message) override;
@@ -55,6 +55,7 @@ class ConsoleUserInterface : public IUserInterface
5555
protected:
5656
bool initialized = false;
5757
std::shared_ptr<MessageService> messageService;
58+
std::shared_ptr<BitchatManager> manager;
5859
std::shared_ptr<spdlog::logger> logger;
5960
};
6061

include/bitchat/ui/dummy_ui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DummyUserInterface : public IUserInterface
1818
~DummyUserInterface() = default;
1919

2020
// Initialize the UI with message service
21-
bool initialize(std::shared_ptr<MessageService> messageService) override;
21+
bool initialize(std::shared_ptr<BitchatManager> manager, std::shared_ptr<MessageService> messageService) override;
2222

2323
// Message event callbacks
2424
void onMessageReceived(const BitchatMessage &message) override;

include/bitchat/ui/ui_interface.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#pragma once
22

3+
#include "bitchat/core/bitchat_manager.h"
34
#include "bitchat/protocol/packet.h"
45
#include "bitchat/services/message_service.h"
56
#include <functional>
67
#include <memory>
8+
#include <spdlog/fmt/fmt.h>
79
#include <string>
810

911
namespace bitchat
1012
{
1113

1214
// Forward declarations
1315
class MessageService;
16+
class BitchatManager;
1417

1518
// IUserInterface: Defines the contract for all UI implementations
1619
class IUserInterface
@@ -19,7 +22,7 @@ class IUserInterface
1922
virtual ~IUserInterface() = default;
2023

2124
// Initialize the UI with message service
22-
virtual bool initialize(std::shared_ptr<MessageService> messageService) = 0;
25+
virtual bool initialize(std::shared_ptr<BitchatManager> manager, std::shared_ptr<MessageService> messageService) = 0;
2326

2427
// Message event callbacks
2528
virtual void onMessageReceived(const BitchatMessage &message) = 0;
@@ -48,6 +51,42 @@ class IUserInterface
4851
// Control methods
4952
virtual void start() = 0;
5053
virtual void stop() = 0;
54+
55+
// Helper methods
56+
template <typename... Args>
57+
void showChatMessage(const std::string &format, Args &&...args)
58+
{
59+
std::string message = fmt::format(fmt::runtime(format), std::forward<Args>(args)...);
60+
showChatMessage(message);
61+
}
62+
63+
template <typename... Args>
64+
void showChatMessageInfo(const std::string &format, Args &&...args)
65+
{
66+
std::string message = fmt::format(fmt::runtime(format), std::forward<Args>(args)...);
67+
showChatMessageInfo(message);
68+
}
69+
70+
template <typename... Args>
71+
void showChatMessageWarn(const std::string &format, Args &&...args)
72+
{
73+
std::string message = fmt::format(fmt::runtime(format), std::forward<Args>(args)...);
74+
showChatMessageWarn(message);
75+
}
76+
77+
template <typename... Args>
78+
void showChatMessageError(const std::string &format, Args &&...args)
79+
{
80+
std::string message = fmt::format(fmt::runtime(format), std::forward<Args>(args)...);
81+
showChatMessageError(message);
82+
}
83+
84+
template <typename... Args>
85+
void showChatMessageSuccess(const std::string &format, Args &&...args)
86+
{
87+
std::string message = fmt::format(fmt::runtime(format), std::forward<Args>(args)...);
88+
showChatMessageSuccess(message);
89+
}
5190
};
5291

5392
} // namespace bitchat

src/bitchat/core/bitchat_manager.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@
1414
namespace bitchat
1515
{
1616

17-
std::shared_ptr<BitchatManager> BitchatManager::instance = nullptr;
18-
19-
std::shared_ptr<BitchatManager> BitchatManager::shared()
20-
{
21-
if (!instance)
22-
{
23-
instance = std::make_shared<BitchatManager>();
24-
}
25-
26-
return instance;
27-
}
28-
2917
BitchatManager::BitchatManager()
3018
{
3119
// Pass
@@ -81,7 +69,7 @@ bool BitchatManager::initialize(
8169
}
8270

8371
// Initialize UI with message service
84-
if (!userInterface->initialize(messageService))
72+
if (!userInterface->initialize(shared_from_this(), messageService))
8573
{
8674
spdlog::error("Failed to initialize UserInterface");
8775
return false;
@@ -194,11 +182,4 @@ std::shared_ptr<NoiseService> BitchatManager::getNoiseService() const
194182
return noiseService;
195183
}
196184

197-
#ifdef UNIT_TEST
198-
void BitchatManager::resetInstance()
199-
{
200-
instance.reset();
201-
}
202-
#endif
203-
204185
} // namespace bitchat

src/bitchat/identity/identity_models.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,12 @@ IdentityData::IdentityData()
1515

1616
std::string IdentityData::generatePeerID()
1717
{
18-
auto cryptoService = BitchatManager::shared()->getCryptoService();
19-
20-
auto randomBytes = cryptoService->generateRandomBytes(8);
21-
if (randomBytes.empty())
22-
{
23-
throw std::runtime_error("Failed to generate random bytes for peer ID");
24-
}
25-
26-
std::string peerID;
27-
for (uint8_t byte : randomBytes)
28-
{
29-
char hex[3];
30-
snprintf(hex, sizeof(hex), "%02x", byte);
31-
peerID += hex;
32-
}
33-
34-
return peerID;
18+
return "";
3519
}
3620

37-
std::vector<uint8_t> IdentityData::generateIdentityHash(const std::string &peerID, const std::string &channelName)
21+
std::vector<uint8_t> IdentityData::generateIdentityHash([[maybe_unused]] const std::string &peerID, [[maybe_unused]] const std::string &channelName)
3822
{
39-
auto cryptoService = BitchatManager::shared()->getCryptoService();
40-
41-
std::string combined = peerID + channelName;
42-
return cryptoService->sha256(combined);
23+
return {};
4324
}
4425

4526
} // namespace bitchat

src/bitchat/ui/console_ui.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ConsoleUserInterface::ConsoleUserInterface()
1616
// Pass
1717
}
1818

19-
bool ConsoleUserInterface::initialize(std::shared_ptr<MessageService> messageService)
19+
bool ConsoleUserInterface::initialize(std::shared_ptr<BitchatManager> manager, std::shared_ptr<MessageService> messageService)
2020
{
2121
if (initialized)
2222
{
@@ -25,12 +25,19 @@ bool ConsoleUserInterface::initialize(std::shared_ptr<MessageService> messageSer
2525

2626
initialized = true;
2727

28+
if (!manager)
29+
{
30+
spdlog::error("ConsoleUserInterface: Cannot initialize with null manager");
31+
return false;
32+
}
33+
2834
if (!messageService)
2935
{
3036
spdlog::error("ConsoleUserInterface: Cannot initialize with null message service");
3137
return false;
3238
}
3339

40+
this->manager = manager;
3441
this->messageService = messageService;
3542

3643
// Set up callbacks to route all events through ConsoleUserInterface
@@ -245,24 +252,24 @@ void ConsoleUserInterface::start()
245252
}
246253
else if (line == "/help")
247254
{
248-
BitchatManager::shared()->getUserInterface()->showHelp();
255+
showHelp();
249256
}
250257
else if (line.rfind("/j ", 0) == 0)
251258
{
252259
std::string channel = line.substr(3);
253-
BitchatManager::shared()->joinChannel(channel);
260+
manager->joinChannel(channel);
254261

255262
showChatMessageSuccess(fmt::format("Joined channel: {}", channel));
256263
}
257264
else if (line == "/j")
258265
{
259-
BitchatManager::shared()->joinChannel("");
266+
manager->joinChannel("");
260267
showChatMessageSuccess("Joined main chat");
261268
}
262269
else if (line.rfind("/nick ", 0) == 0)
263270
{
264271
std::string nickname = line.substr(6);
265-
BitchatManager::shared()->changeNickname(nickname);
272+
manager->changeNickname(nickname);
266273
showChatMessageSuccess(fmt::format("Nickname changed to: {}", nickname));
267274
}
268275
else if (line == "/w")
@@ -288,7 +295,7 @@ void ConsoleUserInterface::start()
288295
else
289296
{
290297
// Send message
291-
if (BitchatManager::shared()->sendMessage(line))
298+
if (manager->sendMessage(line))
292299
{
293300
showChatMessage(fmt::format("{} You: {}", getChatPrefix(), line));
294301
}

src/bitchat/ui/dummy_ui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ DummyUserInterface::DummyUserInterface()
88
// Pass
99
}
1010

11-
bool DummyUserInterface::initialize([[maybe_unused]] std::shared_ptr<MessageService> messageService)
11+
bool DummyUserInterface::initialize([[maybe_unused]] std::shared_ptr<BitchatManager> manager, [[maybe_unused]] std::shared_ptr<MessageService> messageService)
1212
{
1313
return true;
1414
}

src/main.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "bitchat/core/bitchat_manager.h"
2-
#include "bitchat/helpers/user_interface_helper.h"
32
#include "bitchat/platform/bluetooth_factory.h"
43
#include "bitchat/platform/bluetooth_interface.h"
54
#include "bitchat/runners/bluetooth_announce_runner.h"
@@ -53,27 +52,27 @@ int main()
5352
auto consoleUserInterface = std::make_shared<bitchat::ConsoleUserInterface>();
5453

5554
// Create and initialize manager
56-
auto manager = BitchatManager::shared();
55+
auto manager = std::make_shared<BitchatManager>();
5756

5857
// Initialize manager
59-
if (!BitchatManager::shared()->initialize(consoleUserInterface, bluetoothNetworkInterface, networkService, messageService, cryptoService, noiseService, bluetoothAnnounceRunner, cleanupRunner))
58+
if (!manager->initialize(consoleUserInterface, bluetoothNetworkInterface, networkService, messageService, cryptoService, noiseService, bluetoothAnnounceRunner, cleanupRunner))
6059
{
6160
spdlog::error("Failed to initialize BitchatManager");
6261
return EXIT_FAILURE;
6362
}
6463

6564
// Start manager
66-
if (!BitchatManager::shared()->start())
65+
if (!manager->start())
6766
{
6867
spdlog::error("Failed to start BitchatManager");
6968
return EXIT_FAILURE;
7069
}
7170

7271
// Start user interface
73-
BitchatManager::shared()->getUserInterface()->start();
72+
manager->getUserInterface()->start();
7473

7574
// Stop manager
76-
BitchatManager::shared()->stop();
75+
manager->stop();
7776

7877
return EXIT_SUCCESS;
7978
}

0 commit comments

Comments
 (0)