Panda3D
 All Classes Functions Variables Enumerations
pStatClientControlMessage.cxx
1 // Filename: pStatClientControlMessage.cxx
2 // Created by: drose (09Jul00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "config_pstats.h"
16 #include "pStatClientControlMessage.h"
17 #include "pStatClientVersion.h"
18 
19 #include "datagram.h"
20 #include "datagramIterator.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: PStatClientControlMessage::Constructor
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 PStatClientControlMessage::
28 PStatClientControlMessage() {
29  _type = T_invalid;
30 }
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: PStatClientControlMessage::encode
34 // Access: Public
35 // Description: Writes the message into the indicated datagram.
36 ////////////////////////////////////////////////////////////////////
38 encode(Datagram &datagram) const {
39  datagram.clear();
40  datagram.add_uint8(_type);
41  switch (_type) {
42  case T_hello:
43  datagram.add_string(_client_hostname);
44  datagram.add_string(_client_progname);
45  datagram.add_uint16(_major_version);
46  datagram.add_uint16(_minor_version);
47  break;
48 
49  case T_define_collectors:
50  {
51  datagram.add_uint16(_collectors.size());
52  for (int i = 0; i < (int)_collectors.size(); i++) {
53  _collectors[i]->write_datagram(datagram);
54  }
55  }
56  break;
57 
58  case T_define_threads:
59  {
60  datagram.add_uint16(_first_thread_index);
61  datagram.add_uint16(_names.size());
62  for (int i = 0; i < (int)_names.size(); i++) {
63  datagram.add_string(_names[i]);
64  }
65  }
66  break;
67 
68  default:
69  pstats_cat.error()
70  << "Invalid PStatClientControlMessage::Type " << (int)_type << "\n";
71  }
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: PStatClientControlMessage::decode
76 // Access: Public
77 // Description: Extracts the message from the indicated datagram.
78 // Returns true on success, false on error.
79 ////////////////////////////////////////////////////////////////////
81 decode(const Datagram &datagram, PStatClientVersion *version) {
82  DatagramIterator source(datagram);
83  _type = (Type)source.get_uint8();
84 
85  switch (_type) {
86  case T_hello:
87  _client_hostname = source.get_string();
88  _client_progname = source.get_string();
89  if (source.get_remaining_size() == 0) {
90  _major_version = 1;
91  _minor_version = 0;
92  } else {
93  _major_version = source.get_uint16();
94  _minor_version = source.get_uint16();
95  }
96  break;
97 
98  case T_define_collectors:
99  {
100  int num = source.get_uint16();
101  _collectors.clear();
102  for (int i = 0; i < num; i++) {
104  def->read_datagram(source, version);
105  _collectors.push_back(def);
106  }
107  }
108  break;
109 
110  case T_define_threads:
111  {
112  _first_thread_index = source.get_uint16();
113  int num = source.get_uint16();
114  _names.clear();
115  for (int i = 0; i < num; i++) {
116  _names.push_back(source.get_string());
117  }
118  }
119  break;
120 
121  case T_datagram:
122  // Not, strictly speaking, a control message.
123  return false;
124 
125  default:
126  pstats_cat.error()
127  << "Read invalid PStatClientControlMessage type: " << (int)_type << "\n";
128  _type = T_invalid;
129  return false;
130  }
131 
132  return true;
133 }
void add_uint8(PN_uint8 value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:138
void add_string(const string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:351
virtual void clear()
Resets the datagram to empty, in preparation for building up a new datagram.
Definition: datagram.cxx:41
void encode(Datagram &datagram) const
Writes the message into the indicated datagram.
PN_uint8 get_uint8()
Extracts an unsigned 8-bit integer.
string get_string()
Extracts a variable-length string.
PN_uint16 get_uint16()
Extracts an unsigned 16-bit integer.
int get_remaining_size() const
Return the bytes left in the datagram.
bool decode(const Datagram &datagram, PStatClientVersion *version)
Extracts the message from the indicated datagram.
Records the version number of a particular client.
void add_uint16(PN_uint16 value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:181
A class to retrieve the individual data elements previously stored in a Datagram. ...
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.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43