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

📄 framework.cpp

📁 extremeDB s sample code,useful for you
💻 CPP
字号:
/***************************************************************** *                                                               * * This file is a part of the eXtremeDB-HA Application Framework * * Copyright (c) 2001-2006 McObject LLC                          *  * All Rights Reserved                                           * *                                                               *     ***************************************************************** * ++ *  * PROJECT:   eXtremeDB(tm) (c) 2003 McObject LLC * * SUBSYSTEM: HA framework. * * MODULE:    framework.cpp * * ABSTRACT:  Implementation of wrapper methods for accessing HA communication *            channels from within the C++ application * * * VERSION:   1.0 * * * HISTORY: *            1.0- 1 SS     18-Feb-2004 Created it was * * -- * *****************************************************************//* * See framework_cpp.h for details */#include  <stdio.h>#include <stdlib.h>#include  "framework.hpp"extern "C" {  uint4        NumBytes;}/* * If you wish to use HA with several master processes accessing the * shared memory database in your own implementation of the communication channel, * you should create a separate thread in the 'main' master process in order to implement * the "shared commit". This thread should call mco_HA_trans_commit_synch() function. * Note that the mco_HA_trans_commit_synch()  will return an error if the shared commit * has not been initialized yet. If the shared commit mode has been set the function enters * the internal loop and never return control back to the application * (see the thread SynchCommit() in modules appnw... .c) * You must set the shared commit mode via mco_HA_set_mode () function. * In order to set this mode set the par.is_master field to * MCO_MULTIPROCESS_COMMIT. */THREAD_ID synchcommit = (THREAD_ID)-1;void HA::SynchCommit(){#ifdef CFG_SHARED_COMMIT  int	i;    THREAD_PROC_MODE();    Printf("\nshared commit started\n");    for( i = 0; i< 60; i++) {      if (mco_HA_trans_commit_synch( db ) != MCO_S_OK) return;      Sleep(50);    }#endif  //  CFG_SHARED_COMMIT}/**************************************************            HA constructor  obtains database address (if shared memory) or  creates the database memory pool, sets the instance ID **************************************************/HA::HA(int instance){  mco_runtime_info_t  run_info;  int                 size;    isValid = 0;    mco_get_runtime_info( &run_info);    if(  run_info.mco_shm_supported ) {#ifdef _WIN32_WCE    /*      * shared memory is NOT supported under Windows CE!     */      Printf("\n Shared memory is NOT supported under Windows CE !!!\n");      EXIT(-1);#endif      size  = DBSIZE & (~(SHM_PAGESIZE-1));      if( (DBSIZE & (SHM_PAGESIZE-1))) size += SHM_PAGESIZE;      start_mem = (char*)MAP_ADDRESS+(size*instance);          }    else    {      if ((start_mem = new char[DBSIZE]) == 0)      {        Printf("Couldn't allocate memory\n");        EXIT(-1);      }    }    isValid++;}/**************************************************        HA destructor  Cancels the threads that belong to this HA context,  disconnects & closes the database, if it was  created & connected **************************************************/HA::~HA(){  mco_runtime_info_t  run_info;      GetRuntimeInfo( &run_info);      /* kill ListenToReplicas() thread */      if (hConnThread != INVALID_HANDLE_VALUE) {  // for "main" master only        cancelThread(hConnThread);        hConnThread = (THREAD_ID)INVALID_HANDLE_VALUE;      }      if(isValid)        Close();        // close connections to replicas #ifdef CFG_SHARED_COMMIT      /* kill SynchCommit() thread */      if (synchcommit  != INVALID_HANDLE_VALUE) {  // for "main" master only        cancelThread(synchcommit);        synchcommit = (THREAD_ID)INVALID_HANDLE_VALUE;      }#endif            if(db != 0) {      DbDisconnect();  // disconnect from the database      #ifdef _SOLARIS      if(isMainMaster)#endif        DbClose(dbName);  // close the database      }      if( ! run_info.mco_shm_supported ) {        free(start_mem);     // free database's memory        start_mem = 0;      }}/* initialise threads */bool    HA::InitThreads(){    if (isMainMaster) {  // for "main" master only      /* start the ListenToReplicas thread*/     BeginThread(HA, ListenToReplicas, &hConnThread);#ifdef CFG_SHARED_COMMIT  /*   * Creation of the thread that implements shared commit. It is necessary   * if you need to run several masters sharing the same database in the shared memory segment.   */      Sleep(1);      BeginThread(HA, SynchCommit, &synchcommit);#endif   //CFG_SHARED_COMMIT    }    return  true;}/* get free database memory value */uint4 HA::free_mem( int verbose){	uint4 totalpg, freepg;	uint2 pgsize;	DbFreePages(&freepg);	DbTotalPages(&totalpg);	DbPageSize(&pgsize);    if (verbose)    Printf ("Maximum database size is %dK, avialable %dK\n",      totalpg*pgsize / 1024, freepg*pgsize / 1024);  return (freepg*pgsize / 1024);}/* * interface layer, class HaInterface * Wrapper methods *//***************************************************************************/MCO_RET HA::AttachReplica(       	              const char* devname,                      const char* replica_name,                      unsigned long timeout)/* * const char* devname  -  Device name. For example "/dev/name/haTest" for QNX Messaging or, *                          \\\\.\\pipe\\test for the Named Pipes * const char* replica_name - replica's name (optional); * unsigned long timeout) - connection timeout in user defined units. * * Descriprion: * This function includes connection algotrithm dependent on the type of a communication * channel. It is called by master, estabishes connection between master and replica & * attaches connected replica to HA subsystem. *  * Return: returns MCO_S_OK if success or HA error code (see mcoHA.h) */{    return mco_nw_attach_replica( db, devname, &conpar, replica_name, timeout,                                  this ); // void* arg is used as a pointer to the object}/***************************************************************************/MCO_RET  HA::AttachMaster(                      const char* conn_string,                      MCO_E_HA_REPLICA_STOP_REASON* stop_reason,                      const char*	db_name,                      mco_dictionary_h dict,                      void* mem_ptr,                      uint4 total_size,                      unsigned long timeout)/* * const char* conn_string  - master's remote connection attributes  *                            (nostname:port name for TCP/UDP); * MCO_E_HA_REPLICA_STOP_REASON* stop_reason - returned reason by which replica is stopped * const char*	db_name     - replica's database name (if the database is not created externally.                               if database is not yet created, member "db" must be equal to 0) * mco_dictionary_h dict    - database's dictionary * void* mem_ptr            - pointer to the memory area reserved for the database * uint4 total_size         - the size of the newly created database * unsigned long timeout) *  * Descriprion: * This function includes connection algotrithm dependent on the type of a communication * channel. It is called by replica,  estabishes connection between replica and master &  * attaches the replica to the connected master. *  * Return: returns MCO_S_OK if success or HA error code (see mcoHA.h) */{      return mco_nw_attach_master( &db, conn_string, &conpar, stop_reason, db_name,                                  dict, mem_ptr, total_size, timeout,                                  this ); // void* arg is used as a pointer to the object}

⌨️ 快捷键说明

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