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

📄 server_repository.cpp

📁 这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用于网络游戏医学图像网关的高qos要求.更详细的内容可阅读相应的材料
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "Server_Repository.h"
#include "XML_ContentHandler.h"
#include "Activator_Options.h"

#include "ACEXML/parser/parser/Parser.h"
#include "ACEXML/common/FileCharStream.h"

ACE_RCSID (ImplRepo_Service,
           Server_Repository,
           "Server_Repository.cpp,v 1.7 2003/11/10 17:45:16 michel_j Exp")


int
Server_Repository::init (void)
{
  int rmode = OPTIONS::instance ()->repository_mode ();
  if (rmode != Options::REPO_XML_FILE)
    {
      Repository_Configuration *config = OPTIONS::instance ()->config ();
      ACE_ASSERT(config != 0);

      // iterate through the list of registered servers and register them

      config->open_section (config->root_section (),
                            ACE_LIB_TEXT ("Servers"),
                            1,
                            this->servers_);
      int index = 0;
      ACE_CString name;

      while (config->enumerate_sections (servers_, index, name) == 0)
        {
          ACE_CString logical, startup, working_dir;
          u_int activation_val = 0;
          ImplementationRepository::ActivationMode activation;

          ImplementationRepository::EnvironmentList environment_vars;

          ACE_Configuration_Section_Key server_key;
          int error = 0;

          error += config->open_section (this->servers_,
                                         name.c_str(),
                                         0,
                                         server_key);

          error += config->get_string_value (server_key,
                                             ACE_LIB_TEXT ("LogicalServer"),
                                             logical);

          error += config->get_string_value (server_key,
                                             ACE_LIB_TEXT ("StartupCommand"),
                                             startup);

          error += config->get_string_value (server_key,
                                             ACE_LIB_TEXT ("WorkingDir"),
                                             working_dir);

          error += config->get_integer_value (server_key,
                                              ACE_LIB_TEXT ("Activation"),
                                              activation_val);

          activation =
            ACE_static_cast (ImplementationRepository::ActivationMode,
                             activation_val);

          // Maybe environments variables?? need a straightforward
          // way to store env vars.

          if (error != 0)
            {              
              ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT ("Error reading configuration data for ")
                ACE_TEXT ("service '%s',skipping\n"),
                name.c_str ()));
            }
          else
            {
              this->add (name,
                         logical,
                         startup,
                         environment_vars,
                         working_dir,
                         activation);
            }

          index++;
        }
    }
  else
    {
      /// @@ Not quite sure what I want to do here .. leave for
      /// now. I guess similar stuff like check if that particular
      /// server info exists etc.
    }

  return 0;
}


// Add a new server to the Repository

int
Server_Repository::add (
    const ACE_CString& POA_name,
    const ACE_CString& logical_server_name,
    const ACE_CString& startup_command,
    const ImplementationRepository::EnvironmentList& environment_vars,
    const ACE_CString& working_dir,
    const ImplementationRepository::ActivationMode& activation
  )
{
  int rmode = OPTIONS::instance ()->repository_mode ();
  if (rmode != Options::REPO_XML_FILE)
    {
      Repository_Configuration *config = OPTIONS::instance ()->config ();
      ACE_ASSERT(config != 0);

      // @@ Add this to the persistent configuration; environment_vars??
      ACE_Configuration_Section_Key server;
      config->open_section (this->servers_,
                            POA_name.c_str(),
                            1,
                            server);

      config->set_string_value (server,
                                ACE_LIB_TEXT ("LogicalServer"),
                                logical_server_name);

      config->set_string_value (server,
                                ACE_LIB_TEXT ("StartupCommand"),
                                startup_command);

      config->set_string_value (server,
                                ACE_LIB_TEXT ("WorkingDir"),
                                working_dir);

      config->set_integer_value (server,
                                 ACE_LIB_TEXT ("Activation"),
                                 activation);

      Server_Info *new_server = 0;
      ACE_NEW_RETURN (new_server,
                      Server_Info (POA_name,
                                   logical_server_name,
                                   startup_command,
                                   environment_vars,
                                   working_dir,
                                   activation),
                      -1);

      return this->repository_.bind (POA_name, new_server);
    }
  else
    {
      ACE_CString filename = OPTIONS::instance ()->file_name ();

      FILE *fp = ACE_OS::fopen (filename.c_str(), "r");
      
      if (fp != 0) 
        {
          ACE_TCHAR buffer[4096];
          while (ACE_OS::fgets (buffer, sizeof (buffer), fp))
            {
              /// Obviously, we need to/can update only if we find an
              /// entry for it in the xml file.
              ACE_TCHAR* found = ACE_OS::strstr (buffer, POA_name.c_str ());

              if (found != 0)
                {
                  /// An entry found for the POA_name. So, dont
                  /// add it again.
                  ACE_DEBUG ((LM_DEBUG,
                              "ImR Activator: The %s is already added.\n", POA_name.c_str ()));

                  ACE_OS::fclose (fp);

                  return 0;
                }
            }
        }

      /// If control comes here, it means this server isnt added already.
      fp = ACE_OS::fopen (filename.c_str(), "a");

      if (fp == 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Coudnt open the file to append\n"),
                            -1);
        }

      ACE_CString server_info = "<SERVER_INFORMATION>";
      server_info += "<Server>";
      server_info += POA_name.c_str ();
      server_info += "</Server>\n<StartupOptions>\n<Command_Line>";
      server_info += startup_command;
      server_info += "</Command_Line>\n<WorkingDir>";
      server_info += working_dir;
      server_info += "</WorkingDir>\n<Activation>";
      server_info += OPTIONS::instance ()->convert_str (activation);
      server_info += "</Activation>\n</StartupOptions>\n</SERVER_INFORMATION>\n";

      ACE_OS::fprintf(fp, server_info.c_str());
      ACE_OS::fclose(fp);

      return 0;
    }
}

