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
|
/*
* The Unix Channel
*
* by Michel Beaudouin-Lafon
*
* Copyright 1990-1993
* Laboratoire de Recherche en Informatique (LRI)
*
* Text streams
*
* $Id$
* $CurLog$
*/
#ifndef TextStream_H_
#define TextStream_H_
#include "cplus_bugs.h"
#include "Stream.h"
#include "MsgBuffer.h"
#include "ccu/String.h"
class UchTextWord {
protected:
const char* Sval;
int Ival;
public:
inline UchTextWord () : Sval (0), Ival (0) {}
inline UchTextWord (const char* s) { SetVal (s); }
inline UchTextWord (int i) { SetVal (i); }
void SetVal (const char*);
inline void SetVal (int i) { Sval = 0; Ival = i; }
inline bool IsInt () const { return (Sval == 0) ? TRUE : FALSE; }
inline bool IsString () const { return (Sval != 0) ? TRUE : FALSE; }
const char* GetQuotes () const;
inline operator int () const { return Ival; }
inline operator const char* () const { return Sval; }
};
class UchTextLine {
protected:
int Num;
int Max;
UchTextWord* Words;
void NewWord (const char* s, int i);
public:
UchTextLine ();
~UchTextLine ();
inline UchTextWord& operator [] (int i) const { return Words [i]; }
inline int NumWords () const { return Num; }
inline void AddWord (const char* s) { NewWord (s, 0); }
inline void AddWord (int i) { NewWord (0, i); }
void AddTrailer (const UchTextLine&, int);
inline UchTextLine& operator << (int i) { AddWord (i); return *this; }
inline UchTextLine& operator << (const char* s) { AddWord (s); return *this; }
inline UchTextLine& operator << (const UchTextLine& l) { AddTrailer (l, 0); return *this; }
bool Parse (char*);
char* Unparse (char* dest, int len) const;
char* Unparse (UchMsgBuffer*) const;
bool Match (const char*, const char* = 0) const;
int Index (const char*) const;
int Index (int) const;
inline bool Contains (const char* w) const { return (Index (w) == -1) ? FALSE : TRUE; }
inline bool Contains (int i) const { return (Index (i) == -1) ? FALSE : TRUE; }
inline void Reset () { Num = 0; }
};
class UchTextStream : public UchStream {
public:
enum cmd_res {
isCmdSyntax, // syntax error when parsing command
isCmdUnknown, // unknown command
isCmdOk, // request executed
isCmdError, // problem while executing request
isCmdClose, // close connection
isCmdQuit, // quit server
isCmdTerminate, // terminate multiplexer
isCmdAbort, // abort multiplexer
isCmdExit // call exit
};
protected:
UchMsgBuffer InBuffer;
UchMsgBuffer OutBuffer;
virtual void Closing (bool);
void HandleRead ();
void ProcessCmdResult (cmd_res, const UchTextLine&);
cmd_res TryPredefined (const UchTextLine&);
virtual cmd_res Execute (const UchTextLine&) = 0;
virtual void DoSend ();
public:
UchTextStream ();
~UchTextStream ();
UchTextStream (const UchTextStream&);
// UchChannel* Copy () const;
virtual void Close (); // 'close' request
virtual void Quit (); // 'quit' request
inline void Append (const char* l) { OutBuffer.Append (l, FALSE); }
inline void Append (const UchTextLine& l) { l.Unparse (&OutBuffer); OutBuffer.Append ('\n');}
inline UchTextStream& operator << (const char* l) { Append (l); return *this; }
inline UchTextStream& operator << (const UchTextLine& l) { Append (l); return *this; }
inline void Send () { DoSend (); }
inline void Send (const char* l) { Append (l); DoSend (); }
inline void Send (const UchTextLine& l) { Append (l); DoSend (); }
};
class UchTextService : public UchTextStream {
friend class UchServiceStarter;
public:
enum status {
isUnavailable, // address not found in port server
isError, // could not init connection
isRunning, // connection established
isLost, // no connection (auto-starting)
};
protected:
status StatusFlag;
UchServiceStarter* Starter;
bool Closed;
CcuString User;
CcuString Service;
CcuString Host;
void Closing (bool);
status Restart ();
void AutoStart (int = -1, int = -1);
virtual void LostServer ();
virtual void GotServer ();
virtual void AbandonRestart ();
cmd_res Execute (const UchTextLine&);
void DoSend ();
public:
UchTextService (UchBaseMultiplexer&, const char*, const char* = 0);
UchTextService ();
~UchTextService ();
void Init (UchBaseMultiplexer&, const char*, const char* = 0);
status GetStatus () { return StatusFlag; }
int GetRetryTime ();
int GetMaxRetries ();
void Close (); // close after output buffer emptied
void CloseNow (); // close now
};
// the simplest interface to a server.
extern bool TellServer (const char*, const char*, const char*);
#endif /* TextStream_H_ */
|