-
Notifications
You must be signed in to change notification settings - Fork 2
/
statecontroller.cpp
219 lines (158 loc) · 6.79 KB
/
statecontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
VoIPApp- A simple p2p voip application written in C++ using the QT Framework.
Copyright (C) 2011 Sam Gunaratne University of Plymouth
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "statecontroller.h"
StateController::StateController(QObject *parent) :
QObject(parent)
{
//setup tcp listening for commands....
client = new CommandClient(this);
server = new CommandServer(this);
discover = new NetworkDiscover(this);
//setup two threads for sending and receiving audio data
recThread = new ReceiveThread(this);
sendThread = new SendThread(this);
//Incoming call
connect(server,SIGNAL(callInitiated(QHostAddress)),this,SLOT(receiveCall(QHostAddress)));
//Call terminated
connect(server,SIGNAL(callEnded()),this,SLOT(endCall()));
connect(client,SIGNAL(callEnded()),this,SLOT(endCall()));
//Initiated call busy.
connect(client,SIGNAL(callerAccepted()),this,SLOT(outCallAccepted()));
connect(client,SIGNAL(callerBusy()),this,SLOT(outCallRejected()));
connect(client,SIGNAL(callerMicMuted(bool)),this,SIGNAL(callerMicMuted(bool)));
connect(server,SIGNAL(callerMicMuted(bool)),this,SIGNAL(callerMicMuted(bool)));
connect(client,SIGNAL(callerSoundMuted(bool)),this,SIGNAL(callerSoundMuted(bool)));
connect(server,SIGNAL(callerSoundMuted(bool)),this,SIGNAL(callerSoundMuted(bool)));
connect(this,SIGNAL(muteSound(bool)),recThread,SIGNAL(muteSound(bool)));
connect(this,SIGNAL(muteMic(bool)),sendThread,SIGNAL(muteMic(bool)));
connect(this,SIGNAL(muteSound(bool)),this,SLOT(sendMuteSound(bool)));
connect(this,SIGNAL(muteMic(bool)),this,SLOT(sendMuteMic(bool)));
//Rebroadcast for the view.
connect(discover,SIGNAL(peersChanged(QList<Peer*>)),this,SIGNAL(updatePeerList(QList<Peer*>)));
//Start broadcast/listen for peer-to-peer nature
discover->startTimers();
//set initial state
state = StateController::Ready;
}
StateController::~StateController() {
delete recThread;
delete sendThread;
delete client;
delete server;
delete discover;
}
void StateController::callPeer(QString name) {
if(state == StateController::Ready) {
//Lets get this rolling! initiate call.
commPeer = discover->peerList.value(name);
client->connectToPeer(commPeer);
state = StateController::Calling;
emit newState(state);
}
else {
emit callError("You are already in a call. You must terminate before calling someone else");
}
}
void StateController::endCall() {
//Ended calls are processed at the controller level instead of directly at the
//model (ie. listen/send threads) just incase we want to update the view with anthing later...probably won't use this
if (state == InCall) {
//Find out which signal emitted. If the signal did not come from the network we need to tell
//our caller to end their call.
CommandClient* clientSender = dynamic_cast<CommandClient*>(sender());
CommandServer* serverSender = dynamic_cast<CommandServer*>(sender());
QApplication* app = dynamic_cast<QApplication*>(sender());
//Reqiures compiler rtti support!
if (clientSender == 0 && serverSender ==0) {
if (client->state() == QTcpSocket::ConnectedState)
client->hangUp();
else
server->sendCommand(*commPeer->getAddress(),CommandClient::EndCall);
}
//if qApp emitted us we do not want to end threads. This causes segmentation fault.
if(app == 0) {
if (recThread->isRunning()) {
recThread->quit();
}
if (sendThread->isRunning()) {
sendThread->quit();
}
}
state = StateController::Ready;
emit newState(state);
//TODO deal with incomingCaller here too, delete?
}
}
void StateController::receiveCall(const QHostAddress &address) {
//This could have been achieved by connecting the direct received calls to this acton, but we want to check
//the state and perhaps perform gui operations. Controller assumes responsability.
if (state == StateController::Ready || state == StateController::Calling) {
commPeer = discover->getPeerFromAddress(address);
if (!commPeer->getName().isNull()) {
emit callIncoming(commPeer->getName());
}
}
else {
rejectCall();
}
}
void StateController::acceptCall() {
//Inform caller that we accept the call.
server->sendCommand(*commPeer->getAddress(),CommandClient::CallAccepted);
//Begin send/listen.
sendThread->recordSound(*commPeer->getAddress());
recThread->listen();
state = StateController::InCall;
emit newState(state);
}
void StateController::rejectCall() {
//server reponses here?
server->sendCommand(*commPeer->getAddress(),CommandClient::Busy);
}
void StateController::outCallAccepted() {
if (state == StateController::Calling) {
sendThread->recordSound(*commPeer->getAddress());
recThread->listen();
state = StateController::InCall;
emit newState(state);
}
}
void StateController::outCallRejected() {
state = StateController::Ready;
emit newState(state);
emit callerBusy();
}
void StateController::sendMuteSound(bool toggle) {
if (state == StateController::InCall) {
CommandClient::CallCommand cmd;
cmd = toggle ? CommandClient::disableSound : CommandClient::enableSound;
//At the moment we check to see if we are client or server and respond
//down the correct avenue. Ideally we should only have to respond down one channel. Makes for bloated code.
if (client->state() == QTcpSocket::ConnectedState)
client->sendCommand(cmd);
else
server->sendCommand(*commPeer->getAddress(),cmd);
}
}
void StateController::sendMuteMic(bool toggle) {
if (state == StateController::InCall) {
CommandClient::CallCommand cmd;
cmd = toggle ? CommandClient::disableMic : CommandClient::enableMic;
if (client->state() == QTcpSocket::ConnectedState)
client->sendCommand(cmd);
else
server->sendCommand(*commPeer->getAddress(),cmd);
}
}