summaryrefslogtreecommitdiffstats
path: root/tdeio/tdeio/slave.h
blob: ae4eb00db1aef419b2f74bd460d7e1f68069a60f (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
268
269
/*
 *  This file is part of the KDE libraries
 *  Copyright (c) 2000 Waldo Bastian <bastian@kde.org>
 *                2000 Stephan Kulow <coolo@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#ifndef TDEIO_SLAVE_H
#define TDEIO_SLAVE_H

#include <time.h>
#include <unistd.h>

#include <tqobject.h>

#include <kurl.h>

#include "tdeio/slaveinterface.h"
#include "tdeio/connection.h"

class TDEServerSocket;
class TDESocket;

namespace TDEIO {

    /** Attention developers: If you change the implementation of TDEIO::Slave,
    * do *not* use connection() or slaveconn but the respective TDEIO::Slave
    * accessor methods. Otherwise classes derived from Slave might break. (LS)
    */
    class TDEIO_EXPORT Slave : public TDEIO::SlaveInterface
    {
	Q_OBJECT
	

    protected:
	/**
	 * Use this constructor if you derive your own class from Slave
	 * @p derived must be true in any case
	 * @internal
	 * @since 3.2
	 */
	Slave(bool derived, TDEServerSocket *unixdomain, const TQString &protocol,
		const TQString &socketname);	// TODO(BIC): Remove in KDE 4

    public:
	Slave(TDEServerSocket *unixdomain,
	      const TQString &protocol, const TQString &socketname);

        virtual ~Slave();

	void setPID(pid_t);

        int slave_pid() { return m_pid; }

	/**
	 * Force termination
	 */
	void kill();

        /**
         * @return true if the slave survived the last mission.
         */
        bool isAlive() { return !dead; }

        /**
         * Set host for url
         * @param host to connect to.
         * @param port to connect to.
         * @param user to login as
         * @param passwd to login with
         */
        void setHost( const TQString &host, int port,
                      const TQString &user, const TQString &passwd); // TODO(BIC): make virtual

        /**
         * Clear host info.
         */
        void resetHost();

        /**
         * Configure slave
         */
        void setConfig(const MetaData &config);	// TODO(BIC): make virtual

        /**
	 * The protocol this slave handles.
	 *
         * @return name of protocol handled by this slave, as seen by the user
         */
        TQString protocol() { return m_protocol; }

        void setProtocol(const TQString & protocol);
        /**
	 * The actual protocol used to handle the request.
	 *
	 * This method will return a different protocol than
	 * the one obtained by using protocol() if a
	 * proxy-server is used for the given protocol.  This
	 * usually means that this method will return "http"
	 * when the actuall request was to retrieve a resource
	 * from an "ftp" server by going through a proxy server.
	 *
         * @return the actual protocol (io-slave) that handled the request
         */
        TQString slaveProtocol() { return m_slaveProtocol; }

        /**
         * @return Host this slave is (was?) connected to
         */
        TQString host() { return m_host; }

        /**
         * @return port this slave is (was?) connected to
         */
        int port() { return m_port; }

        /**
         * @return User this slave is (was?) logged in as
         */
        TQString user() { return m_user; }

        /**
         * @return Passwd used to log in
         */
        TQString passwd() { return m_passwd; }

	/**
	 * Creates a new slave.
	 *
	 * @param protocol protocol the slave is for.
	 * @param url URL the slave should operate on.
	 * @param error is the error code on failure and undefined else.
	 * @param error_text is the error text on failure and undefined else.
	 *
	 * @return 0 on failure, or a pointer to a slave otherwise.
	 * @todo What are legal @p protocol values?
	 */
	static Slave* createSlave( const TQString &protocol, const KURL& url, int& error, TQString& error_text );

        static Slave* holdSlave( const TQString &protocol, const KURL& url );

	// == communication with connected tdeioslave ==
	// whenever possible prefer these methods over the respective
	// methods in connection()
	/**
	 * Suspends the operation of the attached tdeioslave.
	 */
        void suspend();		// TODO(BIC): make virtual
	/**
	 * Resumes the operation of the attached tdeioslave.
	 */
        void resume();		// TODO(BIC): make virtual
	/**
	 * Tells wether the tdeioslave is suspended.
	 * @return true if the tdeioslave is suspended.
	 * @since 3.2
	 */
        bool suspended();	// TODO(BIC): make virtual
	/**
	 * Sends the given command to the tdeioslave.
	 * @param cmd command id
	 * @param data byte array containing data
	 * @since 3.2
	 */
        void send(int cmd, const TQByteArray &data = TQByteArray());// TODO(BIC): make virtual
	// == end communication with connected tdeioslave ==

	/**
	 * Puts the tdeioslave associated with @p url at halt.
	 */
	void hold(const KURL &url);	// TODO(BIC): make virtual

	/**
	 * @return The time this slave has been idle.
	 */
	time_t idleTime();

	/**
	 * Marks this slave as idle.
	 */
	void setIdle();

        /*
         * @returns Whether the slave is connected
         * (Connection oriented slaves only)
         */
        bool isConnected() { return contacted; }
        void setConnected(bool c) { contacted = c; }

	/** @deprecated This method is obsolete, use the accessor methods
	  * within TDEIO::Slave instead. Old code directly accessing connection()
	  * will not be able to access special protocols.
	  */
        KDE_DEPRECATED Connection *connection() { return &slaveconn; }	// TODO(BIC): remove before KDE 4

        void ref() { m_refCount++; }
        void deref() { m_refCount--; if (!m_refCount) delete this; }

    public slots:
        void accept(TDESocket *socket);
	void gotInput();
	void timeout();
    signals:
        void slaveDied(TDEIO::Slave *slave);

    protected:
        void unlinkSocket();

    private:
        TQString m_protocol;
        TQString m_slaveProtocol;
        TQString m_host;
        int m_port;
        TQString m_user;
        TQString m_passwd;
	TDEServerSocket *serv;
	TQString m_socket;
	pid_t m_pid;
	bool contacted;
	bool dead;
	time_t contact_started;
	time_t idle_since;
	TDEIO::Connection slaveconn;
	int m_refCount;
    protected:
	virtual void virtual_hook( int id, void* data );
	// grant SlaveInterface all IDs < 0x200
	enum { VIRTUAL_SUSPEND = 0x200, VIRTUAL_RESUME, VIRTUAL_SEND,
		VIRTUAL_HOLD, VIRTUAL_SUSPENDED,
		VIRTUAL_SET_HOST, VIRTUAL_SET_CONFIG };
	struct SendParams {
	  int cmd;
	  const TQByteArray *arr;
	};
	struct HoldParams {
	  const KURL *url;
	};
	struct SuspendedParams {
	  bool retval;
	};
	struct SetHostParams {
	  const TQString *host;
	  int port;
	  const TQString *user;
	  const TQString *passwd;
	};
	struct SetConfigParams {
	  const MetaData *config;
	};
    private:
	class SlavePrivate* d;
    };

}

#endif