Skip to content

Commit 92ac09e

Browse files
authored
Merge pull request #24 from domoticz/master
merge
2 parents a77d65b + 278c1b9 commit 92ac09e

File tree

9 files changed

+72
-44
lines changed

9 files changed

+72
-44
lines changed

hardware/Kodi.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,12 @@ void CKodiNode::handleMessage(std::string& pMessage)
400400
bCanShutdown = root["result"]["canshutdown"].asBool();
401401
if (bCanShutdown) sAction = "Shutdown";
402402
}
403-
if (root["result"].isMember("canhibernate"))
403+
if (root["result"].isMember("canhibernate") && sAction != "Nothing")
404404
{
405405
bCanHibernate = root["result"]["canhibernate"].asBool();
406406
if (bCanHibernate) sAction = "Hibernate";
407407
}
408-
if (root["result"].isMember("cansuspend"))
408+
if (root["result"].isMember("cansuspend")&& sAction != "Nothing")
409409
{
410410
bCanSuspend = root["result"]["cansuspend"].asBool();
411411
if (bCanSuspend) sAction = "Suspend";

main/EventSystem.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,8 @@ void CEventSystem::EvaluatePython(const std::string &reason, const std::string &
22372237
else
22382238
_log.Log(LOG_ERROR, "%s",formatted_str.c_str());
22392239
}
2240-
fclose(PythonScriptFile);
2240+
if (PythonScriptFile!=NULL)
2241+
fclose(PythonScriptFile);
22412242
//Py_Finalize();
22422243
}
22432244
#endif // ENABLE_PYTHON

