PSDL_Code_Gen.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: PSDL_Code_Gen.cpp 77020 2007-02-12 13:56:49Z johnnyw $
00003 
00004 #include "PSDL_Code_Gen.h"
00005 #include "PSDL_Datastore.h"
00006 #include "tao/OctetSeqC.h"
00007 #include "tao/AnyTypeCode/Any.h"
00008 #include "tao/CodecFactory/CodecFactory.h"
00009 
00010 ACE_RCSID (PSS, PSDL_Code_Gen, "$Id: PSDL_Code_Gen.cpp 77020 2007-02-12 13:56:49Z johnnyw $")
00011 
00012 TAO_PSDL_Code_Gen::TAO_PSDL_Code_Gen (CORBA::ORB_ptr orb)
00013   : file_name_ ("Data_Store"),
00014     psdl_datastore_ (),
00015     orb_ (orb),
00016     codec_ (0)
00017 {
00018   this->set_codec ();
00019   ACE_NEW (this->psdl_datastore_,
00020            TAO_PSDL_Datastore);
00021 
00022 }
00023 
00024 TAO_PSDL_Code_Gen::~TAO_PSDL_Code_Gen (void)
00025 {
00026   delete this->psdl_datastore_;
00027 }
00028 
00029 int
00030 TAO_PSDL_Code_Gen::set_codec (void)
00031 {
00032 
00033   // Obtain a reference to the CodecFactory.
00034   CORBA::Object_var obj =
00035     this->orb_->resolve_initial_references ("CodecFactory");
00036 
00037   IOP::CodecFactory_var codec_factory =
00038     IOP::CodecFactory::_narrow (obj.in ());
00039 
00040   // Set up a structure that contains information necessary to
00041   // create a GIOP 1.1 CDR encapsulation Codec.
00042   IOP::Encoding encoding;
00043   encoding.format = IOP::ENCODING_CDR_ENCAPS;
00044   encoding.major_version = 1;
00045   encoding.minor_version = 1;
00046 
00047   // Obtain the CDR encapsulation Codec.
00048   this->codec_ =
00049     codec_factory->create_codec (encoding);
00050 
00051   if (this->codec_.in () == 0)
00052     {
00053       ACE_DEBUG ((LM_DEBUG,
00054                   "codec pointer not set correctly\n"));
00055       return -1;
00056     }
00057   return 0;
00058 }
00059 
00060 int
00061 TAO_PSDL_Code_Gen::set_name_obj_ref (const char *name,
00062                                      const char *string_obj_ref)
00063 {
00064   // Invoke the helper encode method which will
00065   // convert the stringified object reference to a CORBA::OctetSeq.
00066   // Insert the name-CORBA::OCtetSeq pair to a hash_map and save the
00067   // hash_map to the database.
00068 
00069   // Encode the stringified object reference to a CORBA::OctetSeq *
00070   CORBA::OctetSeq_var octet_seq = this->encode (string_obj_ref);
00071 
00072   // Insert the new entry to the hash map which contains all the
00073   // name-octet_seq entries. And, write the hash_map to a file.
00074   int result = this->psdl_datastore_->bind (name,
00075                                             octet_seq.in ());
00076 
00077   if (result == -1)
00078     {
00079       ACE_DEBUG ((LM_DEBUG,
00080                   "Bind not done successfully \n"));
00081     }
00082   else if (result == 1)
00083     {
00084       /*ACE_DEBUG ((LM_DEBUG,
00085                     "Bind already done.\n"));
00086       */
00087       return 0;
00088     }
00089 
00090   return result;
00091 }
00092 
00093 const char *
00094 TAO_PSDL_Code_Gen::get_obj_ref (const char *name)
00095 {
00096   // Get from the hash_map saved in the database, the corresponding entry
00097   // (CORBA::OctetSeq *) for the name. Then, decode the octetseq to
00098   // get the stringified object reference and return it.
00099 
00100   CORBA::OctetSeq octet_seq;
00101 
00102   // Find the octet_seq for the name.
00103   int result = this->psdl_datastore_->find (name,
00104                                             octet_seq);
00105 
00106   if (result == 0)
00107     {
00108       // Decode the octet_seq.
00109       const char *obj_ref = this->decode (octet_seq);
00110 
00111       return CORBA::string_dup (obj_ref);
00112     }
00113   else
00114     {
00115       ACE_DEBUG ((LM_DEBUG,
00116                   "An entry for name %s is not found\n",
00117                   name));
00118       return 0;
00119     }
00120 }
00121 
00122 
00123 CORBA::OctetSeq *
00124 TAO_PSDL_Code_Gen::encode (const char *string_obj_ref)
00125 {
00126   CORBA::Any data;
00127   data <<= string_obj_ref;
00128 
00129   CORBA::OctetSeq *encoded_data = 0;
00130 
00131   encoded_data = this->codec_->encode (data);
00132 
00133   CORBA::OctetSeq_var safe_encoded_data = encoded_data;
00134 
00135   return safe_encoded_data._retn ();
00136 }
00137 
00138 const char *
00139 TAO_PSDL_Code_Gen::decode (const CORBA::OctetSeq &data)
00140 {
00141   const char *extracted_value;
00142 
00143   // Extract the data from the octet sequence.
00144   CORBA::Any_var decoded_data =
00145     this->codec_->decode (data);
00146 
00147   decoded_data.in() >>= extracted_value;
00148 
00149   return CORBA::string_dup (extracted_value);
00150 }

Generated on Tue Feb 2 17:50:13 2010 for TAO_PSS by  doxygen 1.4.7