]> git.tdb.fi Git - xinema.git/blobdiff - remote/source/discovery.cpp
Add a remote control program for Sailfish OS
[xinema.git] / remote / source / discovery.cpp
diff --git a/remote/source/discovery.cpp b/remote/source/discovery.cpp
new file mode 100644 (file)
index 0000000..b4f0662
--- /dev/null
@@ -0,0 +1,37 @@
+#include "discovery.h"
+
+Discovery::Discovery():
+       broadcast_addr("ff02::1"),
+       port(34588)
+{
+       socket.bind(QHostAddress::AnyIPv6);
+       connect(&socket, &QIODevice::readyRead, this, &Discovery::datagram_available);
+
+       connect(&timer, &QTimer::timeout, this, &Discovery::send_beacon);
+       timer.setInterval(5000);
+}
+
+void Discovery::start()
+{
+       timer.start();
+       send_beacon();
+}
+
+void Discovery::stop()
+{
+       timer.stop();
+}
+
+void Discovery::send_beacon()
+{
+       socket.writeDatagram("xinema", 6, broadcast_addr, port);
+}
+
+void Discovery::datagram_available()
+{
+       char rbuf[1024];
+       QHostAddress peer_addr;
+       socket.readDatagram(rbuf, sizeof(rbuf), &peer_addr, 0);
+       
+       emit server_discovered(peer_addr);
+}