aboutsummaryrefslogtreecommitdiff
path: root/source/server/QXmppServerConnection.cpp
blob: 1bf98cce96273e5fa7111887ea475fb70475822f (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2010 Sjors Gielen, Manjeet Dahiya
 *
 * Authors:
 *	Manjeet Dahiya
 *  Sjors Gielen
 *
 * Source:
 *	http://code.google.com/p/qxmpp
 *
 * This file is a part of QXmpp library.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 */


#include "QXmppServerConnection.h"
#include "QXmppLogger.h"
#include "QXmppStream.h"
#include "QXmppMessage.h"
#include "QXmppUtils.h"

#define QXMPPSERVERCONNECTION_DEBUG

#define ASSERT_CONNECT(a, b, c, d) \
      { bool check = connect(a, b, c, d); \
      Q_ASSERT( check ); }

/**
 * @brief Creates a QXmppServerConnection object.
 *
 * This class is meant for server to server connections, both initiated by this
 * server (in which case the connectToHost method should be used), and
 * initiated by other servers (in which case a QSslSocket should be given to
 * this class). To listen to incoming connections, use QXmppServer.
 * 
 * @param socket Server socket if nonzero, ignored if zero
 * @param data   Server data to start parsing, only if server socket nonzero
 * @param parent QObject parent of this object
 */
QXmppServerConnection::QXmppServerConnection(QSslSocket *socket,
    const QByteArray &data, QObject *parent)
: QObject(parent)
, m_logger(0)
, m_stream(0)
{
    Q_ASSERT( socket == 0 || socket->state() == QAbstractSocket::ConnectedState );

    m_logger = QXmppLogger::getLogger();
    //m_stream = new QXmppStream(this);
    //m_stream->setSocket( socket );

    ASSERT_CONNECT(m_stream, SIGNAL(messageReceived(const QXmppMessage&)),
                       this, SIGNAL(messageReceived(const QXmppMessage&)));

    ASSERT_CONNECT(m_stream, SIGNAL(disconnected()),
                       this, SIGNAL(disconnected()));

    ASSERT_CONNECT(m_stream, SIGNAL(xmppConnected()),
                       this, SIGNAL(connected()));

    ASSERT_CONNECT(m_stream, SIGNAL(error(QXmppServerConnection::Error)),
                       this, SIGNAL(error(QXmppServerConnection::Error)));
}

/// Destructor, destroys the QXmppServerConnection object.
///

QXmppServerConnection::~QXmppServerConnection()
{
}

/**
 * @brief Attempts to connect to another XMPP server.
 *
 * @param host Hostname of the XMPP server where connection has to be made
 * (e.g. "jabber.org" and "talk.google.com"). It can also be an IP address in
 * the form of a string (e.g. "192.168.1.25").
 * @param domain Domain name of the other side e.g. "gmail.com", "jabber.org".
 * @param port Port number at which the XMPP server is listening. The default
 * value is 5269.
 */
void QXmppServerConnection::connectToServer(const QString& host, const QString& domain,
                                  int port)
{
    disconnect();
    //m_stream->connect( host, domain, port );
}

/// After successfully connecting to the server use this function to send
/// stanzas to the server. This function can solely be used to send various kind
/// of stanzas to the server. QXmppPacket is a parent class of all the stanzas
/// QXmppMessage, QXmppPresence, QXmppIq, QXmppBind, QXmppRosterIq, QXmppSession
/// and QXmppVCard.
///
/// Following code snippet illustrates how to send a message using this function:
/// \code
/// QXmppMessage message(from, to, message);
/// client.sendPacket(message);
/// \endcode
///
/// \param packet A valid XMPP stanza. It can be an iq, a message or a presence stanza.
///

void QXmppServerConnection::sendPacket(const QXmppPacket& packet)
{
    Q_ASSERT( m_stream != 0 );
    m_stream->sendPacket(packet);
}

/// Disconnects the client and the current presence of client changes to
/// QXmppPresence::Unavailable and statatus text changes to "Logged out".
///
/// \note Make sure that the clientPresence is changed to
/// QXmppPresence::Available, if you are again calling connectToServer() after
/// calling the disconnect() function.
///

void QXmppServerConnection::disconnect()
{
    Q_ASSERT( m_stream != 0 );
    m_stream->disconnect();
}

/// Returns the socket error if QXmppServerConnection::Error is QXmppServerConnection::SocketError.
///
/// \return QAbstractSocket::SocketError
///

QAbstractSocket::SocketError QXmppServerConnection::getSocketError()
{
    return m_stream->getSocketError();
}

/// Returns the XMPP stream error if QXmppServerConnection::Error is QXmppServerConnection::XmppStreamError.
///
/// \return QXmppServerConnection::Error::Condition
///

QXmppStanza::Error::Condition QXmppServerConnection::getXmppStreamError()
{
    return m_stream->getXmppStreamError();
}

/// Return the QXmppLogger associated with the client.

QXmppLogger *QXmppServerConnection::logger()
{
    return m_logger;
}

void QXmppServerConnection::setLogger(QXmppLogger *logger)
{
    m_logger = logger;
}