source: pacpussensors/trunk/TelnetClient/TelnetClient.cpp@ 73

Last change on this file since 73 was 54, checked in by DHERBOMEZ Gérald, 10 years ago

add plugin that gives a telnet connection

File size: 4.7 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
16
17using namespace pacpus;
18
19
20DECLARE_STATIC_LOGGER("pacpus.base.TelnetClient");
21
22
23//////////////////////////////////////////////////////////////////////////
24// Construct the factory
25//////////////////////////////////////////////////////////////////////////
26static ComponentFactory <TelnetClient> sFactory("TelnetClient");
27
28
29//////////////////////////////////////////////////////////////////////////
30// Constructeur
31//////////////////////////////////////////////////////////////////////////
32TelnetClient::TelnetClient(QString name)
33 : ComponentBase(name)
34{
35}
36
37
38//////////////////////////////////////////////////////////////////////////
39// Destructor
40//////////////////////////////////////////////////////////////////////////
41TelnetClient::~TelnetClient()
42{
43}
44
45
46//////////////////////////////////////////////////////////////////////////
47// Called by the ComponentManager to pass the XML parameters to the
48// component
49//////////////////////////////////////////////////////////////////////////
50ComponentBase::COMPONENT_CONFIGURATION TelnetClient::configureComponent(XmlComponentConfig config)
51{
52 address_.setAddress(param.getProperty("address"));
53 port_ = param.getProperty("port").toUInt();
54
55 return ComponentBase::CONFIGURED_OK;
56}
57
58
59//////////////////////////////////////////////////////////////////////////
60// Called by the ComponentManager to start the component
61//////////////////////////////////////////////////////////////////////////
62void TelnetClient::startActivity()
63{
64 if (!(socket_ = new QTcpSocket()))
65 qFatal("Failed to init socket!");
66
67 connect(socket_, SIGNAL(connected()), this, SLOT(ConnectionMessage()));
68
69 socket_->connectToHost(address_, port_, QIODevice::ReadWrite);
70
71 connect(socket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
72}
73
74void TelnetClient::ConnectionMessage()
75{
76 qDebug() << "Connected to the server";
77}
78
79/*
80void TelnetClient::sendDatagrams(QString frame)
81{
82 int sent=0;
83
84 if (socketType_ == "client")
85 {
86 if ((sent = socket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
87 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
88 //else
89 //qDebug() << "TO NETWORK:" << address2send_ << port2send_ << frame << "size" << sent;
90 }
91 else if (socketType_ == "server")
92 {
93 for (int i = 0; i < listClients.size(); i++)
94 {
95 if ((sent = socket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
96 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
97 //else
98 //qDebug() << "TO NETWORK:" << listClients[i]->getAddress() << listClients[i]->getPort() << frame << "size" << sent;
99 }
100 }
101}
102
103
104void TelnetClient::sendDatagrams(QByteArray frame)
105{
106 int sent=0;
107
108 if (socketType_ == "client")
109 {
110 if ((sent = socket_->writeDatagram(frame, address2send_, port2send_)) == -1)
111 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
112 //else
113 //qDebug() << "TO NETWORK:" << address2send_ << port2send_ << frame << "size" << sent;
114 }
115 else if (socketType_ == "server")
116 {
117 for (int i = 0; i < listClients.size(); i++)
118 {
119 if ((sent = socket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
120 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
121 //else
122 //qDebug() << "TO NETWORK:" << listClients[i]->getAddress() << listClients[i]->getPort() << frame << "size" << sent;
123 }
124 }
125}
126*/
127
128void TelnetClient::readPendingDatagrams()
129{
130 /*
131 while (socket_->hasPendingDatagrams())
132 {
133 QByteArray datagram;
134 datagram.resize(socket_->pendingDatagramSize());
135 QHostAddress sender;
136 quint16 senderPort;
137
138 }*/
139
140
141 QDataStream in(socket_);
142 QString data;
143 in >> data;
144 qDebug() << "New data: " << data;
145}
146
147
148//////////////////////////////////////////////////////////////////////////
149// Called by the ComponentManager to stop the component
150//////////////////////////////////////////////////////////////////////////
151void TelnetClient::stopActivity()
152{
153 socket_->close();
154 delete socket_;
155}
Note: See TracBrowser for help on using the repository browser.