summaryrefslogtreecommitdiffstats
blob: d961030d51774ca9920a0c998fdf52d3726e73c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/LinkLocal/LinkLocalConnector.h>

#include <boost/bind.hpp>

#include <Swiften/LinkLocal/DNSSD/DNSSDQuerier.h>
#include <Swiften/LinkLocal/DNSSD/DNSSDResolveHostnameQuery.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/HostAddress.h>
#include <Swiften/Network/HostAddressPort.h>

namespace Swift {

LinkLocalConnector::LinkLocalConnector(
        const LinkLocalService& service,
        std::shared_ptr<DNSSDQuerier> querier,
        std::shared_ptr<Connection> connection) :
            service(service),
            querier(querier),
            connection(connection) {
}

LinkLocalConnector::~LinkLocalConnector() {
    assert(!resolveQuery);
}

void LinkLocalConnector::connect() {
    resolveQuery = querier->createResolveHostnameQuery(
            service.getHostname(),
            service.getID().getNetworkInterfaceID());
    resolveQueryHostNameResolvedConnection = resolveQuery->onHostnameResolved.connect(boost::bind(
            &LinkLocalConnector::handleHostnameResolved,
            std::dynamic_pointer_cast<LinkLocalConnector>(shared_from_this()),
            _1));
    resolveQuery->run();
}

void LinkLocalConnector::cancel() {
    if (resolveQuery) {
        resolveQuery->finish();
        resolveQueryHostNameResolvedConnection.disconnect();
        resolveQuery.reset();
    }
    connectionConnectFinishedConnection.disconnect();
    connection->disconnect();
}

void LinkLocalConnector::handleHostnameResolved(const boost::optional<HostAddress>& address) {
    resolveQuery->finish();
    resolveQueryHostNameResolvedConnection.disconnect();
    resolveQuery.reset();
    if (address) {
        connectionConnectFinishedConnection = connection->onConnectFinished.connect(
                boost::bind(&LinkLocalConnector::handleConnected, shared_from_this(), _1));
        connection->connect(HostAddressPort(*address, service.getPort()));
    }
    else {
        onConnectFinished(true);
    }
}

void LinkLocalConnector::handleConnected(bool error) {
    onConnectFinished(error);
    assert(connectionConnectFinishedConnection.connected());
    connectionConnectFinishedConnection.disconnect();
}

void LinkLocalConnector::queueElement(std::shared_ptr<ToplevelElement> element) {
    queuedElements.push_back(element);
}


}