File: nxclientlib.h

package info (click to toggle)
nxcl 0.9-3
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 556 kB
  • ctags: 412
  • sloc: cpp: 3,149; makefile: 114
file content (459 lines) | stat: -rw-r--r-- 14,695 bytes parent folder | download | duplicates (4)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/***************************************************************************
                               nxclientlib.h
                             -------------------
    begin                : Sat 22nd July 2006
    remove Qt dependency : Started June 2007
    modifications        : June-July 2007
    copyright            : (C) 2006 by George Wright
    modifications        : (C) 2007 Embedded Software Foundry Ltd. (U.K.)
                         :     Author: Sebastian James
                         : (C) 2008 Defuturo Ltd
                         :     Author: George Wright
    email                : seb@esfnet.co.uk, gwright@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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef _NXCLIENTLIB_H_
#define _NXCLIENTLIB_H_

#include <iostream>
#include "nxsession.h"
#include <list>
#include "notQt.h"


using namespace std;

namespace nxcl {

    struct ProxyData {
        string id;
        int    display;
        string cookie;
        string proxyIP;
        bool   encrypted;
        int    port;
        string server;
    };

    /*!
     * Callbacks which are to be defined by the client code of
     * NXClientLib objects. In the case of nxcl, that means the
     * code in the class Nxcl in nxcl.cpp.
     */
    class NXClientLibExternalCallbacks
    {
        public:
            NXClientLibExternalCallbacks () {}
            virtual ~NXClientLibExternalCallbacks () {}
            virtual void write (string msg) {}
            virtual void write (int num, string msg) {}
            virtual void error (string msg) {}
            virtual void debug (string msg) {}
            virtual void stdoutSignal (string msg) {}
            virtual void stderrSignal (string msg) {}
            virtual void stdinSignal (string msg) {}
            virtual void resumeSessionsSignal (list<NXResumeData>) {}
            virtual void noSessionsSignal (void) {}
            virtual void serverCapacitySignal (void) {}
            virtual void connectedSuccessfullySignal (void) {}
    };

    /*!
     * Have to derive NXClientLib from an abstract base class,
     * NXClientLibBase, so that NXClientLibCallbacks can
     * hold a pointer to an NXClientLib object.
     *
     * The functions that are declared in NXClientLibBase are the
     * ones that we want to call via this->parent in
     * NXClientLibCallbacks. They're the ones that are called from
     * within objects of other classes (such as this->session
     * (NXSession) or this->nxsshProcess (notQProcess).
     */
    class NXClientLibBase 
    {
        public:
            NXClientLibBase() {}
            virtual ~NXClientLibBase() {}

            virtual void setIsFinished (bool status) {}
            virtual void processParseStdout (void) {}
            virtual void processParseStderr (void) {}
            virtual void loginFailed (void) {}
            virtual void readyproxy (void) {}
            virtual void doneAuth (void) {}

            /*!
             * External callbacks pointer is held in NXClientLibBase
             * because NXClientLibProcessCallbacks::parent is of
             * type NXClientLibBase and in NXClientLibProcessCallbacks we
             * refer to this->parent->externalCallbacks->write()
             */
            NXClientLibExternalCallbacks * externalCallbacks;
    };

    /*!
     * Callbacks class. This derives from several other base
     * callbacks classes, defining the behaviour of the callbacks.
     */
    class NXClientLibCallbacks : public notQProcessCallbacks,
        public NXSessionCallbacks
    {
        public:
            NXClientLibCallbacks();
            ~NXClientLibCallbacks();

            /*!
             * The callback signals
             */
            //@{
            /*!
             * From notQProcess:
             */
            //@{
            void startedSignal (string name);
            void errorSignal (int error);
            void processFinishedSignal (string name);
            void readyReadStandardOutputSignal (void);
            void readyReadStandardErrorSignal (void);
            //@}
            /*!
             * From NXSession:
             */
            //@{
            void noSessionsSignal (void);
            void loginFailedSignal (void);
            void readyForProxySignal (void);
            void authenticatedSignal (void);
            void sessionsSignal (list<NXResumeData>);
            //@}
            //@}

            /*!
             * Accessor function to set a pointer to the parent NXCLientLib
             * object.
             */
            void setParent (NXClientLibBase * p) { this->parent = p; }
        private:
            NXClientLibBase * parent;
    };

    class NXClientLib : public NXClientLibBase
    {
        public:
            NXClientLib();
            ~NXClientLib();

            /*!
             * Set up data and then call this->nxsshProcess.start().
             * 
             * \param publicKey is the path to the ssh public key
             * file to authenticate with.  Pass "supplied" to use
             * a new key, which you should then supply as the
             * parameter key.
             *
             * \param serverHost is the hostname of the NX server to
             * connect to
             *
             * \param encryption is whether to use an encrypted NX
             * session
             *
             * \param key ssh key to use for authentication of the
             * nx user if publicKey is "supplied".
             *
             * \param port TCP port to use for the ssh connection.
             */
            void invokeNXSSH (string publicKey = "supplied",
                    string serverHost = "",
                    bool encryption = true,
                    string key = "",
                    int port = 22);

            /*!
             * Overloaded to give callback data on write.
             * 
             * Writes data to this->nxsshProcess stdin and also
             * out to the user via stdoutCallback
             */
            void write (string data);

            /*!
             * Sets a custom binary search path
             */
            void setCustomPath(string path)
            {
                this->customPath = path;
            }
            
            /*!
             * Passes auth to this->session.setContinue()
             */
            void allowSSHConnect (bool auth);

            /*!
             * Set up data and then call this->nxproxyProcess.start()
             */
            void invokeProxy (void);

            /*!
             * Parse a line of output from
             * this->nxsshProcess. This is called when the proxy
             * has started, or if NX authentication
             * failed. Otherwise, this->session.parseSSH() is
             * used.
             */
            string parseSSH (string message);

            /*!
             * Read through the nx session file, and if we find a
             * message saying "Session: Terminating session at
             * 'some date'" we need to set isFinished to true.
             */
            //void checkSession (void);

            /*!
             * Re-set the contents of this->session.sessionData
             * with the nth choice.
             *
             * \return true if the nth session is resumable, false
             * if not, or if there is no nth session.
             */
            bool chooseResumable (int n); 

            /*!
             * Re-set the contents of this->session.sessionData
             * with the nth choice such that a terminate session
             * message will be sent to the nxserver
             *
             * \return true if the nth session is terminatable, false
             * if not, or if there is no nth session.
             */
            bool terminateSession (int n);

            void runSession (void);

            void startX11 (string resolution, string name);

            bool needX11Probe (void)
            {
                return x11Probe;
            }
            
            // public slots:
            //@{
            void doneAuth (void);
            void loginFailed (void);
            
            void finished (void)
            {
                dbgln ("Finishing up on signal"); this->isFinished = true;
            }

            void readyproxy (void)
            {
                dbgln ("ready for nxproxy"); this->readyForProxy = true;
            }

            void reset (void);
            void processParseStdout (void);
            void processParseStderr (void);

            /*!
             * SSH requests confirmation to go ahead with
             * connecting (e.g. if you haven't connected to the
             * host before)
             */
            void requestConfirmation (string msg);
            //@}

            // Accessors
            //@{
            /*!
             *  Set the username for NX to log in with
             */
            void setUsername (string& user)
            {
                this->nxuser = user;
                this->session.setUsername (this->nxuser);
            }

            /*!
             *  Set the password for NX to log in with
             */
            void setPassword (string& pass)
            {
                this->nxpass = pass;
                this->session.setPassword (this->nxpass);
            }

            void setResolution (int x, int y)
            {
                this->session.setResolution(x, y);
            }

            void setDepth (int depth)
            {
                this->session.setDepth(depth);
            }

            void setRender (bool render)
            {
                this->session.setRender(render);
            }

            void setSessionData (NXSessionData *);

            notQProcess* getNXSSHProcess (void)
            {
                return this->nxsshProcess;
            }

            notQProcess* getNXProxyProcess (void)
            {
                return this->nxproxyProcess;
            }

            notQProcess* getX11Process (void)
            {
                return this->x11Process;
            }

            notQProcess* getNXAuthProcess (void)
            {
                return this->nxauthProcess;
            }

            bool getIsFinished (void)
            {
                return this->isFinished;
            }

            bool getReadyForProxy (void)
            {
                return this->readyForProxy;
            }

            NXSession* getSession (void)
            {
                return &this->session;
            }

            void setIsFinished (bool status)
            {
                this->isFinished = status;
            }

            void setExternalCallbacks (NXClientLibExternalCallbacks * cb)
            {
                this->externalCallbacks = cb;
            }

            bool getSessionRunning (void)
            {
                return this->sessionRunning;
            }
            //@}

        private:
            /*!
             * Try a number of different paths to try to find the
             * program prog's full path.
             *
             * \param prog The program to find, likely to be nxssh
             * or nxproxy.
             *
             * \return The full path; e.g. /usr/bin/nxssh
             */
            string getPath (string prog);

            /*!
             * Custom search path
             */
            string customPath;

            bool x11Probe;
            /*!
             * Set true when the program is ready to end, e.g if
             * authentication failed, nxssh failed to start amoung
             * other reasons.
             */
            bool isFinished;
            /*!
             * Set true when nxssh is ready to launch the nxproxy process.
             */
            bool readyForProxy;
            /*!
             * Set true when the NX session is under way. This
             * means we can reduce the polling frequency right
             * down to a level which won't impact on power
             * consumption.
             */
            bool sessionRunning;
            /*!
             * Have we said we need to enter a password?
             */
            bool password;

            // FIXME: I hold the actual data, and a pointer to the
            // data here. I tried to get rid of the pointer, and
            // modify main.cpp in ../nxcl and that didn't work
            // properly - I'm not sure why. I suppose I could get
            // rid of the objects here, and then call
            // pNxsshProcess = new notQProcess; in the
            // constructor...
            /*!
             * The nxssh process object
             */
            notQProcess* nxsshProcess;
            /*!
             * The nxproxy process object
             */
            notQProcess* nxproxyProcess;
            /*!
             * The X11 process object
             */
            notQProcess* x11Process;
            /*!
             * The nxauth process object
             */
            notQProcess* nxauthProcess;
            /*!
             * A callbacks object. This holds the various callback
             * methods. The callback methods are defined here, but
             * are callable from notQProcess etc.
             */
            NXClientLibCallbacks callbacks;
            /*!
             * A temporary file to hold the ssl key
             */
            notQTemporaryFile *keyFile;
            /*!
             * The NX Session object.
             */
            NXSession session;
            /*!
             * A structure holding information about the
             * connection to be made, such as server address, port
             * and id.
             */
            ProxyData proxyData;
            /*!
             * Username for the connection
             */
            string nxuser;
            /*!
             * Password for the connection
             */
            string nxpass;
    };

} // namespace
#endif