BinarySerialization.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #ifndef BLOCXX_BinarySerialization_HPP_
00039 #define BLOCXX_BinarySerialization_HPP_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/Types.hpp"
00042 #include "blocxx/Bool.hpp"
00043 #include "blocxx/String.hpp"
00044 #include "blocxx/Array.hpp"
00045 #include "blocxx/ByteSwap.hpp"
00046 
00047 #if defined(BLOCXX_HAVE_OSTREAM) && defined(BLOCXX_HAVE_ISTREAM)
00048 #include <ostream>
00049 #include <istream>
00050 #else
00051 #include <iostream>
00052 #endif
00053 
00054 // The classes and functions defined in this file are not meant for general
00055 // use, they are internal implementation details.  They may change at any time.
00056 
00057 namespace BLOCXX_NAMESPACE
00058 {
00059 
00060 // Values for local API calls
00061 // This should be kept in sync with the repository version in HDBCommon.hpp
00062 // The general idea is to have it be a concatenation of release numbers with
00063 // a revision on the end (to prevent problems during development)
00064 // So 3000003 originated from version 3.0.0 rev 4
00065 //
00066 // 8/21/2003 - Changed from 3000003 to 3000004 because of a change in the
00067 //   structure of CIMInstance.  The name and alias were removed.
00068 // 10/25/2003 - 3000005. Changed enumClassNames to send over an enum of strings
00069 //   instead of object paths.
00070 // 2/6/2004 - 3000006. Changed CIMDateTime::{read,write}Object to write each
00071 //   item individually so struct packing doesn't cause incompatibilities.
00072 // 2/20/2004 - 3000007. Changed to not send an extra unnecessary signature,
00073 //   since all the CIM objects already send a signature.  Note that the 
00074 //   repository version was not incremented.
00075 // 7/22/2004 - 3000008. Changed signatures and added versioning. Now all
00076 //   readObject() calls will be able to read older versions as well as the
00077 //   current.  Introduced MinBinaryProtocolVersion which is the oldest version
00078 //   we can sucessfully read.
00079 const UInt32 BinaryProtocolVersion = 3000008;
00080 
00081 // This is the oldest version the code can handle.
00082 const UInt32 MinBinaryProtocolVersion = 3000007;
00083 
00084 // These values are all used by the binary protocol
00085 const UInt8 BIN_OK =          0;    // Success returned from server
00086 const UInt8 BIN_ERROR =       1;    // Error returned from server
00087 const UInt8 BIN_EXCEPTION =      2;    // CIM Exception returned from server
00088 const UInt8 IPC_AUTHENTICATE =   10;   // Authenticate
00089 const UInt8 IPC_FUNCTIONCALL =   11;   // Regular function call
00090 const UInt8 IPC_CLOSECONN =      12;   // Close connection
00091 const UInt8 BIN_DELETECLS =      20;      // Delete class
00092 const UInt8 BIN_DELETEINST =     21;      // Delete instance
00093 const UInt8 BIN_DELETEQUAL =     22;      // Delete qualifier type
00094 const UInt8 BIN_ENUMCLSS =    23;      // Enum class
00095 const UInt8 BIN_ENUMCLSNAMES =   24;      // Enum class names
00096 const UInt8 BIN_ENUMINSTS =      25;      // Enum instances
00097 const UInt8 BIN_ENUMINSTNAMES =  26;      // Enum instance names
00098 const UInt8 BIN_ENUMQUALS =      27;      // Enum qualifiers types
00099 const UInt8 BIN_GETCLS =         28;      // Get class
00100 const UInt8 BIN_GETINST =        29;      // Get instance
00101 const UInt8 BIN_INVMETH =        30;      // Invoke method
00102 const UInt8 BIN_GETQUAL =        31;      // Get qualifier type
00103 const UInt8 BIN_SETQUAL =        32;      // Set qualifier type
00104 const UInt8 BIN_MODIFYCLS =      33;      // Modify class
00105 const UInt8 BIN_CREATECLS =      34;      // Create class
00106 const UInt8 BIN_MODIFYINST =     35;      // Modify instances
00107 const UInt8 BIN_CREATEINST =     36;      // Create instance
00108 const UInt8 BIN_GETPROP =        37;      // Get property
00109 const UInt8 BIN_SETPROP =        38;      // Set property
00110 #ifndef BLOCXX_DISABLE_ASSOCIATION_TRAVERSAL
00111 const UInt8 BIN_ASSOCNAMES =     39;      // Associator names
00112 const UInt8 BIN_ASSOCIATORS =    40;      // Associators
00113 const UInt8 BIN_REFNAMES =    41;      // Reference names
00114 const UInt8 BIN_REFERENCES =     42;      // References
00115 #endif
00116 const UInt8 BIN_EXECQUERY =      43;      // Execute query
00117 const UInt8 BIN_GETSVRFEATURES = 44;      // Get Server Features
00118 const UInt8 BINSIG_NS   =        100;
00119 const UInt8 BINSIG_OP =       101;
00120 const UInt8 BINSIG_CLS =         102;
00121 const UInt8 BINSIG_INST =        103;
00122 const UInt8 BINSIG_BOOL =        104;
00123 const UInt8 BINSIG_CLSENUM =     105;
00124 const UInt8 BINSIG_STR =         106;
00125 const UInt8 BINSIG_STRARRAY =    107;
00126 const UInt8 BINSIG_QUAL_TYPE =         108;
00127 const UInt8 BINSIG_VALUE =    109;
00128 const UInt8 BINSIG_OPENUM =      110;
00129 const UInt8 BINSIG_INSTENUM =    111;
00130 const UInt8 BINSIG_QUAL_TYPEENUM =     112;
00131 const UInt8 BINSIG_VALUEARRAY =  113;
00132 const UInt8 BINSIG_PARAMVALUEARRAY =   114;
00133 const UInt8 BINSIG_STRINGENUM =  115;
00134 const UInt8 END_CLSENUM =        150;
00135 const UInt8 END_OPENUM =         151;
00136 const UInt8 END_INSTENUM =    152;
00137 const UInt8 END_QUALENUM =    153;
00138 const UInt8 END_STRINGENUM =     154;
00140 namespace BinarySerialization
00141 {
00142    BLOCXX_COMMON_API void write(std::ostream& ostrm, const void* dataOut,
00143       int dataOutLen);
00144    BLOCXX_COMMON_API void verifySignature(std::istream& istrm, UInt8 validSig);
00145    inline void write(std::ostream& ostrm, Int32 val)
00146    {
00147       val = hton32(val);
00148       BinarySerialization::write(ostrm, &val, sizeof(val));
00149    }
00150    inline void write(std::ostream& ostrm, UInt32 val)
00151    {
00152       val = hton32(val);
00153       BinarySerialization::write(ostrm, &val, sizeof(val));
00154    }
00155    BLOCXX_COMMON_API void writeLen(std::ostream& ostrm, UInt32 len);
00156    inline void write(std::ostream& ostrm, UInt8 val)
00157    {
00158       BinarySerialization::write(ostrm, &val, sizeof(val));
00159    }
00160    inline void write(std::ostream& ostrm, UInt16 val)
00161    {
00162       val = hton16(val);
00163       BinarySerialization::write(ostrm, &val, sizeof(val));
00164    }
00165    inline void write(std::ostream& ostrm, Int16 val)
00166    {
00167       val = hton16(val);
00168       BinarySerialization::write(ostrm, &val, sizeof(val));
00169    }
00170    inline void write(std::ostream& ostrm, const String& str)
00171    {
00172       str.writeObject(ostrm);
00173    }
00174    inline void writeBool(std::ostream& ostrm,
00175       Bool arg)
00176    {
00177       BinarySerialization::write(ostrm, BINSIG_BOOL);
00178       arg.writeObject(ostrm);
00179    }
00180    inline void writeString(std::ostream& ostrm, const String& str)
00181    {
00182       BinarySerialization::write(ostrm, BINSIG_STR);
00183       str.writeObject(ostrm);
00184    }
00185    BLOCXX_COMMON_API void readLen(std::istream& istrm, UInt32& len);
00187    template <typename T>
00188    inline void
00189    readArray(std::istream& istr, T& a)
00190    {
00191       a.clear();
00192       UInt32 len;
00193       BinarySerialization::readLen(istr, len);
00194       
00195       a.reserve(len);
00196       for (UInt32 i = 0; i < len; i++)
00197       {
00198          typename T::value_type x;
00199          x.readObject(istr);
00200          a.push_back(x);
00201       }
00202    
00203    }
00204    
00206    template <typename T>
00207    inline void
00208    writeArray(std::ostream& ostrm, const T& a)
00209    {
00210       UInt32 len = static_cast<UInt32>(a.size());
00211       BinarySerialization::writeLen(ostrm, len);
00212       for (UInt32 i = 0; i < len; i++)
00213       {
00214          a.operator[](i).writeObject(ostrm);
00215       }
00216    }
00217    inline void writeStringArray(std::ostream& ostrm,
00218       const StringArray& stra)
00219    {
00220       BinarySerialization::write(ostrm, BINSIG_STRARRAY);
00221       writeArray(ostrm, stra);
00222    }
00223    BLOCXX_COMMON_API void writeStringArray(std::ostream& ostrm,
00224       const StringArray* propertyList);
00225    
00226    
00227    BLOCXX_COMMON_API void read(std::istream& istrm, void* dataIn, int dataInLen);
00228    inline void read(std::istream& istrm, String& arg)
00229    {
00230       arg.readObject(istrm);
00231    }
00232    inline void read(std::istream& istrm, Int32& val)
00233    {
00234       BinarySerialization::read(istrm, &val, sizeof(val));
00235       val = ntoh32(val);
00236    }
00237    inline void read(std::istream& istrm, UInt32& val)
00238    {
00239       BinarySerialization::read(istrm, &val, sizeof(val));
00240       val = ntoh32(val);
00241    }
00242    inline void read(std::istream& istrm, UInt16& val)
00243    {
00244       BinarySerialization::read(istrm, &val, sizeof(val));
00245       val = ntoh16(val);
00246    }
00247    inline void read(std::istream& istrm, Int16& val)
00248    {
00249       BinarySerialization::read(istrm, &val, sizeof(val));
00250       val = ntoh16(val);
00251    }
00252    inline void read(std::istream& istrm, UInt8& val)
00253    {
00254       BinarySerialization::read(istrm, &val, sizeof(val));
00255    }
00256    inline Bool readBool(std::istream& istrm)
00257    {
00258       BinarySerialization::verifySignature(istrm, BINSIG_BOOL);
00259       Bool b;
00260       b.readObject(istrm);
00261       return b;
00262    }
00263    inline String readString(std::istream& istrm)
00264    {
00265       BinarySerialization::verifySignature(istrm, BINSIG_STR);
00266       String rv;
00267       rv.readObject(istrm);
00268       return rv;
00269    }
00270    inline StringArray readStringArray(std::istream& istrm)
00271    {
00272       BinarySerialization::verifySignature(istrm, BINSIG_STRARRAY);
00273       StringArray stra;
00274       readArray(istrm, stra);
00275       return stra;
00276    }
00277 } // end namespace BinarySerialization
00278 
00279 } // end namespace BLOCXX_NAMESPACE
00280 
00281 #endif   // BLOCXX_BinarySerialization_HPP_

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6