⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 catior.cpp

📁 TAO学习工具
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// $Id: catior.cpp,v 1.1.1.5.2.13 2007/02/01 15:04:43 mesnierp Exp $

// ============================================================================
//
// = LIBRARY
//    TAO/Utils/catior
//
// = FILENAME
//    catior.cpp
//
// = DESCRIPTION
//    Reads stringified IORs and outputs the encoded information.
//
// = AUTHORS
//      Jeff Hopper <jrhopper@cts.com>
//      SCIOP and Tagged component modifications by:
//      Jason Cohen, Lockheed Martin ATL <jcohen@atl.lmco.com>
//
// ============================================================================

#include "ace/Codeset_Registry.h"
#include "ace/Get_Opt.h"
#include "ace/streams.h"
#include "ace/OS_NS_ctype.h"
#include "ace/OS_NS_stdio.h"
#include "ace/Argv_Type_Converter.h"
#include "tao/corba.h"
#include "tao/IIOP_Profile.h"
#include "tao/Messaging_PolicyValueC.h"
#include "tao/Messaging/Messaging_RT_PolicyC.h"
#include "tao/Messaging/Messaging_SyncScope_PolicyC.h"
#include "tao/Messaging/Messaging_No_ImplC.h"
#include "tao/RTCORBA/RTCORBA.h"
//#include "tao/Typecode.h"
#include "tao/Marshal.h"
//#include "tao/ORB_Constants.h"
#include "tao/Transport_Acceptor.h"
#include "tao/IIOP_EndpointsC.h"


static CORBA::Boolean
catiiop (char* string
         ACE_ENV_ARG_DECL)
{
  // NIL objref encodes as just "iiop:" ... which has already been
  // removed, so we see it as an empty string.

  if (!string || !*string)
    return 0;

  // Type ID not encoded in this string ... makes narrowing rather
  // expensive, though it does ensure that type-safe narrowing code
  // gets thoroughly excercised/debugged!  Without a typeID, the
  // _narrow will be required to make an expensive remote "is_a" call.

  // Remove the "N.N//" prefix, and verify the version's one that we
  // accept

  CORBA::Short  iiop_version_major, iiop_version_minor;

  if (ACE_OS::ace_isdigit (string [0])
      && ACE_OS::ace_isdigit (string [2])
      && string [1] == '.'
      && string [3] == '/'
      && string [4] == '/')
    {
      iiop_version_major = (char) (string [0] - '0');
      iiop_version_minor = (char) (string [2] - '0');
      string += 5;
    }
  else
    {
      iiop_version_major = 0;
      iiop_version_minor = 0;
      string += 2;
    }

  ACE_DEBUG ((LM_DEBUG,
              "IIOP Version:\t%d.%d\n",
              iiop_version_major,
              iiop_version_minor));

  // Pull off the "hostname:port/" part of the objref Get host and
  // port.
  CORBA::UShort port_number;
  char* hostname;
  char *cp = ACE_OS::strchr (string, ':');

  if (cp == 0)
    {
      ACE_THROW_RETURN (CORBA::DATA_CONVERSION (), 0);
    }

  hostname = CORBA::string_alloc (1 + cp - string);

  for (cp = hostname;
       *string != ':';
       *cp++ = *string++)
    continue;

  *cp = 0;
  string++;

  cp = ACE_OS::strchr ((char *) string, '/');

  if (cp == 0)
    {
      CORBA::string_free (hostname);
      ACE_THROW_RETURN (CORBA::DATA_CONVERSION (), 0);
    }

  port_number = (short) ACE_OS::atoi ((char *) string);
  string = ++cp;

  ACE_DEBUG ((LM_DEBUG,
              "Host Name:\t%s\n",
              hostname));
  ACE_DEBUG ((LM_DEBUG,
              "Port Number:\t%d\n",
              port_number));
  CORBA::string_free (hostname);

  // Parse the object key.
  // dump the object key to stdout
  //  TAO_POA::decode_string_to_sequence (data->profile.object_key,
  //                                      string);
  ACE_DEBUG ((LM_DEBUG,
              "\nThe Object Key as string:\n%s\n",
              string));
  return 1;
}

static CORBA::Boolean
cat_iiop_profile (TAO_InputCDR& cdr);

static CORBA::Boolean
cat_sciop_profile (TAO_InputCDR& cdr);

static CORBA::Boolean
cat_uiop_profile (TAO_InputCDR& cdr);

static CORBA::Boolean
cat_shmiop_profile (TAO_InputCDR& cdr);

static CORBA::Boolean
cat_nskpw_profile (TAO_InputCDR& cdr);

