~ubuntu-branches/ubuntu/trusty/jreen/trusty

« back to all changes in this revision

Viewing changes to src/sjdns.cpp

  • Committer: Package Import Robot
  • Author(s): Prasad Murthy
  • Date: 2013-03-08 00:00:33 UTC
  • Revision ID: package-import@ubuntu.com-20130308000033-x8thp6syo1kkh63s
Tags: upstream-1.1.1
Import upstream version 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Jreen
 
4
**
 
5
** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
 
6
**
 
7
*****************************************************************************
 
8
**
 
9
** $JREEN_BEGIN_LICENSE$
 
10
** This program is free software: you can redistribute it and/or modify
 
11
** it under the terms of the GNU General Public License as published by
 
12
** the Free Software Foundation, either version 2 of the License, or
 
13
** (at your option) any later version.
 
14
**
 
15
** This program is distributed in the hope that it will be useful,
 
16
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
18
** See the GNU General Public License for more details.
 
19
**
 
20
** You should have received a copy of the GNU General Public License
 
21
** along with this program.  If not, see http://www.gnu.org/licenses/.
 
22
** $JREEN_END_LICENSE$
 
23
**
 
24
****************************************************************************/
 
25
 
 
26
#include "sjdns_p.h"
 
27
#include "logger.h"
 
28
 
 
29
namespace Jreen
 
30
{
 
31
 
 
32
SJDns &SJDns::instance()
 
33
{
 
34
        static SJDns *sjdns = 0;
 
35
        if (sjdns && !sjdns->m_valid) {
 
36
                delete sjdns;
 
37
                sjdns = 0;
 
38
        }
 
39
        if (!sjdns) {
 
40
                sjdns = new SJDns;
 
41
                sjdns->m_qjdns = new QJDns;
 
42
                sjdns->m_valid = true;
 
43
                if (!sjdns->m_qjdns->init(QJDns::Unicast, QHostAddress::Any)) {
 
44
                        delete sjdns->m_qjdns;
 
45
                        sjdns->m_qjdns = 0;
 
46
                        sjdns->m_valid = false;
 
47
                        return *sjdns;
 
48
                }
 
49
                connect(sjdns->m_qjdns, SIGNAL(resultsReady(int,QJDns::Response)), sjdns, SLOT(resultsReady(int,QJDns::Response)));
 
50
                connect(sjdns->m_qjdns, SIGNAL(published(int)), sjdns, SLOT(published(int)));
 
51
                connect(sjdns->m_qjdns, SIGNAL(error(int,QJDns::Error)), sjdns, SLOT(error(int,QJDns::Error)));
 
52
 
 
53
                QJDns::SystemInfo info = QJDns::systemInfo();
 
54
                if (info.nameServers.isEmpty()) {
 
55
                        QJDns::NameServer server;
 
56
                        server.address = QLatin1String("8.8.8.8");
 
57
                        info.nameServers << server;
 
58
                        server.address = QLatin1String("77.88.39.152");
 
59
                        info.nameServers << server;
 
60
                }
 
61
                sjdns->m_qjdns->setNameServers(info.nameServers);
 
62
        }
 
63
        return *sjdns;
 
64
}
 
65
 
 
66
bool SJDns::isValid()
 
67
{
 
68
        return m_valid;
 
69
}
 
70
 
 
71
void SJDns::doLookup(const QString &host, QObject *receiver, const char *member)
 
72
{
 
73
        Q_ASSERT(m_valid);
 
74
        int id = m_qjdns->queryStart("_xmpp-client._tcp." + QUrl::toAce(host), QJDns::Srv);
 
75
        Action *action = new Action(this);
 
76
        action->setData(host);
 
77
        connect(action, SIGNAL(triggered()), receiver, member);
 
78
        m_actions.insert(id, action);
 
79
}
 
80
 
 
81
const QJDns::Response *SJDns::servers(const QString &host)
 
82
{
 
83
        Q_ASSERT(m_valid);
 
84
        QHash<QString, QJDns::Response>::const_iterator iter = m_results.constFind(host);
 
85
        if(iter == m_results.constEnd())
 
86
                return 0;
 
87
        return &iter.value();
 
88
}
 
89
 
 
90
void SJDns::resultsReady(int id, const QJDns::Response &results)
 
91
{
 
92
        Action *action = m_actions.value(id, 0);
 
93
        Q_ASSERT(action);
 
94
        foreach(const QJDns::Record &record, results.answerRecords)
 
95
                Logger::debug() << record.name << record.port << record.priority << record.weight;
 
96
        m_results.insert(action->data().toString(), results);
 
97
        action->trigger();
 
98
}
 
99
 
 
100
void SJDns::published(int id)
 
101
{
 
102
        Q_UNUSED(id);
 
103
}
 
104
 
 
105
void SJDns::error(int id, QJDns::Error e)
 
106
{
 
107
        Action *action = m_actions.value(id, 0);
 
108
        Q_ASSERT(action);
 
109
        QJDns::Response response;
 
110
        QJDns::Record record;
 
111
        record.port = 5222;
 
112
        record.name = action->data().toString().toUtf8();
 
113
        response.answerRecords << record;
 
114
        m_results.insert(record.name, response);
 
115
        action->trigger();
 
116
        switch (e) {
 
117
        case QJDns::ErrorGeneric:
 
118
                Logger::critical() << "error Generic" << id;
 
119
                break;
 
120
        case QJDns::ErrorNXDomain:
 
121
                Logger::critical() << "error NXDomain" << id;
 
122
                break;
 
123
        case QJDns::ErrorTimeout:
 
124
                Logger::critical() << "error Timeout" << id;
 
125
                break;
 
126
        case QJDns::ErrorConflict:
 
127
                Logger::critical() << "error Conflict" << id;
 
128
                break;
 
129
        }
 
130
}
 
131
 
 
132
}