2 // customized (for tableVisi) version of DGclient.C in tclVisi directory
6 * Revision 1.1 1995/11/04 00:44:11 tamches
7 * First version of new table visi
11 #include <stdlib.h> // exit()
18 #include "tableVisiTcl.h"
20 #include "visi/h/visualization.h"
23 void my_visi_callback(ClientData, int) {
24 // Installed as a file-handler routine for whenever data arrives over
25 // the socket returned from VisiInit()
26 if (visi_callback() == -1)
37 #define NUMRESOURCES 7
39 #define RESOURCENAME 9
40 #define STARTSTREAM 10
48 #define FIRSTBUCKET 18
56 static struct cmdTabEntry Dg_Cmds[] = {
57 {"aggregate", AGGREGATE, 2},
58 {"binwidth", BINWIDTH, 0},
59 {"firstbucket", FIRSTBUCKET, 2},
60 {"foldmethod", FOLDMETHOD, 2},
61 {"lastbucket", LASTBUCKET, 2},
62 {"metricname", METRICNAME, 1},
63 {"metricunits", METRICUNITS, 1},
64 {"numbins", NUMBINS, 0},
65 {"nummetrics", NUMMETRICS, 0},
66 {"numresources", NUMRESOURCES, 0},
67 {"phase", DEFINEPHASE, 3},
68 {"resourcename", RESOURCENAME, 1},
69 {"start", STARTSTREAM, 2},
70 {"stop", STOPSTREAM, 2},
72 {"valid", DGVALID, 2},
73 {"enabled", DGENABLED, 2},
78 int findCommand(Tcl_Interp *interp,
83 sprintf(interp->result, "USAGE: Dg <option> [args...]\n");
87 for (cmdTabEntry *C = Dg_Cmds; C->cmdname!=NULL; C++) {
88 if (strcmp(argv[0], C->cmdname) == 0) {
89 if (argc-1 == C->numargs)
90 return C->index; // successful parsing
92 sprintf(interp->result,
93 "%s: wrong number of args (%d). Should be %d\n",
94 argv[0], argc-1, C->numargs);
99 sprintf(interp->result, "unknown option (%s)\n", argv[0]);
103 int Dg_TclCommand(ClientData, Tcl_Interp *interp,
104 int argc, char *argv[]) {
105 // entrypoint to the tcl "Dg" command we've installed
106 // all the sprintf()'s are rather slow...
108 // parse the arguments, using global vrble Dg_Cmds[] to tell what's what.
109 int cmdDex = findCommand(interp, argc-1, argv+1);
110 if (cmdDex == CMDERROR)
113 int m, r, buck; // metric number, resource number, bucket number
119 sprintf(interp->result,"%g", dataGrid.AggregateValue(m,r));
123 sprintf(interp->result, "%g", dataGrid.BinWidth());
129 sprintf(interp->result,"%d", dataGrid[m][r].FirstValidBucket());
134 sprintf(interp->result,"%d", dataGrid.FoldMethod(m));
140 sprintf(interp->result,"%d", dataGrid[m][r].LastBucketFilled());
145 sprintf(interp->result, "%s", dataGrid.MetricName(m));
150 sprintf(interp->result, "%s", dataGrid.MetricUnits(m));
154 sprintf(interp->result, "%d", dataGrid.NumBins());
158 sprintf(interp->result, "%d", dataGrid.NumMetrics());
162 sprintf(interp->result, "%d", dataGrid.NumResources());
166 DefinePhase(-1.0,NULL);
171 sprintf(interp->result, "%s", dataGrid.ResourceName(r));
175 GetMetsRes(argv[2], atoi(argv[3]), 0); // 0-->histogram (1-->scalar)
188 sprintf(interp->result,"%g", dataGrid.SumValue(m,r));
194 sprintf(interp->result, "%d", dataGrid.Valid(m,r));
200 sprintf(interp->result, "%d", dataGrid[m][r].Enabled());
206 buck = atoi(argv[4]);
207 sprintf(interp->result,"%g", dataGrid[m][r].Value(buck));
211 sprintf(interp->result, "Internal error (func findCommand)\n");
215 void (*UsersNewDataCallbackRoutine)(int firstBucket, int lastBucket);
216 // we will call this routine for you when we get a new-data callback
217 // from the visi lib (first, we do a bit of processing for you, such
218 // as determining what the range is buckets you haven't seen yet is).
220 extern void panic(const char *);
222 int Dg2_Init(Tcl_Interp *interp) {
223 // initialize with the visi lib
226 cerr << "Dg2_Init() -- could not initialize with the visi lib" << endl;
230 // Register C++ Callback routines with the visi lib when
231 // certain events happen. The most important (performance-wise)
232 // is the DATAVALUES callback, which signals the arrival of
233 // new barchart data. We must process this callback very quickly,
234 // in order to perturb the system as little as possible.
236 if (RegistrationCallback(ADDMETRICSRESOURCES, Dg2AddMetricsCallback) != 0)
237 panic("Dg2_Init() -- couldn't install ADDMETRICSRESOURCES callback");
239 if (RegistrationCallback(FOLD, Dg2Fold) != 0)
240 panic("Dg2_Init() -- couldn't install FOLD callback");
242 if (RegistrationCallback(INVALIDMETRICSRESOURCES, Dg2InvalidMetricsOrResources) != 0)
243 panic("Dg2_Init() -- couldn't install INVALID callback");
245 if (RegistrationCallback(PHASESTART, Dg2PhaseNameCallback) != 0)
246 panic("Dg2_Init() -- couldn't install PHASENAME callback");
248 if (RegistrationCallback(DATAVALUES, Dg2NewDataCallback) != 0)
249 panic("Dg2_Init() -- couldn't install DATAVALUES callback");
251 // install "Dg" as a new tcl command; Dg_TclCommand() will be invoked when
252 // a tcl script calls Dg
253 Tcl_CreateCommand(interp, "Dg", Dg_TclCommand,
254 (ClientData *) NULL,(Tcl_CmdDeleteProc *) NULL);
256 // Arrange for my_visi_callback() to be called whenever data is waiting
257 // to be read off of descriptor "fd". Extremely important! [tcl book
259 Tk_CreateFileHandler(fd, TK_READABLE, my_visi_callback, 0);