VPTissue Reference Manual
NodeAdvertiser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "NodeAdvertiser.h"
21 
22 #include <QTimer>
23 #include <QUdpSocket>
24 
25 #include <iostream>
26 
27 using namespace std;
28 
29 namespace SimPT_Parex {
30 
31 const int NodeAdvertiser::g_initial_interval = 1000;
32 const int NodeAdvertiser::g_maximal_interval = 30 * 1000;
33 
34 NodeAdvertiser::NodeAdvertiser(int port, bool verbose, QObject *parent)
35  : QObject(parent), m_port(port), m_verbose(verbose),
36  m_resend_timer(new QTimer(this)), m_socket(new QUdpSocket(this))
37 {
38  connect(m_resend_timer, SIGNAL(timeout()), this, SLOT(SendAdvertisement()));
39 }
40 
42 {
43  m_socket->close();
44 }
45 
46 void NodeAdvertiser::BackoffTimer()
47 {
48  if (m_resend_timer->interval() < g_maximal_interval) {
49  int newInterval = m_resend_timer->interval() * 2;
50  if (newInterval >= g_maximal_interval) {
51  //when we are at the max interval, switch from multiple single-shot timers to one continuous timer
52  newInterval = g_maximal_interval;
53  m_resend_timer->setSingleShot(false);
54  }
55  m_resend_timer->start(newInterval);
56  }
57 }
58 
59 void NodeAdvertiser::SendAdvertisement()
60 {
61  if (m_verbose) {
62  cout << "Trying to find server, time passed since last tick: "
63  << m_resend_timer->interval() << "msec." << endl;
64  }
65  QByteArray datagram;
66  if (m_exploration == "") {
67  datagram = "Job Application" + QByteArray(" ") + QByteArray::number(m_server_port);
68  } else {
69  datagram = "Server Discovery" + QByteArray(" ")
70  + QByteArray::number(m_task_id) + QByteArray(" ")
71  + QByteArray::number(m_server_port) + QByteArray(" ")
72  + QByteArray(m_exploration.c_str());
73  }
74  m_socket->writeDatagram(datagram, QHostAddress::Broadcast, m_port);
75  BackoffTimer();
76 }
77 
78 void NodeAdvertiser::Start(int serverPort)
79 {
80  m_resend_timer->stop();
81  m_server_port = serverPort;
82  m_exploration = "";
83  m_resend_timer->setSingleShot(true);
84  m_resend_timer->start(g_initial_interval);
85 }
86 
87 void NodeAdvertiser::Start(int serverPort, const std::string &exploration, int taskId)
88 {
89  m_resend_timer->stop();
90  m_server_port = serverPort;
91  m_exploration = exploration;
92  m_task_id = taskId;
93  m_resend_timer->setSingleShot(true);
94  m_resend_timer->start(g_initial_interval);
95 }
96 
98 {
99  m_resend_timer->stop();
100 }
101 
102 } // namespace
Interface for the NodeAdvertiser.
STL namespace.
void Start(int serverPort)
Start finding a server to do work for.
void Stop()
Stop finding a server.
virtual ~NodeAdvertiser()
Destructor.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
see the online Qt documentation