summaryrefslogtreecommitdiffstats
path: root/knode/knprotocolclient.h
blob: fa4d28d5b5f4492a4105f36f4ed991fbc482e0fb (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
/*
    knprotocolclient.h

    KNode, the KDE newsreader
    Copyright (c) 1999-2001 the KNode authors.
    See file AUTHORS for details

    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.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/

#ifndef KNPROTOCOLCLIENT_H
#define KNPROTOCOLCLIENT_H

#include <tqdatetime.h>
#include <tqthread.h>
#include <tqcstring.h>

#include <knserverinfo.h>

class TQStrList;
class KNJobData;
struct in_addr;


class KNProtocolClient : public TQThread  {

  public:
    enum threadSignal { TSworkDone=0, TSjobStarted=1, TSconnect=2, TSloadGrouplist=3,
                        TSwriteGrouplist=4, TSdownloadGrouplist=5, TSdownloadNew=6,
                        TSsortNew=7, TSdownloadArticle=8, TSsendArticle=9, TSsendMail=10,
                        TSprogressUpdate=11, TSdownloadDesc=12, TSdownloadNewGroups=13 };

    KNProtocolClient(int NfdPipeIn, int NfdPipeOut);
    ~KNProtocolClient();
  
    virtual void run();

    void insertJob(KNJobData *newJob);
    void removeJob();

    void updatePercentage(int percent);

    int getProgressValue() const { return progressValue; };
    /** bytes in&out for the current connection */
    int getByteCount() const { return byteCount; };
    bool isInByteCountMode() const { return byteCountMode; };

    void terminateClient() { mTerminate = true; }
  protected:

    /** main loop, maintains connection and waits for next job */
    void waitForWork();
    /** examines the job and calls the suitable handling method */
    virtual void processJob();

    /** connect, handshake and authorization */
    virtual bool openConnection();
    bool isConnected()   { return (tcpSocket!=-1); };
    /** sends QUIT-command and closes the socket */
    virtual void closeConnection();

    /** sends a command (one line), return code is written to rep */
    virtual bool sendCommand(const TQCString &cmd, int &rep);
    /** checks return code and calls handleErrors() if necessary */
    bool sendCommandWCheck(const TQCString &cmd, int rep);
    /** sends a message (multiple lines) */
    bool sendMsg(const TQCString &msg);

    /** reads next complete line of input */
    bool getNextLine();
    /** returns pointer to current line of input */
    char* getCurrentLine()  { return thisLine; };
    /** receives a message (multiple lines) */
    bool getMsg(TQStrList &msg);
    /** reads next line and returns the response code */
    bool getNextResponse(int &rep);
    /** checks return code and calls handleErrors() if necessary */
    bool checkNextResponse(int rep);

    /** interprets error code, generates error message and closes the connection */
    virtual void handleErrors();

    void sendSignal(threadSignal s);

    KNJobData *job;
    KNServerInfo account;
    /** handleErrors() adds this string to the error message */
    TQString errorPrefix;
    int progressValue, predictedLines, doneLines;
    bool byteCountMode;

  private:
    /** waits until socket is readable */
    bool waitForRead();
    /** waits until socket is writeable */
    bool waitForWrite();
    void closeSocket();
    /** sends str to the server */
    bool sendStr(const TQCString &str);
    /** removes start/stop signal */
    void clearPipe();

    char *input;
    char *thisLine, *nextLine, *inputEnd;
    unsigned int inputSize;
    /** IPC-Pipes to/from async thread */
    int fdPipeIn,fdPipeOut;
    int tcpSocket;
    /** bytes in&out for the current connection */
    int byteCount;
    TQTime timer;
    bool mTerminate;

};

#endif