📄 framework.c
字号:
PVOID buffer, int2 buflen, /* OUT */ int2* recvlen, long timeout)/* * IN nw_channel_h chan - pointer to a channel descriptor * IN uint1 type - message type * IN void* buffer - receive buffer * IN uint2 buflen - receive buffer limit * OUT uint2* recvlen - return actual length * IN long timeout - receive timeout * * Description: * Guaranteed data transfer. MAY NOT be implemented if the transport layer is based * on guaranteed data transfer protocol. * Receive message of certain type from a communication channel * * Returns MSO_S_OK if successful or error code (see above). */{/* * Insert here the code for guaranteed receiving data frm a communication channel: */ return MCO_S_OK;}/*************************************************************************** * Interface layer. Used by master - replica interconnections. * * The interface is simplified. All the internal protocol details are * * hidden behind this layer from the application and MCOdb HA * ***************************************************************************//******************************************************************************/static int mco_nw_send( nw_channel_h ch, const void* buffer, unsigned buflen, timer_unit timeout)/* * IN nw_channel_h ch - pointer to a channel descriptor. * IN const void* buffer - buffer of data to send. * IN unsigned buflen - number of bytes to send * IN timer_unit timeout - wait-for-send-completion timeout. * * Description: * This send function is used implicitly by master-replica interconnection channel. * The pointers to this function MUST be set to HA internal descriptor mco_channel_t * in each channel descriptor. * * Returns the actual length of sent data or -1 if error. */{ MCO_RET ret = (MCO_RET)0; return ret;}/******************************************************************************/static int mco_nw_recv( nw_channel_h ch, const void* buffer, unsigned buflen, timer_unit timeout)/* * IN nw_channel_h ch - pointer to a channel descriptor. * OUT void* buffer - buffer for data to receive. * IN unsigned buflen - number of bytes to receive. * IN timer_unit timeout - wait-for-receive-completion timeout. * * Description: * This receive function is used implicitly by master-replica interconnection channel * mco_nw_recv MUST read exactly buflen bytes. * The pointers to this function MUST be set to HA internal descriptor mco_channel_t * in each channel descriptor. * * Returns the actual length of sent data or -1 if error. */{ MCO_RET ret =(MCO_RET)0; return ret;}/***************************************************************************/static void ErrorHandler( ha_error_h HAerror)/* * IN ha_error_h HAerror - pointer to ha_error_t structure described in mcoha.h. * The structure contains error code that caused the handler call & pointer to * the descriptor of communication channel of the deleted replica. If the pointer not * equal to NULL then replica was deleted from master's list of replicas. * * Description: * HA error handler callback - closes replica channel & deallocates memory * used by it's descriptor */{/* * The working sample of ErrorHandler(). * Add here the details of the certain protocol or change it * if it is necessary */#ifdef NW_DEBUG_OUTPUT Printf("HA error handler called, errcode = %d\n", HAerror->errcode);#endif if(HAerror->IOchannel != 0) { // if (replica was deleted) {} nw_close((nw_channel_h)HAerror->IOchannel); // close communication channel free(HAerror->IOchannel); // free the memory allocated by the descriptor }}/***************************************************************************/MCO_RET mco_nw_attach_replica( mco_db_h db, const char* devname, const mco_connection_param_t * params, const char* replica_name, uint4 timeout, void * arg)/* * IN mco_db_h db - database descriptor pointer. * IN const char* - devname. * IN const mco_connection_param_t* params - connection & transaction timeouts. * IN const char* replica_name - replica name (optional). * IN unsigned long timeout - wait-for-connection timeout * IN OUT void * arg - user defined argument. In C++ applications it may be * implemented as pointer to the instance of user defined class. * In C application it may be implemented in any way. * * Description: * This function includes connection algotrithm dependent on the type of a communication * channel. It attaches connected replica to HA subsystem. * * Returns MSO_S_OK if successful or error code (see above). */{/* * The working sample of mco_nw_attach_replica(). * Add the details of the certain protocol here or change it * if it is necessary */ MCO_RET ret; nw_channel_t _ch; // copy of IO channel in the stack nw_channel_h ch; // pointer to the new IO channel/* * If the transport protocol isn't initialized * then initialize it */ if ( !(BaseCh.status&NWST_INITIALIZED) ) {#ifdef NW_DEBUG_OUTPUT Printf( "\n Initializing master communication channel's context, please wait..." );#endif if( (ret = nw_init(&BaseCh)) != MCO_S_OK) {#ifdef NW_DEBUG_OUTPUT Printf("Error initializing");#endif return ret; } BaseCh.status |= NWST_INITIALIZED; // initialisation flag MUST be set here or // in function nw_init() }/* * If master's listener channel isn't initialized * then open listener channel */ if(!(BaseCh.status&NWST_LISTEN)) { if ( (ret=nw_listen(&BaseCh, devname)) != MCO_S_OK ) {#ifdef NW_DEBUG_OUTPUT Printf("Error listening: %d\n", ret);#endif return ret; }#ifdef NW_DEBUG_OUTPUT Printf("done\n");#endif BaseCh.status |= NWST_LISTEN; // listen flag MUST be set here or // in function nw_listen() } BaseCh.db = db; // database handle. Is used by mco_nw_close() BaseCh.status |= NWST_IS_MASTER; // IS_MASTER flag used by function mco_nw_close() // to check whether the transport layer still // belongs to master /* * Waiting for connection request */#ifdef NW_DEBUG_OUTPUT Printf( "\n Listener: Waiting for the connection request..." );#endif if ( (ret = nw_accept(&BaseCh, &_ch, timeout)) != MCO_S_OK ) {#ifdef NW_DEBUG_OUTPUT if(ret != MCO_E_NW_TIMEOUT) Printf("Error accepting connection %d\n", ret); else Printf("\nConnection timeout\n");#endif return ret; }#ifdef NW_DEBUG_OUTPUT Printf("connection accepted\n");#endif/* * Creating and initializing channel descriptor */ ch = (nw_channel_h)malloc(sizeof(nw_channel_t)); // create the new channel descriptor memcpy(ch,&_ch,sizeof(nw_channel_t)); // copy the descriptor from stack to the new instance ch->mco_channel.fsend = (int (__cdecl *)(void *,const void *,unsigned int,long))&mco_nw_send; // set pointer to virtual HA send-receive methods ch->mco_channel.frecv = (int (__cdecl *)(void *,void *,unsigned int,long))&mco_nw_recv; // to it's internal descriptor /* * Connection request is just arrived, attaching to replica */#ifdef NW_DEBUG_OUTPUT Printf( " Attaching to replica, please wait..." );#endif ret = mco_HA_attach_replica(db, &ch->mco_channel, params, replica_name,ErrorHandler);#ifdef NW_DEBUG_OUTPUT if(ret) Printf("Error attaching to replica %d\n", ret);#endif return ret;}/***************************************************************************/MCO_RET mco_nw_attach_master( /* OUT */ mco_db_h* db, const char* conn_string, const mco_connection_param_t* params, MCO_E_HA_REPLICA_STOP_REASON* stop_reason, const char* db_name, mco_dictionary_h dict, void* mem_ptr, uint4 total_size, uint4 timeout, void * arg)/* * IN OUT mco_db_h* db - database descriptor pointer. * IN const char*conn_string - interconnect dependent connection-string. * IN const mco_connection_param_t* params - connection & transaction timeouts. * OUT MCO_E_HA_REPLICA_STOP_REASON* stop_reason - the reason why the replica is stopped. * IN const char* db_name - database name. * IN mco_dictionary_h dict - pointer to database dictionary * IN void* mem_ptr - pointer to the memory allocated for database copy * IN uint4 total_size - size of allocated memory * IN unsigned long timeout - wait-for-connection timeout * * Description: * This function includes connection algotrithm dependent on the type of a communication * channel. It attaches the replica to the connected master. * * Returns MSO_S_OK if successful or error code (see above). */{/* * The working sample of mco_nw_attach_master(). * Add the details of the certain protocol here or change it * if it is necessary */ MCO_RET ret; *stop_reason = MCO_HA_REPLICA_HANDSHAKE_FAILED;/* * If the base channel still belongs to master then delete all replicas & close all * master's channels */ if ( BaseCh.status & NWST_IS_MASTER ) { mco_nw_close(0); }/* * If transport layer isn't initialized * then initialize it */ if ( !(BaseCh.status & NWST_INITIALIZED) ) {#ifdef NW_DEBUG_OUTPUT Printf( "\n Initializing master communication channel's context, please wait..." );#endif if( (ret=nw_init(&BaseCh))!=MCO_S_OK) {#ifdef NW_DEBUG_OUTPUT Printf("Error initializing: %d\n",ret);#endif return ret; }#ifdef NW_DEBUG_OUTPUT Printf("done\n");#endif }/* * Connecting to master */#ifdef NW_DEBUG_OUTPUT Printf("Connecting...");#endif if ( (ret=nw_connect(&BaseCh, conn_string, timeout)) != MCO_S_OK ) {#ifdef NW_DEBUG_OUTPUT Printf("Error connecting: %d\n", ret); return ret;#endif }#ifdef NW_DEBUG_OUTPUT Printf("done\n");#endif/* * Initializing channel descriptor */ BaseCh.mco_channel.fsend = (int (__cdecl *)(void *,const void *,unsigned int,long))&mco_nw_send; // set pointer to virtual HA send-receive methods BaseCh.mco_channel.frecv = (int (__cdecl *)(void *,void *,unsigned int,long))&mco_nw_recv; // to it's internal descriptor/* * Connection request is accepted, attaching to master */#ifdef NW_DEBUG_OUTPUT Printf( " Attaching to master..." );#endif ret = mco_HA_attach_master(db, // database descriptor pointer. (mco_channel_h)&BaseCh, // pointer to the communication channel params, // connection & transaction timeouts stop_reason, // stop reason db_name, // database name dict, // pointer to database dictionary mem_ptr, // pointer to the memory allocated for database copy total_size, (uint2)arg); // size of allocated memory nw_close(&BaseCh); return ret;}/***************************************************************************/MCO_RET mco_nw_close (void * arg)/* * Description: * If called by the replica the function closes the communication channel. * if called by the master then disconnects all replicas * connected to the master and closes all master/replica communication channels * * Returns MSO_S_OK if successful or error code (see above). */{/* * The working sample of mco_nw_close(). * Add the details of the certain protocol here or change it * if it is necessary */ nw_channel_h ch; if ( BaseCh.status & NWST_IS_MASTER ) { //if transport layer still belongs to master while(mco_HA_get_number_of_replicas((mco_db_h)BaseCh.db)) { // delete all replicas ch = (nw_channel_h)mco_HA_get_io_channel((mco_db_h)BaseCh.db, 0); mco_HA_detach_replica((mco_db_h)BaseCh.db, (mco_channel_h)ch); nw_close(ch); // close communicatin channel free(ch); // free the memory allocated for the channel descriptor } } nw_close(&BaseCh); return MCO_S_OK;}#endif// CFG_USER_DEFINED_CHANNEL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -