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

📄 framework.c

📁 extremeDB s sample code,useful for you
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************* *                                                                 * *  framework.c                                                    * *                                                                 * *  This file is a part of the eXtremeDB HA framework              * *  Copyright (c) 2001-2006 McObject LLC                           *  *  All Rights Reserved                                            * *                                                                 * *******************************************************************//* * ++ *  * PROJECT:   eXtremeDB(tm) (c) McObject LLC * * SUBSYSTEM: HA support * * MODULE:    framework.C * * ABSTRACT:  User implementation of communication channel * * * VERSION:   1.0 * * HISTORY: *            1.0- 1 SS     21-Jul-2003 Created it was * * -- *//* *      Abstraction of the High Availability master/replica communication channels. * *  The eXtremeDB HA master and replica applications use user-defined communication channels *  to exchange data and communication messages during transactions. * *  Below is the schematic view of the master/replica interaction.  *  * *   Master application                                Replica application  *------------------------------------------------------------------------------- *                                                     ------------------------ *   -------------------------     Communication       | Allocate memory for  | * --| Creating the database |        channel          | the copy of database | * | -------------------------                         ------------------------       * |            |                  --------------                 V * V ---------------------------<--| Connect()  |---<--------------------------     * --|  Attaching replicas   |     --------------      | Connecting to master | * | --------------------------->------------------->-------------------------- * |            |                  | Accept()   |                 | * |            |                  -------------- '- - - - - - - -|- - - - - - - - * |            V                  -------------- '               V               ' * | --------------------------->--|   Send()   |-'->---------------------------  ' * V | Sending a copy of the |     -------------- '    | Receiving a copy of   |  ' * --| database to replicas  |     -------------- '    | the database          |  ' * | ---------------------------<--|  Receive() |-'-<---------------------------  ' * |            |                  -------------- '               |               ' * |            |       ---<-----                 '               |               ' * |            V       |       |                 '               |               ' * | -------------------------  |                 '               |               ' * | |   loop :updating the  |  |                 '               |               ' * | |      database         |--|                 '               |               ' * | -------------------------                    '               |               ' * |       |          A                           '               |               ' * V       V          |                           '               |               ' *------------------------------------------------'               |               ' *            High Availability subsystem                         |               ' *------------------------------------------------                |               ' *          | |                    -------------- '               V               ' *          | |------<----->-------|   Send()   |-'-<->-------------------------- '   *          |                      -------------- '     |   loop of updating    | ' *          |                      -------------- '     | the copy of database  | ' *          ---------<----->-------|  Receive() |-'-<->-------------------------- ' *                                 -------------- '- - - - - - - - - - - - - - - - * * Communication methods depend on the platforms and communication media. Therefore, * application interfaces and the High Availability subsystem (HA) should be isolated * from the particulars of the communication protocol. * *  Communication protocol architecture * *  A communication protocol consists of two layers. The lower layer is a transport layer. *  The transport layer is responsible for  *  1) providing guaranteed data transfer between the master and the replicas. This is *     implemented via frame sequence, and data integrity checks and via recovering of *     a transfer after the loss of data; *  2) isolating the application and the HA subsystem from the platforms dependencies and *     a communication media; *  3) implementation of the separate and independent point-to-point transfer channels between *     the master and each replica. * * The transport layer can also be used by the application for implementation of it's own * communication channels. *   * The higher layer is the interface layer. It is intended to isolate an application from * the implementaion details of the transport layer and communication protocol. * * The architecture of the transport layer described below is not mandatory but it * is recomended in order to provide point-to-point connections and data exchange * between master and replicas. * * Communication channels provided by the transport layer are based on channel * descriptors. The descriptor is implemented as a user defined class nw_channel_t and * MUST contain the channel context: * *  typedef struct nw_channel_ *  { * 	  mco_channel_t   mco_channel;  // HA internal descriptor, MUST be placed first *    ...                           // channel context *  }nw_channel_t, *nw_channel_h; * * Pointer to a channel descriptor  is passed to all methods of transport layer as the * first argument. All the properties of the communication channel MUST be places in * it's descriptor to separate different channels from each other. * * The internal channel descriptor of HA mco_channel_t is included to the transport * layer channel descriptor as the parent class to provide the interface with HA. * It is described in mcoha.h and contains pointers to HA virtual methods * (*mco_xstream_write)() and (*mco_xstream_read)(): * * typedef struct { *   mco_xstream_write fsend; *   mco_xstream_read  frecv; *     ... * }mco_channel_t, *mco_channel_h; * * The recommended implementaion API is the following: * * MCO_RET nw_init(IN nw_channel_h chan) - initialize transport layer. The argument  * is a pointer to the server channel for master or to the only channel for replica. * * Functions nw_listen(), nw_accept() are intended for implementation of the connection  * server in master application. It is recommended to implement it in a separate thread. * * Functions nw_send() and nw_recv() - low level data transfer. * * Functions nw_send_message(), nw_recv_message() - for providing guaranteed data * transfer (see appnwUDP.c example). * * Function nw_close() - to close communication channel. * * The interface layer API: * * Function mco_nw_attach_replica() is used by the connection server of master * application and is intented for creating and connecting replica channels and to attaching * replicas to the master. * * Function mco_nw_attach_master() is used by replica application and is intended to   * establish the connection and to attach the replica to the master. * * Functions mco_nw_recv() and mco_nw_send() - is the implementation of the HA virtual methods * (*mco_xstream_write)() and (*mco_xstream_read)(). The pointers to these functions MUST  * be set to HA internal descriptor mco_channel_t in each channel descriptor. * * Function mco_nw_close() is used by master and replicas. If the application is a application * master, the function closes all the communications channels created by master and detaches * all replicas from the master. * If the application is a replica, then this API closes replica's channel. * * This sample contains templates of functions of transport and interface layer * of master/replica communication channels. Channels implementaion over * TCP, UDP and pipes can also be used as the prototypes for the user-defined channel * (see modules appnwTCP.c, appnwUDP,c, * appnwpip.c). */#include <stdio.h>#include <stdlib.h>#include <string.h>#include "app.h"#ifdef CFG_USER_DEFINED_CHANNELint           conn_flag = 0;timer_unit    init_time = 0;static nw_channel_t BaseCh;   // The base channel descriptor for master/replica                              // interconnections. You may implement it in other way./**************************************************************************** * Transport layer. Used by application for providing of communication      * * channels, guaranteed data transfer. Can be used by application for       * * implementation of it's own communications channels.                      *  * Some details of a communication channel are accessible to application    *  ****************************************************************************//***************************************************************************/MCO_RET nw_init(nw_channel_h chan)/* * IN nw_channel_h chan   - pointer to the base channel descriptor. * * Description: * Initializes the network protocol & base channel descriptor. * The base channel is the main channel descriptor used by server for accepting * client's connections requests. The descriptor of base channel MAY contain * extention including common properties of the transport layer. * * Returns MSO_S_OK if successful or error code. */{/* * Insert the code for initializing the certain communication media and * transport layer here: */    return  MCO_S_OK;}/**************************************************************************/MCO_RET nw_listen(nw_channel_h  chan, const char *  nw_dev)/* * IN nw_channel_h  chan    - pointer to a channel descriptor. * IN const char *  nw_dev  - listener channel name *  * Description: * Sets the IO channel to listener state. * * Returns MSO_S_OK if successful or error code. */{/* * Insert here the code for setting the listener state of the channel: * transport layer. */    return  MCO_S_OK;}/***************************************************************************/MCO_RET nw_accept( nw_channel_h chan,  /* OUT */ nw_channel_h  ioch,                   uint4 timeout)/* * IN  nw_channel_h chan      - pointer to the listener channel. * IN OUT nw_channel_h  ioch  - pointer to the IO channel waiting for connection. * IN  unsigned long timeout) - wait-for-connect timeout. *  * Description: * Waits for the connection of a remote host to the IO channel. * * Returns MSO_S_OK if successful or error code (see above). */{/* * Insert here the code for accepting connection of remote host: */    return  MCO_S_OK;}/***************************************************************************/MCO_RET nw_connect( nw_channel_h chan,  const char* connect_string,                     uint4 timeout)/* * IN nw_channel_h   chan       - pointer to a channel descriptor * IN const char connect_string - interconnect dependent connection-string * IN  unsigned long timeout); *  * Description: * Connects IO channel to the remote host by it's name. * * Returns MSO_S_OK if successful or error code (see above). */{/* * Insert here the code for connection to master application: */    return  MCO_S_OK;}/***************************************************************************/MCO_RET nw_send ( nw_channel_h chan,   char* buffer,                  int2 buflen,   uint4 timeout)/* * IN nw_channel_h  chan    - pointer to a channel descriptor. * IN  const char*  buffer  - buffer to send. * IN int2 buflen           - buffer length,is cut to 32767 bytes. * IN  unsigned long timeout - send timeout in milliseconds. It is media dependent *                            & application dependent. Application MUST set this  *                            parameter considering the media rate & it's own needs. *  * Description: * Low level data transfer. * Sends a block of data to the communication channel * * Returns MSO_S_OK if successful or error code (see above). */{/* * Insert here the code for sending block of data to communication channel: */    return  MCO_S_OK;}/***************************************************************************/MCO_RET nw_recv( nw_channel_h chan, /* OUT */ char* buffer,  int2 buflen,                /* OUT */ int2* recvlen,  uint4 timeout)/* * IN nw_channel_h chan     - pointer to a channel descriptor. * OUT char* buffer         - buffer to receive. * IN int2 buflen           - buffer length limit,  is cut to 32767 bytes. * OUT int2* recvlen,       - actual received length, is cut to 32767 bytes. * IN unsigned long timeout - receive timeout in milliseconds. It is media dependent *                            & application dependet. Application MUST set this parameter *                            considering the media rate & it's own needs * * Description: * Low level data transfer. * Receives block of data from the communication channel *  * Returns MSO_S_OK if successful or error code (see above). */{/* * Insert here the code for receiving block of data from communication cchannel: */    return MCO_S_OK;}/***************************************************************************/MCO_RET nw_close( nw_channel_h chan)/* * IN nw_channel_h ch)      - pointer to a channel descriptor. * * Description: * Closes the communication channel *  * Returns MSO_S_OK if successful or error code. */{/* * Insert here the code for closing communication cchannel: */    return  MCO_S_OK;}/******************************************************************************/MCO_RET nw_send_message( nw_channel_h chan,	 uint1 type,                          const void*  buffer,  int2 buflen,                          long timeout)/* * IN nw_channel_h  chan    - pointer to channel descriptor. * IN uint1 type            - message type  * IN const char*  buffer   - buffer to send. * IN int2 buflen           - buffer length,is cut to 32767 bytes. * IN  unsigned long timeout - send timeout in milliseconds. It is media dependent *                            & application dependent. Application MUST set this  *                            parameter considering the media rate & it's own needs. *  * Description: * Guaranteed data transfer. MAY NOT be implemented if the transport layer is based * on guaranteed data transfer protocol. * Sends message of any type to a communication channel * * Returns MSO_S_OK if successful or error code (see above). */{/* * Insert here the code for guaranteed sending data to a communication channel: */    return  MCO_S_OK;}/******************************************************************************/MCO_RET nw_recv_message( nw_channel_h chan, uint1 type,

⌨️ 快捷键说明

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