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
|
/*
* The Unix Channel
*
* by Michel Beaudouin-Lafon
*
* Copyright 1990-1993
* Laboratoire de Recherche en Informatique (LRI)
*
* Send a request to a server
*
* $Id$
* $CurLog$
*/
/* Usage:
* tellagent agentname request
* tellagent agentname -f file
* tellagent agentname
*/
#include "TextStream.h"
#include "Multiplexer.h"
#include "error.h"
#include <sys/file.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
class INPUT;
class TELLER;
class INPUT : public UchChannel {
protected:
TELLER* tell;
UchMsgBuffer input;
public:
INPUT (TELLER* t, int fd) : UchChannel (fd) { tell = t; }
void HandleRead ();
};
class TELLER : public UchTextService {
protected:
pUchChannel in;
pUchChannel out;
cmd_res Execute (const UchTextLine&);
void AbandonRestart ();
public:
TELLER () : UchTextService () { in = 0; out = 0; }
void SetOutput (int);
void SetInput (int fd);
void Close ();
};
//---------------- INPUT
void
INPUT :: HandleRead ()
{
input.NeedSize (128);
int n = Read (input);
if (n < 0)
SysError (ErrFatal, "read input");
if (n == 0) {
if (input.BufLength ()) {
input.Append ('\n');
input.Append ('\0');
tell->Send ((const char*) input.Buffer ());
input.Flush ();
}
tell->Close ();
return;
}
input.Append ('\0');
tell->Send ((const char*) input.Buffer ());
input.Flush ();
}
void
TELLER :: SetOutput (int fd)
{
out = new UchChannel (fd);
}
void
TELLER :: SetInput (int fd)
{
in = new INPUT (this, fd);
in->SetMode (IORead);
if (Mpx)
Mpx->Add (*in);
}
UchTextService::cmd_res
TELLER :: Execute (const UchTextLine& l)
{
if (! out)
return isCmdOk;
UchMsgBuffer buf;
l.Unparse (&buf);
buf.Append ('\n');
out->WriteBuffer (buf);
return isCmdOk;
}
void
TELLER :: AbandonRestart ()
{
if (in) {
Mpx->Remove (*in);
in = 0;
}
out = 0;
UchTextService::AbandonRestart ();
}
void
TELLER :: Close ()
{
if (in) {
Mpx->Remove (*in);
in = 0;
}
out = 0;
UchTextService::Close ();
}
//---------------- Usage
void
Usage ()
{
Error (ErrUsage, NULL, "server[@host] [request | -f file]");
}
//---------------- main
main (int argc, char** argv)
{
ProgramName (argv [0]);
// LogfileName ("tellagent.log");
if (argc < 2)
Usage ();
const char* sname = argv [1];
const char* host = 0;
char* p = strchr (sname, '@');
if (p) {
*p++ = '\0';
host = p;
}
UchMultiplexer mpx;
TELLER* tell = new TELLER;
tell->Init (mpx, sname, host);
int fdin = 0; /*stdin*/
if (argc == 2) {
// tellagent server : start interactive session
} else if (strcmp (argv [2], "-f") == 0) {
// tellagent server -f file: read from file
if (argc != 4)
Usage ();
const char* fname = argv [3];
fdin = open (fname, O_RDONLY);
if (fdin < 0)
SysError (ErrFatal, fname);
} else {
// tellagent server request: send request and close
UchTextLine line;
for (int i = 2; i < argc; i++)
line << argv [i];
tell->SetOutput (1 /*stdout*/);
tell->Send (line);
tell->Close ();
while (tell->GetStatus () != UchTextService::isRunning) {
mpx.LoopScan ();
wait (0);
}
exit (1);
}
if (fdin >= 0)
tell->SetInput (fdin);
tell->SetOutput (1 /*stdout*/);
#if 0
if (MpxRun () == isMpxEmpty)
exit (0);
#else
mpx.LoopScan ();
#endif
exit (1);
}
|