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"
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  */
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  */
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++) {
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 }
A class to retrieve the individual data elements previously stored in a Datagram.
uint8_t get_uint8()
Extracts an unsigned 8-bit integer.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
std::string get_string()
Extracts a variable-length string.
size_t get_remaining_size() const
Return the bytes left in the datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void add_uint8(uint8_t value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:50
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
Definition: datagram.cxx:35
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
void add_string(const std::string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:219
bool decode(const Datagram &datagram, PStatClientVersion *version)
Extracts the message from the indicated datagram.
void encode(Datagram &datagram) const
Writes the message into the indicated datagram.
Records the version number of a particular client.
Defines the details about the Collectors: the name, the suggested color, etc.
void read_datagram(DatagramIterator &source, PStatClientVersion *version)
Extracts the collectorDef definition from the datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.