broadcastmanager.cpp
1 //------------------------------------------------------------------------------
2 // broadcastmanager.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2015 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "broadcastmanager.h"
24 
25 #include "plugins/engineplugin.h"
26 #include "refresher/refresher.h"
27 #include "serverapi/broadcast.h"
28 #include "serverapi/server.h"
29 #include "log.h"
30 #include <QSet>
31 
32 DClass<BroadcastManager>
33 {
34 public:
35  QSet<ServerPtr> servers;
36 };
37 DPointered(BroadcastManager)
38 
39 BroadcastManager::BroadcastManager(QObject *parent)
40 : QObject(parent)
41 {
42 }
43 
44 BroadcastManager::~BroadcastManager()
45 {
46 }
47 
48 void BroadcastManager::forgetServer(ServerPtr server)
49 {
50  gLog << tr("%1 LAN server gone: %2, %3:%4").arg(server->plugin()->data()->name)
51  .arg(server->name()).arg(server->address().toString())
52  .arg(server->port());
53  d->servers.remove(server);
54  emit serverLost(server);
55 }
56 
57 void BroadcastManager::registerServer(ServerPtr server, bool needsRefresh)
58 {
59  if (!d->servers.contains(server))
60  {
61  server->setLan(true);
62  d->servers.insert(server);
63  gLog << tr("New %1 LAN server detected: %2:%3").arg(server->plugin()->data()->name)
64  .arg(server->address().toString()).arg(server->port());
65  emit newServerDetected(server, server->lastResponse());
66  }
67  if (needsRefresh)
68  {
69  gRefresher->registerServer(server.data());
70  }
71 }
72 
73 void BroadcastManager::registerPlugin(const EnginePlugin *plugin)
74 {
75  this->connect(plugin->data()->broadcast,
76  SIGNAL(serverLost(ServerPtr)),
77  SLOT(forgetServer(ServerPtr)));
78  this->connect(plugin->data()->broadcast,
79  SIGNAL(serverDetected(ServerPtr, bool)),
80  SLOT(registerServer(ServerPtr, bool)));
81 }
82 
83 QList<ServerPtr> BroadcastManager::servers() const
84 {
85  return d->servers.toList();
86 }