Panda3D

pStatClientControlMessage.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 pStatClientControlMessage.cxx
10 * @author drose
11 * @date 2000-07-09
12 */
13
14 #include "config_pstatclient.h"
15 #include "pStatClientControlMessage.h"
16 #include "pStatClientVersion.h"
17
18 #include "datagram.h"
19 #include "datagramIterator.h"
20
21 /**
22 *
23 */
24 PStatClientControlMessage::
25 PStatClientControlMessage() {
26 _type = T_invalid;
27 }
28
29 /**
30 * Writes the message into the indicated datagram.
31 */
32 void PStatClientControlMessage::
33 encode(Datagram &datagram) const {
34 datagram.clear();
35 datagram.add_uint8(_type);
36 switch (_type) {
37 case T_hello:
38 datagram.add_string(_client_hostname);
39 datagram.add_string(_client_progname);
40 datagram.add_uint16(_major_version);
41 datagram.add_uint16(_minor_version);
42 break;
43
44 case T_define_collectors:
45 {
46 datagram.add_uint16(_collectors.size());
47 for (int i = 0; i < (int)_collectors.size(); i++) {
48 _collectors[i]->write_datagram(datagram);
49 }
50 }
51 break;
52
53 case T_define_threads:
54 {
55 datagram.add_uint16(_first_thread_index);
56 datagram.add_uint16(_names.size());
57 for (int i = 0; i < (int)_names.size(); i++) {
58 datagram.add_string(_names[i]);
59 }
60 }
61 break;
62
63 default:
64 pstats_cat.error()
65 << "Invalid PStatClientControlMessage::Type " << (int)_type << "\n";
66 }
67 }
68
69 /**
70 * Extracts the message from the indicated datagram. Returns true on success,
71 * false on error.
72 */
73 bool PStatClientControlMessage::
74 decode(const Datagram &datagram, PStatClientVersion *version) {
75 DatagramIterator source(datagram);
76 _type = (Type)source.get_uint8();
77
78 switch (_type) {
79 case T_hello:
80 _client_hostname = source.get_string();
81 _client_progname = source.get_string();
82 if (source.get_remaining_size() == 0) {
83 _major_version = 1;
84 _minor_version = 0;
85 } else {
86 _major_version = source.get_uint16();
87 _minor_version = source.get_uint16();
88 }
89 break;
90
91 case T_define_collectors:
92 {
93 int num = source.get_uint16();
94 _collectors.clear();
95 for (int i = 0; i < num; i++) {
96 PStatCollectorDef *def = new PStatCollectorDef;
97 def->read_datagram(source, version);
98 _collectors.push_back(def);
99 }
100 }
101 break;
102
103 case T_define_threads:
104 {
105 _first_thread_index = source.get_uint16();
106 int num = source.get_uint16();
107 _names.clear();
108 for (int i = 0; i < num; i++) {
109 _names.push_back(source.get_string());
110 }
111 }
112 break;
113
114 case T_datagram:
115 // Not, strictly speaking, a control message.
116 return false;
117
118 default:
119 pstats_cat.error()
120 << "Read invalid PStatClientControlMessage type: " << (int)_type << "\n";
121 _type = T_invalid;
122 return false;
123 }
124
125 return true;
126 }
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition: datagramIterator.h:27
DatagramIterator::get_uint8
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
Definition: datagramIterator.I:72
DatagramIterator::get_uint16
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
Definition: datagramIterator.I:145
DatagramIterator::get_string
std::string get_string()
Extracts a variable-length string.
Definition: datagramIterator.cxx:26
DatagramIterator::get_remaining_size
size_t get_remaining_size() const
Return the bytes left in the datagram.
Definition: datagramIterator.I:454
Datagram
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
Datagram::add_uint8
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:50
Datagram::clear
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
Definition: datagram.cxx:35
Datagram::add_uint16
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
Datagram::add_string
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:219
PStatClientControlMessage::decode
bool decode(const Datagram &datagram, PStatClientVersion *version)
Extracts the message from the indicated datagram.
Definition: pStatClientControlMessage.cxx:74
PStatClientControlMessage::encode
void encode(Datagram &datagram) const
Writes the message into the indicated datagram.
Definition: pStatClientControlMessage.cxx:33
PStatClientVersion
Records the version number of a particular client.
Definition: pStatClientVersion.h:26
PStatCollectorDef
Defines the details about the Collectors: the name, the suggested color, etc.
Definition: pStatCollectorDef.h:29
PStatCollectorDef::read_datagram
void read_datagram(DatagramIterator &source, PStatClientVersion *version)
Extracts the collectorDef definition from the datagram.
Definition: pStatCollectorDef.cxx:92
config_pstatclient.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagramIterator.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
datagram.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pStatClientControlMessage.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pStatClientVersion.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.