main/WebServerHelper.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace http {
77

88
typedef std::vector<CWebServer*>::iterator server_iterator;
99
#ifndef NOCLOUD
10-
typedef std::vector<CProxyManager*>::iterator proxy_iterator;
10+
typedef std::vector<boost::shared_ptr<CProxyManager> >::iterator proxy_iterator;
1111
extern CProxySharedData sharedData;
1212
#endif
1313

@@ -70,8 +70,7 @@ namespace http {
7070

7171
#ifndef NOCLOUD
7272
for (proxy_iterator it = proxymanagerCollection.begin(); it != proxymanagerCollection.end(); ++it) {
73-
((CProxyManager*) *it)->Stop();
74-
if (*it) delete (*it);
73+
(*it)->Stop();
7574
}
7675
proxymanagerCollection.clear();
7776
#endif
@@ -82,14 +81,13 @@ namespace http {
8281
sharedData.StopTCPClients();
8382
for (proxy_iterator it = proxymanagerCollection.begin(); it != proxymanagerCollection.end(); ++it) {
8483
(*it)->Stop();
85-
delete (*it);
8684
}
8785

8886
// restart threads
8987
const unsigned int connections = GetNrMyDomoticzThreads();
90-
proxymanagerCollection.resize(connections);
88+
proxymanagerCollection.clear();
9189
for (unsigned int i = 0; i < connections; i++) {
92-
proxymanagerCollection[i] = new CProxyManager(our_serverpath, plainServer_->m_pWebEm, m_pDomServ);
90+
proxymanagerCollection.push_back(boost::shared_ptr<CProxyManager>(new CProxyManager(our_serverpath, plainServer_->m_pWebEm, m_pDomServ)));
9391
proxymanagerCollection[i]->Start(i == 0);
9492
}
9593
_log.Log(LOG_STATUS, "Proxymanager started.");

main/WebServerHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace http {
5656

5757
std::string our_serverpath;
5858
#ifndef NOCLOUD
59-
std::vector<CProxyManager*> proxymanagerCollection;
59+
std::vector<boost::shared_ptr<CProxyManager> > proxymanagerCollection;
6060
int GetNrMyDomoticzThreads();
6161

6262
#endif

scripts/templates/All.Python

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ for name, device in domoticz.devices.items():
7474
if device.is_off():
7575
domoticz.log("could turn it on in 3 seconds")
7676
#device.on(after=3)
77-
for name, value in user_variables:
77+
for name, value in user_variables.items():
7878
domoticz.log("var", name, "has value", value)

webserver/proxyclient.cpp

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace http {
2727
CProxySharedData sharedData;
2828

2929
CProxyClient::CProxyClient(boost::asio::io_service& io_service, boost::asio::ssl::context& context, http::server::cWebem *webEm)
30-
: _socket(io_service, context),
30+
: _context(context),
3131
_io_service(io_service),
3232
doStop(false),
3333
b_Connected(false),
@@ -49,6 +49,7 @@ namespace http {
4949
}
5050
m_pWebEm = webEm;
5151
m_pDomServ = NULL;
52+
_socket.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(_io_service, _context));
5253
}
5354

5455
void CProxyClient::WriteSlaveData(const std::string &token, const char *pData, size_t Length)
@@ -75,10 +76,7 @@ namespace http {
7576

7677
void CProxyClient::Reconnect()
7778
{
78-
79-
std::string address = "my.domoticz.com";
80-
std::string port = "9999";
81-
79+
b_Connected = false;
8280
if (we_locked_prefs_mutex) {
8381
// avoid deadlock if we got a read or write error in between handle_handshake() and HandleAuthresp()
8482
we_locked_prefs_mutex = false;
@@ -87,41 +85,57 @@ namespace http {
8785
if (doStop) {
8886
return;
8987
}
90-
if (b_Connected) {
91-
_socket.lowest_layer().close();
88+
if (_socket->lowest_layer().is_open()) {
89+
boost::system::error_code ec;
90+
_socket->lowest_layer().cancel(ec);
91+
_socket->lowest_layer().shutdown(boost::asio::socket_base::shutdown_both, ec);
92+
_socket->lowest_layer().close(ec);
9293
}
93-
sleep_seconds(15);
94-
b_Connected = false;
94+
timer_.expires_from_now(boost::posix_time::seconds(15));
95+
timer_.async_wait(boost::bind(&CProxyClient::ContinueConnect, shared_from_this(), boost::asio::placeholders::error));
96+
}
97+
98+
void CProxyClient::ContinueConnect(const boost::system::error_code& error)
99+
{
100+
std::string address = "my.domoticz.com";
101+
std::string port = "9999";
102+
103+
_socket.reset(new boost::asio::ssl::stream<boost::asio::ip::tcp::socket>(_io_service, _context));
104+
// set timeout timer
105+
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
106+
timer_.async_wait(boost::bind(&CProxyClient::handle_timeout, shared_from_this(), boost::asio::placeholders::error));
95107
boost::asio::ip::tcp::resolver resolver(_io_service);
96108
boost::asio::ip::tcp::resolver::query query(address, port);
97109
boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
98110
boost::asio::ip::tcp::endpoint endpoint = *iterator;
99-
_socket.lowest_layer().async_connect(endpoint,
111+
_socket->lowest_layer().async_connect(endpoint,
100112
boost::bind(&CProxyClient::handle_connect, shared_from_this(),
101113
boost::asio::placeholders::error, iterator));
102114
}
103115

104116
void CProxyClient::handle_connect(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
105117
{
118+
if (doStop) {
119+
return;
120+
}
106121
if (!error)
107122
{
108-
_socket.async_handshake(boost::asio::ssl::stream_base::client,
123+
_socket->async_handshake(boost::asio::ssl::stream_base::client,
109124
boost::bind(&CProxyClient::handle_handshake, shared_from_this(),
110125
boost::asio::placeholders::error));
111126
}
112-
else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator())
127+
else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator())
113128
{
114-
_socket.lowest_layer().close();
129+
_socket->lowest_layer().close();
115130
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
116-
_socket.lowest_layer().async_connect(endpoint,
131+
_socket->lowest_layer().async_connect(endpoint,
117132
boost::bind(&CProxyClient::handle_connect, shared_from_this(),
118133
boost::asio::placeholders::error, ++endpoint_iterator));
119134
}
120135
else
121136
{
122137
if (!doStop) {
123138
_log.Log(LOG_ERROR, "PROXY: Connect failed, reconnecting: %s", error.message().c_str());
124-
b_Connected = true; // signal re-connect
125139
Reconnect();
126140
}
127141
}
@@ -131,12 +145,15 @@ namespace http {
131145
{
132146
if (error != boost::asio::error::operation_aborted) {
133147
_log.Log(LOG_ERROR, "PROXY: timeout occurred, reconnecting");
134-
_socket.lowest_layer().close(); // should induce a reconnect in handle_read with error != 0
148+
Reconnect();
135149
}
136150
}
137151

138152
void CProxyClient::handle_write(const boost::system::error_code& error, size_t bytes_transferred)
139153
{
154+
if (doStop) {
155+
return;
156+
}
140157
boost::unique_lock<boost::mutex>(writeMutex);
141158
if (bytes_transferred < writePdu->length()) {
142159
_log.Log(LOG_ERROR, "PROXY: Only wrote %ld of %ld bytes.", bytes_transferred, writePdu->length());
@@ -158,7 +175,7 @@ namespace http {
158175
writePdu = pdu;
159176
_writebuf.clear(); // make sure
160177
_writebuf.push_back(boost::asio::buffer(writePdu->content(), writePdu->length()));
161-
boost::asio::async_write(_socket, _writebuf, boost::bind(&CProxyClient::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
178+
boost::asio::async_write(*_socket, _writebuf, boost::bind(&CProxyClient::handle_write, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
162179
}
163180

164181
void CProxyClient::MyWrite(pdu_type type, CValueLengthPart &parameters)
@@ -197,6 +214,9 @@ namespace http {
197214

198215
void CProxyClient::handle_handshake(const boost::system::error_code& error)
199216
{
217+
if (doStop) {
218+
return;
219+
}
200220
if (!error)
201221
{
202222
// lock until we have a valid api id
@@ -209,10 +229,8 @@ namespace http {
209229
}
210230
else
211231
{
212-
if (!doStop) {
213-
_log.Log(LOG_ERROR, "PROXY: Handshake failed, reconnecting: %s", error.message().c_str());
214-
Reconnect();
215-
}
232+
_log.Log(LOG_ERROR, "PROXY: Handshake failed, reconnecting: %s", error.message().c_str());
233+
Reconnect();
216234
}
217235
}
218236

@@ -225,7 +243,7 @@ namespace http {
225243
timer_.expires_from_now(boost::posix_time::seconds(timeout_));
226244
timer_.async_wait(boost::bind(&CProxyClient::handle_timeout, shared_from_this(), boost::asio::placeholders::error));
227245

228-
_socket.async_read_some(buf,
246+
_socket->async_read_some(buf,
229247
boost::bind(&CProxyClient::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)
230248
);
231249
}
@@ -529,6 +547,9 @@ namespace http {
529547
void CProxyClient::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
530548
{
531549
// data read, no need for timeouts anymore
550+
if (!b_Connected || doStop) {
551+
return;
552+
}
532553
timer_.cancel();
533554
if (!error)
534555
{
@@ -560,14 +581,17 @@ namespace http {
560581

561582
void CProxyClient::Stop()
562583
{
584+
timer_.cancel(); // to be sure
563585
if (we_locked_prefs_mutex) {
564586
we_locked_prefs_mutex = false;
565587
sharedData.UnlockPrefsMutex();
566588
}
567589

568590
doStop = true;
569591
// signal end of WriteThread
570-
_socket.lowest_layer().close();
592+
boost::system::error_code ec;
593+
_socket->shutdown(ec);
594+
_socket->lowest_layer().close(ec);
571595
}
572596

573597
void CProxyClient::SetSharedServer(tcp::server::CTCPServerProxied *domserv)
@@ -581,6 +605,7 @@ namespace http {
581605

582606
CProxyManager::CProxyManager(const std::string& doc_root, http::server::cWebem *webEm, tcp::server::CTCPServer *domServ)
583607
{
608+
m_pDocRoot = doc_root;
584609
m_pWebEm = webEm;
585610
m_pDomServ = domServ;
586611
m_thread = NULL;
@@ -590,14 +615,14 @@ namespace http {
590615
CProxyManager::~CProxyManager()
591616
{
592617
if (m_thread) {
593-
m_thread->join();
618+
delete m_thread;
594619
}
595620
}
596621

597622
int CProxyManager::Start(bool first)
598623
{
599624
_first = first;
600-
m_thread = new boost::thread(boost::bind(&CProxyManager::StartThread, this));
625+
m_thread = new boost::thread(boost::bind(&CProxyManager::StartThread, shared_from_this()));
601626
return 1;
602627
}
603628

@@ -624,10 +649,11 @@ namespace http {
624649

625650
void CProxyManager::Stop()
626651
{
627-
proxyclient->Stop();
628-
io_service.stop();
629-
m_thread->interrupt();
630-
m_thread->join();
652+
if (m_thread) {
653+
io_service.post(boost::bind(&CProxyClient::Stop, proxyclient));
654+
delete m_thread;
655+
m_thread = NULL;
656+
}
631657
}
632658

633659
boost::shared_ptr<CProxyClient> CProxyManager::GetProxyForMaster(DomoticzTCP *master) {

webserver/proxyclient.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ namespace http {
3838
~CProxyClient();
3939

4040
void Reconnect();
41+
void ContinueConnect(const boost::system::error_code& error);
4142
void Stop();
4243
void WriteMasterData(const std::string &token, const char *pData, size_t Length);
4344
void WriteSlaveData(const std::string &token, const char *pData, size_t Length);
@@ -84,11 +85,12 @@ namespace http {
8485

8586
int _allowed_subsystems;
8687
std::string GetResponseHeaders(const http::server::reply &reply_);
87-
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> _socket;
88+
boost::shared_ptr<boost::asio::ssl::stream<boost::asio::ip::tcp::socket> > _socket;
8889
std::string _apikey;
8990
std::string _password;
9091
boost::asio::streambuf _readbuf;
9192
boost::asio::io_service& _io_service;
93+
boost::asio::ssl::context& _context;
9294
bool doStop;
9395
http::server::cWebem *m_pWebEm;
9496
tcp::server::CTCPServerProxied *m_pDomServ;
@@ -104,7 +106,7 @@ namespace http {
104106
std::queue<ProxyPdu *> writeQ;
105107
};
106108

107-
class CProxyManager {
109+
class CProxyManager : public boost::enable_shared_from_this<CProxyManager> {
108110
public:
109111
CProxyManager(const std::string& doc_root, http::server::cWebem *webEm, tcp::server::CTCPServer *domServ);
110112
~CProxyManager();
@@ -116,6 +118,7 @@ namespace http {
116118
boost::asio::io_service io_service;
117119
boost::shared_ptr<CProxyClient> proxyclient;
118120
boost::thread* m_thread;
121+
std::string m_pDocRoot;
119122
http::server::cWebem *m_pWebEm;
120123
tcp::server::CTCPServer *m_pDomServ;
121124
bool _first;

www/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,7 @@ <h1 id="theader" data-i18n="Usage this Year">Usage this Year</h1>
14811481
<li class="divider"></li>
14821482
<li ng-class="{'current_page_item':getClass('/Log')}" id="mLog"><a id="cLog" href="#Log"><img src="images/log.png"> <span data-i18n="Log">Log</span></a></li>
14831483
<li class="divider"></li>
1484-
<li ng-class="{'current_page_item':getClass('/About')}" id="mAbout"><a id="cLog" href="#About"><img src="images/about.png"> <span data-i18n="About">About</span></a></li>
1484+
<li ng-class="{'current_page_item':getClass('/About')}" id="mAbout"><a id="cAbout" href="#About"><img src="images/about.png"> <span data-i18n="About">About</span></a></li>
14851485
<li id="dLogoutSetup" class="divider" style="display: none;" has-Login=true></li>
14861486
<li id="mLogoutSetup" style="display: none;" has-Login=true><a id="cLogout" href="#Logout"><img src="images/logout.png"> <span data-i18n="Logout">Logout</span></a></li>
14871487
</ul>

www/zwavetopology.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
+ '<br>On node ' + getNodeName(node)), function(result) {
6767
if (result==true) {
6868
$.ajax({
69-
url: "json.htm?type=command&param=zwaveremovegroupnode&idx=" + hwid + "&node=" + node.nodeID + "&group=" + group.id + "&removeNode=" + removeNode,
69+
url: "json.htm?type=command&param=zwaveremovegroupnode&idx=" + hwid + "&node=" + node.nodeID + "&group=" + group.id + "&removenode=" + removeNodeId,
7070
async: false,
7171
dataType: 'json',
7272
success: function(data) {

0 commit comments

Comments
 (0)