~ubuntu-branches/ubuntu/utopic/psi/utopic

« back to all changes in this revision

Viewing changes to iris/src/xmpp/xmpp-core/xmlprotocol.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * xmlprotocol.h - state machine for 'jabber-like' protocols
 
3
 * Copyright (C) 2004  Justin Karneges
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#ifndef XMLPROTOCOL_H
 
22
#define XMLPROTOCOL_H
 
23
 
 
24
#include <qdom.h>
 
25
#include <QList>
 
26
#include <QObject>
 
27
#include "parser.h"
 
28
 
 
29
#define NS_XML "http://www.w3.org/XML/1998/namespace"
 
30
 
 
31
namespace XMPP
 
32
{
 
33
        class XmlProtocol : public QObject
 
34
        {
 
35
        public:
 
36
                enum Need {
 
37
                        NNotify,      // need a data send and/or recv update
 
38
                        NCustom = 10
 
39
                };
 
40
                enum Event {
 
41
                        EError,       // unrecoverable error, see errorCode for details
 
42
                        ESend,        // data needs to be sent, use takeOutgoingData()
 
43
                        ERecvOpen,    // breakpoint after root element open tag is received
 
44
                        EPeerClosed,  // root element close tag received
 
45
                        EClosed,      // finished closing
 
46
                        ECustom = 10
 
47
                };
 
48
                enum Error {
 
49
                        ErrParse,     // there was an error parsing the xml
 
50
                        ErrCustom = 10
 
51
                };
 
52
                enum Notify {
 
53
                        NSend = 0x01, // need to know if data has been written
 
54
                        NRecv = 0x02  // need incoming data
 
55
                };
 
56
 
 
57
                XmlProtocol();
 
58
                virtual ~XmlProtocol();
 
59
 
 
60
                virtual void reset();
 
61
 
 
62
                // byte I/O for the stream
 
63
                void addIncomingData(const QByteArray &);
 
64
                QByteArray takeOutgoingData();
 
65
                void outgoingDataWritten(int);
 
66
 
 
67
                // advance the state machine
 
68
                bool processStep();
 
69
 
 
70
                // set these before returning from a step
 
71
                int need, event, errorCode, notify;
 
72
 
 
73
                inline bool isIncoming() const { return incoming; }
 
74
                QString xmlEncoding() const;
 
75
                QString elementToString(const QDomElement &e, bool clip=false);
 
76
 
 
77
                class TransferItem
 
78
                {
 
79
                public:
 
80
                        TransferItem();
 
81
                        TransferItem(const QString &str, bool sent, bool external=false);
 
82
                        TransferItem(const QDomElement &elem, bool sent, bool external=false);
 
83
 
 
84
                        bool isSent; // else, received
 
85
                        bool isString; // else, is element
 
86
                        bool isExternal; // not owned by protocol
 
87
                        QString str;
 
88
                        QDomElement elem;
 
89
                };
 
90
                QList<TransferItem> transferItemList;
 
91
                void setIncomingAsExternal();
 
92
 
 
93
        protected:
 
94
                virtual QDomElement docElement()=0;
 
95
                virtual void handleDocOpen(const Parser::Event &pe)=0;
 
96
                virtual bool handleError()=0;
 
97
                virtual bool handleCloseFinished()=0;
 
98
                virtual bool stepAdvancesParser() const=0;
 
99
                virtual bool stepRequiresElement() const;
 
100
                virtual bool doStep(const QDomElement &e)=0;
 
101
                virtual void itemWritten(int id, int size);
 
102
 
 
103
                // 'debug'
 
104
                virtual void stringSend(const QString &s);
 
105
                virtual void stringRecv(const QString &s);
 
106
                virtual void elementSend(const QDomElement &e);
 
107
                virtual void elementRecv(const QDomElement &e);
 
108
 
 
109
                void startConnect();
 
110
                void startAccept();
 
111
                bool close();
 
112
                int writeString(const QString &s, int id, bool external);
 
113
                int writeElement(const QDomElement &e, int id, bool external, bool clip=false);
 
114
                QByteArray resetStream();
 
115
 
 
116
        private:
 
117
                enum { SendOpen, RecvOpen, Open, Closing };
 
118
                class TrackItem
 
119
                {
 
120
                public:
 
121
                        enum Type { Raw, Close, Custom };
 
122
                        int type, id, size;
 
123
                };
 
124
 
 
125
                bool incoming;
 
126
                QDomDocument elemDoc;
 
127
                QDomElement elem;
 
128
                QString tagOpen, tagClose;
 
129
                int state;
 
130
                bool peerClosed;
 
131
                bool closeWritten;
 
132
 
 
133
                Parser xml;
 
134
                QByteArray outData;
 
135
                QList<TrackItem> trackQueue;
 
136
 
 
137
                void init();
 
138
                int internalWriteData(const QByteArray &a, TrackItem::Type t, int id=-1);
 
139
                int internalWriteString(const QString &s, TrackItem::Type t, int id=-1);
 
140
                void sendTagOpen();
 
141
                void sendTagClose();
 
142
                bool baseStep(const Parser::Event &pe);
 
143
        };
 
144
}
 
145
 
 
146
#endif