source: pacpussensors/trunk/TelnetClient/TelnetClient.cpp

Last change on this file was 99, checked in by nguyenhu, 9 years ago

compilation under linux with 0.2.X framework

File size: 4.9 KB
Line 
1/*********************************************************************
2// created: 2012/03/01 - 14:06
3// filename: TelnetClient.cpp
4//
5// author: Pierre Hudelaine
6// Copyright Heudiasyc UMR UTC/CNRS 7253
7//
8// version: $Id: $
9//
10// purpose: Create network socket if needed in Pacpus
11//
12*********************************************************************/
13
14#include "TelnetClient.h"
15#include <string>
16
17using namespace pacpus;
18using namespace std;
19
20/// Default host address = 0.0.0.0 (any address)
21static const string kDefaultHostAddress = "127.0.0.1";
22/// Default host port = 2368
23static const uint16_t kDefaultHostPort = 2368;
24
25DECLARE_STATIC_LOGGER("pacpus.base.TelnetClient");
26
27
28//////////////////////////////////////////////////////////////////////////
29// Construct the factory
30//////////////////////////////////////////////////////////////////////////
31static ComponentFactory <TelnetClient> sFactory("TelnetClient");
32
33
34//////////////////////////////////////////////////////////////////////////
35// Constructeur
36//////////////////////////////////////////////////////////////////////////
37TelnetClient::TelnetClient(QString name)
38 : ComponentBase(name)
39{
40 address_.setAddress(kDefaultHostAddress.c_str());
41 port_ = kDefaultHostPort;
42}
43
44
45//////////////////////////////////////////////////////////////////////////
46// Destructor
47//////////////////////////////////////////////////////////////////////////
48TelnetClient::~TelnetClient()
49{
50}
51
52
53//////////////////////////////////////////////////////////////////////////
54// Called by the ComponentManager to pass the XML parameters to the
55// component
56//////////////////////////////////////////////////////////////////////////
57ComponentBase::COMPONENT_CONFIGURATION TelnetClient::configureComponent(XmlComponentConfig config)
58{
59
60 return ComponentBase::CONFIGURED_OK;
61}
62
63
64//////////////////////////////////////////////////////////////////////////
65// Called by the ComponentManager to start the component
66//////////////////////////////////////////////////////////////////////////
67void TelnetClient::startActivity()
68{
69 if (!(socket_ = new QTcpSocket()))
70 qFatal("Failed to init socket!");
71
72 connect(socket_, SIGNAL(connected()), this, SLOT(ConnectionMessage()));
73
74 socket_->connectToHost(address_, port_, QIODevice::ReadWrite);
75
76 connect(socket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
77}
78
79void TelnetClient::ConnectionMessage()
80{
81 qDebug() << "Connected to the server";
82}
83
84/*
85void TelnetClient::sendDatagrams(QString frame)
86{
87 int sent=0;
88
89 if (socketType_ == "client")
90 {
91 if ((sent = socket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
92 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
93 //else
94 //qDebug() << "TO NETWORK:" << address2send_ << port2send_ << frame << "size" << sent;
95 }
96 else if (socketType_ == "server")
97 {
98 for (int i = 0; i < listClients.size(); i++)
99 {
100 if ((sent = socket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
101 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
102 //else
103 //qDebug() << "TO NETWORK:" << listClients[i]->getAddress() << listClients[i]->getPort() << frame << "size" << sent;
104 }
105 }
106}
107
108
109void TelnetClient::sendDatagrams(QByteArray frame)
110{
111 int sent=0;
112
113 if (socketType_ == "client")
114 {
115 if ((sent = socket_->writeDatagram(frame, address2send_, port2send_)) == -1)
116 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
117 //else
118 //qDebug() << "TO NETWORK:" << address2send_ << port2send_ << frame << "size" << sent;
119 }
120 else if (socketType_ == "server")
121 {
122 for (int i = 0; i < listClients.size(); i++)
123 {
124 if ((sent = socket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
125 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
126 //else
127 //qDebug() << "TO NETWORK:" << listClients[i]->getAddress() << listClients[i]->getPort() << frame << "size" << sent;
128 }
129 }
130}
131*/
132
133void TelnetClient::readPendingDatagrams()
134{
135 /*
136 while (socket_->hasPendingDatagrams())
137 {
138 QByteArray datagram;
139 datagram.resize(socket_->pendingDatagramSize());
140 QHostAddress sender;
141 quint16 senderPort;
142
143 }*/
144
145
146 QDataStream in(socket_);
147 QString data;
148 in >> data;
149 qDebug() << "New data: " << data;
150}
151
152
153//////////////////////////////////////////////////////////////////////////
154// Called by the ComponentManager to stop the component
155//////////////////////////////////////////////////////////////////////////
156void TelnetClient::stopActivity()
157{
158 socket_->close();
159 delete socket_;
160}
Note: See TracBrowser for help on using the repository browser.