• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

krfb

  • sources
  • kde-4.14
  • kdenetwork
  • krfb
  • krfb
tubesrfbserver.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009-2011 Collabora Ltd. <info@collabora.co.uk>
3  @author George Goldberg <george.goldberg@collabora.co.uk>
4  @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "tubesrfbserver.h"
20 #include "tubesrfbclient.h"
21 #include "sockethelpers.h"
22 
23 #include <KDebug>
24 #include <KRandom>
25 
26 #include <TelepathyQt/Debug>
27 #include <TelepathyQt/Contact>
28 #include <TelepathyQt/AccountFactory>
29 #include <TelepathyQt/ConnectionFactory>
30 #include <TelepathyQt/ContactFactory>
31 #include <TelepathyQt/ChannelFactory>
32 #include <TelepathyQt/OutgoingStreamTubeChannel>
33 #include <TelepathyQt/StreamTubeServer>
34 
35 #ifdef KRFB_WITH_KDE_TELEPATHY
36 #include <TelepathyQt/AccountSet>
37 #include <TelepathyQt/AccountManager>
38 #include <TelepathyQt/PendingReady>
39 #include <KTp/Models/contacts-list-model.h>
40 #include <KTp/contact-factory.h>
41 #endif
42 
43 struct TubesRfbServer::Private
44 {
45  Tp::StreamTubeServerPtr stubeServer;
46  QHash<quint16, Tp::ContactPtr> contactsPerPort;
47  QHash<quint16, PendingTubesRfbClient*> clientsPerPort;
48 };
49 
50 //static
51 TubesRfbServer *TubesRfbServer::instance;
52 
53 //static
54 void TubesRfbServer::init()
55 {
56  instance = new TubesRfbServer;
57  //RfbServerManager takes care of deletion
58 
59  instance->startAndCheck();
60 }
61 
62 TubesRfbServer::TubesRfbServer(QObject *parent)
63  : RfbServer(parent), d(new Private)
64 {
65  kDebug() << "starting";
66 
67  Tp::enableDebug(true);
68  Tp::enableWarnings(true);
69  Tp::registerTypes();
70 
71  Tp::AccountFactoryPtr accountFactory = Tp::AccountFactory::create(
72  QDBusConnection::sessionBus(),
73  Tp::Features() << Tp::Account::FeatureCore
74  << Tp::Account::FeatureAvatar
75  << Tp::Account::FeatureCapabilities
76  << Tp::Account::FeatureProtocolInfo
77  << Tp::Account::FeatureProfile);
78 
79  Tp::ConnectionFactoryPtr connectionFactory = Tp::ConnectionFactory::create(
80  QDBusConnection::sessionBus(),
81  Tp::Features() << Tp::Connection::FeatureCore
82  << Tp::Connection::FeatureSelfContact);
83 
84  Tp::ChannelFactoryPtr channelFactory = Tp::ChannelFactory::create(
85  QDBusConnection::sessionBus());
86 
87 #ifdef KRFB_WITH_KDE_TELEPATHY
88  Tp::ContactFactoryPtr contactFactory = KTp::ContactFactory::create(
89  Tp::Features() << Tp::Contact::FeatureAlias
90  <<Tp::Contact::FeatureAvatarToken
91  <<Tp::Contact::FeatureAvatarData
92  <<Tp::Contact::FeatureSimplePresence
93  <<Tp::Contact::FeatureCapabilities
94  <<Tp::Contact::FeatureClientTypes);
95 
96  m_accountManager = Tp::AccountManager::create(
97  QDBusConnection::sessionBus(),
98  accountFactory,
99  connectionFactory,
100  channelFactory,
101  contactFactory);
102 
103  d->stubeServer = Tp::StreamTubeServer::create(
104  m_accountManager,
105  QStringList() << QLatin1String("rfb"),
106  QStringList(),
107  QLatin1String("krfb_rfb_handler"),
108  true);
109 
110  connect(m_accountManager->becomeReady(),
111  SIGNAL(finished(Tp::PendingOperation*)),
112  this,
113  SLOT(onAccountManagerReady()));
114 
115  m_contactsListModel = new KTp::ContactsListModel(this);
116 #else
117  Tp::ContactFactoryPtr contactFactory = Tp::ContactFactory::create(
118  Tp::Contact::FeatureAlias);
119 
120  d->stubeServer = Tp::StreamTubeServer::create(
121  QStringList() << QLatin1String("rfb"),
122  QStringList(),
123  QLatin1String("krfb_rfb_handler"),
124  true,
125  accountFactory,
126  connectionFactory,
127  channelFactory,
128  contactFactory);
129 #endif //KRFB_WITH_KDE_TELEPATHY
130 
131  connect(d->stubeServer.data(),
132  SIGNAL(tubeRequested(Tp::AccountPtr,Tp::OutgoingStreamTubeChannelPtr,
133  QDateTime,Tp::ChannelRequestHints)),
134  SLOT(onTubeRequested()));
135  connect(d->stubeServer.data(),
136  SIGNAL(tubeClosed(Tp::AccountPtr,Tp::OutgoingStreamTubeChannelPtr,QString,QString)),
137  SLOT(onTubeClosed()));
138 
139  connect(d->stubeServer.data(),
140  SIGNAL(newTcpConnection(QHostAddress,quint16,Tp::AccountPtr,
141  Tp::ContactPtr,Tp::OutgoingStreamTubeChannelPtr)),
142  SLOT(onNewTcpConnection(QHostAddress,quint16,Tp::AccountPtr,
143  Tp::ContactPtr,Tp::OutgoingStreamTubeChannelPtr)));
144  connect(d->stubeServer.data(),
145  SIGNAL(tcpConnectionClosed(QHostAddress,quint16,Tp::AccountPtr,Tp::ContactPtr,
146  QString,QString,Tp::OutgoingStreamTubeChannelPtr)),
147  SLOT(onTcpConnectionClosed(QHostAddress,quint16,Tp::AccountPtr,Tp::ContactPtr,
148  QString,QString,Tp::OutgoingStreamTubeChannelPtr)));
149 
150  // Pick a random port in the private range (49152–65535)
151  setListeningPort((KRandom::random() % 16383) + 49152);
152  // Listen only on the loopback network interface
153  setListeningAddress("127.0.0.1");
154  setPasswordRequired(false);
155 }
156 
157 TubesRfbServer::~TubesRfbServer()
158 {
159  kDebug();
160  stop();
161  delete d;
162 }
163 
164 #ifdef KRFB_WITH_KDE_TELEPATHY
165 KTp::ContactsListModel *TubesRfbServer::contactsListModel()
166 {
167  return m_contactsListModel;
168 }
169 #endif
170 
171 void TubesRfbServer::startAndCheck()
172 {
173  if (!start()) {
174  //try a few times with different ports
175  bool ok = false;
176  for (int i=0; !ok && i<5; i++) {
177  setListeningPort((KRandom::random() % 16383) + 49152);
178  ok = start();
179  }
180 
181  if (!ok) {
182  kError() << "Failed to start tubes rfb server";
183  return;
184  }
185  }
186 
187  //TODO listeningAddress() should be a QHostAddress
188  d->stubeServer->exportTcpSocket(QHostAddress(QString::fromAscii(listeningAddress())),
189  listeningPort());
190 }
191 
192 void TubesRfbServer::onTubeRequested()
193 {
194  kDebug() << "Got a tube";
195 }
196 
197 void TubesRfbServer::onTubeClosed()
198 {
199  kDebug() << "tube closed";
200 }
201 
202 void TubesRfbServer::onNewTcpConnection(const QHostAddress & sourceAddress,
203  quint16 sourcePort,
204  const Tp::AccountPtr & account,
205  const Tp::ContactPtr & contact,
206  const Tp::OutgoingStreamTubeChannelPtr & tube)
207 {
208  Q_UNUSED(account);
209  Q_UNUSED(tube);
210 
211  kDebug() << "CM signaled tube connection from" << sourceAddress << ":" << sourcePort;
212 
213  d->contactsPerPort[sourcePort] = contact;
214  if (d->clientsPerPort.contains(sourcePort)) {
215  kDebug() << "client already exists";
216  d->clientsPerPort[sourcePort]->setContact(contact);
217  }
218 }
219 
220 void TubesRfbServer::onTcpConnectionClosed(const QHostAddress& sourceAddress,
221  quint16 sourcePort,
222  const Tp::AccountPtr& account,
223  const Tp::ContactPtr& contact,
224  const QString& error,
225  const QString& message,
226  const Tp::OutgoingStreamTubeChannelPtr& tube)
227 {
228  Q_UNUSED(account);
229  Q_UNUSED(contact);
230  Q_UNUSED(tube);
231 
232  kDebug() << "Connection from" << sourceAddress << ":" << sourcePort << "closed."
233  << error << message;
234 
235  d->clientsPerPort.remove(sourcePort);
236  d->contactsPerPort.remove(sourcePort);
237 }
238 
239 PendingRfbClient* TubesRfbServer::newClient(rfbClientPtr client)
240 {
241  PendingTubesRfbClient *c = new PendingTubesRfbClient(client, this);
242  quint16 port = peerPort(client->sock);
243 
244  kDebug() << "new tube client on port" << port;
245 
246  d->clientsPerPort[port] = c;
247  if (d->contactsPerPort.contains(port)) {
248  kDebug() << "already have a contact";
249  c->setContact(d->contactsPerPort[port]);
250  }
251 
252  return c;
253 }
254 
255 #ifdef KRFB_WITH_KDE_TELEPATHY
256 void TubesRfbServer::onAccountManagerReady()
257 {
258  m_contactsListModel->setAccountManager(m_accountManager);
259 }
260 #endif
261 
262 #include "tubesrfbserver.moc"
RfbServer
Definition: rfbserver.h:27
QString::fromAscii
QString fromAscii(const char *str, int size)
RfbServer::setListeningPort
void setListeningPort(int port)
Definition: rfbserver.cpp:78
QHostAddress
QDBusConnection::sessionBus
QDBusConnection sessionBus()
peerPort
unsigned short peerPort(int sock)
Definition: sockethelpers.cpp:54
_rfbClientRec::sock
SOCKET sock
Definition: libvncserver/rfb/rfb.h:437
_rfbClientRec
Definition: libvncserver/rfb/rfb.h:417
TubesRfbServer::newClient
virtual PendingRfbClient * newClient(rfbClientPtr client)
Definition: tubesrfbserver.cpp:239
RfbServer::listeningPort
int listeningPort() const
Definition: rfbserver.cpp:63
QHash< quint16, Tp::ContactPtr >
tubesrfbclient.h
QObject
RfbServer::start
virtual bool start()
Definition: rfbserver.cpp:88
RfbServer::stop
virtual void stop(bool disconnectClients=true)
Definition: rfbserver.cpp:146
TubesRfbServer::instance
static TubesRfbServer * instance
Definition: tubesrfbserver.h:36
QString
RfbServer::setPasswordRequired
void setPasswordRequired(bool passwordRequired)
Definition: rfbserver.cpp:83
QStringList
TubesRfbServer::~TubesRfbServer
virtual ~TubesRfbServer()
Definition: tubesrfbserver.cpp:157
tubesrfbserver.h
QLatin1String
TubesRfbServer::init
static void init()
Definition: tubesrfbserver.cpp:54
RfbServer::setListeningAddress
void setListeningAddress(const QByteArray &address)
Definition: rfbserver.cpp:73
PendingRfbClient
Definition: krfb/rfbclient.h:72
TubesRfbServer::TubesRfbServer
TubesRfbServer(QObject *parent=0)
Definition: tubesrfbserver.cpp:62
PendingTubesRfbClient::setContact
void setContact(const Tp::ContactPtr &contact)
Definition: tubesrfbclient.cpp:39
RfbServer::listeningAddress
QByteArray listeningAddress() const
Definition: rfbserver.cpp:58
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
TubesRfbServer
Definition: tubesrfbserver.h:32
sockethelpers.h
PendingTubesRfbClient
Definition: tubesrfbclient.h:40
QDateTime
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

krfb

Skip menu "krfb"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal