Skip to content

Commit

Permalink
#585 refactor CoreThread, provide another method to consume the event
Browse files Browse the repository at this point in the history
  • Loading branch information
lidaobing authored May 15, 2024
1 parent 1213010 commit c27a8d2
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 148 deletions.
3 changes: 2 additions & 1 deletion src/api/iptux-core/CoreThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class CoreThread {
* @return std::shared_ptr<const Event>
*/
std::shared_ptr<const Event> getLastEvent() const;
bool HasEvent() const;
std::shared_ptr<const Event> PopEvent();

const std::string& GetAccessPublicLimit() const;
void SetAccessPublicLimit(const std::string& val);
Expand Down Expand Up @@ -193,7 +195,6 @@ class CoreThread {

private:
void bind_iptux_port();
void processEvents();

private:
static void RecvUdpData(CoreThread* pcthrd);
Expand Down
50 changes: 24 additions & 26 deletions src/api/iptux-core/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@ enum class EventType {
CONFIG_CHANGED,
};

const char* EventTypeToStr(EventType type);

class Event {
public:
explicit Event(EventType type);
virtual ~Event() = default;

EventType getType() const;
virtual std::string getSource() const;

private:
EventType type;
};

class NewPalOnlineEvent : public Event {
class PalEvent : public Event {
public:
explicit PalEvent(PalKey palKey, EventType type)
: Event(type), palKey(palKey) {}
const PalKey& GetPalKey() const { return palKey; }
std::string getSource() const override;

private:
PalKey palKey;
};

class NewPalOnlineEvent : public PalEvent {
public:
explicit NewPalOnlineEvent(CPPalInfo palInfo);
CPPalInfo getPalInfo() const;
Expand All @@ -42,7 +56,7 @@ class NewPalOnlineEvent : public Event {
CPPalInfo palInfo;
};

class PalUpdateEvent : public Event {
class PalUpdateEvent : public PalEvent {
public:
explicit PalUpdateEvent(CPPalInfo palInfo);
CPPalInfo getPalInfo() const;
Expand All @@ -51,7 +65,7 @@ class PalUpdateEvent : public Event {
CPPalInfo palInfo;
};

class NewMessageEvent : public Event {
class NewMessageEvent : public PalEvent {
public:
explicit NewMessageEvent(MsgPara&& msgPara);
const MsgPara& getMsgPara() const;
Expand All @@ -60,43 +74,27 @@ class NewMessageEvent : public Event {
MsgPara msgPara;
};

class PalOfflineEvent : public Event {
class PalOfflineEvent : public PalEvent {
public:
explicit PalOfflineEvent(PalKey palKey);
const PalKey& GetPalKey() const;

private:
PalKey palKey;
};

class IconUpdateEvent : public Event {
class IconUpdateEvent : public PalEvent {
public:
explicit IconUpdateEvent(PalKey palKey)
: Event(EventType::ICON_UPDATE), palKey(palKey) {}
const PalKey& GetPalKey() const { return palKey; }

private:
PalKey palKey;
: PalEvent(palKey, EventType::ICON_UPDATE) {}
};

class PasswordRequiredEvent : public Event {
class PasswordRequiredEvent : public PalEvent {
public:
explicit PasswordRequiredEvent(PalKey palKey)
: Event(EventType::PASSWORD_REQUIRED), palKey(palKey) {}
const PalKey& GetPalKey() const { return palKey; }

private:
PalKey palKey;
: PalEvent(palKey, EventType::PASSWORD_REQUIRED) {}
};

class PermissionRequiredEvent : public Event {
class PermissionRequiredEvent : public PalEvent {
public:
explicit PermissionRequiredEvent(PalKey palKey)
: Event(EventType::PERMISSION_REQUIRED), palKey(palKey) {}
const PalKey& GetPalKey() const { return palKey; }

private:
PalKey palKey;
: PalEvent(palKey, EventType::PERMISSION_REQUIRED) {}
};

class NewShareFileFromFriendEvent : public Event {
Expand Down
41 changes: 13 additions & 28 deletions src/iptux-core/CoreThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,8 @@ struct CoreThread::Impl {
future<void> udpFuture;
future<void> tcpFuture;
future<void> notifyToAllFuture;
future<void> processEventsFuture;
};

void CoreThread::processEvents() {
while (true) {
if (!started) {
return;
}

shared_ptr<const Event> event;

{
lock_guard<std::mutex> l(pImpl->waitingEventsMutex);
if (!pImpl->waitingEvents.empty()) {
event = pImpl->waitingEvents.front();
pImpl->waitingEvents.pop_front();
}
}

if (!event) {
this_thread::sleep_for(10ms);
} else {
signalEvent.emit(event);
}
}
}

CoreThread::CoreThread(shared_ptr<ProgramData> data)
: programData(data),
config(data->getConfig()),
Expand Down Expand Up @@ -174,8 +149,6 @@ void CoreThread::start() {

pImpl->udpFuture = async([](CoreThread* ct) { RecvUdpData(ct); }, this);
pImpl->tcpFuture = async([](CoreThread* ct) { RecvTcpData(ct); }, this);
pImpl->processEventsFuture =
async([](CoreThread* ct) { ct->processEvents(); }, this);
pImpl->notifyToAllFuture =
async([](CoreThread* ct) { SendNotifyToAll(ct); }, this);
}
Expand Down Expand Up @@ -297,7 +270,6 @@ void CoreThread::stop() {
ClearSublayer();
pImpl->udpFuture.wait();
pImpl->tcpFuture.wait();
pImpl->processEventsFuture.wait();
pImpl->notifyToAllFuture.wait();
}

Expand Down Expand Up @@ -455,6 +427,7 @@ void CoreThread::emitEvent(shared_ptr<const Event> event) {
pImpl->waitingEvents.push_back(event);
this->pImpl->eventCount++;
this->pImpl->lastEvent = event;
signalEvent.emit(event);
}

/**
Expand Down Expand Up @@ -773,4 +746,16 @@ string CoreThread::getUserIconPath() const {
return stringFormat("%s%s", g_get_user_cache_dir(), ICON_PATH);
}

bool CoreThread::HasEvent() const {
lock_guard<std::mutex> l(pImpl->waitingEventsMutex);
return !this->pImpl->waitingEvents.empty();
}

shared_ptr<const Event> CoreThread::PopEvent() {
lock_guard<std::mutex> l(pImpl->waitingEventsMutex);
auto event = pImpl->waitingEvents.front();
pImpl->waitingEvents.pop_front();
return event;
}

} // namespace iptux
2 changes: 2 additions & 0 deletions src/iptux-core/CoreThreadTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ TEST(CoreThread, GetPalList) {
EXPECT_EQ(int(thread->GetPalList().size()), 1);
EXPECT_EQ(thread->getEventCount(), eventCount + 1);
EXPECT_EQ(thread->getLastEvent()->getType(), EventType::NEW_PAL_ONLINE);
EXPECT_TRUE(thread->HasEvent());
thread->PopEvent();
delete thread;
}

Expand Down
46 changes: 38 additions & 8 deletions src/iptux-core/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,66 @@ namespace iptux {

Event::Event(EventType type) : type(type) {}

static const char* event_type_strs[] = {
[(int)EventType::NEW_PAL_ONLINE] = "NEW_PAL_ONLINE",
[(int)EventType::PAL_UPDATE] = "PAL_UPDATE",
[(int)EventType::PAL_OFFLINE] = "PAL_OFFLINE",
[(int)EventType::NEW_MESSAGE] = "NEW_MESSAGE",
[(int)EventType::ICON_UPDATE] = "ICON_UPDATE",
[(int)EventType::PASSWORD_REQUIRED] = "PASSWORD_REQUIRED",
[(int)EventType::PERMISSION_REQUIRED] = "PERMISSION_REQUIRED",
[(int)EventType::NEW_SHARE_FILE_FROM_FRIEND] = "NEW_SHARE_FILE_FROM_FRIEND",
[(int)EventType::SEND_FILE_STARTED] = "SEND_FILE_STARTED",
[(int)EventType::SEND_FILE_FINISHED] = "SEND_FILE_FINISHED",
[(int)EventType::RECV_FILE_STARTED] = "RECV_FILE_STARTED",
[(int)EventType::RECV_FILE_FINISHED] = "RECV_FILE_FINISHED",
[(int)EventType::TRANS_TASKS_CHANGED] = "TRANS_TASKS_CHANGED",
[(int)EventType::CONFIG_CHANGED] = "CONFIG_CHANGED",
};

const char* EventTypeToStr(EventType type) {
if (type < EventType::NEW_PAL_ONLINE || type > EventType::CONFIG_CHANGED) {
return "UNKNOWN";
}
return event_type_strs[(int)type];
}

EventType Event::getType() const {
return type;
}

string Event::getSource() const {
return "NOT IMPLEMENTED";
}

string PalEvent::getSource() const {
return GetPalKey().ToString();
}

NewPalOnlineEvent::NewPalOnlineEvent(CPPalInfo palInfo)
: Event(EventType::NEW_PAL_ONLINE), palInfo(palInfo) {}
: PalEvent(palInfo->GetKey(), EventType::NEW_PAL_ONLINE),
palInfo(palInfo) {}

CPPalInfo NewPalOnlineEvent::getPalInfo() const {
return palInfo;
}

PalUpdateEvent::PalUpdateEvent(CPPalInfo palInfo)
: Event(EventType::PAL_UPDATE), palInfo(palInfo) {}
: PalEvent(palInfo->GetKey(), EventType::PAL_UPDATE), palInfo(palInfo) {}

CPPalInfo PalUpdateEvent::getPalInfo() const {
return palInfo;
}

NewMessageEvent::NewMessageEvent(MsgPara&& msgPara)
: Event(EventType::NEW_MESSAGE), msgPara(msgPara) {}
: PalEvent(msgPara.getPal()->GetKey(), EventType::NEW_MESSAGE),
msgPara(msgPara) {}

const MsgPara& NewMessageEvent::getMsgPara() const {
return msgPara;
}

PalOfflineEvent::PalOfflineEvent(PalKey palKey)
: Event(EventType::PAL_OFFLINE), palKey(move(palKey)) {}

const PalKey& PalOfflineEvent::GetPalKey() const {
return palKey;
}
: PalEvent(palKey, EventType::PAL_OFFLINE) {}

} // namespace iptux
27 changes: 20 additions & 7 deletions src/iptux/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Application::Application(shared_ptr<IptuxConfig> config)

transModel = transModelNew();
menuBuilder = nullptr;
eventAdaptor = nullptr;
logSystem = nullptr;

app = gtk_application_new(application_id.c_str(), G_APPLICATION_FLAGS_NONE);
Expand All @@ -98,9 +97,6 @@ Application::~Application() {
g_object_unref(menuBuilder);
}
transModelDelete(transModel);
if (eventAdaptor) {
delete eventAdaptor;
}
if (logSystem) {
delete logSystem;
}
Expand Down Expand Up @@ -156,9 +152,6 @@ void Application::onStartup(Application& self) {
}

self.window = new MainWindow(&self, *self.cthrd);
self.eventAdaptor = new EventAdaptor(
self.cthrd->signalEvent,
[&](shared_ptr<const Event> event) { self.onEvent(event); });

GActionEntry app_entries[] = {
makeActionEntry("quit", G_ACTION_CALLBACK(onQuit)),
Expand Down Expand Up @@ -212,6 +205,7 @@ void Application::onActivate(Application& self) {
exit(1);
}
iptux_init(self.logSystem);
g_idle_add(G_SOURCE_FUNC(Application::ProcessEvents), &self);
}

void Application::onQuit(void*, void*, Application& self) {
Expand Down Expand Up @@ -401,4 +395,23 @@ void Application::onOpenChat(GSimpleAction*,
DialogPeer::PeerDialogEntry(&self, groupInfo);
}

gboolean Application::ProcessEvents(gpointer data) {
auto self = static_cast<Application*>(data);
if (self->getCoreThread()->HasEvent()) {
auto start = chrono::high_resolution_clock::now();
auto e = self->getCoreThread()->PopEvent();
self->onEvent(e);
self->getMainWindow()->ProcessEvent(e);
auto elapsed = std::chrono::high_resolution_clock::now() - start;
LOG_INFO(
"type: %s, from: %s, time: %jdus", EventTypeToStr(e->getType()),
e->getSource().c_str(),
(intmax_t)chrono::duration_cast<chrono::microseconds>(elapsed).count());
g_idle_add(Application::ProcessEvents, data);
} else {
g_timeout_add(100, Application::ProcessEvents, data); // 100ms
}
return G_SOURCE_REMOVE;
}

} // namespace iptux
5 changes: 3 additions & 2 deletions src/iptux/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <gtk/gtk.h>
#include <memory>

#include "iptux-core/Event.h"
#include "iptux-core/IptuxConfig.h"
#include "iptux-core/Models.h"
#include "iptux/EventAdaptor.h"
#include "iptux/NotificationService.h"
#include "iptux/UiModels.h"

Expand Down Expand Up @@ -55,7 +55,6 @@ class Application {
MainWindow* window = 0;
ShareFile* shareFile = 0;
TransWindow* transWindow = 0;
EventAdaptor* eventAdaptor = 0;
LogSystem* logSystem = 0;
NotificationService* notificationService = 0;
GMenuModel* menu_ = 0;
Expand All @@ -67,11 +66,13 @@ class Application {
void startup();
void activate();
void set_enable_app_indicator(bool enable) { enable_app_indicator_ = enable; }
void _ForTestProcessEvents() { ProcessEvents(this); }

private:
void onEvent(std::shared_ptr<const Event> event);
void onConfigChanged();
void updateItemToTransTree(const TransFileModel& para);
static gboolean ProcessEvents(gpointer data);
static void onAbout(void*, void*, Application& self);
static void onActivate(Application& self);
static void onOpenMainWindow(void*, void*, Application& self);
Expand Down
6 changes: 6 additions & 0 deletions src/iptux/ApplicationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "iptux/Application.h"
#include "iptux/TestHelper.h"
#include "iptux/UiCoreThread.h"

using namespace std;
using namespace iptux;
Expand All @@ -16,5 +17,10 @@ TEST(Application, Constructor) {
do_action(app, "help.whats_new");
do_action(app, "tools.open_chat_log");
do_action(app, "tools.open_system_log");

PPalInfo pal = make_shared<PalInfo>("127.0.0.1", 2425);
app->getCoreThread()->AttachPalToList(pal);
app->_ForTestProcessEvents();
app->_ForTestProcessEvents();
DestroyApplication(app);
}
Loading

0 comments on commit c27a8d2

Please sign in to comment.