1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "outputcollector.h"
37 #include <QtNetwork/QLocalServer>
38 #include <QtNetwork/QLocalSocket>
39 #include <QtCore/QCoreApplication>
45 #include <QtCore/QFile>
46 #include <QtCore/QSocketNotifier>
47 #include <QtCore/QTemporaryFile>
48 #include <QtCore/QVarLengthArray>
50 #include <sys/ioctl.h>
51 #include <sys/types.h>
54 # include <sys/filio.h> // FIONREAD
66 OutputCollector::OutputCollector(QObject *parent)
75 OutputCollector::~OutputCollector()
80 bool OutputCollector::listen()
84 return m_server->isListening();
85 m_server = new QLocalServer(this);
86 connect(m_server, SIGNAL(newConnection()), SLOT(newConnectionAvailable()));
87 return m_server->listen(QString::fromLatin1("creator-%1-%2")
88 .arg(QCoreApplication::applicationPid())
91 if (!m_serverPath.isEmpty())
93 QByteArray codedServerPath;
98 m_errorString = tr("Cannot create temporary file: %1").arg(tf.errorString());
102 m_serverPath = tf.fileName();
104 // By now the temp file was deleted again
105 codedServerPath = QFile::encodeName(m_serverPath);
106 if (!::mkfifo(codedServerPath.constData(), 0600))
108 if (errno != EEXIST) {
109 m_errorString = tr("Cannot create FiFo %1: %2").arg(m_serverPath, strerror(errno));
110 m_serverPath.clear();
114 if ((m_serverFd = ::open(codedServerPath.constData(), O_RDONLY|O_NONBLOCK)) < 0) {
115 m_errorString = tr("Cannot open FiFo %1: %2").arg(m_serverPath, strerror(errno));
116 m_serverPath.clear();
119 m_serverNotifier = new QSocketNotifier(m_serverFd, QSocketNotifier::Read, this);
120 connect(m_serverNotifier, SIGNAL(activated(int)), SLOT(bytesAvailable()));
125 void OutputCollector::shutdown()
128 delete m_server; // Deletes socket as well (QObject parent)
132 if (!m_serverPath.isEmpty()) {
134 ::unlink(QFile::encodeName(m_serverPath).constData());
135 delete m_serverNotifier;
136 m_serverPath.clear();
141 QString OutputCollector::errorString() const
144 return m_socket ? m_socket->errorString() : m_server->errorString();
146 return m_errorString;
150 QString OutputCollector::serverName() const
153 return m_server->fullServerName();
160 void OutputCollector::newConnectionAvailable()
164 m_socket = m_server->nextPendingConnection();
165 connect(m_socket, SIGNAL(readyRead()), SLOT(bytesAvailable()));
169 void OutputCollector::bytesAvailable()
172 emit byteDelivery(m_socket->readAll());
175 if (::ioctl(m_serverFd, FIONREAD, (char *) &nbytes) < 0)
177 QVarLengthArray<char, 8192> buff(nbytes);
178 if (::read(m_serverFd, buff.data(), nbytes) != (int)nbytes)
180 if (nbytes) // Skip EOF notifications
181 emit byteDelivery(QByteArray::fromRawData(buff.data(), nbytes));
185 } // namespace Internal
186 } // namespace Debugger