implementing message cache
This commit is contained in:
@@ -13,6 +13,7 @@ file(GLOB source_files
|
|||||||
)
|
)
|
||||||
add_executable(${PROJECT_NAME} ${source_files})
|
add_executable(${PROJECT_NAME} ${source_files})
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} PUBLIC ${source_dir}/include)
|
target_include_directories(${PROJECT_NAME} PRIVATE ${source_dir}/include)
|
||||||
|
|
||||||
target_link_libraries(${PROJECT_NAME} ${USOCKETS_LIBRARY})
|
target_link_libraries(${PROJECT_NAME} ${USOCKETS_LIBRARY})
|
||||||
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
|
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
|
||||||
|
|||||||
+5
-14
@@ -1,28 +1,19 @@
|
|||||||
|
#include "include/DateTime.hpp"
|
||||||
|
#include <MessageCache.hpp>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
#include <uWebSockets/App.h>
|
#include <uWebSockets/App.h>
|
||||||
#include <uWebSockets/WebSocket.h>
|
#include <uWebSockets/WebSocket.h>
|
||||||
|
|
||||||
namespace DateTime
|
|
||||||
{
|
|
||||||
std::chrono::system_clock::time_point now()
|
|
||||||
{
|
|
||||||
return std::chrono::system_clock::now();
|
|
||||||
}
|
|
||||||
|
|
||||||
void outputTime(const char *desc, std::chrono::system_clock::time_point &tp)
|
|
||||||
{
|
|
||||||
std::time_t now = std::chrono::system_clock::to_time_t(tp);
|
|
||||||
std::cout << desc << std::put_time(std::localtime(&now), "%Y-%m-%d %H:%M") << "\n";
|
|
||||||
}
|
|
||||||
} // namespace DateTime
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << "Welcome to the homechat server!\n";
|
std::cout << "Welcome to the homechat server!\n";
|
||||||
|
|
||||||
|
std::unique_ptr<MessageCache> message_cache = std::make_unique<MessageCache>();
|
||||||
|
|
||||||
/* ws->getUserData returns one of these */
|
/* ws->getUserData returns one of these */
|
||||||
struct PerSocketData
|
struct PerSocketData
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include "include/MessageCache.hpp"
|
||||||
|
|
||||||
|
bool MessageCache::add_message(std::string message)
|
||||||
|
{
|
||||||
|
if (m_messages.size() == m_cache_size)
|
||||||
|
{
|
||||||
|
m_messages.erase(m_messages.begin());
|
||||||
|
}
|
||||||
|
m_messages.push_back(message);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageCache::MessageCache()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MessageCache::empty()
|
||||||
|
{
|
||||||
|
return m_messages.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> MessageCache::last_ten_messages() const
|
||||||
|
{
|
||||||
|
return m_messages.size() > 10 ? std::vector<std::string>(m_messages.end() - 10, m_messages.end()) : m_messages;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace DateTime
|
||||||
|
{
|
||||||
|
std::chrono::system_clock::time_point now()
|
||||||
|
{
|
||||||
|
return std::chrono::system_clock::now();
|
||||||
|
}
|
||||||
|
|
||||||
|
void outputTime(const char *desc, std::chrono::system_clock::time_point &tp)
|
||||||
|
{
|
||||||
|
std::time_t now = std::chrono::system_clock::to_time_t(tp);
|
||||||
|
std::cout << desc << std::put_time(std::localtime(&now), "%Y-%m-%d %H:%M") << "\n";
|
||||||
|
}
|
||||||
|
} // namespace DateTime
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class MessageCache
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MessageCache();
|
||||||
|
|
||||||
|
// adds and removes the oldest message if the cache is full
|
||||||
|
bool add_message(std::string message);
|
||||||
|
|
||||||
|
bool empty();
|
||||||
|
std::vector<std::string> last_ten_messages() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<std::string> m_messages;
|
||||||
|
static constexpr int m_cache_size = 100;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user