Skip to content

Commit

Permalink
All hardware classes based on ASyncTCP, are now able to also use a ho…
Browse files Browse the repository at this point in the history
…stname instead of IP Address only
  • Loading branch information
gizmocuz committed Aug 7, 2016
1 parent 546fc17 commit cfd6e6b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 72 deletions.
27 changes: 26 additions & 1 deletion hardware/ASyncTCP.cpp
Expand Up @@ -2,6 +2,10 @@
#include "ASyncTCP.h"
#include "../main/Logger.h"

#ifndef WIN32
#include <unistd.h> //gethostbyname
#endif

/*
#ifdef WIN32
#include <Mstcpip.h>
Expand Down Expand Up @@ -37,7 +41,28 @@ void ASyncTCP::connect(const std::string &ip, unsigned short port)
// connect socket
try
{
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(ip), port);
std::string fip = ip;

unsigned long ipn = inet_addr(fip.c_str());
// if we have a error in the ip, it means we have entered a string
if (ipn == INADDR_NONE)
{
// change Hostname in Server Address
hostent *he = gethostbyname(fip.c_str());
if (he != NULL)
{
char szIP[20];
sprintf(szIP, "%d.%d.%d.%d", (uint8_t)he->h_addr_list[0][0], (uint8_t)he->h_addr_list[0][1], (uint8_t)he->h_addr_list[0][2], (uint8_t)he->h_addr_list[0][3]);
fip = szIP;
}
else
{
//we will fail
_log.Log(LOG_ERROR, "TCP: Unable to resolve '%s'", fip.c_str());
}
}

boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(fip), port);

connect(endpoint);
}
Expand Down
54 changes: 9 additions & 45 deletions hardware/MochadTCP.cpp
Expand Up @@ -53,8 +53,8 @@ MochadTCP::MochadTCP(const int ID, const std::string &IPAddress, const unsigned
m_szIPAddress(IPAddress)
{
m_HwdID=ID;
m_socket=INVALID_SOCKET;
m_stoprequested=false;
m_bDoRestart = false;
m_usIPPort=usIPPort;
m_linecount=0;
m_exclmarkfound=0;
Expand Down Expand Up @@ -93,32 +93,7 @@ MochadTCP::~MochadTCP(void)
bool MochadTCP::StartHardware()
{
m_stoprequested=false;

memset(&m_addr,0,sizeof(sockaddr_in));
m_addr.sin_family = AF_INET;
m_addr.sin_port = htons(m_usIPPort);

unsigned long ip;
ip=inet_addr(m_szIPAddress.c_str());

// if we have a error in the ip, it means we have entered a string
if(ip!=INADDR_NONE)
{
m_addr.sin_addr.s_addr=ip;
}
else
{
// change Hostname in serveraddr
hostent *he=gethostbyname(m_szIPAddress.c_str());
if(he==NULL)
{
return false;
}
else
{
memcpy(&(m_addr.sin_addr),he->h_addr_list[0],4);
}
}
m_bDoRestart = false;

//force connect the next first time
// m_retrycntr=RETRY_DELAY;
Expand Down Expand Up @@ -150,39 +125,23 @@ void MochadTCP::OnConnect()
{
_log.Log(LOG_STATUS, "Mochad: connected to: %s:%ld", m_szIPAddress.c_str(), m_usIPPort);
m_bIsStarted = true;
m_bDoRestart = false;

sOnConnected(this);
}

void MochadTCP::OnDisconnect()
{
_log.Log(LOG_STATUS, "Mochad: disconnected");
m_bDoRestart = true;
}



bool MochadTCP::ConnectInternal()
{

// connect to the server
connect((const std::string &)m_addr, sizeof(m_addr));
_log.Log(LOG_STATUS,"Mochad: connected to: %s:%ld", m_szIPAddress.c_str(), m_usIPPort);

// Init();

sOnConnected(this);
return true;
}


void MochadTCP::OnData(const unsigned char *pData, size_t length)
{
boost::lock_guard<boost::mutex> l(readQueueMutex);
ParseData(pData, length);
}



void MochadTCP::Do_Work()
{
bool bFirstTime = true;
Expand All @@ -209,6 +168,11 @@ void MochadTCP::Do_Work()
}
else
{
if ((m_bDoRestart) && (ltime.tm_sec % 30 == 0))
{
_log.Log(LOG_STATUS, "Mochad: trying to connect to %s:%d", m_szIPAddress.c_str(), m_usIPPort);
connect(m_szIPAddress, m_usIPPort);
}
sleep_milliseconds(40);
update();
}
Expand Down
5 changes: 1 addition & 4 deletions hardware/MochadTCP.h
Expand Up @@ -34,12 +34,9 @@ class MochadTCP: public CDomoticzHardwareBase, ASyncTCP
void setSecID(unsigned char *p);

void Do_Work();
bool ConnectInternal();
boost::shared_ptr<boost::thread> m_thread;
volatile bool m_stoprequested;
sockaddr_in m_addr;
int m_socket;

bool m_bDoRestart;
int selected[17][17];
int currentHouse;
int currentUnit;
Expand Down
20 changes: 10 additions & 10 deletions msbuild/domoticz.vcxproj.filters
Expand Up @@ -33,9 +33,6 @@
<Filter Include="Windows">
<UniqueIdentifier>{b2fc9567-e487-4cee-b23e-9f6002b503b9}</UniqueIdentifier>
</Filter>
<Filter Include="Devices\P1Smart Meter">
<UniqueIdentifier>{44ff1baf-dce6-4a20-b295-313ef10e0509}</UniqueIdentifier>
</Filter>
<Filter Include="SMTP">
<UniqueIdentifier>{acc2dd9a-c3ba-4abc-9dd5-a6871fd4d465}</UniqueIdentifier>
</Filter>
Expand Down Expand Up @@ -435,6 +432,9 @@
<Filter Include="Devices\OpenWeatherMap">
<UniqueIdentifier>{db1e850c-e0ec-47fc-84cd-6528ce21f0aa}</UniqueIdentifier>
</Filter>
<Filter Include="Devices\P1 Smart Meter">
<UniqueIdentifier>{44ff1baf-dce6-4a20-b295-313ef10e0509}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="..\CMakeLists.txt">
Expand Down Expand Up @@ -1328,7 +1328,7 @@
<Filter>Devices\Domoticz</Filter>
</ClInclude>
<ClInclude Include="..\hardware\P1MeterBase.h">
<Filter>Devices\P1Smart Meter</Filter>
<Filter>Devices\P1 Smart Meter</Filter>
</ClInclude>
<ClInclude Include="..\hardware\YouLess.h">
<Filter>Devices\YouLess</Filter>
Expand Down Expand Up @@ -1742,10 +1742,10 @@
<Filter>Devices\RFXCom</Filter>
</ClInclude>
<ClInclude Include="..\hardware\P1MeterSerial.h">
<Filter>Devices\P1Smart Meter</Filter>
<Filter>Devices\P1 Smart Meter</Filter>
</ClInclude>
<ClInclude Include="..\hardware\P1MeterTCP.h">
<Filter>Devices\P1Smart Meter</Filter>
<Filter>Devices\P1 Smart Meter</Filter>
</ClInclude>
<ClInclude Include="..\webserver\GZipHelper.h">
<Filter>Webserver</Filter>
Expand Down Expand Up @@ -1939,7 +1939,7 @@
<Filter>Devices\Domoticz</Filter>
</ClCompile>
<ClCompile Include="..\hardware\P1MeterBase.cpp">
<Filter>Devices\P1Smart Meter</Filter>
<Filter>Devices\P1 Smart Meter</Filter>
</ClCompile>
<ClCompile Include="..\hardware\YouLess.cpp">
<Filter>Devices\YouLess</Filter>
Expand Down Expand Up @@ -2284,10 +2284,10 @@
<Filter>Devices\RFXCom</Filter>
</ClCompile>
<ClCompile Include="..\hardware\P1MeterSerial.cpp">
<Filter>Devices\P1Smart Meter</Filter>
<Filter>Devices\P1 Smart Meter</Filter>
</ClCompile>
<ClCompile Include="..\hardware\P1MeterTCP.cpp">
<Filter>Devices\P1Smart Meter</Filter>
<Filter>Devices\P1 Smart Meter</Filter>
</ClCompile>
<ClCompile Include="..\hardware\Kodi.cpp">
<Filter>Devices\Kodi</Filter>
Expand Down Expand Up @@ -3648,4 +3648,4 @@
<Filter>www\secpanel\media</Filter>
</Media>
</ItemGroup>
</Project>
</Project>
4 changes: 0 additions & 4 deletions push/GooglePubSubPush.cpp
Expand Up @@ -12,10 +12,6 @@
#include "../webserver/Base64.h"
#include "../webserver/cWebem.h"

#ifndef WIN32
#include <unistd.h> //gethostbyname
#endif

#ifdef ENABLE_PYTHON
extern "C" {
#include <Python.h>
Expand Down
4 changes: 0 additions & 4 deletions push/HttpPush.cpp
Expand Up @@ -13,10 +13,6 @@
#include "../main/mainworker.h"
#include "../json/json.h"

#ifndef WIN32
#include <unistd.h> //gethostbyname
#endif

CHttpPush::CHttpPush()
{
m_bLinkActive = false;
Expand Down
4 changes: 0 additions & 4 deletions webserver/cWebem.cpp
Expand Up @@ -24,10 +24,6 @@
#include "../main/localtime_r.h"
#include "../main/Logger.h"

#ifndef WIN32
#include <unistd.h> //gethostbyname
#endif

//10 minutes
#define SESSION_TIMEOUT 600

Expand Down

0 comments on commit cfd6e6b

Please sign in to comment.