Panda3D
textStats.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file textStats.cxx
10  * @author drose
11  * @date 2000-07-12
12  */
13 
14 #include "textStats.h"
15 #include "textMonitor.h"
16 
17 #include "pStatServer.h"
18 #include "config_pstatclient.h"
19 
20 #include <signal.h>
21 
22 static bool user_interrupted = false;
23 
24 // This simple signal handler lets us know when the user has pressed
25 // control-C, so we can clean up nicely.
26 static void signal_handler(int) {
27  user_interrupted = true;
28 }
29 
30 /**
31  *
32  */
33 TextStats::
34 TextStats() {
35  set_program_brief("text-based PStats client");
36  set_program_description
37  ("This is a simple PStats server that listens on a TCP port for a "
38  "connection from a PStatClient in a Panda player. It will then report "
39  "frame rate and timing information sent by the player.");
40 
41  add_option
42  ("p", "port", 0,
43  "Specify the TCP port to listen for connections on. By default, this "
44  "is taken from the pstats-host Config variable.",
45  &TextStats::dispatch_int, nullptr, &_port);
46 
47  add_option
48  ("r", "", 0,
49  "Show the raw frame data, in addition to boiling it down to a total "
50  "time per collector.",
51  &TextStats::dispatch_none, &_show_raw_data, nullptr);
52 
53  add_option
54  ("o", "filename", 0,
55  "Filename where to print. If not given then stderr is being used.",
56  &TextStats::dispatch_string, &_got_outputFileName, &_outputFileName);
57 
58  _outFile = nullptr;
59  _port = pstats_port;
60 }
61 
62 
63 /**
64  *
65  */
66 PStatMonitor *TextStats::
67 make_monitor() {
68 
69  return new TextMonitor(this, _outFile, _show_raw_data);
70 }
71 
72 
73 /**
74  *
75  */
76 void TextStats::
77 run() {
78  // Set up a global signal handler to catch Interrupt (Control-C) so we can
79  // clean up nicely if the user stops us.
80  signal(SIGINT, &signal_handler);
81 
82  if (!listen(_port)) {
83  nout << "Unable to open port.\n";
84  exit(1);
85  }
86 
87  nout << "Listening for connections.\n";
88 
89  if (_got_outputFileName) {
90  _outFile = new std::ofstream(_outputFileName.c_str(), std::ios::out);
91  } else {
92  _outFile = &(nout);
93  }
94 
95  main_loop(&user_interrupted);
96  nout << "Exiting.\n";
97 }
98 
99 
100 int main(int argc, char *argv[]) {
101  TextStats prog;
102  prog.parse_command_line(argc, argv);
103  prog.run();
104  return 0;
105 }
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A simple, scrolling-text stats server.
Definition: textStats.h:29
This is an abstract class that presents the interface to any number of different front-ends for the s...
Definition: pStatMonitor.h:39
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void main_loop(bool *interrupt_flag=nullptr)
An alternative to repeatedly calling poll(), this function yields control of the program to the PStat...
A simple, scrolling-text stats monitor.
Definition: textMonitor.h:30
bool listen(int port=-1)
Establishes a port number that the manager will listen on for TCP connections.
Definition: pStatServer.cxx:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.