summaryrefslogtreecommitdiff
path: root/comm/OLD/TclAgent.cc
blob: a72bee0b26f855c47b3f9b508ea6283cdd15f396 (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
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
/*
 *	The Unix Channel
 *
 *	by Michel Beaudouin-Lafon
 *
 *	Copyright 1990-1993
 *	Laboratoire de Recherche en Informatique (LRI)
 *
 *	An extension to Tcl for using agents
 *
 *	$Id$
 *	$CurLog$
 */

#include "TextStream.h"
#include "ccu/String.h"
#include "TkMultiplexer.h"
#include <stdio.h>
#include <string.h>

extern "C" {
#include <tcl.h>
}
#undef const

class TclAgent : public UchTextService {
public:
		TclAgent (Tcl_Interp*, const char*);
		~TclAgent ();
	
	void	SetGotServerCmd (const char*);
	void	SetLostServerCmd (const char*);
	void	SetAbandonCmd (const char*);
	void	SetClosingCmd (const char*);
inline	const char*	GetName () const	{ return tclName; }

	
protected:
	CcuString	tclName;
	Tcl_Interp*	interp;
	CcuString		gotServerCmd;
	CcuString		lostServerCmd;
	CcuString		abandonCmd;

	Tcl_CmdBuf	buffer;
	
	cmd_res	Execute (const UchTextLine&);
	void	LostServer ();
	void	GotServer ();
	void	AbandonRestart ();
};


TclAgent :: TclAgent (Tcl_Interp* i, const char* nm)
: UchTextService (),
  tclName (nm),
  gotServerCmd (),
  lostServerCmd (),
  abandonCmd ()
{
	interp = i;	
	buffer = Tcl_CreateCmdBuf ();
}

void
TclAgent :: SetGotServerCmd (const char* cmd)
{
	gotServerCmd = cmd;
}

void
TclAgent :: SetLostServerCmd (const char* cmd)
{
	lostServerCmd = cmd;
}

void
TclAgent :: SetAbandonCmd (const char* cmd)
{
	abandonCmd = cmd;
}

TclAgent :: ~TclAgent ()
{
	Tcl_DeleteCmdBuf (buffer);
}

/*
 *	eval the following command:
 *		catch { agentname.cmd args }
 *	where cmd is the first word of the line, i.e. the reply sent by the agent
 *	and agentname the name of the agent as defined with the 'agent' command.
 *	catch allows to handle undefined handlers for replies gracefully.
 */
UchTextStream::cmd_res
TclAgent :: Execute (const UchTextLine& line)
{
	char buf [1024];
	char* p;
	
	
	sprintf (buf, "catch { %s.%s ", tclName, (const char*) (line [0]));
	p = buf + strlen (buf);
	
	for (int i = 1; i < line.NumWords (); i++) {
		if (line[i].IsInt ())
			sprintf (p, "%d ", int (line[i]));
		else
			sprintf (p, "{%s} ", (const char*) (line[i]));
		p += strlen (p);
	}
	strcpy (p, "}");
	
	if (Tcl_GlobalEval (interp, buf) == TCL_OK)
		return isCmdOk;
	return isCmdError;
}

void
TclAgent :: LostServer ()
{
	UchTextService::LostServer ();
	
	if (lostServerCmd)
		Tcl_GlobalEval (interp, (char*)(const char*)lostServerCmd);
}

void
TclAgent :: GotServer ()
{
	UchTextService::GotServer ();

	if (gotServerCmd)
		Tcl_GlobalEval (interp, (char*)(const char*)gotServerCmd);
}

void
TclAgent :: AbandonRestart ()
{
	if (abandonCmd)
		Tcl_GlobalEval (interp, (char*)(const char*) abandonCmd);
	
	Tcl_DeleteCommand (interp, (char*)(const char*) tclName);
	CloseNow ();
}

//---------------- Tcl commands

/*
 *	command to send requests to an agent and to operate an agent
 *		agentname send request args...
 *		agentname status
 *			returns unavailable, error, running, lost
 *		agentname close
 *		agentname closenow
 */
int
AgentCmd (ClientData data, Tcl_Interp* interp, int argc, char* argv [])
{
	TclAgent*	agent = (TclAgent*) data;
	
	/* check arguments: at least two */
	if (argc < 2) {
		Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
			" status | close | closenow | send request [args ...]\"", (char *) NULL);
		return TCL_ERROR;
	}
	
	const char* cmd = argv [1];
	
	/* send command */
	if (strcmp (cmd, "send") == 0) {
		if (argc < 3) {
			Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
				" send request [args ...]\"", (char *) NULL);
			return TCL_ERROR;
		}
	
		for (int i = 2; i < argc; i++) {
			agent->Append (argv [i]);
			if (i < argc -1)
				agent->Append (" ");
		}
		agent->Send ("\n");
		return TCL_OK;
	}
	
	/* simple commands */
	if (argc != 2) {
		Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
			" status | close | closenow | send request [args ...]\"", (char *) NULL);
		return TCL_ERROR;
	}
	
	if (strcmp (cmd, "status") == 0) {
		switch (agent->GetStatus ()) {
		case UchTextService::isUnavailable:
			sprintf (interp->result, "unavailable");
			break;
		case UchTextService::isError:
			sprintf (interp->result, "error");
			break;
		case UchTextService::isRunning:
			sprintf (interp->result, "running");
			break;
		case UchTextService::isLost:
			sprintf (interp->result, "lost");
			break;
		}
	} else if (strcmp (cmd, "close") == 0) {
		Tcl_DeleteCommand (interp, (char*) agent->GetName ());
		agent->Close ();
	} else if (strcmp (cmd, "closenow") == 0) {
		Tcl_DeleteCommand (interp, (char*) agent->GetName ());
		agent->CloseNow ();
	} else {
		Tcl_AppendResult(interp, "bad option name: \"", cmd,
			"\"", (char *) NULL);
		return TCL_ERROR;
	}
	
	return TCL_OK;
}

