summaryrefslogtreecommitdiff
path: root/src/pmview/main.cpp
blob: 115a90b6af0d348b1a3df9edbd533a2ecf45c5ae (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 * Copyright (c) 2009, Aconex.  All Rights Reserved.
 * 
 * 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.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
#include <QtCore/QSettings>
#include <QtCore/QTextStream>
#include <QtGui/QMessageBox>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoBaseColor.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoTranslation.h>
#include "qed_fileiconprovider.h"
#include "qed_timecontrol.h"
#include "qed_console.h"
#include "scenegroup.h"
#include "pcpcolor.h"
#include "modlist.h"
#include "viewobj.h"
#include "main.h"

#include <iostream>
using namespace std;

int Cflag;
int Lflag;
int Wflag;
char *outgeometry;
Settings globalSettings;

float theScale = 1.0;
const int theBufferLen = 2048;
char theBuffer[theBufferLen];

// Globals used to provide single instances of classes used across pmview
SceneGroup *liveGroup;	// one metrics class group for all hosts
SceneGroup *archiveGroup;	// one metrics class group for all archives
SceneGroup *activeGroup;	// currently active metric fetchgroup
QedTimeControl *pmtime;		// one timecontrol class for pmtime
PmView *pmview;

static const char *options = "A:a:Cc:D:g:h:Ln:O:p:S:T:t:VzZ:?";

static void usage(void)
{
    pmprintf("Usage: %s [options] [sources]\n\n"
"Options:\n"
"  -A align      align sample times on natural boundaries\n"
"  -a archive    add PCP log archive to metrics source list\n"
"  -c configfile initial view to load\n"
"  -C            with -c, parse config, report any errors and exit\n"
"  -CC           like -C, but also connect to pmcd to check semantics\n"
"  -g geometry   image geometry Width x Height (WxH)\n"
"  -h host       add host to list of live metrics sources\n"
"  -L            directly fetch metrics from localhost, PMCD is not used\n"
"  -O offset     initial offset into the time window\n"
"  -p port       port name for connection to existing time control\n"
"  -S starttime  start of the time window\n"
"  -T endtime    end of the time window\n"
"  -t interval   sample interval [default: %d seconds]\n"
"  -V            display pmview version number and exit\n"
"  -Z timezone   set reporting timezone\n"
"  -z            set reporting timezone to local time of metrics source\n",
	pmProgname, (int)PmView::defaultViewDelta());
    pmflush();
    exit(1);
}

int warningMsg(const char *file, int line, const char *msg, ...)
{
    int sts = QMessageBox::Ok;
    va_list arg;
    va_start(arg, msg);

    int pos = sprintf(theBuffer, "%s: Warning: ", pmProgname);
    pos += vsprintf(theBuffer + pos, msg, arg);
    sprintf(theBuffer+pos, "\n");

    if (pmDebug) {
	QTextStream cerr(stderr);
	cerr << file << ":" << line << ": " << theBuffer << endl;
    }

    sts = QMessageBox::warning(pmview, "Warning", theBuffer, sts, sts);
    va_end(arg);

    return sts;
}

double QmcTime::secondsToUnits(double value, QmcTime::DeltaUnits units)
{
    switch (units) {
    case Milliseconds:
	value = value * 1000.0;
	break;
    case Minutes:
	value = value / 60.0;
	break;
    case Hours:
	value = value / (60.0 * 60.0);
	break;
    case Days:
	value = value / (60.0 * 60.0 * 24.0);
	break;
    case Weeks:
	value = value / (60.0 * 60.0 * 24.0 * 7.0);
	break;
    case Seconds:
    default:
	break;
    }
    return value;
}

double QmcTime::deltaValue(QString delta, QmcTime::DeltaUnits units)
{
    return QmcTime::secondsToUnits(delta.trimmed().toDouble(), units);
}

QString QmcTime::deltaString(double value, QmcTime::DeltaUnits units)
{
    QString delta;

    value = QmcTime::secondsToUnits(value, units);
    if ((double)(int)value == value)
	delta.sprintf("%.2f", value);
    else
	delta.sprintf("%.6f", value);
    return delta;
}

void writeSettings(void)
{
    QSettings userSettings;

    userSettings.beginGroup(pmProgname);
    if (globalSettings.viewDeltaModified) {
	globalSettings.viewDeltaModified = false;
	userSettings.setValue("viewDelta", globalSettings.viewDelta);
    }
    if (globalSettings.loggerDeltaModified) {
	globalSettings.loggerDeltaModified = false;
	userSettings.setValue("loggerDelta", globalSettings.loggerDelta);
    }
    if (globalSettings.viewBackgroundModified) {
	globalSettings.viewBackgroundModified = false;
	userSettings.setValue("viewBackgroundColor",
				globalSettings.viewBackgroundName);
    }
    if (globalSettings.viewHighlightModified) {
	globalSettings.viewHighlightModified = false;
	userSettings.setValue("viewHighlightColor",
				globalSettings.viewHighlightName);
    }
    if (globalSettings.gridBackgroundModified) {
	globalSettings.gridBackgroundModified = false;
	userSettings.setValue("gridBackgroundColor",
				globalSettings.gridBackgroundName);
    }
    if (globalSettings.initialToolbarModified) {
	globalSettings.initialToolbarModified = false;
	userSettings.setValue("initialToolbar", globalSettings.initialToolbar);
    }
    if (globalSettings.nativeToolbarModified) {
	globalSettings.nativeToolbarModified = false;
	userSettings.setValue("nativeToolbar", globalSettings.nativeToolbar);
    }
    if (globalSettings.toolbarLocationModified) {
	globalSettings.toolbarLocationModified = false;
	userSettings.setValue("toolbarLocation",
				globalSettings.toolbarLocation);
    }
    if (globalSettings.toolbarActionsModified) {
	globalSettings.toolbarActionsModified = false;
	userSettings.setValue("toolbarActions", globalSettings.toolbarActions);
    }
    userSettings.endGroup();
}

void readSettings(void)
{
    QSettings userSettings;
    userSettings.beginGroup(pmProgname);

    //
    // Parameters related to sampling
    //
    globalSettings.viewDeltaModified = false;
    globalSettings.viewDelta = userSettings.value("viewDelta",
				PmView::defaultViewDelta()).toDouble();
    globalSettings.loggerDeltaModified = false;
    globalSettings.loggerDelta = userSettings.value("loggerDelta",
				PmView::defaultLoggerDelta()).toDouble();

    //
    // Everything colour related
    //
    globalSettings.viewBackgroundModified = false;
    globalSettings.viewBackgroundName = userSettings.value(
		"viewBackgroundColor", "black").toString();
    globalSettings.viewBackground = QColor(globalSettings.viewBackgroundName);

    globalSettings.viewHighlightModified = false;
    globalSettings.viewHighlightName = userSettings.value(
		"viewHighlightColor", "white").toString();
    globalSettings.viewHighlight = QColor(globalSettings.viewHighlightName);

    globalSettings.gridBackgroundModified = false;
    globalSettings.gridBackgroundName = userSettings.value(
		"gridBackgroundColor", "rgbi:0.15/0.15/0.15").toString();
    globalSettings.gridBackground = QColor(globalSettings.gridBackgroundName);

    //
    // Toolbar user preferences
    //
    globalSettings.initialToolbarModified = false;
    globalSettings.initialToolbar = userSettings.value(
					"initialToolbar", 1).toInt();
    globalSettings.nativeToolbarModified = false;
    globalSettings.nativeToolbar = userSettings.value(
					"nativeToolbar", 1).toInt();
    globalSettings.toolbarLocationModified = false;
    globalSettings.toolbarLocation = userSettings.value(
					"toolbarLocation", 0).toInt();
    QStringList actionList;
    globalSettings.toolbarActionsModified = false;
    if (userSettings.contains("toolbarActions") == true)
	globalSettings.toolbarActions =
			userSettings.value("toolbarActions").toStringList();
    // else: (defaults come from the pmview.ui interface specification)

    userSettings.endGroup();
}

int
genInventor(void)
{
    int sts = 0;
    const char *configfile;
    QTextStream cerr(stderr);

    if (theConfigName.length()) {
	configfile = strdup((const char *)theConfigName.toAscii());
	if (!(yyin = fopen(configfile, "r"))) {
	    pmprintf(
		"%s: Error: Unable to open configuration file \"%s\": %s\n",
		pmProgname, configfile, strerror(errno));
	    return -1;
	}
	theAltConfigName = theConfigName;
    } else {
	configfile = mktemp(strdup("/tmp/pmview.XXXXXX"));
	if (pmDebug & DBG_TRACE_APPL0)
	    cerr << "genInventor: Copy of configuration saved to "
		 << configfile << endl;
	if (!(theAltConfig = fopen(configfile, "w"))) {
            pmprintf("%s: Warning: Unable to save configuration for "
		     "recording to \"%s\": %s\n",
		    pmProgname, configfile, strerror(errno));
	}
	theAltConfigName = configfile;
    }

    yyparse();

    if (theAltConfig)
	fclose(theAltConfig);

    if (pmDebug & DBG_TRACE_APPL0) {
	cerr << pmProgname << ": " << errorCount << " errors detected in "
	     << theConfigName << endl;
    }

    sts = -errorCount;

    if (rootObj != NULL) {
	rootObj->setTran(0, 0, rootObj->width(), rootObj->depth());
	
	SoSeparator *sep = new SoSeparator;
	SoTranslation *tran = new SoTranslation();
	tran->translation.setValue(rootObj->width() / -2.0, 0.0,
				   rootObj->depth() / -2.0);
	sep->addChild(tran);

	if (pmDebug & DBG_TRACE_APPL0 ||
	    pmDebug & DBG_TRACE_APPL1 ||
	    pmDebug & DBG_TRACE_APPL2) {
	    SoBaseColor *col = new SoBaseColor;
	    col->rgb.setValue(1.0, 0.0, 0.0);
	    sep->addChild(col);
	    SoCube * cube  = new SoCube;
	    cube->width = 10;
	    cube->depth = 5.0;
	    cube->height = 25;
	    sep->addChild(cube);
	}

	sep->addChild(rootObj->root());
	theModList->setRoot(sep);
    }

    if ((ViewObj::numModObjects() == 0 || theModList->size() == 0) && 
	 elementalNodeList.getLength() == 0) {
	pmprintf("%s: No valid modulated objects in the scene\n",
		 pmProgname);
	sts--;
    }
    else if (sts < 0) {
	pmprintf("%s: Unrecoverable errors in the configuration file %s\n",
	    pmProgname, (const char *)theConfigName.toAscii());
    }

    return sts;
}

int
main(int argc, char **argv)
{
    int			c, sts;
    int			errflg = 0;
    char		*msg;

    QedApp a(argc, argv);
    readSettings();

    liveGroup = new SceneGroup();
    archiveGroup = new SceneGroup();

    while ((c = a.getopts(options)) != EOF) {
	switch (c) {

	case 'C':
	    Cflag++;
	    break;

	case 'g':
	    outgeometry = optarg;
	    break;

	case 'c':
	    theConfigName = optarg;
	    break;

	case '?':
	default:
	    usage();
	}
    }

    a.startconsole();

    if (a.my.archives.size() > 0)
	while (optind < argc)
	    a.my.archives.append(argv[optind++]);
    else
	while (optind < argc)
	    a.my.hosts.append(argv[optind++]);

    if (optind != argc)
	errflg++;
    if (errflg)
	usage();

    if (a.my.pmnsfile && (sts = pmLoadNameSpace(a.my.pmnsfile)) < 0) {
	pmprintf("%s: %s\n", pmProgname, pmErrStr(sts));
	pmflush();
	exit(1);
    }

    // Create all of the sources
    if (a.my.Lflag) {
	liveGroup->use(PM_CONTEXT_LOCAL, QmcSource::localHost);
	Lflag = 1;
    }
    for (c = 0; c < a.my.hosts.size(); c++) {
	if (liveGroup->use(PM_CONTEXT_HOST, a.my.hosts[c]) < 0)
	    a.my.hosts.removeAt(c);
    }
    for (c = 0; c < a.my.archives.size(); c++) {
	if (archiveGroup->use(PM_CONTEXT_ARCHIVE, a.my.archives[c]) < 0)
	    a.my.archives.removeAt(c);
    }
    if (!a.my.Lflag && a.my.hosts.size() == 0 && a.my.archives.size() == 0)
	liveGroup->createLocalContext();
    pmflush();
    console->post("Metric group setup complete (%d hosts, %d archives)",
			a.my.hosts.size(), a.my.archives.size());

    if (a.my.zflag) {
	if (a.my.archives.size() > 0)
	    archiveGroup->useTZ();
	if (a.my.hosts.size() > 0)
	    liveGroup->useTZ();
    }
    else if (a.my.tz != NULL) {
	if (a.my.archives.size() > 0)
	    archiveGroup->useTZ(QString(a.my.tz));
	if (a.my.hosts.size() > 0)
	    liveGroup->useTZ(QString(a.my.tz));
	if ((sts = pmNewZone(a.my.tz)) < 0) {
	    pmprintf("%s: cannot set timezone to \"%s\": %s\n",
		    pmProgname, (char *)a.my.tz, pmErrStr(sts));
	    pmflush();
	    exit(1);
	}
    }

    //
    // Choose which View will be displayed initially - archive/live.
    // If any archives given on command line, we go Archive mode;
    // otherwise Live mode wins.  Our initial pmtime connection is
    // set in that mode too.  Later we'll make a second connection
    // in the other mode (and only "on-demand").
    //
    if (a.my.archives.size() > 0) {
	activeGroup = archiveGroup;
	archiveGroup->defaultTZ(a.my.tzLabel, a.my.tzString);
	archiveGroup->updateBounds();
	a.my.logStartTime = archiveGroup->logStart();
	a.my.logEndTime = archiveGroup->logEnd();
	if ((sts = pmParseTimeWindow(a.my.Sflag, a.my.Tflag,
				     a.my.Aflag, a.my.Oflag,
				     &a.my.logStartTime, &a.my.logEndTime,
				     &a.my.realStartTime, &a.my.realEndTime,
				     &a.my.position, &msg)) < 0) {
	    pmprintf("Cannot parse archive time window\n%s\n", msg);
	    free(msg);
	    usage();
	}
    }
    else {
	activeGroup = liveGroup;
	liveGroup->defaultTZ(a.my.tzLabel, a.my.tzString);
	gettimeofday(&a.my.logStartTime, NULL);
	a.my.logEndTime.tv_sec = a.my.logEndTime.tv_usec = INT_MAX;
	if ((sts = pmParseTimeWindow(a.my.Sflag, a.my.Tflag,
					a.my.Aflag, a.my.Oflag,
					&a.my.logStartTime, &a.my.logEndTime,
					&a.my.realStartTime, &a.my.realEndTime,
					&a.my.position, &msg)) < 0) {
	    pmprintf("Cannot parse live time window\n%s\n", msg);
	    free(msg);
	    usage();
	}
    }
    console->post("Timezones and time window setup complete");

    pmview = new PmView;
    pmtime = new QedTimeControl;
    fileIconProvider = new QedFileIconProvider();
    console->post("Phase1 user interface constructors complete");

    // Start pmtime process for time management
    pmtime->init(a.my.port, a.my.archives.size() == 0, &a.my.delta,
		 &a.my.position, &a.my.realStartTime, &a.my.realEndTime,
		 a.my.tzString, a.my.tzLabel);

    pmview->init();
    liveGroup->init(pmtime->liveInterval(), pmtime->livePosition());
    archiveGroup->init(pmtime->archiveInterval(), pmtime->archivePosition());
    console->post("Phase2 user interface setup complete");

    PCPColor::initClass();
    theModList = new ModList(pmview->viewer(), &PmView::selectionCB, NULL, NULL);
    if (genInventor() < 0) {
	pmflush();
	exit(1);
    }

    if (Cflag)	// done with -c config, quit
	return 0;

    if (pmview->view(false, 0, 1, 0, M_PI / 4.0, theGlobalScale) == false) {
	pmflush();
	exit(1);
    }

    pmview->viewer()->viewAll();
    pmview->enableUi();
    pmview->show();
    console->post("Top level window shown");

    a.connect(&a, SIGNAL(lastWindowClosed()), pmview, SLOT(quit()));
    return a.exec();
}