submitting patch from enterprise version
[csql.git] / include / Network.h
blob6768eaafa0e1dd79ce3e7695fd02733ff5a7abca
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>
25 #include <Parser.h>
27 /*enum DataSyncMode {
28 NOSYNC=0,
29 OSYNC=1,
30 TSYNC=1,
31 ASYNC=2
34 enum NetworkPacketType
36 NW_PKT_PREPARE =1,
37 NW_PKT_EXECUTE =2,
38 NW_PKT_COMMIT =3,
39 NW_PKT_FREE =4,
40 NW_PKT_CONNECT =5,
41 NW_PKT_DISCONNECT =6,
42 SQL_NW_PKT_CONNECT=101,
43 SQL_NW_PKT_PREPARE=102,
44 SQL_NW_PKT_PARAM_METADATA=103,
45 SQL_NW_PKT_PROJ_METADATA=104,
46 SQL_NW_PKT_EXECUTE=105,
47 SQL_NW_PKT_FETCH=106,
48 SQL_NW_PKT_RESULT_SET=107,
49 SQL_NW_PKT_COMMIT=108,
50 SQL_NW_PKT_ROLLBACK=109,
51 SQL_NW_PKT_FREE=110,
52 SQL_NW_PKT_DISCONNECT=111,
53 SQL_NW_PKT_SHOWTABLES=112,
56 class ResponsePacket
58 public:
59 ResponsePacket()
60 { stmtID = 0; retVal = 0; isSelect = false; rows=0; }
61 ~ResponsePacket() { }
62 int retVal; // will include for fetch end flag, params flag, proj flag
63 DbRetVal errRetVal;
64 int stmtID;
65 int rows;
66 bool isSelect;
67 char errorString[ERROR_STRING_LENGTH];
68 DbRetVal marshall();
69 DbRetVal unmarshall();
72 class NetworkClient {
73 protected:
74 char hostName[IDENTIFIER_LENGTH];
75 int port;
76 int networkid;
77 bool isConnectedFlag;
78 int responseTimeout; //in secs
79 int connectTimeout;
80 bool encrypt;
81 bool cacheClient;
83 public:
84 virtual DbRetVal send( NetworkPacketType type) =0;
85 virtual DbRetVal send( NetworkPacketType type, int stmtid)=0;
86 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
87 virtual DbRetVal receive()=0;
88 virtual DbRetVal connect()=0;
89 virtual DbRetVal disconnect()=0;
90 virtual void * getResponsePacket()=0;
91 virtual ~NetworkClient(){}
92 void setHost(char *host, int portno, int nwid)
94 strcpy(hostName, host);
95 port = portno;
96 networkid = nwid;
98 int getNetworkID() { return networkid; }
99 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
100 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
101 void setEntryption(bool encr) { encrypt=encr;}
102 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
103 bool isConnected() { return isConnectedFlag; }
104 void setCacheClient() { cacheClient = true; }
105 bool isCacheClient() { return cacheClient; }
107 class UDPClient : public NetworkClient{
108 public:
109 int sockfd;
110 struct sockaddr_in srvAddr;
111 struct sockaddr_in fromAddr;
112 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
113 DbRetVal send(NetworkPacketType type);
114 DbRetVal send(NetworkPacketType type,int stmtid);
115 DbRetVal send(NetworkPacketType type, char *buf, int len);
116 DbRetVal receive();
117 DbRetVal connect();
118 DbRetVal disconnect();
119 void * getResponsePacket() { return NULL; }
120 ~UDPClient();
122 struct PacketHeader;
123 class TCPClient : public NetworkClient{
124 public:
125 int sockfd;
126 struct sockaddr_in srvAddr;
127 ResponsePacket *respPkt;
128 PacketHeader *pktHdr;
129 TCPClient();
130 DbRetVal send(NetworkPacketType type);
131 DbRetVal send(NetworkPacketType type, int stmtid);
132 DbRetVal send(NetworkPacketType type, char *buf, int len);
133 DbRetVal receive();
134 DbRetVal connect();
135 DbRetVal disconnect();
136 void * getResponsePacket() { return respPkt; }
137 ~TCPClient();
139 enum NetworkMode
141 UDP,
144 class NetworkTable
146 NetworkClient* nwClient;
147 public:
148 ~NetworkTable();
149 DbRetVal initialize();
150 void destroy(){}
151 DbRetVal readNetworkConfig();
152 NetworkClient* getNetworkClient() { return nwClient; }
153 DbRetVal connect();
154 DbRetVal disconnect();
155 DbRetVal connectIfNotConnected();
157 class NetworkFactory
159 public:
160 static NetworkClient* createClient(NetworkMode mode)
162 NetworkClient* client = NULL;
163 switch(mode)
165 case UDP:
166 client = new UDPClient();
167 break;
168 case TCP:
169 client = new TCPClient();
170 break;
172 return client;
175 class PacketHeader
177 public:
178 int packetType;
179 int packetLength;
180 int srcNetworkID;
181 int stmtID;
182 int version;
184 /*class SqlPacketHeader
186 public:
187 int packetType;
188 int packetLength;
189 };*/
190 class BasePacket{
191 protected:
192 //TOTOD:: bool encrypt;
193 char *buffer;
194 int bufferSize;
195 NetworkPacketType pktType;
196 public:
197 //should be called after successful marshall call
198 char* getMarshalledBuffer(){ return buffer; }
199 int getBufferSize() { return bufferSize; }
200 void setBuffer(char *buf) { buffer = buf; }
201 void setBufferSize(int bufSize) { bufferSize = bufSize; }
203 virtual DbRetVal marshall()=0;
204 virtual DbRetVal unmarshall()=0;
205 virtual ~BasePacket(){};
207 class PacketPrepare:public BasePacket
209 public:
210 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
211 type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
212 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
213 int stmtID;
214 int syncMode;
215 int stmtLength;
216 int noParams;
217 int *type;
218 int *length;
219 char *stmtString;
220 DbRetVal marshall();
221 DbRetVal unmarshall();
223 class PacketFree : public BasePacket
225 public:
226 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
227 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
228 int stmtID;
229 DbRetVal marshall();
230 DbRetVal unmarshall();
233 class PacketExecute : public BasePacket
235 public:
236 PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
237 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
238 //TODO::need to free paramvalues based on marshall or unmarshall
240 int stmtID;
241 int noParams;
242 char **paramValues;
244 List paramList;
245 List stmtList;
247 void setStatementList(List stmtlist);
248 void setParams(List list);
250 DbRetVal marshall();
251 DbRetVal unmarshall();
253 class PacketCommit : public BasePacket
255 public:
256 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
257 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
258 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
259 int txnID;
260 int noOfStmts;
261 int *stmtBufSize;
262 char **stmtBuffer;
263 void setExecPackets(int tid, List list);
264 void getExecPacketList(List stmtList, List &list);
265 DbRetVal marshall();
266 DbRetVal unmarshall();
269 class SqlPacketConnect : public BasePacket
271 public:
272 SqlPacketConnect()
274 strcpy(userName, "");
275 strcpy(passWord, "");
276 buffer = NULL;
277 bufferSize = 0;
278 pktType = SQL_NW_PKT_CONNECT;
280 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL; }
281 char userName[IDENTIFIER_LENGTH];
282 char passWord[IDENTIFIER_LENGTH];
283 char sqlApiImplType;
284 void setConnParam(char *user, char *pass, char tp)
285 { strcpy(userName, user); strcpy(passWord, pass); sqlApiImplType = tp; }
286 DbRetVal marshall();
287 DbRetVal unmarshall();
290 class SqlPacketPrepare : public BasePacket
292 public:
293 SqlPacketPrepare()
294 { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_PREPARE;}
295 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
296 int stmtLength;
297 char *stmtString;
298 DbRetVal marshall();
299 DbRetVal unmarshall();
302 class SqlPacketExecute : public BasePacket
304 public:
305 SqlPacketExecute();// { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_EXECUTE;}
306 ~SqlPacketExecute();// { free(buffer); bufferSize = 0; buffer = NULL; paramValues = NULL; noParams = 0; }
307 //TODO::need to free paramvalues based on marshall or unmarshall
309 int stmtID;
310 int noParams;
311 char *nullInfo;
312 char **paramValues;
314 List paramList;
315 List stmtList;
316 char *localBuf[10]; //to store paramValues if noParams <10
318 void setStatementList(List stmtlist);
319 void setParams(List list);
320 void setNullInfo(char *nInfo) { nullInfo = nInfo; }
321 char *getNullInfo() { return nullInfo; }
322 DbRetVal marshall();
323 DbRetVal unmarshall();
326 class SqlPacketParamMetadata : public BasePacket
328 public:
329 SqlPacketParamMetadata()
330 { buffer=NULL; bufferSize =0; noParams = 0;
331 data = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
332 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
333 int stmtID;
334 int noParams;
335 void *data;
336 DbRetVal marshall();
337 DbRetVal unmarshall();
340 class SqlPacketProjMetadata : public BasePacket
342 public:
343 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
344 data = NULL; pktType = SQL_NW_PKT_PROJ_METADATA; }
345 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
346 int stmtID;
347 int noProjs;
348 void *data;
349 DbRetVal marshall();
350 DbRetVal unmarshall();
353 class SqlPacketFetch : public BasePacket
355 public:
356 SqlPacketFetch() { buffer=NULL; bufferSize = 0;
357 pktType = SQL_NW_PKT_FETCH; }
358 ~SqlPacketFetch() { free(buffer); bufferSize = 0; buffer = NULL; }
359 int stmtID;
360 DbRetVal marshall();
361 DbRetVal unmarshall();
364 class SqlPacketFree : public BasePacket
366 public:
367 SqlPacketFree() { buffer=NULL; bufferSize = 0;
368 pktType = SQL_NW_PKT_FREE; }
369 ~SqlPacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
370 int stmtID;
371 DbRetVal marshall();
372 DbRetVal unmarshall();
375 class SqlPacketResultSet : public BasePacket
377 public:
378 SqlPacketResultSet() { buffer=NULL; bufferSize = 0; noProjs = 0;
379 nullInfo = NULL; projValues=NULL;
380 pktType = SQL_NW_PKT_RESULT_SET; }
381 ~SqlPacketResultSet() { free(buffer); bufferSize = 0; buffer = NULL;
382 nullInfo = NULL; }
383 int hasData;
384 int noProjs;
385 int nullInfoLen;
386 void *nullInfo;
387 char **projValues;
388 List projList;
389 void setNullInfo(char *info){ nullInfo = info; }
390 void setProjList(List list);
391 DbRetVal marshall();
392 DbRetVal unmarshall();
395 class SqlPacketShowTables : public BasePacket
397 public:
398 SqlPacketShowTables() { buffer = NULL; bufferSize = 0; data = NULL;
399 pktType= SQL_NW_PKT_SHOWTABLES; }
400 ~SqlPacketShowTables() { free(buffer); bufferSize = 0; buffer = NULL; }
401 int numOfTables;
402 void *data;
403 DbRetVal marshall();
404 DbRetVal unmarshall();
407 class NetworkStmt
409 public:
410 int srcNetworkID;
411 int stmtID;
412 StatementType type;
413 AbsSqlStatement *stmt;
414 List paramList;
415 List projList;
416 List tableNamesList; // will be populated only for show tables query
419 class NetworkServer
421 protected:
422 int sockfd;
423 int port;
424 public:
425 void setServerPort(int p) { port = p; }
426 int getSocket(){ return sockfd; }
427 virtual DbRetVal start()=0;
428 virtual DbRetVal stop()=0;
429 virtual DbRetVal handleClient()=0;
430 virtual DbRetVal send(NetworkPacketType type, char *buf, int len)=0;
433 class UDPServer : public NetworkServer
435 struct sockaddr_in clientAddress;
436 public:
437 UDPServer() { port = 0; sockfd = -1; }
438 DbRetVal start();
439 DbRetVal stop();
440 DbRetVal handleClient();
441 DbRetVal send(NetworkPacketType type, char *buf, int len) { }//dont know what to write
444 class TCPServer : public NetworkServer
446 public:
447 struct sockaddr_in clientAddress;
448 int clientfd;
449 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
450 DbRetVal start();
451 DbRetVal stop();
452 DbRetVal handleClient();
453 DbRetVal send(NetworkPacketType type, char *buf, int len);
456 #endif