static CORBA::Boolean
cat_nskfs_profile (TAO_InputCDR& cdr);

static CORBA::Boolean
cat_octet_seq (const char *object_name,
               TAO_InputCDR& stream);

static CORBA::Boolean
cat_profile_helper(TAO_InputCDR& stream, const char *protocol);

static CORBA::Boolean
catior (char* str)
{
  // Unhex the bytes, and make a CDR deencapsulation stream from the
  // resulting data.

  ACE_Message_Block mb (ACE_OS::strlen ((char *) str)  / 2 + 1
                        + ACE_CDR::MAX_ALIGNMENT);
  ACE_CDR::mb_align (&mb);

  char *buffer = mb.rd_ptr ();
  char *tmp = (char *) str;
  size_t len = 0;

  CORBA::Boolean continue_decoding;

  // The prefix of the IOR must be removed, and the string must start
  // with the encapsulation byte

  while (tmp [0] && tmp [1])
    {
      u_char byte;

      if (! (ACE_OS::ace_isxdigit (tmp [0]) && ACE_OS::ace_isxdigit (tmp [1])))
        break;

      byte = (u_char) (ACE::hex2byte (tmp [0]) << 4);
      byte |= ACE::hex2byte (tmp [1]);

      buffer [len++] = byte;
      tmp += 2;
    }

  // Create deencapsulation stream ... then unmarshal objref from that
  // stream.

  int byteOrder = *(mb.rd_ptr ());

  mb.rd_ptr (1);
  mb.wr_ptr (2 * len - 1);
  TAO_InputCDR stream (&mb, ACE_static_cast(int,byteOrder));

  if (byteOrder == 1)
    ACE_DEBUG ((LM_DEBUG,
                "The Byte Order:\tLittle Endian\n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "The Byte Order:\tBig Endian\n"));

  // First, read the type hint. This will be the type_id encoded in an
  // object reference.
  char* type_hint;

  if (!(stream >> type_hint))
    {
      ACE_DEBUG ((LM_DEBUG,
                  "cannot read type id\n"));
      return TAO::TRAVERSE_STOP;
    }

  ACE_DEBUG ((LM_DEBUG,
              "The Type Id:\t\"%s\"\n",
              type_hint));

  // Release any memory associated with the type_hint.
  CORBA::string_free (type_hint);

  // Read the profiles, discarding all until an IIOP profile comes by.
  // Once we see an IIOP profile, ignore any further ones.
  //
  // XXX this will need to change someday to let different protocol
  // code be accessed, not just IIOP.  Protocol modules will be
  // dynamically loaded from shared libraries via ORB_init (), and we
  // just need to be able to access such preloaded libraries here as
  // we unmarshal objrefs.

  CORBA::ULong profiles = 0;

  continue_decoding = stream.read_ulong (profiles);

  // Get the count of profiles that follow.
  if (continue_decoding == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "cannot read the profile count\n"));
      return TAO::TRAVERSE_STOP;
    }

  CORBA::ULong profile_counter = 0;

  ACE_DEBUG ((LM_DEBUG,
              "Number of Profiles in IOR:\t%d\n",
              profiles));

  // No profiles means a NIL objref.
  if (profiles == 0)
    return TAO::TRAVERSE_CONTINUE;
  else
    while (profiles-- != 0)
      {
        ACE_DEBUG ((LM_DEBUG,
                    "Profile number:\t%d\n",
                    ++profile_counter));

        // We keep decoding until we find a valid IIOP profile.
        CORBA::ULong tag;

        continue_decoding = stream.read_ulong (tag);

        // Get the profile ID tag.
        if (continue_decoding == 0)
          {
            ACE_DEBUG ((LM_DEBUG,
                        "cannot read profile tag\n"));
            continue;
          }

        if (tag == IOP::TAG_INTERNET_IOP)
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            continue_decoding = cat_iiop_profile (stream);
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
        else if (tag == TAO_TAG_SCIOP_PROFILE)
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            continue_decoding = cat_sciop_profile (stream);
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
        else if (tag == TAO_TAG_UIOP_PROFILE)
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            continue_decoding = cat_uiop_profile (stream);
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
        else if (tag == TAO_TAG_SHMEM_PROFILE)
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            continue_decoding = cat_shmiop_profile (stream);
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
        else if (tag == TAO_TAG_DIOP_PROFILE)
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            continue_decoding =  cat_profile_helper(stream, "DIOP (GIOP over UDP)");
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
        else if (tag == TAO_TAG_NSKPW_PROFILE)
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            continue_decoding = cat_nskpw_profile (stream);
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
        else if (tag == TAO_TAG_NSKFS_PROFILE)
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            continue_decoding = cat_nskfs_profile (stream);
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
        else
          {
            ACE_DEBUG ((LM_DEBUG, "%{"));
            ACE_DEBUG ((LM_DEBUG,
                        "%I Profile tag = %d (unknown protocol)\n", tag));
            continue_decoding = cat_octet_seq("Profile body", stream);
            ACE_DEBUG ((LM_DEBUG, "%}"));
          }
      }
  return 1;
}

