mastermanager.cpp
1 //------------------------------------------------------------------------------
2 // mastermanager.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include <cassert>
24 
25 #include "mastermanager.h"
26 
27 #include "customservers.h"
28 #include "serverapi/broadcastmanager.h"
29 #include "serverapi/message.h"
30 #include "serverapi/server.h"
31 
32 // TODO: I don't think that MasterManager should store a duplicate of each
33 // server (~Zalewa).
34 
35 MasterManager::MasterManager() : MasterClient()
36 {
37  customServers = new CustomServers();
38 }
39 
40 MasterManager::~MasterManager()
41 {
42  clearServers();
43 
44  qDeleteAll(masters);
45 
46  delete customServers;
47 }
48 
49 void MasterManager::addMaster(MasterClient *master)
50 {
51  if (master == nullptr)
52  return;
53 
54  masters.append(master);
55  master->setEnabled(true);
56 
57  this->connect(master, SIGNAL(listUpdated()), SLOT(masterListUpdated()));
58  this->connect(master, SIGNAL(message(QString,QString,bool)),
59  SLOT(forwardMasterMessage(QString,QString,bool)));
60  this->connect(master, SIGNAL(messageImportant(Message)),
61  SLOT(forwardMasterMessageImportant(Message)));
62 }
63 
64 QList<ServerPtr> MasterManager::allServers() const
65 {
66  QSet<ServerPtr> result;
67  for (MasterClient *master : masters)
68  {
69  for (ServerPtr server : master->servers())
70  result.insert(server);
71  }
72  for (ServerPtr server : customServers->servers())
73  result.insert(server);
74  if (broadcastManager != nullptr)
75  {
76  for (ServerPtr server : broadcastManager->servers())
77  result.insert(server);
78  }
79  return result.toList();
80 }
81 
82 void MasterManager::masterListUpdated()
83 {
84  auto master = static_cast<MasterClient *>(sender());
85  for (ServerPtr pServer : master->servers())
86  {
87  registerNewServer(pServer);
88  }
89 
90  emit listUpdatedForMaster(master);
91  mastersBeingRefreshed.remove(master);
92  if (mastersBeingRefreshed.isEmpty())
93  {
94  emit listUpdated();
95  }
96 }
97 
98 MasterManager::Response MasterManager::readMasterResponse(const QByteArray &data)
99 {
100  Q_UNUSED(data);
101  assert(0 && "MasterManager::readMasterResponse should not get called.");
102  return RESPONSE_BAD;
103 }
104 
105 void MasterManager::refreshStarts()
106 {
107  setTimeouted(false);
108 
109  clearServers();
110 
111  for (auto *master : masters)
112  {
113  if (!master->isEnabled())
114  {
115  continue;
116  }
117 
118  mastersBeingRefreshed.insert(master);
119  master->refreshStarts();
120  }
121 }
122 
123 void MasterManager::timeoutRefreshEx()
124 {
125  for (MasterClient *pMaster : mastersBeingRefreshed)
126  {
127  pMaster->timeoutRefresh();
128  }
129 
130  mastersBeingRefreshed.clear();
131 }
132 
133 void MasterManager::setBroadcastManager(BroadcastManager *broadcastManagerPtr)
134 {
135  broadcastManager = broadcastManagerPtr;
136 }
void clearServers()
Message object used to pass messages throughout the Doomseeker&#39;s system.
Definition: message.h:63
void registerNewServer(ServerPtr server)
Registers new server with this MasterClient.
void message(const QString &title, const QString &content, bool isError)
void setEnabled(bool b)
virtual void refreshStarts()
void messageImportant(const Message &message)
Signal used to forward important message to Doomseeker.
Abstract base for all MasterClients.
Definition: masterclient.h:49
bool isEnabled() const