📄 framework.hpp
字号:
/***************************************************************** * * * 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.hpp * * ABSTRACT: Wrapper methods for accessing HA communication * channels from within the C++ application * and wrapper methods on eXtremedb interface * * * VERSION: 1.0 * * HISTORY: * 1.0- 1 SS 18-Feb-2004 Created it was * * -- *****************************************************************/#ifndef _WRAPPER_H_ #define _WRAPPER_H_#include "app.h"#include "sensor.hpp"/* * * If we don't need to write our own communication channel in C++ * then we can use the framework modules that were already written without any changes. * The only thing we have to do is to write wrapper methods or even better the wrapper * class in order to access the interface of HA communication channels which are written * in pure C. * The short description of the architecture of HA communication channels is placed in * \haframework\framework\framework.c. * * If we need to write our own communication channels in C++ and don't want to use .c modules * within the application then we have to create our own class of communication channel in the * same way as it is done in framework.c: * * This sample uses the wrapper classes to the modules which are already written on C. * *//******************************************************************** helpful macros ********************************************************************/#define LinkTo(func_name) LinkTo_##func_name #ifdef _WIN32 #define THREAD_ID HANDLE #define MEMBER_THREAD(class_name, func_name) \ void func_name(); \ static void cdecl LinkTo(func_name)( LPVOID Context) \ { static_cast<class_name*>(Context)->func_name(); } #define BeginThread(class_name, func_name, handle) \ createThread((void (__cdecl *)(void *))class_name::LinkTo(func_name), (void*)this, (THREAD_ID*)handle);#else #define THREAD_ID pthread_t #define MEMBER_THREAD(class_name, func_name) \ void func_name(); \ static void LinkTo(func_name)( LPVOID Context) \ { static_cast<class_name*>(Context)->func_name(); } #define BeginThread(class_name, func_name, handle) \ createThread((void*)(class_name::LinkTo(func_name)), (void*)this, (THREAD_ID*)handle);#endif/********************************************************************************************* * The wrapper class "MCOwrapper" contains the wrapper methods to the necessary functions of * MCOLIB. You can easily add here the wrapper methods to the rest functions which are not * included into this sample. * This class also contains the pointers to the database instance and to the current transaction * in order to include them into the database context. *********************************************************************************************/class MCOwrapper{ public:// MCO_RET StartTransaction( MCO_TRANS_TYPE type, MCO_TRANS_PRIORITY pri); MCO_RET TransCommit(); MCO_RET TransRollback(); MCO_RET DbFreePages(uint4 * retvalue); MCO_RET DbTotalPages( uint4 * retvalue); MCO_RET DbPageSize(uint2 * retvalue); MCO_RET DbConnect( const char * dbname); MCO_RET DbKill( const char * db_name); MCO_RET DbOpen( const char * db_name, mco_dictionary_h dict, void * mem_ptr, uint4 total_size, uint2 page_size); timer_unit GetCurrentTime(); MCO_RET GetCurrentAutoOid(mco_uquad* retvalue); MCO_RET DbDisconnect(); MCO_RET DbClose( const char * db_name); void GetRuntimeInfo( mco_runtime_info_t * pinf); MCO_RET RuntimeStop(); void SetErrorHandler( mco_error_handler_f f );// mco_db_h db; // current database handle mco_trans_h t; // current transaction descriptor pointer};/******************************************************************** inline eXtremeDB wrapper methods ********************************************************************/inline MCO_RET MCOwrapper::StartTransaction( MCO_TRANS_TYPE type, MCO_TRANS_PRIORITY pri){ return mco_trans_start( db, type, MCO_TRANS_FOREGROUND, &t );}inline MCO_RET MCOwrapper::TransCommit(){ return mco_trans_commit (t);}inline MCO_RET MCOwrapper::TransRollback(){ return mco_trans_rollback (t);}inline MCO_RET MCOwrapper::DbFreePages (uint4 * retvalue){ return mco_db_free_pages ( db, retvalue);}inline MCO_RET MCOwrapper::DbTotalPages ( uint4 * retvalue){ return mco_db_total_pages( db, retvalue);}inline MCO_RET MCOwrapper::DbPageSize (uint2 * retvalue){ return mco_db_page_size ( db, retvalue);}inline MCO_RET MCOwrapper::DbConnect( const char * dbname){ return mco_db_connect( dbname, &db);}inline MCO_RET MCOwrapper::DbKill( const char * db_name){ return mco_db_kill(db_name);}inline MCO_RET MCOwrapper::DbOpen( const char * db_name, mco_dictionary_h dict, void * mem_ptr, uint4 total_size, uint2 page_size){ return mco_db_open(db_name, dict, mem_ptr, total_size, page_size);}inline timer_unit MCOwrapper::GetCurrentTime(){ return mco_system_get_current_time ();}inline MCO_RET MCOwrapper::GetCurrentAutoOid(mco_uquad* retvalue){ return mco_get_current_auto_oid(db, retvalue);}inline MCO_RET MCOwrapper::DbDisconnect(){ return mco_db_disconnect( db );}inline MCO_RET MCOwrapper::DbClose( const char * db_name){ return mco_db_close( db_name);}inline void MCOwrapper::GetRuntimeInfo( mco_runtime_info_t * pinf){ mco_get_runtime_info(pinf);}inline MCO_RET MCOwrapper::RuntimeStop(){ return mco_runtime_stop();}inline void MCOwrapper::SetErrorHandler( mco_error_handler_f f ){ mco_error_set_handler( f );}/******************************************************************** * The class "HA" is the wrapper on the functions and structures of the interface * layer of HA. * In fact we need to use explicitly only a few C functions in this class: * * mco_nw_attach_replica() - is used by the connection server of master * application and is intented for creating and connecting replica channels and attaching * replicas to the master. * * mco_nw_attach_master() - is used by replica application and is intended to * establish the connection and to attach the replica to the master. * * mco_nw_close() - closes all communication channels of MASTER or REPLICA. * * and some HA control functions: * * mco_HA_keep_alive() - keep connection between master and replica "alive" * mco_HA_replica_stop() - break replica's internal loop of receiving data from master * mco_HA_set_mode() - set the desired mode of the replication * mco_HA_get_number_of_replicas() - get current number of replicas connected to master * * Functions mco_nw_recv() and mco_nw_send() are the implementation of the HA virtual methods * (*mco_xstream_write)() and (*mco_xstream_read)(). The pointers to these functions are * set to the HA interface class "mco_channel_t", which is inherited by the * communication-channel-class "nw_channel_t" as the BASE class. * * Class HA inherits the "extern "C"" structure ha_t, which defines the C descriptor of HA * interface and includes class "nw_channel_t" and other HA interface properties. * ********************************************************************/class HA: public ha_t, /* ha_t is the HA instance base class (see app.h) */ public MCOwrapper{ public: HA(int instance); ~HA();//------------------------------------------------------------------- MCO_RET AttachReplica( const char* devname, const char* replica_name, unsigned long timeout); // = mco_nw_attach_replica()/* 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) -------------------------------------------------------------------*/ MCO_RET 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); // = mco_nw_attach_master()/* 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) --------------------------------------------------------------------*/ MCO_RET Close (); // = mco_nw_close ()/* Description: If it is called by the replica the function closes the communication channel. if it is called by the master then disconnects all replicas connected to the master and closes all master/replica communication channels --------------------------------------------------------------------*//* keep the connection between master and replica alive*/ MCO_RET KeepAlive( ULONG timeout); // send signal "KEEP_ALIVE" to replicas/* break replica's internal loop of receiving data from master */ MCO_RET ReplicaStop();/* set the desired mode of the replication */ MCO_RET SetHAmode(mco_HA_params_t *par);/* get current number of replicas connected to master */ uint2 GetNumberOfReplicas(); //---------- member variables --------------------- bool InitThreads(); uint4 free_mem(int verbose); PLONG pStop_flag; timer_unit* pIinit_time;//---------- member threads ----------------------- MEMBER_THREAD(HA, ListenToReplicas); // attach HA::ListenToReplicas to C object function MEMBER_THREAD(HA, SynchCommit); // thread for shared commit support/* * public variables */ bool isValid; // this flag indicates that HA instance was created // successfully/* * This structure describes timeouts that are necessary to set the finite times to functions * of attaching and detaching replicas and of transaction commits */ mco_connection_param_t conpar; // connection parameters};inline MCO_RET HA::Close () // close all master's or replica's HA channels{ return mco_nw_close ( this ); // void* arg is used as a pointer to the object}inline MCO_RET HA::KeepAlive( ULONG timeout) // send signal "KEEP_ALIVE" to replicas{ return mco_HA_keep_alive( db, timeout);}inline MCO_RET HA::ReplicaStop() // break replica's internal loop of receiving data from master{ return mco_HA_replica_stop (db);}inline uint2 HA::GetNumberOfReplicas() // get current number of replicas connected to master { return mco_HA_get_number_of_replicas(db);}inline MCO_RET HA::SetHAmode(mco_HA_params_t *par) // set the desired mode of the replication { return mco_HA_set_mode (db, par);}#endif // _WRAPPER_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -