Panda3D

pStatClientControlMessage.cxx

00001 // Filename: pStatClientControlMessage.cxx
00002 // Created by:  drose (09Jul00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "config_pstats.h"
00016 #include "pStatClientControlMessage.h"
00017 #include "pStatClientVersion.h"
00018 
00019 #include "datagram.h"
00020 #include "datagramIterator.h"
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: PStatClientControlMessage::Constructor
00024 //       Access: Public
00025 //  Description:
00026 ////////////////////////////////////////////////////////////////////
00027 PStatClientControlMessage::
00028 PStatClientControlMessage() {
00029   _type = T_invalid;
00030 }
00031 
00032 ////////////////////////////////////////////////////////////////////
00033 //     Function: PStatClientControlMessage::encode
00034 //       Access: Public
00035 //  Description: Writes the message into the indicated datagram.
00036 ////////////////////////////////////////////////////////////////////
00037 void PStatClientControlMessage::
00038 encode(Datagram &datagram) const {
00039   datagram.clear();
00040   datagram.add_uint8(_type);
00041   switch (_type) {
00042   case T_hello:
00043     datagram.add_string(_client_hostname);
00044     datagram.add_string(_client_progname);
00045     datagram.add_uint16(_major_version);
00046     datagram.add_uint16(_minor_version);
00047     break;
00048 
00049   case T_define_collectors:
00050     {
00051       datagram.add_uint16(_collectors.size());
00052       for (int i = 0; i < (int)_collectors.size(); i++) {
00053         _collectors[i]->write_datagram(datagram);
00054       }
00055     }
00056     break;
00057 
00058   case T_define_threads:
00059     {
00060       datagram.add_uint16(_first_thread_index);
00061       datagram.add_uint16(_names.size());
00062       for (int i = 0; i < (int)_names.size(); i++) {
00063         datagram.add_string(_names[i]);
00064       }
00065     }
00066     break;
00067 
00068   default:
00069     pstats_cat.error()
00070       << "Invalid PStatClientControlMessage::Type " << (int)_type << "\n";
00071   }
00072 }
00073 
00074 ////////////////////////////////////////////////////////////////////
00075 //     Function: PStatClientControlMessage::decode
00076 //       Access: Public
00077 //  Description: Extracts the message from the indicated datagram.
00078 //               Returns true on success, false on error.
00079 ////////////////////////////////////////////////////////////////////
00080 bool PStatClientControlMessage::
00081 decode(const Datagram &datagram, PStatClientVersion *version) {
00082   DatagramIterator source(datagram);
00083   _type = (Type)source.get_uint8();
00084 
00085   switch (_type) {
00086   case T_hello:
00087     _client_hostname = source.get_string();
00088     _client_progname = source.get_string();
00089     if (source.get_remaining_size() == 0) {
00090       _major_version = 1;
00091       _minor_version = 0;
00092     } else {
00093       _major_version = source.get_uint16();
00094       _minor_version = source.get_uint16();
00095     }
00096     break;
00097 
00098   case T_define_collectors:
00099     {
00100       int num = source.get_uint16();
00101       _collectors.clear();
00102       for (int i = 0; i < num; i++) {
00103         PStatCollectorDef *def = new PStatCollectorDef;
00104         def->read_datagram(source, version);
00105         _collectors.push_back(def);
00106       }
00107     }
00108     break;
00109 
00110   case T_define_threads:
00111     {
00112       _first_thread_index = source.get_uint16();
00113       int num = source.get_uint16();
00114       _names.clear();
00115       for (int i = 0; i < num; i++) {
00116         _names.push_back(source.get_string());
00117       }
00118     }
00119     break;
00120 
00121   case T_datagram:
00122     // Not, strictly speaking, a control message.
00123     return false;
00124 
00125   default:
00126     pstats_cat.error()
00127       << "Read invalid PStatClientControlMessage type: " << (int)_type << "\n";
00128     _type = T_invalid;
00129     return false;
00130   }
00131 
00132   return true;
00133 }
 All Classes Functions Variables Enumerations