include/Network.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #ifndef NETWORK_H
00017 #define NETWORK_H
00018 #include <CSql.h>
00019 #include <sys/types.h>
00020 #include <sys/socket.h>
00021 #include <netinet/in.h>
00022 #include <arpa/inet.h>
00023 #include <netdb.h>
00024 #include <AbsSqlStatement.h>
00025 
00026 /*enum DataSyncMode {
00027     NOSYNC=0,
00028     OSYNC=1,
00029     TSYNC=1,
00030     ASYNC=2
00031 };
00032 */
00033 enum NetworkPacketType
00034 {
00035     NW_PKT_PREPARE =1,
00036     NW_PKT_EXECUTE =2,
00037     NW_PKT_COMMIT =3,
00038     NW_PKT_FREE =4,
00039     NW_PKT_CONNECT =5,
00040     NW_PKT_DISCONNECT =6
00041 };
00042 class NetworkClient {
00043     protected:
00044     char hostName[IDENTIFIER_LENGTH];
00045     int port;
00046     int networkid;
00047     bool isConnectedFlag;
00048     int responseTimeout; //in secs
00049     int connectTimeout;
00050     bool encrypt;
00051     bool cacheClient;
00052 
00053     public:
00054     virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
00055     virtual DbRetVal receive()=0;
00056     virtual DbRetVal connect()=0;
00057     virtual DbRetVal disconnect()=0;
00058     virtual ~NetworkClient(){}
00059     void setHost(char *host, int portno, int nwid)
00060     {
00061         strcpy(hostName, host);
00062         port = portno;
00063         networkid = nwid;
00064     }
00065     int getNetworkID() { return networkid; }
00066     void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
00067     void setResponseTimeout(int timeout) { responseTimeout=timeout;}
00068     void setEntryption(bool encr) { encrypt=encr;}
00069     void setConnectFlag(bool flag) { isConnectedFlag=flag;}
00070     bool isConnected() { return isConnectedFlag; }
00071     void setCacheClient() { cacheClient = true; }
00072     bool isCacheClient() { return cacheClient; }
00073 };
00074 class UDPClient : public NetworkClient{
00075     public:
00076     int sockfd;
00077     struct sockaddr_in srvAddr;
00078     struct sockaddr_in fromAddr;
00079     UDPClient(){ isConnectedFlag =false; cacheClient = false;}
00080     DbRetVal send(NetworkPacketType type, char *buf, int len);
00081     DbRetVal receive();
00082     DbRetVal connect();
00083     DbRetVal disconnect();
00084     ~UDPClient();
00085 };
00086 class TCPClient : public NetworkClient{
00087     public:
00088     int sockfd;
00089     struct sockaddr_in srvAddr;
00090     TCPClient(){ isConnectedFlag =false; cacheClient = false;}
00091     DbRetVal send(NetworkPacketType type, char *buf, int len);
00092     DbRetVal receive();
00093     DbRetVal connect();
00094     DbRetVal disconnect();
00095     ~TCPClient();
00096 };
00097 enum NetworkMode
00098 {
00099     UDP,
00100     TCP
00101 };
00102 class NetworkTable
00103 {
00104     NetworkClient* nwClient;
00105     public:
00106     ~NetworkTable();
00107     DbRetVal initialize();
00108     void destroy(){}
00109     DbRetVal readNetworkConfig();
00110     NetworkClient* getNetworkClient() { return nwClient; }
00111     DbRetVal connect();
00112     DbRetVal disconnect();
00113     DbRetVal connectIfNotConnected();
00114 };
00115 class NetworkFactory 
00116 {
00117     public:
00118     static NetworkClient* createClient(NetworkMode mode)
00119     {
00120         NetworkClient* client = NULL;
00121         switch(mode)
00122         {
00123             case UDP:
00124                 client = new UDPClient();
00125                 break;
00126             case TCP:
00127                 client = new TCPClient();
00128                 break;
00129         }
00130         return client;
00131     }
00132 };
00133 class PacketHeader
00134 {
00135     public:
00136     int packetType;
00137     int packetLength;
00138     int srcNetworkID;
00139     int version;
00140 };
00141 class BasePacket{
00142     protected:
00143     //TOTOD:: bool encrypt;
00144     char *buffer;
00145     int bufferSize; 
00146     NetworkPacketType pktType;
00147     public:
00148     //should be called after successful marshall call
00149     char* getMarshalledBuffer(){ return buffer; }
00150     int getBufferSize() { return bufferSize; }
00151     void setBuffer(char *buf) { buffer =  buf; }
00152     void setBufferSize(int bufSize) { bufferSize = bufSize; }
00153 
00154     virtual DbRetVal marshall()=0;
00155     virtual DbRetVal unmarshall()=0;
00156     virtual ~BasePacket(){};
00157 };
00158 class PacketPrepare:public BasePacket
00159 {
00160     public:
00161     PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
00162                       type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
00163     ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
00164     int stmtID;
00165     int syncMode;
00166     int stmtLength;
00167     int noParams;
00168     int *type;
00169     int *length;
00170     char *stmtString;
00171     DbRetVal marshall();
00172     DbRetVal unmarshall();
00173 };
00174 class PacketFree : public BasePacket
00175 {
00176     public:
00177     PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
00178     ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
00179     int stmtID;
00180     DbRetVal marshall();
00181     DbRetVal unmarshall();
00182 };
00183 
00184 class PacketExecute : public BasePacket
00185 {
00186     public:
00187     PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
00188     ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
00189     //TODO::need to free paramvalues based on marshall or unmarshall
00190 
00191     int stmtID;
00192     int noParams;
00193     char **paramValues;
00194 
00195     List paramList;
00196     List stmtList;
00197 
00198     void setStatementList(List stmtlist);
00199     void setParams(List list);
00200   
00201     DbRetVal marshall();
00202     DbRetVal unmarshall();
00203 };
00204 class PacketCommit : public BasePacket
00205 {
00206     public:
00207     PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
00208                      buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
00209     ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
00210     int txnID;
00211     int noOfStmts;
00212     int *stmtBufSize;
00213     char **stmtBuffer;
00214     void setExecPackets(int tid, List list);
00215     void getExecPacketList(List stmtList, List &list);
00216     DbRetVal marshall();
00217     DbRetVal unmarshall();
00218 };
00219 class NetworkStmt
00220 {
00221     public:
00222     int srcNetworkID;
00223     int stmtID;
00224     AbsSqlStatement *stmt;
00225     List paramList;
00226 
00227 };
00228 class NetworkServer
00229 {
00230    protected:
00231    int sockfd;
00232    int port;
00233    public:
00234    void setServerPort(int p) { port = p; }
00235    int getSocket(){ return sockfd; }
00236    virtual DbRetVal start()=0;
00237    virtual DbRetVal stop()=0;
00238    virtual DbRetVal handleClient()=0;
00239  
00240 };
00241 class UDPServer : public NetworkServer
00242 {
00243    struct sockaddr_in clientAddress;
00244    public:
00245    UDPServer() { port = 0; sockfd = -1; }
00246    DbRetVal start();
00247    DbRetVal stop();
00248    DbRetVal handleClient();
00249 };
00250 class TCPServer : public NetworkServer
00251 {
00252    public:
00253    struct sockaddr_in clientAddress;
00254    int clientfd;
00255    TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
00256    DbRetVal start();
00257    DbRetVal stop();
00258    DbRetVal handleClient();
00259 };
00260 
00261 #endif

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7