// Parse the Orbix-style POOP object references, which have the form:
//:\ntlj3corba:NS:NC_2::IR:CosNaming_NamingContext
// :\ hostname
// : server_name
// : marker
// : IR_host
// : IR_server
// : interface_marker

static CORBA::Boolean
catpoop (char* string
         ACE_ENV_ARG_DECL)
{
  if (!string || !*string)
    return 0;

  if (string [0] == ':'
      && string [1] == '\\')
    ACE_DEBUG ((LM_DEBUG,
                "\nPerhaps we've found some POOP\n"));
  string += 2;

  char *cp = ACE_OS::strchr (string, ':');

  if (cp == 0)
    {
      ACE_THROW_RETURN (CORBA::DATA_CONVERSION (), 0);
    }

  // Read the hostname.
  char* hostname = CORBA::string_alloc (1 + cp - string);

  for (cp = hostname;
       *string != ':';
       *cp++ = *string++)
    continue;

  *cp = 0;
  string++;

  ACE_DEBUG ((LM_DEBUG,
              "Host Name:\t%s\n",
              hostname));
  CORBA::string_free (hostname);

  //  read the server name
  cp = ACE_OS::strchr (string, ':');

  char* server_name = CORBA::string_alloc (1 + cp - string);

  for (cp = server_name;
       *string != ':';
       *cp++ = *string++)
    continue;

  *cp = 0;
  string++;

  ACE_DEBUG ((LM_DEBUG,
              "Server Name:\t%s\n",
              server_name));

  CORBA::string_free (server_name);

  // Read the Orbix specific marker.
  cp = ACE_OS::strchr (string, ':');

  char* marker = CORBA::string_alloc (1 + cp - string);

  for (cp = marker;
       *string != ':';
       *cp++ = *string++)
    continue;

  *cp = 0;
  string++;

  ACE_DEBUG ((LM_DEBUG,
              "Marker:\t\t%s\n",
              marker));
  CORBA::string_free (marker);

  cp = ACE_OS::strchr (string, ':');

  // Read the IR_host.
  char* IR_host = CORBA::string_alloc (1 + cp - string);

  for (cp = IR_host;
       *string != ':';
       *cp++ = *string++)
    continue;

  *cp = 0;
  string++;

  ACE_DEBUG ((LM_DEBUG,
              "IR Host:\t\t%s\n",
              IR_host));
  CORBA::string_free (IR_host);

  // Read the IR_server
  cp = ACE_OS::strchr (string, ':');

  char* IR_server = CORBA::string_alloc (1 + cp - string);

  for (cp = IR_server;
       *string != ':';
       *cp++ = *string++)
    continue;

  *cp = 0;
  string++;

  ACE_DEBUG ((LM_DEBUG,
              "IR Server:\t\t%s\n",
              IR_server));
  CORBA::string_free (IR_server);

  // Read the interface_marker
  ACE_DEBUG ((LM_DEBUG,
              "Interface Marker:\t%s\n",
              string));
  return 1;
}

static CORBA::Boolean
cat_from_string (ACE_CString &aString
                 ACE_ENV_ARG_DECL)
{
  CORBA::Boolean b = false;
  ACE_DEBUG ((LM_DEBUG,
              "\nhere is the IOR\n%s\n\n",
              aString.rep ()));

  char* str;
  if (aString.find ("IOR:") == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "decoding an IOR:\n"));

      // Strip the IOR: off the string.
      ACE_CString prefix = "IOR:";
      size_t prefixLength = prefix.length ();

      ACE_CString subString =
        aString.substring (prefixLength,
                           aString.length () - prefixLength);
      subString[subString.length ()] = '\0';
      str = subString.rep ();
      b = catior (str);
    }
  else if (aString.find ("iiop:") == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "decoding an IIOP URL IOR\n"));

      ACE_CString prefix = "IIOP:";
      size_t prefixLength = prefix.length ();

      ACE_CString subString =
        aString.substring (prefixLength,
                           aString.length () - prefixLength);
      //subString[subString.length () - 1] = '\0';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -