checkpoint server changes
[csql.git] / include / Network.h
bloba0cf72a988a16ef4e600b63cb10c6477e2a216e2
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
35 enum NetworkPacketType
37 NW_PKT_PREPARE =1,
38 NW_PKT_EXECUTE =2,
39 NW_PKT_COMMIT =3,
40 NW_PKT_FREE =4,
41 NW_PKT_CONNECT =5,
42 NW_PKT_DISCONNECT =6,
43 SQL_NW_PKT_EXECDIRECT=100,
44 SQL_NW_PKT_CONNECT=101,
45 SQL_NW_PKT_PREPARE=102,
46 SQL_NW_PKT_PARAM_METADATA=103,
47 SQL_NW_PKT_PROJ_METADATA=104,
48 SQL_NW_PKT_EXECUTE=105,
49 SQL_NW_PKT_FETCH=106,
50 SQL_NW_PKT_RESULT_SET=107,
51 SQL_NW_PKT_COMMIT=108,
52 SQL_NW_PKT_ROLLBACK=109,
53 SQL_NW_PKT_FREE=110,
54 SQL_NW_PKT_DISCONNECT=111,
55 SQL_NW_PKT_SHOWTABLES=112,
56 SQL_NW_PKT_ISTABLEPRESENT=113,
57 SQL_NW_PKT_GETRECORDS=114,
58 SQL_NW_PKT_LOADRECORDS=115,
61 class ResponsePacket
63 public:
64 ResponsePacket()
66 stmtID = 0; retVal = 0; errRetVal = OK; isSelect = false; rows=0;
67 errorString[0] = '\0';
69 ~ResponsePacket() { }
70 int retVal; // will include for fetch end flag, params flag, proj flag
71 DbRetVal errRetVal;
72 int stmtID;
73 int rows;
74 bool isSelect;
75 char errorString[ERROR_STRING_LENGTH];
76 DbRetVal marshall();
77 DbRetVal unmarshall();
80 class NetworkClient {
81 protected:
82 char hostName[IDENTIFIER_LENGTH];
83 int port;
84 int networkid;
85 bool isConnectedFlag;
86 int responseTimeout; //in secs
87 int connectTimeout;
88 bool encrypt;
89 bool cacheClient;
91 public:
92 virtual DbRetVal send( NetworkPacketType type) =0;
93 virtual DbRetVal send( NetworkPacketType type, int stmtid)=0;
94 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
95 virtual DbRetVal receive()=0;
96 virtual DbRetVal connect()=0;
97 virtual DbRetVal disconnect()=0;
98 virtual void * getResponsePacket()=0;
99 virtual ~NetworkClient(){}
100 void setHost(char *host, int portno, int nwid)
102 strcpy(hostName, host);
103 port = portno;
104 networkid = nwid;
106 int getNetworkID() { return networkid; }
107 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
108 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
109 void setEntryption(bool encr) { encrypt=encr;}
110 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
111 bool isConnected() { return isConnectedFlag; }
112 void setIsConnectedFlag(bool fl) { isConnectedFlag = fl; }
113 void setCacheClient() { cacheClient = true; }
114 bool isCacheClient() { return cacheClient; }
116 class UDPClient : public NetworkClient{
117 public:
118 int sockfd;
119 struct sockaddr_in srvAddr;
120 struct sockaddr_in fromAddr;
121 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
122 DbRetVal send(NetworkPacketType type);
123 DbRetVal send(NetworkPacketType type,int stmtid);
124 DbRetVal send(NetworkPacketType type, char *buf, int len);
125 DbRetVal receive();
126 DbRetVal connect();
127 DbRetVal disconnect();
128 void * getResponsePacket() { return NULL; }
129 ~UDPClient();
131 struct PacketHeader;
132 class TCPClient : public NetworkClient{
133 public:
134 int sockfd;
135 struct sockaddr_in srvAddr;
136 ResponsePacket *respPkt;
137 PacketHeader *pktHdr;
138 TCPClient();
139 DbRetVal send(NetworkPacketType type);
140 DbRetVal send(NetworkPacketType type, int stmtid);
141 DbRetVal send(NetworkPacketType type, char *buf, int len);
142 DbRetVal receive();
143 DbRetVal connect();
144 DbRetVal disconnect();
145 void * getResponsePacket() { return respPkt; }
146 ~TCPClient();
148 enum NetworkMode
150 UDP,
153 class NetworkTable
155 NetworkClient* nwClient;
156 public:
157 NetworkTable() { nwClient = NULL; }
158 ~NetworkTable();
159 DbRetVal initialize();
160 void destroy(){}
161 DbRetVal readNetworkConfig();
162 NetworkClient* getNetworkClient() { return nwClient; }
163 DbRetVal connect();
164 DbRetVal disconnect();
165 DbRetVal connectIfNotConnected();
167 class NetworkFactory
169 public:
170 static NetworkClient* createClient(NetworkMode mode)
172 NetworkClient* client = NULL;
173 switch(mode)
175 case UDP:
176 client = new UDPClient();
177 break;
178 case TCP:
179 client = new TCPClient();
180 break;
182 return client;
185 class PacketHeader
187 public:
188 int packetType;
189 int packetLength;
190 int srcNetworkID;
191 int stmtID;
192 int version;
194 /*class SqlPacketHeader
196 public:
197 int packetType;
198 int packetLength;
199 };*/
200 class BasePacket{
201 protected:
202 //TOTOD:: bool encrypt;
203 char *buffer;
204 int bufferSize;
205 NetworkPacketType pktType;
206 public:
207 //should be called after successful marshall call
208 char* getMarshalledBuffer(){ return buffer; }
209 int getBufferSize() { return bufferSize; }
210 void setBuffer(char *buf) { buffer = buf; }
211 void setBufferSize(int bufSize) { bufferSize = bufSize; }
213 virtual DbRetVal marshall()=0;
214 virtual DbRetVal unmarshall()=0;
215 virtual ~BasePacket(){};
217 class PacketPrepare:public BasePacket
219 public:
220 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0; stmtString = NULL;
221 stmtID = 0; stmtLength = 0; type = NULL; length = NULL;
222 pktType = NW_PKT_PREPARE;}
223 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
224 int stmtID;
225 int syncMode;
226 int stmtLength;
227 int noParams;
228 int *type;
229 int *length;
230 char *stmtString;
231 DbRetVal marshall();
232 DbRetVal unmarshall();
234 class PacketFree : public BasePacket
236 public:
237 PacketFree() { buffer=NULL; bufferSize =0; stmtID = 0; pktType = NW_PKT_FREE;}
238 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
239 int stmtID;
240 DbRetVal marshall();
241 DbRetVal unmarshall();
244 class PacketExecute : public BasePacket
246 public:
247 PacketExecute() { buffer=NULL; bufferSize =0;
248 stmtID= 0; noParams= 0; paramValues = NULL;
249 pktType = NW_PKT_EXECUTE;}
250 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
251 //TODO::need to free paramvalues based on marshall or unmarshall
253 int stmtID;
254 int noParams;
255 char **paramValues;
257 List paramList;
258 List stmtList;
260 void setStatementList(List stmtlist);
261 void setParams(List list);
263 DbRetVal marshall();
264 DbRetVal unmarshall();
266 class PacketCommit : public BasePacket
268 public:
269 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
270 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
271 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
272 int txnID;
273 int noOfStmts;
274 int *stmtBufSize;
275 char **stmtBuffer;
276 void setExecPackets(int tid, List list);
277 void getExecPacketList(List stmtList, List &list);
278 DbRetVal marshall();
279 DbRetVal unmarshall();
282 class SqlPacketConnect : public BasePacket
284 public:
285 SqlPacketConnect()
287 strcpy(userName, "");
288 strcpy(passWord, "");
289 buffer = NULL;
290 bufferSize = 0;
291 pktType = SQL_NW_PKT_CONNECT;
293 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL;
295 char userName[IDENTIFIER_LENGTH];
296 char passWord[IDENTIFIER_LENGTH];
297 char sqlApiImplType;
298 void setConnParam(char *user, char *pass, char tp)
300 strcpy(userName, user);
301 strcpy(passWord, pass);
302 sqlApiImplType = tp;
304 DbRetVal marshall();
305 DbRetVal unmarshall();
308 class SqlPacketPrepare : public BasePacket
310 public:
311 SqlPacketPrepare()
312 { buffer=NULL; bufferSize =0;
313 stmtString = NULL; stmtLength = 0; pktType = SQL_NW_PKT_PREPARE; }
314 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
315 int stmtLength;
316 char *stmtString;
317 DbRetVal marshall();
318 DbRetVal unmarshall();
321 class SqlPacketExecute : public BasePacket
323 public:
324 SqlPacketExecute();
325 ~SqlPacketExecute();
326 //TODO::need to free paramvalues based on marshall or unmarshall
328 int stmtID;
329 int noParams;
330 char *nullInfo;
331 char **paramValues;
333 List paramList;
334 List stmtList;
335 char *localBuf[10]; //to store paramValues if noParams <10
337 void setStatementList(List stmtlist);
338 void setParams(List list);
339 void setNullInfo(char *nInfo) { nullInfo = nInfo; }
340 char *getNullInfo() { return nullInfo; }
341 DbRetVal marshall();
342 DbRetVal unmarshall();
345 class SqlPacketParamMetadata : public BasePacket
347 public:
348 SqlPacketParamMetadata()
349 { buffer=NULL; bufferSize =0; noParams = 0;
350 stmtID= 0; data = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
351 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
352 int stmtID;
353 int noParams;
354 void *data;
355 DbRetVal marshall();
356 DbRetVal unmarshall();
359 class SqlPacketProjMetadata : public BasePacket
361 public:
362 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
363 data = NULL; stmtID= 0; pktType = SQL_NW_PKT_PROJ_METADATA; }
364 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
365 int stmtID;
366 int noProjs;
367 void *data;
368 DbRetVal marshall();
369 DbRetVal unmarshall();
372 class SqlPacketFetch : public BasePacket
374 public:
375 SqlPacketFetch() { buffer=NULL; bufferSize = 0;
376 stmtID= 0; pktType = SQL_NW_PKT_FETCH; }
377 ~SqlPacketFetch() { free(buffer); bufferSize = 0; buffer = NULL; }
378 int stmtID;
379 DbRetVal marshall();
380 DbRetVal unmarshall();
383 class SqlPacketFree : public BasePacket
385 public:
386 SqlPacketFree() { buffer=NULL; stmtID=0;bufferSize = 0;
387 pktType = SQL_NW_PKT_FREE; }
388 ~SqlPacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
389 int stmtID;
390 DbRetVal marshall();
391 DbRetVal unmarshall();
394 class SqlPacketResultSet : public BasePacket
396 public:
397 SqlPacketResultSet() { buffer=NULL; bufferSize = 0; noProjs = 0;
398 nullInfo = NULL; projValues=NULL;
399 hasData=0; nullInfoLen=0;
400 pktType = SQL_NW_PKT_RESULT_SET; }
401 ~SqlPacketResultSet() { free(buffer);
402 bufferSize = 0; buffer = NULL;
403 if (projValues) delete[] projValues;
404 nullInfo = NULL; }
405 int hasData;
406 int noProjs;
407 int nullInfoLen;
408 void *nullInfo;
409 char **projValues;
410 List projList;
411 void setNullInfo(char *info){ nullInfo = info; }
412 void setProjList(List list);
413 DbRetVal marshall();
414 DbRetVal unmarshall();
417 class SqlPacketShowTables : public BasePacket
419 public:
420 SqlPacketShowTables() { buffer = NULL; bufferSize = 0; data = NULL;
421 numOfTables=0; pktType= SQL_NW_PKT_SHOWTABLES; }
422 ~SqlPacketShowTables() { free(buffer); bufferSize = 0; buffer = NULL; }
423 int numOfTables;
424 void *data;
425 DbRetVal marshall();
426 DbRetVal unmarshall();
429 class SqlPacketIsTablePresent : public BasePacket
431 public:
432 SqlPacketIsTablePresent() { buffer = NULL; bufferSize = 0;
433 tblName[0] = '\0'; pktType = SQL_NW_PKT_ISTABLEPRESENT; }
434 ~SqlPacketIsTablePresent() { free(buffer); bufferSize = 0; buffer = NULL; }
435 char tblName[IDENTIFIER_LENGTH];
436 void setTableName(char *tName) { strcpy(tblName, tName); }
437 DbRetVal marshall();
438 DbRetVal unmarshall();
441 class SqlPacketGetRecords : public BasePacket
443 public:
444 SqlPacketGetRecords() { buffer = NULL; bufferSize = 0; pages=0;
445 tblName[0] = '\0'; pktType = SQL_NW_PKT_GETRECORDS; }
446 ~SqlPacketGetRecords() { free(buffer); bufferSize = 0; buffer = NULL; }
447 char tblName[IDENTIFIER_LENGTH];
448 int pages;
449 void setTableName(char *tName) { strcpy(tblName, tName); }
450 void setPages(int pgs) { pages = pgs; }
451 DbRetVal marshall();
452 DbRetVal unmarshall();
455 class SqlPacketLoadRecords : public BasePacket
457 public:
458 SqlPacketLoadRecords() { buffer = NULL; bufferSize = 0; pages = 0;
459 pktType = SQL_NW_PKT_LOADRECORDS; }
460 ~SqlPacketLoadRecords() { free(buffer); bufferSize = 0; buffer = NULL; }
461 int pages;
462 void setPages(int pgs) { pages = pgs; }
463 DbRetVal marshall();
464 DbRetVal unmarshall();
467 class NetworkStmt
469 public:
470 int srcNetworkID;
471 int stmtID;
472 StatementType type;
473 AbsSqlStatement *stmt;
474 List paramList;
475 List projList;
476 List tableNamesList; // will be populated only for show tables query
477 NetworkStmt() { srcNetworkID=0; stmtID=0; stmt=NULL; type = UnknownStatement; }
480 class NetworkServer
482 protected:
483 int sockfd;
484 int port;
485 public:
486 NetworkServer() { port =0; sockfd= -1;}
487 void setServerPort(int p) { port = p; }
488 int getSocket(){ return sockfd; }
489 virtual DbRetVal start()=0;
490 virtual DbRetVal stop()=0;
491 virtual DbRetVal handleClient()=0;
494 class UDPServer : public NetworkServer
496 struct sockaddr_in clientAddress;
497 public:
498 UDPServer() { port = 0; sockfd = -1; }
499 DbRetVal start();
500 DbRetVal stop();
501 DbRetVal handleClient();
504 class TCPServer : public NetworkServer
506 public:
507 struct sockaddr_in clientAddress;
508 int clientfd;
509 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
510 DbRetVal start();
511 DbRetVal stop();
512 DbRetVal handleClient();
515 #endif