33 #include "private/ArnDiscoverConnect_p.hpp" 37 #include <QElapsedTimer> 39 #include <QMetaObject> 44 ArnDiscoverConnectorPrivate::ArnDiscoverConnectorPrivate()
46 _discoverHostPrio = 1;
48 _resolveRefreshTimeout = 30;
49 _directHosts =
new QObject;
50 _directHosts->setObjectName(
"dirHosts");
51 _hasBeenSetupClient =
false;
52 _resolveRefreshBlocked =
false;
54 _externalClientConnect =
false;
55 _resolveRefreshTime = arnNullptr;
56 _resolver = arnNullptr;
57 _arnDisHostService = arnNullptr;
58 _arnDisHostServicePv = arnNullptr;
59 _arnDisHostAddress = arnNullptr;
60 _arnDisHostPort = arnNullptr;
64 ArnDiscoverConnectorPrivate::~ArnDiscoverConnectorPrivate()
72 , d_ptr( new ArnDiscoverConnectorPrivate)
103 d->_client->clearArnList( d->_directHostPrio);
111 d->_client->addToArnList( arnHost, port, d->_directHostPrio);
121 d->_resolver = arnNullptr;
123 if (!resolver)
return;
125 if (!d->_resolveRefreshTime)
126 d->_resolveRefreshTime =
new QElapsedTimer;
128 d->_resolver = resolver;
129 d->_resolver->setParent(
this);
132 if (d->_hasBeenSetupClient)
149 return d->_resolveRefreshTimeout;
165 return d->_discoverHostPrio;
181 return d->_directHostPrio;
197 return d->_externalClientConnect;
223 if (d->_resolver && d->_arnDisHostService)
224 *d->_arnDisHostService =
service;
232 connect( d->_client, SIGNAL(tcpConnected(QString,quint16)),
233 this, SLOT(doClientConnectChange(QString,quint16)));
235 QMetaObject::invokeMethod(
this,
237 Qt::QueuedConnection);
241 void ArnDiscoverConnector::doClientConnectChanged(
int stat,
int curPrio)
248 <<
" prio=" << curPrio;
249 if (!d->_resolver || (cs == cs.
Connecting))
return;
251 d->_resolveRefreshBlocked =
false;
254 if (d->_isResolved || (cs != cs.
TriedAll)) {
255 if (curPrio != d->_discoverHostPrio)
return;
259 if (d->_resolveRefreshTime->elapsed() >= d->_resolveRefreshTimeout * 1000)
260 d->_resolveRefreshBlocked =
false;
261 if (d->_resolveRefreshBlocked)
return;
264 d->_resolveRefreshBlocked =
true;
265 d->_resolveRefreshTime->start();
267 if (
Arn::debugDiscover) qDebug() <<
"Discover ClientConnectChanged 3 resolve: service=" << d->_arnDisHostService->toString();
268 bool forceUpdate = d->_isResolved;
269 d->_resolver->resolve( d->_arnDisHostService->toString(), forceUpdate);
273 void ArnDiscoverConnector::postSetupClient()
283 path = connectIdPath +
"Status/";
285 *arnConnectStatus = d->_client->connectStatus();
286 connect( d->_client, SIGNAL(connectionStatusChanged(
int,
int)), arnConnectStatus, SLOT(setValue(
int)));
289 path = connectIdPath +
"Request/";
291 *arnConnectReqPV =
"0";
292 connect( arnConnectReqPV, SIGNAL(changed(
int)),
this, SLOT(doClientConnectRequest(
int)));
298 path = connectIdPath +
"DirectHosts/Host-" + QString::number(i) +
"/";
299 ArnItem* hostAddr =
new ArnItem( path +
"value", d->_directHosts);
300 ArnItem* hostPort =
new ArnItem( path +
"Port/value", d->_directHosts);
301 *hostAddr = host.
addr;
302 *hostPort = host.
port;
305 connect( hostAddr, SIGNAL(changed()),
this, SLOT(doClientDirHostChanged()));
306 connect( hostPort, SIGNAL(changed()),
this, SLOT(doClientDirHostChanged()));
309 doClientDirHostChanged();
311 connect( d->_client, SIGNAL(connectionStatusChanged(
int,
int)),
this, SLOT(doClientConnectChanged(
int,
int)));
312 d->_hasBeenSetupClient =
true;
315 doClientReadyToConnect( d->_client, d->_id);
319 void ArnDiscoverConnector::doClientConnectChange(
const QString& arnHost, quint16 port)
325 ArnM::setValue( path +
"Port/value", port ? QString::number( port) : QString());
329 void ArnDiscoverConnector::doClientDirHostChanged()
333 QObjectList dirHostOList = d->_directHosts->children();
334 int dirHostOListSize = dirHostOList.size();
335 Q_ASSERT((dirHostOListSize & 1) == 0);
338 d->_client->clearArnList( d->_directHostPrio);
339 for (
int i = 0; i < dirHostOListSize / 2; ++i) {
340 ArnItem* hostAddr = qobject_cast<
ArnItem*>( dirHostOList.at( 2 * i + 0));
342 ArnItem* hostPort = qobject_cast<
ArnItem*>( dirHostOList.at( 2 * i + 1));
344 d->_client->addToArnList( hostAddr->
toString(), quint16( hostPort->
toInt()), d->_directHostPrio);
349 void ArnDiscoverConnector::doClientConnectRequest(
int reqCode)
354 doClientConnectChange(
"", 0);
355 d->_client->connectToArnList();
360 void ArnDiscoverConnector::postSetupResolver()
367 path = connectIdPath +
"DiscoverHost/";
368 d->_arnDisHostServicePv =
new ArnItem( path +
"Service/value!", d->_resolver);
369 d->_arnDisHostService =
new ArnItem( path +
"Service/value", d->_resolver);
370 d->_arnDisHostAddress =
new ArnItem( path +
"Host/value", d->_resolver);
371 d->_arnDisHostPort =
new ArnItem( path +
"Host/Port/value", d->_resolver);
372 d->_arnDisHostStatus =
new ArnItem( path +
"Status/value", d->_resolver);
375 QString(
"%1=Resolved %2=Resolving %3=Bad_request_sequence %4=Resolv_timeout " 376 "%5=Unicast_DNS_Fail")
377 .arg(Err::Ok).arg(Err::Running).arg(Err::BadReqSeq).arg(Err::Timeout)
378 .arg(Err::UDnsFail));
380 *d->_arnDisHostServicePv = d->_resolver->defaultService();
382 if (d->_service.isEmpty())
383 d->_service = d->_arnDisHostService->toString();
384 *d->_arnDisHostService = d->_service;
386 connect( d->_arnDisHostServicePv, SIGNAL(changed()),
this, SLOT(doClientServicetChanged()));
390 doClientServicetChanged();
394 void ArnDiscoverConnector::doClientServicetChanged()
398 Q_ASSERT(d->_arnDisHostServicePv);
399 QString serviceName = d->_arnDisHostServicePv->toString();
400 if (serviceName.isEmpty())
401 serviceName = d->_resolver->defaultService();
402 d->_service = serviceName;
403 *d->_arnDisHostServicePv = serviceName;
405 d->_resolver->resolve( serviceName,
true);
413 Q_ASSERT(d->_arnDisHostService);
414 Q_ASSERT(d->_arnDisHostAddress);
415 Q_ASSERT(d->_arnDisHostPort);
418 if (info.
serviceName() != d->_arnDisHostService->toString())
return;
426 d->_isResolved =
false;
427 d->_client->clearArnList( d->_discoverHostPrio);
429 else if (state == state.
HostIp) {
430 d->_isResolved =
true;
431 d->_client->clearArnList( d->_discoverHostPrio);
434 doClientReadyToConnect( d->_client, d->_id);
438 d->_client->clearArnList( d->_discoverHostPrio);
440 doClientReadyToConnect( d->_client, d->_id);
446 void ArnDiscoverConnector::doClientReadyToConnect(
ArnClient* arnClient,
const QString&
id)
450 if (d->_externalClientConnect)
void clearDirectHosts()
Clear the direct host connection list.
int directHostPrio() const
Return the priority for direct hosts
No data flow within set timeout (still connected)
int discoverHostPrio() const
Return the priority for discovered hosts
Unsuccessfull when trying to connect to an Arn host.
static void setValue(const QString &path, int value)
Assign an integer to an Arn Data Object at path
QString id() const
Return the identifier for this connector.
Successfully connected to an Arn host.
void setResolver(ArnDiscoverResolver *resolver)
Set the ArnDiscoverResolver to be used.
State of Arn discover browse data. Can be tested by relative order.
void clientReadyToConnect(ArnClient *arnClient, const QString &id)
Signal for external client connection.
QList< HostAddrPort > HostList
Unsuccessfully tried to connect to all hosts in the Arn connection List.
int resolvCode() const
Return the latest resolv error code for this service.
void setService(const QString &service)
Set the service name for the connection.
TCP connection is broken (has been successfull)
Got service name and domain (from browsing)
void addMode(Arn::ObjectMode mode)
Add general mode settings for this Arn Data Object
Negotiating terms and compatibility with an Arn host.
void addToDirectHosts(const QString &arnHost, quint16 port=0)
Add an Arn Server to the direct host connection list.
const QString pathDiscoverConnect
QString service() const
Returns the service name for this connection.
bool externalClientConnect() const
Return the external client connect mode.
void connectToArnList()
Connect to an Arn Server in the Arn connection list.
Class for connecting to an Arn Server.
QString hostWithInfo() const
Get the the HostWithInfo string.
void setDirectHostPrio(int directHostPrio)
Set the priority for direct hosts
An automatic client discover connector.
Trying to connect to an Arn host.
QString hostPortString() const
Return the printable host port for this service.
QString toString(bool *isOk=arnNullptr) const
QString serviceName() const
Return the service name for this service.
void setDiscoverHostPrio(int discoverHostPrio)
Set the priority for discovered hosts
void setResolveRefreshTimeout(int resolveRefreshTimeout)
Set the resolv refresh period.
void setDefaultStopState(ArnDiscoverInfo::State defaultStopState)
Set the default stop state for this service discover browser.
void start()
Start connector.
int resolveRefreshTimeout() const
Return the resolv refresh period.
void setExternalClientConnect(bool externalClientConnect)
Set the external client connect mode.
Initialized, not yet any result of trying to connect ...
Also got HostIp (from DNS lookup)
bool isError() const
Is in an error state for this service.
Class for holding current discover info of one service.
ArnDiscoverConnector(ArnClient &client, const QString &id)
quint16 hostPort() const
Return the port for this service.
int toInt(bool *isOk=arnNullptr) const
Data is persistent and will be saved.
Errors of ZeroConfig, other values are defined in dns_sd.h.
Handle for an Arn Data Object.