// Update the associated process information.

int
Server_Repository::update (const ACE_CString& POA_name,
                           const ACE_CString& location,
                           const ACE_CString& server_object_ior)
{
  int rmode = OPTIONS::instance ()->repository_mode ();
  if (rmode != Options::REPO_XML_FILE)
    {
      Server_Info *server = 0;
      int retval = this->repository_.find (POA_name, server);

      // Only fill in data if it was found
      if (retval == 0)
        {
          ACE_ASSERT(server != 0);
          server->update_running_info (location, server_object_ior);
        }

      return retval;
    }
  else
    {
      ACE_CString filename = OPTIONS::instance ()->file_name ();

      FILE *fp = ACE_OS::fopen (filename.c_str(), "r+");
      ACE_ASSERT(fp != 0);

      ACE_TCHAR buffer[4096];

      while (ACE_OS::fgets (buffer, sizeof (buffer), fp))
        {
          /// Obviously, we need to/can update only if we find an
          /// entry for it in the xml file.
          ACE_TCHAR* found = ACE_OS::strstr (buffer, POA_name.c_str ());

          if (found != 0)
            {
              /// found an entry. So, need to update the entry
              /// information.
              this->handler_->update_running_information (POA_name,
                                                          location,
                                                          server_object_ior);
              return 0;
            }
        }
      return -1;
    }
}

// Returns information related to startup.

int
Server_Repository::get_startup_info (
    const ACE_CString& POA_name,
    ACE_CString& logical_server_name,
    ACE_CString& startup_command,
    ImplementationRepository::EnvironmentList& environment_vars,
    ACE_CString& working_dir,
    ImplementationRepository::ActivationMode& activation
  )
{
  int rmode = OPTIONS::instance ()->repository_mode ();
  if (rmode != Options::REPO_XML_FILE)
    {
      Server_Info* server = 0;
      int retval = this->repository_.find (POA_name, server);

      // Only fill in data if it was found
      if (retval == 0)
        {
          ACE_ASSERT(server != 0);
          server->get_startup_info (logical_server_name,
                                    startup_command,
                                    environment_vars,
                                    working_dir,
                                    activation);
        }
      return retval;
    }
  else
    {
      ACEXML_FileCharStream *fstm = 0;
      ACE_NEW_RETURN (fstm,
                      ACEXML_FileCharStream (),
                      1);

      const char* fname = OPTIONS::instance()->file_name().c_str();
      if (fstm->open (fname) != 0)
        {
          ACE_ERROR((LM_ERROR, ACE_LIB_TEXT("Fail to open XML file: %s\n"), fname));
          return 1;
        }

      this->handler_ = new XML_ContentHandler (POA_name.c_str ());

      ACEXML_Parser parser;

      ACEXML_InputSource input(fstm);

      parser.setContentHandler (this->handler_);
      parser.setDTDHandler (this->handler_);
      parser.setErrorHandler (this->handler_);
      parser.setEntityResolver (this->handler_);

      ACEXML_TRY_NEW_ENV
        {
          parser.parse (&input ACEXML_ENV_ARG_PARAMETER);
          ACEXML_TRY_CHECK;
        }
      ACEXML_CATCH (ACEXML_Exception, ex)
        {
          ex.print();
          return -1;
        }
      ACEXML_ENDTRY;

      ACE_CString activation_mode;

      this->handler_->get_startup_information (logical_server_name,
                                               startup_command,
                                               working_dir,
                                               activation_mode);

      activation = OPTIONS::instance ()->convert_mode (activation_mode.c_str ());

      return 0;
    }
}


// Returns information related to a running copy.

int
Server_Repository::get_running_info (const ACE_CString& POA_name,
                                     ACE_CString& location,
                                     ACE_CString& server_object_ior)
{
  int rmode = OPTIONS::instance ()->repository_mode ();

  if (rmode != Options::REPO_XML_FILE)
    {
      Server_Info *server;
      int retval = this->repository_.find (POA_name, server);

      // Only fill in data if it was found
      if (retval == 0)
        {
          ACE_ASSERT(server != 0);
          server->get_running_info (location, server_object_ior);
        }
      return retval;

⌨️ 快捷键说明

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