direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
networkThread.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "networkThread.h"
22 
24 {
25  stopped = false;
27  networkPortSend = 0;
28  iAmTheMaster = false;
29  iAmTheSlave = false;
30 
31  udpSocket = new QUdpSocket(this);
32 }
33 
34 
36 {
37  delete udpSocket;
38 }
39 
40 
42 {
43  stopped = true;
44 }
45 
46 
48 {
49  bool heartbeatToggle = false;
50 
51 
52  //
53  // start "threading"...
54  //
55  emit dataReceived(tr("Listening on port %1...").arg(networkPortListen));
56 
57  while (!stopped)
58  {
59 
60  // let the thread sleep some time (kind of timer, because we sleep for one second here)
61  msleep(THREADSLEEPTIME);
62 
63 
64  // send master signal when we are the network master
65  if (iAmTheMaster)
66  {
67  sendNetworkCommand("*master#");
68 
69  // send an optical heartbeat signal to the GUI
70  if (heartbeatToggle)
71  {
72  emit heartbeat(GREEN);
73  }
74  else
75  {
76  emit heartbeat(LEDOFF);
77  }
78 
79  heartbeatToggle = !heartbeatToggle;
80  }
81  }
82  stopped = false;
83 }
84 
85 
87 {
88  while (udpSocket->hasPendingDatagrams())
89  {
90  QByteArray datagram;
91  datagram.resize(udpSocket->pendingDatagramSize());
92  udpSocket->readDatagram(datagram.data(), datagram.size());
93 
94  //====================================================================
95  // e m i t Signal
96  //====================================================================
97  emit dataReceived(tr("%1").arg(datagram.data()));
98  }
99 }
100 
101 
103 {
104  if (networkPortSend > 0)
105  {
106  QByteArray datagram = text.toAscii();
107  udpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, networkPortSend);
108  }
109  else
110  {
111  emit dataReceived(tr("ERROR sending network command: Network 'send' port not set! Port is still %1...").arg(networkPortSend));
112  }
113 }
114 
115 
117 {
118  // first disconnect the signal!
119  udpSocket->disconnect(SIGNAL(readyRead()));
120  udpSocket->close();
121 
122  // swap ports
123  unsigned int temp = networkPortListen;
125  networkPortSend = temp;
126 
127 
128  if (init(networkPortListen, networkPortSend) == false)
129  {
130  qDebug("ERROR swapping network ports.");
131  return;
132  }
133 }
134 
135 
136 bool NetworkThread::init(unsigned int portListen, unsigned int portSend)
137 {
138  // store ports locally
139  networkPortListen = portListen;
140  networkPortSend = portSend;
141 
142  // bind the socket to the given port for listening
143  if (udpSocket->bind(networkPortListen) == true)
144  {
145  // do something with the network received data, when completely received
146  connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
147  return true;
148  }
149 
150  // error
151  return false;
152 }
153 
154 
156 {
157  // we are master now
158  // from now on we will send this over network, that we we are master
159  iAmTheMaster = true;
160  iAmTheSlave = false;
161 }
162 
163 
165 {
166  // we are slave now
167  // from now on we will receive data over the network
168  iAmTheSlave = true;
169  iAmTheMaster = false;
170 }