Panda3D

Loading...
Searching...
No Matches
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 */
24PStatClientControlMessage::
25PStatClientControlMessage() {
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 case T_expire_thread:
64 datagram.add_uint16(_first_thread_index);
65 break;
66
67 default:
68 pstats_cat.error()
69 << "Invalid PStatClientControlMessage::Type " << (int)_type << "\n";
70 }
71}
72
73/**
74 * Extracts the message from the indicated datagram. Returns true on success,
75 * false on error.
76 */
77 bool PStatClientControlMessage::
78 decode(const Datagram &datagram, PStatClientVersion *version) {
79 DatagramIterator source(datagram);
80 _type = (Type)source.get_uint8();
81
82 switch (_type) {
83 case T_hello:
84 _client_hostname = source.get_string();
85 _client_progname = source.get_string();
86 if (source.get_remaining_size() == 0) {
87 _major_version = 1;
88 _minor_version = 0;
89 } else {
90 _major_version = source.get_uint16();
91 _minor_version = source.get_uint16();
92 }
93 break;
94
95 case T_define_collectors:
96 {
97 int num = source.get_uint16();
98 _collectors.clear();
99 for (int i = 0; i < num; i++) {
100 PStatCollectorDef *def = new PStatCollectorDef;
101 def->read_datagram(source, version);
102 _collectors.push_back(def);
103 }
104 }
105 break;
106
107 case T_define_threads:
108 {
109 _first_thread_index = source.get_uint16();
110 int num = source.get_uint16();
111 _names.clear();
112 for (int i = 0; i < num; i++) {
113 _names.push_back(source.get_string());
114 }
115 }
116 break;
117
118 case T_expire_thread:
119 _first_thread_index = source.get_uint16();
120 break;
121
122 case T_datagram:
123 // Not, strictly speaking, a control message.
124 return false;
125
126 default:
127 pstats_cat.error()
128 << "Read invalid PStatClientControlMessage type: " << (int)_type << "\n";
129 _type = T_invalid;
130 return false;
131 }
132
133 return true;
134}
DatagramIterator
A class to retrieve the individual data elements previously stored in a Datagram.
Definition datagramIterator.h:30
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:78
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.