2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #include "connection.h"
17 #include <QTextStream>
19 #include <QTextStream>
25 Connection::Connection()
28 , m_initialized(false) {
30 if (!logFileName
.isEmpty()) {
31 logFile
= new QFile(logFileName
);
32 logFile
->open(QIODevice::WriteOnly
);
33 logStream
= 0; //= new QTextStream(logFile);
39 Connection::~Connection() {
46 QProcess
*p
= qobject_cast
<QProcess
*>(m_device
);
53 void Connection::establish(const char* host
, quint16 port
,
54 const QString
& helper
, const QString
& helper_cmd
) {
56 QProcess
*p
= qobject_cast
<QProcess
*>(m_device
);
62 strncpy(m_host
, host
, 256);
65 m_helper_cmd
= helper_cmd
;
67 if(helper
.isEmpty()) {
68 QTcpSocket
*s
= new QTcpSocket(this);
69 s
->setObjectName("IcsSocket");
71 connect(s
, SIGNAL(hostFound()), this, SIGNAL(hostFound()));
72 connect(s
, SIGNAL(connected()), this, SIGNAL(established()));
75 s
->connectToHost(m_host
, m_port
);
81 m_device
= new QProcess(this);
82 m_device
->setObjectName("IcsHelper");
84 if(m_helper_cmd
.contains("$(HOST_IP)")) {
85 QHostInfo::lookupHost(m_host
, this, SLOT(lookedUp(const QHostInfo
)));
88 lookedUp( QHostInfo() );
91 connect(m_device
, SIGNAL(readyRead()), this, SLOT(processLine()));
94 void Connection::lookedUp(const QHostInfo
&hi
) {
95 QProcess
*p
= m_device
? qobject_cast
<QProcess
*>(m_device
) : NULL
;
99 //std::cout << "Resolved to \"" << (hi.addresses().isEmpty() ? QString() :
100 // hi.addresses().first().toString ()) << "\" "
101 // << hi.errorString() << std::endl;
102 QString args
= m_helper_cmd
;
103 args
.replace("$(HOST_NAME)", QString(m_host
) );
104 args
.replace("$(HOST_IP)", hi
.addresses().isEmpty() ? QString() :
105 hi
.addresses().first().toString () );
106 args
.replace("$(PORT)", QString::number(m_port
));
107 p
->start(m_helper
, args
.split(' '));
110 if(!m_unsent_text
.isEmpty()) {
111 QTextStream
os(m_device
);
115 (*logStream
) << "> " << m_unsent_text
;
116 m_unsent_text
= QString();
120 void Connection::processLine() {
122 std::cout
<< " --> Connection::processLine, error, no m_device" << std::endl
;
126 while (m_device
->canReadLine()) {
127 QString line
= m_device
->readLine();
128 line
= buffer
+ line
.replace("\r", "");
129 emit
receivedLine(line
, buffer
.length());
131 (*logStream
) << "< " << line
<< "\n";
136 if ((size
= m_device
->bytesAvailable()) > 0) {
137 char* data
= new char[size
+ 1];
138 m_device
->read(data
, size
);
140 int offset
= buffer
.length();
141 buffer
+= QString(data
).replace("\r", "");
144 emit
receivedText(buffer
, offset
);
148 void Connection::sendText(const QString
& text
) {
150 m_unsent_text
+= text
+ "\n";
155 std::cout
<< " --> Connection::sendText, error, no m_device" << std::endl
;
160 QTextStream
os(m_device
);
164 (*logStream
) << "> " << text
<< "\n";