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

📄 mcoha.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:    mcoha.cpp * * ABSTRACT:  Implementation of common metods of classes Database & App. * * * VERSION:   1.0 * * HISTORY: *            1.0- 1 SS     18-Feb-2004 Created it was * * -- *****************************************************************/#include  "mcoha.hpp"DEFINE_PROCESS_MASKS(set);extern  App app;  // global App istance/* * Watchdog callback procedure */static void  _WatchDog(){    app.WatchDog();}void errhandler( int n ){	Printf( "\neXtremeDB runtime fatal error: %d", n );	exit( -1 );}/********************************************************************                 class Database  - application class -   Database context.  Inherits HA interface layer. Argregates class Sensor_1. ********************************************************************//* construct database instance */Database::Database(int instance): HA(instance){    s1.ha = this;         /* callback pointers */}/* destruct database instance */Database::~Database(){}/********************************************************************                 class App implementation ********************************************************************//* * construct instance of class App */App::App(){    DbInstances[MAX_HA_INSTANCES];    // the list of created databases    NumberOfDb      = 1;              // current number of db instances    CurrentDbInstance = 0;            // number of current db instance for replica    main_master     = -1;    replica_mode    = -1;    isReplica       = 0;}/* * destruct App */App::~App(){}/***************************************************** *    Application's working thread *****************************************************/void App::Run(){  THREAD_PROC_MODE();  PROCESS_MASKS();#ifdef _WIN32/************************************************************** * * the watchdog allows to shutdown the process if the "commit" * thread is interrupted. Only needed in the MULIMASTER_SHM mode * **************************************************************/  SetErrorHandler( &errhandler );  CreateWatchdog(COMMIT_WATCHDOG_TIME, (MCO_PWATCHDOG)_WatchDog);#endif    make_strings();  if(main_master < 0)  {    wdt_flag = -1;    /******************************************************     * Replica Mode     ******************************************************/    if(replica_mode >= 0) {   // analize replica mode      if(replica_mode)        Printf("\n*** Replica is being started in special mode ***\n\n");      else        Printf("\n*** Replica is being started in switch-to-master mode ***\n\n");    }    else {        Printf("\n*** Replica is being started in passive mode ***\n\n");    }    ReplicaMode();      // start replica    ExitHandler(0);   // graceful program termination    KILL();  }  else {    /******************************************************     * Master Mode     ******************************************************/        MasterMode();   // start master mode    ExitHandler(0);   // graceful program termination    KILL();  }}/* * initialize  application, run working thread and wait for a keystroke */void App::init(int argc, char **argv){  SH();/* parse command line */    if (0!=parse_cmd_line(argc,argv ))    return;#ifdef  CFG_QNXMSG_CHANNEL  if(NumberOfDb > 1) {    printf("\multiple master's databases support is not implemented yet\n"           "in QNX Messaging channel\n");  }#endif  BeginThread(App, Run, &thRun);       // start working thread#ifndef _WIN32/************************************************************** * * the watchdog allows to shutdown the process if the "commit" * thread is interrupted. Only needed in the MULIMASTER_SHM mode * **************************************************************/  CreateWatchdog(COMMIT_WATCHDOG_TIME, (MCO_PWATCHDOG)_WatchDog);#endif  StopWait();                         // wait for  a keystroke to stop test}/* *  Exit handler provides emergency & normal exit from the program *  It is called by working thread or by watchog handler */void App::ExitHandler(int code){    wdt_flag = -1;    KillWatchdog();    CloseDatabases(code);}/*  * Watchdog callback procedure. * Checks and increments the wdt_flag. * If it's not equal to zero calls exit handler */void  App::WatchDog(){  if(wdt_flag > 0) {    wdt_flag = -1;    Printf("\nwatchdog timer expired, exiting\n");#ifndef _WIN32    cancelThread(thRun);#endif    ExitHandler(-1);    EXIT(-1);  }  if(!wdt_flag) wdt_flag++; }/************************************************** * wait for  a keystroke to stop test **************************************************/void  App::StopWait(){  char  s[256];  int   i;  Database*   Db;      s[0] = 0;      for(;;){        if(fgets(s,256,stdin) < 0) {          continue;        }        if (s[0] ==0 ) continue;        i = atoi(s);        if ( i == 343 ) EXIT(1);        break;      }      stop_flag++;	      Printf("\n\nApplication interrupted by the keystroke\n");      if(isReplica) {        if( (Db = DbInstances[0])->db != 0) {          while(stop_flag) { // wait wor the termination of the replication            Printf("Stop replica\n");            Db->ReplicaStop();            Sleep(1000);          }        }      }      while(stop_flag) Sleep(50); // wait wor the termination of working thread}/* * initialize Database instance */void App::InitDBinstance(Database* Db, int instance){    Db->baseChan.status = 0;    Db->id          = instance;    Db->hConnThread = (THREAD_ID)INVALID_HANDLE_VALUE;    Db->hSynchcommit= (THREAD_ID)INVALID_HANDLE_VALUE;  // SynchCommit() thread handle     Db->replicaMode = (replica_mode>0);      // replica_mode > 0 = "special mode"    Db->isMainMaster= (main_master > 0);     // main_master > 0 = "main_master"    Db->pStop_flag  = &stop_flag;    Db->pIinit_time = &init_time;    sprintf(Db->dbName,"monitorDB%d", instance);}/*/* close all database instances */void  App::CloseDatabases(int flag){  int     i;    for (i=0; i < NumberOfDb; i++) {      if(flag) DbInstances[i]->isValid = 0;      delete DbInstances[i];    }    mco_runtime_stop();		  stop_flag = 0;  /* give a sign that the thread is terminated */}/* * create the new database instance and connect to the created database. */Database*  App::CreateDatabase(int instance){  Database* Db;  MCO_RET rc;    Db = new Database(instance);    InitDBinstance( Db, instance);    mco_db_kill(Db->dbName);    rc = mco_db_open(Db->dbName, monitorDB_get_dictionary(), Db->start_mem,                     DBSIZE, (uint2) DB_PAGESIZE );    if ( rc )    {      Printf( "\nerror creating database^ %d\n", rc);      delete Db;      EXIT(-1);    }    /* connect to the database, obtain a database handle */    if( (rc=mco_db_connect(Db->dbName, &Db->db )) !=0) {      Printf( "\nCouldn't connect to database^ %d\n", rc);      delete Db;      EXIT(-1);    }    Db->InitThreads();    Db->free_mem( 1 );    return  Db;}/* * connect to existing database instance */Database*  App::ConnectToDatabase( int instance){  Database* Db;  MCO_RET rc;  int i;    Db =  new Database(instance);    InitDBinstance( Db, instance);    sprintf(Db->dbName,"monitorDB%d", instance);	  for (i=0;i < 10; i++) {          /* connect to the database, obtain a database handle */	    if(!(rc=mco_db_connect(Db->dbName, &Db->db ))) {        break;	  	}      sprintf(Db->dbName,"monitorDB%d%d",instance, i);    }    if(i == 10) {      delete Db;      Db  = 0;    }    return Db;}

⌨️ 快捷键说明

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