RDK Documentation (Open Sourced RDK Components)
Client.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 
21 /**
22 * @defgroup trm
23 * @{
24 * @defgroup qtapp
25 * @{
26 **/
27 
28 
29 #include <QObject>
30 #include <QtCore>
31 #include <QTcpServer>
32 #include <QList>
33 #include <QApplication>
34 
35 #include "trm/Header.h"
36 #include "trm/JsonEncoder.h"
37 #include "trm/JsonDecoder.h"
38 
39 #include "Connection.h"
40 #include "Client.h"
41 
42 using namespace TRM;
43 
44 Client::Client (const char *ipaddress, int portNumber)
45 {
46  Client(QHostAddress(ipaddress), portNumber);
47 }
48 
49 Client::Client(const QHostAddress &address, quint16 port)
50 {
51  qDebug() << "Creating client\r\n";
52  std::cout << ("Connecting To Server ...");
53  do {
54  tcpSocket.connectToHost(address, port);
55  std::cout << ".";
56  std::flush(std::cout);
57  sleep(1);
58  } while(!tcpSocket.waitForConnected(1000));
59 
60  connection = new Connection(tcpSocket);
61  QObject::connect(connection, SIGNAL(disconnected(Connection *)), this, SLOT(onDisconnected(Connection *)));
62  QObject::connect(connection, SIGNAL(messageReceived(const Connection &)), this, SLOT(onMessageReceived(const Connection &)));
63 }
64 
65 
66 Client::~Client(void)
67 {
68  printf("Deleting client\r\n");
69  tcpSocket.close();
70  //@TODO: traverse and delete processors.
71  if (connection != 0) {
72  printf("Deleting connection\r\n");
73  delete connection;
74  }
75 }
76 
77 void Client::onDisconnected(Connection *_connection)
78 {
79  _connection = _connection;
80  printf("CLIENT CONNEC is disconnected\r\n");
81  delete connection;
82  connection = 0;
83 }
84 
85 void Client::onMessageReceived(const Connection &fromConnection)
86 {
87  QByteArray incomingMessageArray;
88  fromConnection.recvMessage(incomingMessageArray);
89  std::vector<uint8_t> headerBytes (incomingMessageArray.data(), incomingMessageArray.data() + Header::kHeaderLength);
90  std::vector<uint8_t> payloadBytes (incomingMessageArray.data() + Header::kHeaderLength, incomingMessageArray.data() + incomingMessageArray.size());
91  Header header;
92  header.deserialize(headerBytes);
93  JsonDecoder jdecoder(getMessageProcessor(header.getClientId()));
94  jdecoder.decode(payloadBytes);
95 }
96 
97 
98 
99 /** @} */
100 /** @} */
Header.h
TRM::Client
Definition: Client.h:42
TRM::Header
Definition: Header.h:94
TRM::JsonDecoder
Definition: JsonDecoder.h:73
TRM::Connection
Definition: Connection.h:43