mastermanager.cpp
1 //------------------------------------------------------------------------------
2 // mastermanager.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) 2009 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include <cassert>
24 
25 #include "mastermanager.h"
26 
27 #include "serverapi/message.h"
28 #include "serverapi/server.h"
29 #include "customservers.h"
30 
31 // TODO: I don't think that MasterManager should store a duplicate of each
32 // server (~Zalewa).
33 
34 MasterManager::MasterManager() : MasterClient()
35 {
36  customServers = new CustomServers();
37 }
38 
39 MasterManager::~MasterManager()
40 {
41  clearServers();
42 
43  for(int i = 0;i < masters.size();i++)
44  delete masters[i];
45 
46  delete customServers;
47 }
48 
49 void MasterManager::addMaster(MasterClient *master)
50 {
51  if(master == NULL)
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  QList<ServerPtr> result;
67  foreach (MasterClient *master, masters)
68  {
69  result << master->servers();
70  }
71  result << customServers->servers();
72  return result;
73 }
74 
75 void MasterManager::masterListUpdated()
76 {
77  MasterClient *master = static_cast<MasterClient*>(sender());
78  foreach(ServerPtr pServer, master->servers())
79  {
80  registerNewServer(pServer);
81  }
82 
83  emit listUpdatedForMaster(master);
84  mastersBeingRefreshed.remove(master);
85  if (mastersBeingRefreshed.isEmpty())
86  {
87  emit listUpdated();
88  }
89 }
90 
91 MasterManager::Response MasterManager::readMasterResponse(const QByteArray &data)
92 {
93  assert(0 && "MasterManager::readMasterResponse should not get called.");
94  return RESPONSE_BAD;
95 }
96 
97 void MasterManager::refreshStarts()
98 {
99  setTimeouted(false);
100 
101  clearServers();
102 
103  for(int i = 0;i < masters.size();i++)
104  {
105  if(!masters[i]->isEnabled())
106  {
107  continue;
108  }
109 
110  mastersBeingRefreshed.insert(masters[i]);
111  masters[i]->refreshStarts();
112  }
113 }
114 
115 void MasterManager::timeoutRefreshEx()
116 {
117  foreach(MasterClient* pMaster, mastersBeingRefreshed)
118  {
119  pMaster->timeoutRefresh();
120  }
121 
122  mastersBeingRefreshed.clear();
123 }
Message object used to pass messages throughout the Doomseeker's system.
Definition: message.h:62
void registerNewServer(ServerPtr server)
Registers new server with this MasterClient.
void message(const QString &title, const QString &content, bool isError)
void setEnabled(bool b)
void messageImportant(const Message &message)
Signal used to forward important message to Doomseeker.
void timeoutRefresh()
Times the refreshing process out.
void listUpdatedForMaster(MasterClient *pSender)
Emitted for every MasterClient that emits listUpdated() signal.
Abstract base for all MasterClients.
Definition: masterclient.h:49
bool isEnabled() const