summaryrefslogtreecommitdiffstats
path: root/kmrml/kmrml/mrml.cpp
blob: a61de8f1c35523dd295d0f15fb03999b72b0facc (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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* This file is part of the KDE project
   Copyright (C) 2001,2002 Carsten Pfeiffer <pfeiffer@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program 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
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "config.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <kconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kinstance.h>
#include <klocale.h>

#include <mrml_utils.h>

#include "mrml.h"

extern "C" {
    KDE_EXPORT int kdemain( int argc, char **argv )
    {
        KLocale::setMainCatalogue("tdelibs");
        KInstance instance( "kio_mrml" );
        KGlobal::locale()->insertCatalogue( "kmrml" );

        kdDebug() << "Starting MRML " << getpid() << endl;

        if (argc != 4)
        {
            fprintf(stderr, "Usage: kio_mrml protocol domain-socket1 domain-socket2\n");
            exit(-1);
        }

        Mrml slave(argv[2], argv[3]);
        slave.dispatchLoop();

        kdDebug() << "Done" << endl;
        return 0;
    }
}

const int Mrml::bufsize = 8192;

Mrml::Mrml( const TQCString& pool_socket, const TQCString& app_socket )
    : TCPSlaveBase( 12789, "mrml", pool_socket, app_socket ),
      m_config( KGlobal::config() )
{
    MrmlShared::ref();
}

Mrml::~Mrml()
{
    KMrml::Util::self()->unrequireLocalServer();

    closeDescriptor();
    MrmlShared::deref();
}

bool Mrml::checkLocalServer( const KURL& url )
{
    if ( KMrml::Util::self()->requiresLocalServerFor( url ) )
    {
        if ( !KMrml::Util::self()->startLocalServer( m_config ) )
            return false;
    }

    return true;
}

void Mrml::get( const KURL& url )
{
//      qDebug("******* getting: %s (user: %s)", url.url().latin1(), url.user().latin1());

    if ( !checkLocalServer( url ) )
    {
        error( KIO::ERR_SLAVE_DEFINED, i18n("Unable to start the Indexing Server. "
                                            "Aborting the query.") );
        return;
    }

    int retriesLeft = 5;
tryConnect:

    TQCString utf8;
    bool sendError = (retriesLeft <= 0);

    if ( connectToHost( url.host(), port(url), sendError ) )
    {
//         qDebug(" connected!");

        TQString task = metaData( MrmlShared::kio_task() );

        if ( task == MrmlShared::kio_initialize() ) {
            startSession( url );
        }

        else if ( task == MrmlShared::kio_startQuery() ) {
            TQString meta = metaData( MrmlShared::mrml_data() );
            if ( meta.isEmpty() ) {
                closeDescriptor();
                error( KIO::ERR_SLAVE_DEFINED, i18n("No MRML data is available.") );
                return;
            }

            utf8 = meta.utf8();
            write( utf8, utf8.length() );

            emitData( readAll() );
        }

        // no task metadata available, we're called from KonqRun or something
        // like that. Emitting the mimetype seems to suffice for now. After
        // that, MrmlPart is going to start and start the get() again.
        else
        {
            mimeType( "text/mrml" );
            finished();
        }

    }
    else
    {
        if ( retriesLeft-- >= 0 )
        {
#ifdef HAVE_USLEEP
            usleep( 500 ); // wait a while for gift to start up
#endif
            goto tryConnect;
            return;
        }

        error( KIO::ERR_COULD_NOT_CONNECT,
               i18n("Could not connect to GIFT server.") );
        return;
    }

    closeDescriptor();
    //data( TQByteArray() ); // send an empty TQByteArray to signal end of data.
    finished();
}

// make sure we're connected when you call this!
TQCString Mrml::readAll()
{
    TQCString data;

    char buf[bufsize];
    ssize_t bytes = 0;

    while ( (bytes = read( buf, bufsize-1 )) > 0 ) {
        buf[bytes] = '\0';
        data.append( buf );
    }

//     qDebug("*** readAll()::: %i, %s", data.length(), data.data());
    return data;
}

TQCString Mrml::loginString()
{
    return "<mrml><get-server-properties/></mrml>";
}

TQCString Mrml::getConfigurationString()
{
    return "<mrml><get-configuration/></mrml>";
}

// ### needed?
TQCString Mrml::getSessionsString( const TQString& username,
                                  const TQString& password )
{
    TQCString data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><mrml><get-sessions ";
    if ( !username.isEmpty() ) { // ### pop up auth-dialog???
        data.append( "user-name=\"");
        data.append( username.utf8() );
        data.append( "\"");

        if ( !password.isEmpty() ) {
            data.append( " password=\"");
            data.append( password.utf8() );
            data.append( "\"" );
        }

    }
    data.append( "/></mrml>" );

    return data;
}


bool Mrml::startSession( const KURL& url )
{
    // might first ask for collections, and then for algorithms for the
    // desired collection-id

    // Wolfgang says, we shouldn't create an own session-id here, as gcc 2.95
    // apparently makes problems in exception handling somehow. So we simply
    // accept the server's session-id.
    TQString msg = mrmlString( TQString() ).arg(
        "<open-session user-name=\"%1\" session-name=\"kio_mrml session\" /> \
         <get-algorithms />                                                  \
         <get-collections />                                                 \
         </mrml>" ).arg( user( url ));

    TQCString utf8 = msg.utf8();
//     qDebug(":::Writing: %s", utf8.data());
    write( utf8, utf8.length() );

    emitData( readAll() );

    return true;
}

TQString Mrml::mrmlString( const TQString& sessionId, const TQString& transactionId )
{
    TQString msg =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>            \
         <!DOCTYPE mrml SYSTEM \"http://isrpc85.epfl.ch/Charmer/code/mrml.dtd\"> \
         %1                                                                      \
         </mrml>";

    if ( sessionId.isEmpty() ) // when we don't have one yet
        return msg.arg( "<mrml>%1" );

    if ( transactionId.isNull() )
        return msg.arg( "<mrml session-id=\"%1\">%1" ).arg( sessionId );
    else
        return msg.arg( "<mrml session-id=\"%1\" transaction-id=\"%1\">%1")
                  .arg( sessionId ).arg( transactionId );
}

void Mrml::emitData( const TQCString& msg )
{
    mimeType( "text/mrml" );
    data( msg );
    processedSize( msg.count() );
}

void Mrml::mimetype( const KURL& url )
{
    if ( url.protocol() == "mrml" ) {
        mimeType( "text/mrml" );
        finished();
    }
    else
        KIO::TCPSlaveBase::mimetype( url );
}