linuxdcpp-team team mailing list archive
-
linuxdcpp-team team
-
Mailing list archive
-
Message #06983
[Branch ~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin] Rev 17: use copies of HubData in conjuction with MakeCall to avoid problems with timer hooks (TODO: suppo...
------------------------------------------------------------
revno: 17
committer: crise <crise@xxxxxxxxxx>
branch nick: ScriptPlugin
timestamp: Fri 2013-06-14 00:33:08 +0300
message:
use copies of HubData in conjuction with MakeCall to avoid problems with timer hooks (TODO: support duplicate hubs?)
modified:
src/Plugin.cpp
src/Plugin.h
--
lp:~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin
https://code.launchpad.net/~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin
Your team Dcplusplus-team is subscribed to branch lp:~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcpp-plugin-sdk-cpp/ScriptPlugin/+edit-subscription
=== modified file 'src/Plugin.cpp'
--- src/Plugin.cpp 2013-06-03 21:32:25 +0000
+++ src/Plugin.cpp 2013-06-13 21:33:08 +0000
@@ -53,9 +53,9 @@
Plugin::~Plugin() {
if(L) {
if(!hubs.empty()) {
- for(auto hub: hubs) {
- Hubs::handle()->remove_hub(hub);
- Hubs::handle()->release(hub);
+ for(auto i: hubs) {
+ Hubs::handle()->remove_hub(i.second);
+ Hubs::handle()->release(i.second);
}
Logger::log("Script plugin warning: scripts do not correctly remove hubs they add!");
@@ -186,19 +186,52 @@
HubDataPtr Plugin::createHub(const char* url, const char* nick, const char* password) {
auto hHub = Hubs::handle()->add_hub(url, nick, password);
- if(!hHub) hubs.insert(hHub);
+ if(!hHub) hubs[url] = hHub;
return hHub;
}
void Plugin::destroyHub(HubDataPtr hHub) {
- auto i = hubs.find(hHub);
+ auto i = hubs.find(hHub->url);
if(i != hubs.end()) {
- Hubs::handle()->remove_hub(*i);
- Hubs::handle()->release(*i);
+ Hubs::handle()->remove_hub(i->second);
+ Hubs::handle()->release(i->second);
hubs.erase(i);
}
}
+HubDataPtr Plugin::addHub(HubDataPtr hHub) {
+ auto i = hubs.find(hHub->url);
+ if(i != hubs.end())
+ return i->second;
+
+ auto copy = Hubs::handle()->copy(hHub);
+ hubs[hHub->url] = copy;
+ return copy;
+}
+
+HubDataPtr Plugin::findHub(const string& url) {
+ auto i = hubs.find(url);
+ if(i != hubs.end())
+ return i->second;
+ return nullptr;
+}
+
+bool Plugin::removeHub(HubDataPtr hHub) {
+ auto i = hubs.find(hHub->url);
+ if(i == hubs.end())
+ return false;
+
+ // remove cache for chat formatting
+ auto j = chatCache.find(i->second->url);
+ if(j != chatCache.end())
+ chatCache.erase(j);
+
+ Hubs::handle()->remove_hub(i->second);
+ Hubs::handle()->release(i->second);
+ hubs.erase(i);
+ return true;
+}
+
void Plugin::sendHubCommand(HubDataPtr hHub, const string& cmd) {
if(hHub)
Hubs::handle()->send_protocol_cmd(hHub, cmd.c_str());
@@ -292,26 +325,30 @@
bool Plugin::onOwnChatOut(HubDataPtr hHub, char* message) {
Lock l(cs);
// TODO DC++ may trigger this incorrectly for disconnected hubs...
- return MakeCall("dcpp", "OnCommandEnter", 1, hHub, string(message)) ? static_cast<bool>(GetLuaBool()) : false;
+ if ((hHub = findHub(hHub->url)) != nullptr)
+ return MakeCall("dcpp", "OnCommandEnter", 1, hHub, string(message)) ? static_cast<bool>(GetLuaBool()) : false;
+ return false;
}
bool Plugin::onHubConnected(HubDataPtr hHub) {
- MakeCall(GetHubType(hHub), "OnHubAdded", 0, hHub);
+ MakeCall(GetHubType(hHub), "OnHubAdded", 0, addHub(hHub));
return false;
}
bool Plugin::onHubDisconnected(HubDataPtr hHub) {
// TODO DC++ may trigger this incorrectly (for hubs where OnHubAdded was never invoked), if socket creation fails...
- MakeCall(GetHubType(hHub), "OnHubRemoved", 0, hHub);
- removeChatCache(hHub);
+ if ((hHub = findHub(hHub->url)) != nullptr) {
+ MakeCall(GetHubType(hHub), "OnHubRemoved", 0, hHub);
+ removeHub(hHub);
+ }
return false;
}
bool Plugin::onHubDataIn(HubDataPtr hHub, char* message) {
Lock l(cs);
- return MakeCall(GetHubType(hHub), "DataArrival", 1, hHub, string(message)) ? static_cast<bool>(GetLuaBool()) : false;
+ return MakeCall(GetHubType(hHub), "DataArrival", 1, findHub(hHub->url), string(message)) ? static_cast<bool>(GetLuaBool()) : false;
}
bool Plugin::onHubDataOut(HubDataPtr hHub, char* message, bool& bBreak) {
@@ -352,7 +389,7 @@
bool Plugin::onFormatChat(UserDataPtr hUser, StringDataPtr line, bool& bBreak) {
Lock l(cs);
- if(!hUser || !Config::getBoolConfig("FormatChat") || !MakeCall("dcpp", "FormatChatHTML", 1, hUser, string(line->in)))
+ if(!hUser || !Config::getBoolConfig("FormatChat") || !MakeCall("dcpp", "FormatChatHTML", 1, findHub(hUser->hubHint), string(line->in)))
return False;
if(lua_isstring(L, -1)) {
=== modified file 'src/Plugin.h'
--- src/Plugin.h 2013-06-03 17:52:10 +0000
+++ src/Plugin.h 2013-06-13 21:33:08 +0000
@@ -22,11 +22,9 @@
#include "ScriptInstance.h"
#include <map>
-#include <set>
using std::string;
using std::map;
-using std::set;
class Plugin : public ScriptInstance
{
@@ -38,6 +36,11 @@
HubDataPtr createHub(const char* url, const char* nick, const char* password);
void destroyHub(HubDataPtr hHub);
+ // @todo: support clones?
+ HubDataPtr addHub(HubDataPtr hHub);
+ HubDataPtr findHub(const string& url);
+ bool removeHub(HubDataPtr hHub);
+
void sendHubCommand(HubDataPtr hHub, const string& cmd);
void injectHubCommand(HubDataPtr hHub, const string& cmd);
@@ -64,14 +67,8 @@
bool onFormatChat(UserDataPtr hUser, StringDataPtr line, bool& bBreak);
bool onTimer();
- void removeChatCache(const HubDataPtr hub) {
- auto j = chatCache.find(hub->url);
- if(j != chatCache.end())
- chatCache.erase(j);
- }
-
map<string, string> chatCache;
- set<HubDataPtr> hubs;
+ map<string, HubDataPtr> hubs;
static Plugin* instance;
};