using tree index
[csql.git] / include / Network.h
blobf8b256dd17c728970b542e68cc270ef012108e5e
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef NETWORK_H
17 #define NETWORK_H
18 #include <CSql.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <AbsSqlStatement.h>
26 /*enum DataSyncMode {
27 NOSYNC=0,
28 OSYNC=1,
29 TSYNC=1,
30 ASYNC=2
33 enum NetworkPacketType
35 NW_PKT_PREPARE =1,
36 NW_PKT_EXECUTE =2,
37 NW_PKT_COMMIT =3,
38 NW_PKT_FREE =4,
39 NW_PKT_CONNECT =5,
40 NW_PKT_DISCONNECT =6
42 class NetworkClient {
43 protected:
44 char hostName[IDENTIFIER_LENGTH];
45 int port;
46 int networkid;
47 bool isConnectedFlag;
48 int responseTimeout; //in secs
49 int connectTimeout;
50 bool encrypt;
51 bool cacheClient;
53 public:
54 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
55 virtual DbRetVal receive()=0;
56 virtual DbRetVal connect()=0;
57 virtual DbRetVal disconnect()=0;
58 virtual ~NetworkClient(){}
59 void setHost(char *host, int portno, int nwid)
61 strcpy(hostName, host);
62 port = portno;
63 networkid = nwid;
65 int getNetworkID() { return networkid; }
66 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
67 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
68 void setEntryption(bool encr) { encrypt=encr;}
69 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
70 bool isConnected() { return isConnectedFlag; }
71 void setCacheClient() { cacheClient = true; }
72 bool isCacheClient() { return cacheClient; }
74 class UDPClient : public NetworkClient{
75 public:
76 int sockfd;
77 struct sockaddr_in srvAddr;
78 struct sockaddr_in fromAddr;
79 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
80 DbRetVal send(NetworkPacketType type, char *buf, int len);
81 DbRetVal receive();
82 DbRetVal connect();
83 DbRetVal disconnect();
84 ~UDPClient();
86 class TCPClient : public NetworkClient{
87 public:
88 int sockfd;
89 struct sockaddr_in srvAddr;
90 TCPClient(){ isConnectedFlag =false; cacheClient = false;}
91 DbRetVal send(NetworkPacketType type, char *buf, int len);
92 DbRetVal receive();
93 DbRetVal connect();
94 DbRetVal disconnect();
95 ~TCPClient();
97 enum NetworkMode
99 UDP,
102 class NetworkTable
104 NetworkClient* nwClient;
105 public:
106 ~NetworkTable();
107 DbRetVal initialize();
108 void destroy(){}
109 DbRetVal readNetworkConfig();
110 NetworkClient* getNetworkClient() { return nwClient; }
111 DbRetVal connect();
112 DbRetVal disconnect();
113 DbRetVal connectIfNotConnected();
115 class NetworkFactory
117 public:
118 static NetworkClient* createClient(NetworkMode mode)
120 NetworkClient* client = NULL;
121 switch(mode)
123 case UDP:
124 client = new UDPClient();
125 break;
126 case TCP:
127 client = new TCPClient();
128 break;
130 return client;
133 class PacketHeader
135 public:
136 int packetType;
137 int packetLength;
138 int srcNetworkID;
139 int version;
141 class BasePacket{
142 protected:
143 //TOTOD:: bool encrypt;
144 char *buffer;
145 int bufferSize;
146 NetworkPacketType pktType;
147 public:
148 //should be called after successful marshall call
149 char* getMarshalledBuffer(){ return buffer; }
150 int getBufferSize() { return bufferSize; }
151 void setBuffer(char *buf) { buffer = buf; }
152 void setBufferSize(int bufSize) { bufferSize = bufSize; }
154 virtual DbRetVal marshall()=0;
155 virtual DbRetVal unmarshall()=0;
156 virtual ~BasePacket(){};
158 class PacketPrepare:public BasePacket
160 public:
161 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
162 type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
163 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
164 int stmtID;
165 int syncMode;
166 int stmtLength;
167 int noParams;
168 int *type;
169 int *length;
170 char *stmtString;
171 DbRetVal marshall();
172 DbRetVal unmarshall();
174 class PacketFree : public BasePacket
176 public:
177 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
178 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
179 int stmtID;
180 DbRetVal marshall();
181 DbRetVal unmarshall();
184 class PacketExecute : public BasePacket
186 public:
187 PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
188 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
189 //TODO::need to free paramvalues based on marshall or unmarshall
191 int stmtID;
192 int noParams;
193 char **paramValues;
195 List paramList;
196 List stmtList;
198 void setStatementList(List stmtlist);
199 void setParams(List list);
201 DbRetVal marshall();
202 DbRetVal unmarshall();
204 class PacketCommit : public BasePacket
206 public:
207 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
208 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
209 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
210 int txnID;
211 int noOfStmts;
212 int *stmtBufSize;
213 char **stmtBuffer;
214 void setExecPackets(int tid, List list);
215 void getExecPacketList(List stmtList, List &list);
216 DbRetVal marshall();
217 DbRetVal unmarshall();
219 class NetworkStmt
221 public:
222 int srcNetworkID;
223 int stmtID;
224 AbsSqlStatement *stmt;
225 List paramList;
228 class NetworkServer
230 protected:
231 int sockfd;
232 int port;
233 public:
234 void setServerPort(int p) { port = p; }
235 int getSocket(){ return sockfd; }
236 virtual DbRetVal start()=0;
237 virtual DbRetVal stop()=0;
238 virtual DbRetVal handleClient()=0;
241 class UDPServer : public NetworkServer
243 struct sockaddr_in clientAddress;
244 public:
245 UDPServer() { port = 0; sockfd = -1; }
246 DbRetVal start();
247 DbRetVal stop();
248 DbRetVal handleClient();
250 class TCPServer : public NetworkServer
252 public:
253 struct sockaddr_in clientAddress;
254 int clientfd;
255 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
256 DbRetVal start();
257 DbRetVal stop();
258 DbRetVal handleClient();
261 #endif