canrefreshserver.cpp
1 //------------------------------------------------------------------------------
2 // canrefreshserver.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "canrefreshserver.h"
24 
25 #include "plugins/engineplugin.h"
26 #include "serverapi/server.h"
27 
28 DClass<CanRefreshServer>
29 {
30 public:
31  const Server *server;
32 
33  const EnginePlugin *plugin() const
34  {
35  return server->plugin();
36  }
37 
38  const EnginePlugin::Data *pluginData() const
39  {
40  return plugin()->data();
41  }
42 
43  qint64 msSinceLastRefresh() const
44  {
45  return server->timeMsSinceLastRefresh();
46  }
47 
48  qint64 secsSinceLastRefresh() const
49  {
50  return msSinceLastRefresh() / 1000;
51  }
52 
53  bool wasAlreadyRefreshed() const
54  {
55  return msSinceLastRefresh() >= 0;
56  }
57 };
58 
59 DPointered(CanRefreshServer)
60 
62 {
63  d->server = server;
64 }
65 
66 CanRefreshServer::~CanRefreshServer()
67 {
68 }
69 
70 bool CanRefreshServer::hasEnoughTimeSinceLastRefreshPassed() const
71 {
72  if (!d->wasAlreadyRefreshed())
73  {
74  return true;
75  }
76 
77  if (d->server->lastResponse() == Server::RESPONSE_TIMEOUT)
78  {
79  return d->secsSinceLastRefresh() >= 1;
80  }
81  else
82  {
83  return d->secsSinceLastRefresh() >= d->pluginData()->refreshThreshold;
84  }
85 }
86 
87 bool CanRefreshServer::shouldRefresh() const
88 {
89  return hasEnoughTimeSinceLastRefreshPassed();
90 }