14
14
You should have received a copy of the GNU General Public License
15
15
along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
#include <discovery/CBasicBroadcastServer.h>
19
CBasicBroadcastServer::CBasicBroadcastServer()
18
#include <qjson/serializer.h>
20
#include <discovery/CBasicBroadcaster.h>
21
#include <util/definitions.h>
22
#include <util/network.h>
23
#include <util/settings.h>
25
CBasicBroadcaster::CBasicBroadcaster()
27
connect(&m_timer, SIGNAL(timeout()), SLOT(OnTimer()));
29
/* Store the current time (that this broadcaster was started with)
30
to inform other machines of how long we have been running. */
31
m_uptime = QDateTime::currentMSecsSinceEpoch();
34
void CBasicBroadcaster::Init()
36
/* When this method is called without a network interface,
37
we simply use GetCurrentInterface to either load the user's
38
default interface or find one that is suitable for use. */
39
Init(Network::GetCurrentInterface());
42
void CBasicBroadcaster::Init(QNetworkInterface interface)
44
/* Stop the timer if it is currently running. */
47
/* Attempt to find an IPv4 broadcast and netmask address on the interface. */
48
if(!Network::FindIPv4Address(interface, &m_broadcast_address, &m_netmask_address))
50
emit Error(tr("Unable to find an IPv4 broadcast and netmask address for the current interface."));
54
/* Start the timer and send out an initial ping. */
55
m_timer.setInterval(Settings::Get("Network/BroadcastInterval").toInt());
61
bool CBasicBroadcaster::HasAddress(QHostAddress address)
63
return address.isInSubnet(m_broadcast_address, m_netmask_address.toIPv4Address());
66
void CBasicBroadcaster::OnTimer()
68
/* Construct the JSON map containing some information about our instance. */
70
info["version"] = Definitions::Version;
71
info["id"] = Settings::GetID();
72
info["name"] = Settings::Get("General/MachineName");
73
info["transmission_port"] = Settings::Get("Network/TransmissionPort");
74
info["uptimer"] = m_uptime;
76
/* Write the information to the broadcast port we discovered earlier. */
77
writeDatagram(QJson::Serializer().serialize(QVariant(info)),
78
m_broadcast_address, Settings::Get("Network/BroadcastPort").toUInt());