34 #include <QDataStream>
36 #include <QCoreApplication>
40 #include "trm/JsonDecoder.h"
41 #include "Connection.h"
43 #include "ServerMessageProcessor.h"
49 extern void printThread(
const char *func);
60 void Server::close(
void)
65 const Connection & Server::getConnection(uint32_t clientId)
const
67 QMap<uint32_t, const Connection *>::const_iterator it = connectionClientMap.find(clientId);
68 if (it == connectionClientMap.end()) {
72 Assert(it.key() == clientId);
76 void Server::getConnections(std::list<const Connection *> &connectionlist)
const
78 QList<const Connection *>::const_iterator it = connections.begin();
79 while (it != connections.end()) {
80 connectionlist.push_back(*it);
85 void Server::onDisconnected(
Connection *connection)
88 Log() <<
"Disconnected connection index is " << (
void *)connection << std::endl;
90 QMap<uint32_t, const Connection *>::iterator it = connectionClientMap.begin();
91 while(it != connectionClientMap.end()) {
92 if (it.value() == connection) {
93 Log() <<
"Removing client =======================" << (
void *)it.key() << std::endl;
96 TunerReservation::TokenList tokensForClient;
97 Manager::getInstance().getReservationTokens(tokensForClient, it.key());
99 TunerReservation::TokenList::const_iterator itt = tokensForClient.begin();
100 while(itt != tokensForClient.end()) {
101 Log() <<
"Releasing Token " << (*itt).c_str() <<
" for client " << (
void *)it.key() << std::endl;
102 Manager::getInstance().releaseReservation(*itt);
106 it = connectionClientMap.erase(it);
113 if (connections.removeOne(connection)) {
114 connection->deleteLater();
121 void Server::onMessageReceived(
const Connection &fromConnection)
124 const QByteArray & incomingMessageArray = fromConnection.recvMessage();
125 std::vector<uint8_t> headerBytes (incomingMessageArray.constData(), incomingMessageArray.constData() + Header::kHeaderLength);
126 std::vector<uint8_t> payloadBytes (incomingMessageArray.constData() + Header::kHeaderLength, incomingMessageArray.constData() + incomingMessageArray.size());
130 uint32_t clientId = Connection::kTRMMgrClientId;
131 QMap<uint32_t, const Connection *>::iterator itCon = connectionClientMap.find(clientId);
132 if(itCon != connectionClientMap.end() && Connection::isTRMDiagClient((itCon).key())) {
133 Log() <<
"Server::Sent Recvd Message to TRMDiagClient" << std::endl;
134 std::vector<uint8_t> out(incomingMessageArray.begin(),incomingMessageArray.end());
135 (itCon.value())->onHasMessageToSend(out);
138 delete &incomingMessageArray;
140 header.deserialize(headerBytes);
142 QMap<uint32_t, const Connection *>::iterator it = connectionClientMap.find(header.getClientId());
144 if (it == connectionClientMap.end()) {
145 connectionClientMap[header.getClientId()] = &fromConnection;
148 it = connectionClientMap.find(header.getClientId());
149 if (it != connectionClientMap.end()) {
150 if (payloadBytes.size() > 0) {
153 jdecoder.decode(payloadBytes);
158 Log() <<
"Disconnected clientId is " << header.getClientId() << std::endl;
160 QMap<uint32_t, const Connection *>::iterator it = connectionClientMap.begin();
161 while(it != connectionClientMap.end()) {
162 if (it.value() == &fromConnection) {
163 if (it.key() == header.getClientId()) {
166 TunerReservation::TokenList tokensForClient;
167 Manager::getInstance().getReservationTokens(tokensForClient, it.key());
169 TunerReservation::TokenList::const_iterator itt = tokensForClient.begin();
170 while(itt != tokensForClient.end()) {
171 Log() <<
"Releasing Token " << (*itt).c_str() <<
"for client " << (
void *)it.key() << std::endl;
172 Manager::getInstance().releaseReservation(*itt);
177 Log() <<
"Removing client " << (
void *)it.key() << std::endl;
178 connectionClientMap.erase(it);
196 void Server::onHasMessageToSend(
const std::vector<uint8_t> out)
199 uint32_t clientId = Connection::kTRMMgrClientId;
200 QMap<uint32_t, const Connection *>::iterator it = connectionClientMap.find(clientId);
202 if(it != connectionClientMap.end() && Connection::isTRMDiagClient((it).key())) {
203 std::vector<uint8_t> payloadBytes (out.data() + Header::kHeaderLength, out.data() + out.size());
204 Log() <<
"Server::Sent Has Message to TRMDiagClient " << std::endl;
205 (it.value())->onHasMessageToSend(out);
209 void Server::onNewConnection(
void)
211 qDebug() << (
"New connection detected\r\n");
212 QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
214 connections.append(connection);
215 QObject::connect(connection, SIGNAL(disconnected(
Connection *)),
this, SLOT(onDisconnected(
Connection *)), Qt::QueuedConnection);
216 QObject::connect(connection, SIGNAL(messageReceived(
const Connection &)),
this, SLOT(onMessageReceived(
const Connection &)));
218 QObject::connect(connection, SIGNAL(hasMessageToSend(
const std::vector<uint8_t>)),
this, SLOT(onHasMessageToSend(
const std::vector<uint8_t>)));
221 void Server::onReservationUpdated(
void)
223 Log() <<
"Manager::onReservationUpdate " << std::endl;
226 QMap<uint32_t, const Connection *>::iterator it = connectionClientMap.begin();
228 while (it != connectionClientMap.end()) {
229 Log() <<
"Server::send NotifyTunerReservationUpdate to client " << std::hex << (it).key() << std::endl;
230 if (Connection::isXREClient((it).key()) || Connection::isSDVAgentClient((it).key()) || Connection::isTestClient((it).key()))
233 std::vector<uint8_t> out;
234 SerializeMessage(Manager::getInstance().getTunerStatesUpdate(), (it).key(), out);
235 (it.value())->sendAsync(out);
238 std::cout <<
"Exception caught during getTunerStatesUpdate " << std::endl;
247 Server::Server(
const QHostAddress &address, quint16 port)
250 tcpServer =
new QTcpServer();
251 tcpServer->listen(address, port);
252 QObject::connect(tcpServer, SIGNAL(newConnection()),
this, SLOT(onNewConnection()));
254 QObject::connect(&Manager::getInstance(), SIGNAL(reservationUpdated(
void)),
this, SLOT(onReservationUpdated(
void)), Qt::QueuedConnection);