/*
 *	command to create a new agent:
 *		agent name [-option value ...]
 */
int
CreateAgentCmd (ClientData, Tcl_Interp* interp, int argc, char* argv [])
{
	TclAgent*	agent;
	
	/* check arguments: at least two, and even number overall */
	if (argc < 2 || (argc & 01) != 0) {
		Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
			" name [-option value ...]\"", (char *) NULL);
		return TCL_ERROR;
	}
	
	char* name = argv [1];
	char* host = 0;
	agent = new TclAgent (interp, name);
	
	for (int i = 2; i < argc; i++) {
		char* option = argv [i++];
		char* value = argv [i];
		if (strcmp (option, "-host") == 0)
			host = value;
		else
		if (strcmp (option, "-name") == 0)
			name = value;
		else
		if (strcmp (option, "-init") == 0)
			agent->SetGotServerCmd (value);
		else
		if (strcmp (option, "-lost") == 0)
			agent->SetLostServerCmd (value);
		else
		if (strcmp (option, "-abandon") == 0)
			agent->SetAbandonCmd (value);
		else {
			Tcl_AppendResult(interp, "bad option name: \"", argv[i-1],
				"\"", (char *) NULL);
			return TCL_ERROR;
		}
	}

	agent->Init (name, host);
	
	/* define the command for manipulating the source */
	Tcl_CreateCommand (interp, name, AgentCmd, (ClientData) agent, 0);
	
	return TCL_OK;
}

//---------------- initialization

/*
 *	initialize the audio gizmo
 *	returns 0 if error and leaves the message in interp->result
 */
UchTkMultiplexer mpx;

extern "C" int
InitAgent (Tcl_Interp* interp)
{
	Tcl_CreateCommand (interp, "agent", CreateAgentCmd, 0, 0);
	
	return 1;
}

extern "C" int tkmain (int, char* []);

main (int argc, char* argv [])
{
	return tkmain (argc, argv);
}