reorg for cache modulew
[csql.git] / include / Network.h
blobcc475b85c9454d3060a220beb3ae0c9aec700b54
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 <os.h>
20 #include <AbsSqlStatement.h>
21 #include <Parser.h>
23 /*enum DataSyncMode {
24 NOSYNC=0,
25 OSYNC=1,
26 TSYNC=1,
27 ASYNC=2
31 enum NetworkPacketType
33 NW_PKT_PREPARE =1,
34 NW_PKT_EXECUTE =2,
35 NW_PKT_COMMIT =3,
36 NW_PKT_FREE =4,
37 NW_PKT_CONNECT =5,
38 NW_PKT_DISCONNECT =6,
39 SQL_NW_PKT_EXECDIRECT=100,
40 SQL_NW_PKT_CONNECT=101,
41 SQL_NW_PKT_PREPARE=102,
42 SQL_NW_PKT_PARAM_METADATA=103,
43 SQL_NW_PKT_PROJ_METADATA=104,
44 SQL_NW_PKT_EXECUTE=105,
45 SQL_NW_PKT_FETCH=106,
46 SQL_NW_PKT_RESULT_SET=107,
47 SQL_NW_PKT_COMMIT=108,
48 SQL_NW_PKT_ROLLBACK=109,
49 SQL_NW_PKT_FREE=110,
50 SQL_NW_PKT_DISCONNECT=111,
51 SQL_NW_PKT_SHOWTABLES=112,
52 SQL_NW_PKT_ISTABLEPRESENT=113,
53 SQL_NW_PKT_GETRECORDS=114,
54 SQL_NW_PKT_LOADRECORDS=115,
55 SQL_NW_PKT_LASTAIVAL=116,
58 class ResponsePacket
60 public:
61 ResponsePacket()
63 stmtID = 0; retVal = 0; errRetVal = OK; isSelect = false; rows=0;
64 lastAutoIncVal=0; errorString[0] = '\0';
66 ~ResponsePacket() { }
67 int retVal; // will include for fetch end flag, params flag, proj flag
68 DbRetVal errRetVal;
69 int stmtID;
70 int rows;
71 long long lastAutoIncVal;
72 bool isSelect;
73 char errorString[ERROR_STRING_LENGTH];
74 DbRetVal marshall();
75 DbRetVal unmarshall();
78 class NetworkClient {
79 protected:
80 char hostName[IDENTIFIER_LENGTH];
81 int port;
82 int networkid;
83 bool isConnectedFlag;
84 int responseTimeout; //in secs
85 int connectTimeout;
86 bool encrypt;
87 bool cacheClient;
89 public:
90 virtual DbRetVal send( NetworkPacketType type) =0;
91 virtual DbRetVal send( NetworkPacketType type, int stmtid)=0;
92 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
93 virtual DbRetVal receive()=0;
94 virtual DbRetVal connect()=0;
95 virtual DbRetVal disconnect()=0;
96 virtual void * getResponsePacket()=0;
97 virtual ~NetworkClient(){}
98 void setHost(char *host, int portno, int nwid)
100 strcpy(hostName, host);
101 port = portno;
102 networkid = nwid;
104 int getNetworkID() { return networkid; }
105 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
106 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
107 void setEntryption(bool encr) { encrypt=encr;}
108 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
109 bool isConnected() { return isConnectedFlag; }
110 void setIsConnectedFlag(bool fl) { isConnectedFlag = fl; }
111 void setCacheClient() { cacheClient = true; }
112 bool isCacheClient() { return cacheClient; }
114 class UDPClient : public NetworkClient{
115 public:
116 int sockfd;
117 struct sockaddr_in srvAddr;
118 struct sockaddr_in fromAddr;
119 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
120 DbRetVal send(NetworkPacketType type);
121 DbRetVal send(NetworkPacketType type,int stmtid);
122 DbRetVal send(NetworkPacketType type, char *buf, int len);
123 DbRetVal receive();
124 DbRetVal connect();
125 DbRetVal disconnect();
126 void * getResponsePacket() { return NULL; }
127 ~UDPClient();
129 struct PacketHeader;
130 class TCPClient : public NetworkClient{
131 public:
132 int sockfd;
133 struct sockaddr_in srvAddr;
134 ResponsePacket *respPkt;
135 PacketHeader *pktHdr;
136 TCPClient();
137 DbRetVal send(NetworkPacketType type);
138 DbRetVal send(NetworkPacketType type, int stmtid);
139 DbRetVal send(NetworkPacketType type, char *buf, int len);
140 DbRetVal receive();
141 DbRetVal connect();
142 DbRetVal disconnect();
143 void * getResponsePacket() { return respPkt; }
144 ~TCPClient();
146 enum NetworkMode
148 UDP,
151 class NetworkTable
153 NetworkClient* nwClient;
154 public:
155 NetworkTable() { nwClient = NULL; }
156 ~NetworkTable();
157 DbRetVal initialize();
158 void destroy(){}
159 DbRetVal readNetworkConfig();
160 NetworkClient* getNetworkClient() { return nwClient; }
161 DbRetVal connect();
162 DbRetVal disconnect();
163 DbRetVal connectIfNotConnected();
165 class NetworkFactory
167 public:
168 static NetworkClient* createClient(NetworkMode mode)
170 NetworkClient* client = NULL;
171 switch(mode)
173 case UDP:
174 client = new UDPClient();
175 break;
176 case TCP:
177 client = new TCPClient();
178 break;
180 return client;
183 class PacketHeader
185 public:
186 int packetType;
187 int packetLength;
188 int srcNetworkID;
189 int stmtID;
190 int version;
192 /*class SqlPacketHeader
194 public:
195 int packetType;
196 int packetLength;
197 };*/
198 class BasePacket{
199 protected:
200 //TOTOD:: bool encrypt;
201 char *buffer;
202 int bufferSize;
203 NetworkPacketType pktType;
204 public:
205 //should be called after successful marshall call
206 char* getMarshalledBuffer(){ return buffer; }
207 int getBufferSize() { return bufferSize; }
208 void setBuffer(char *buf) { buffer = buf; }
209 void setBufferSize(int bufSize) { bufferSize = bufSize; }
211 virtual DbRetVal marshall()=0;
212 virtual DbRetVal unmarshall()=0;
213 virtual ~BasePacket(){};
215 class PacketPrepare:public BasePacket
217 public:
218 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0; stmtString = NULL;
219 stmtID = 0; stmtLength = 0; type = NULL; length = NULL;
220 pktType = NW_PKT_PREPARE;}
221 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
222 int stmtID;
223 int syncMode;
224 int stmtLength;
225 int noParams;
226 int *type;
227 int *length;
228 char *stmtString;
229 DbRetVal marshall();
230 DbRetVal unmarshall();
232 class PacketFree : public BasePacket
234 public:
235 PacketFree() { buffer=NULL; bufferSize =0; stmtID = 0; pktType = NW_PKT_FREE;}
236 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
237 int stmtID;
238 DbRetVal marshall();
239 DbRetVal unmarshall();
242 class PacketExecute : public BasePacket
244 public:
245 PacketExecute() { buffer=NULL; bufferSize =0;
246 stmtID= 0; noParams= 0; paramValues = NULL;
247 pktType = NW_PKT_EXECUTE;}
248 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
249 //TODO::need to free paramvalues based on marshall or unmarshall
251 int stmtID;
252 int noParams;
253 char **paramValues;
255 List paramList;
256 List stmtList;
258 void setStatementList(List stmtlist);
259 void setParams(List list);
261 DbRetVal marshall();
262 DbRetVal unmarshall();
264 class PacketCommit : public BasePacket
266 public:
267 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
268 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
269 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
270 int txnID;
271 int noOfStmts;
272 int *stmtBufSize;
273 char **stmtBuffer;
274 void setExecPackets(int tid, List list);
275 void getExecPacketList(List stmtList, List &list);
276 DbRetVal marshall();
277 DbRetVal unmarshall();
280 class SqlPacketConnect : public BasePacket
282 public:
283 SqlPacketConnect()
285 strcpy(userName, "");
286 strcpy(passWord, "");
287 buffer = NULL;
288 bufferSize = 0;
289 pktType = SQL_NW_PKT_CONNECT;
291 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL;
293 char userName[IDENTIFIER_LENGTH];
294 char passWord[IDENTIFIER_LENGTH];
295 char sqlApiImplType;
296 void setConnParam(char *user, char *pass, char tp)
298 strcpy(userName, user);
299 strcpy(passWord, pass);
300 sqlApiImplType = tp;
302 DbRetVal marshall();
303 DbRetVal unmarshall();
306 class SqlPacketPrepare : public BasePacket
308 public:
309 SqlPacketPrepare()
310 { buffer=NULL; bufferSize =0;
311 stmtString = NULL; stmtLength = 0; pktType = SQL_NW_PKT_PREPARE; }
312 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
313 int stmtLength;
314 char *stmtString;
315 DbRetVal marshall();
316 DbRetVal unmarshall();
319 class SqlPacketExecute : public BasePacket
321 public:
322 SqlPacketExecute();
323 ~SqlPacketExecute();
324 //TODO::need to free paramvalues based on marshall or unmarshall
326 int stmtID;
327 int noParams;
328 char *nullInfo;
329 char **paramValues;
331 List paramList;
332 List stmtList;
333 char *localBuf[10]; //to store paramValues if noParams <10
335 void setStatementList(List stmtlist);
336 void setParams(List list);
337 void setNullInfo(char *nInfo) { nullInfo = nInfo; }
338 char *getNullInfo() { return nullInfo; }
339 DbRetVal marshall();
340 DbRetVal unmarshall();
343 class SqlPacketParamMetadata : public BasePacket
345 public:
346 SqlPacketParamMetadata()
347 { buffer=NULL; bufferSize =0; noParams = 0;
348 stmtID= 0; data = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
349 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
350 int stmtID;
351 int noParams;
352 void *data;
353 DbRetVal marshall();
354 DbRetVal unmarshall();
357 class SqlPacketProjMetadata : public BasePacket
359 public:
360 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
361 data = NULL; stmtID= 0; pktType = SQL_NW_PKT_PROJ_METADATA; }
362 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
363 int stmtID;
364 int noProjs;
365 void *data;
366 DbRetVal marshall();
367 DbRetVal unmarshall();
370 class SqlPacketFetch : public BasePacket
372 public:
373 SqlPacketFetch() { buffer=NULL; bufferSize = 0;
374 stmtID= 0; pktType = SQL_NW_PKT_FETCH; }
375 ~SqlPacketFetch() { free(buffer); bufferSize = 0; buffer = NULL; }
376 int stmtID;
377 DbRetVal marshall();
378 DbRetVal unmarshall();
381 class SqlPacketFree : public BasePacket
383 public:
384 SqlPacketFree() { buffer=NULL; stmtID=0;bufferSize = 0;
385 pktType = SQL_NW_PKT_FREE; }
386 ~SqlPacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
387 int stmtID;
388 DbRetVal marshall();
389 DbRetVal unmarshall();
392 class SqlPacketResultSet : public BasePacket
394 public:
395 SqlPacketResultSet() { buffer=NULL; bufferSize = 0; noProjs = 0;
396 nullInfo = NULL; projValues=NULL;
397 hasData=0; nullInfoLen=0;
398 pktType = SQL_NW_PKT_RESULT_SET; }
399 ~SqlPacketResultSet() { free(buffer);
400 bufferSize = 0; buffer = NULL;
401 if (projValues) delete[] projValues;
402 nullInfo = NULL; }
403 int hasData;
404 int noProjs;
405 int nullInfoLen;
406 void *nullInfo;
407 char **projValues;
408 List projList;
409 void setNullInfo(char *info){ nullInfo = info; }
410 void setProjList(List list);
411 DbRetVal marshall();
412 DbRetVal unmarshall();
415 class SqlPacketShowTables : public BasePacket
417 public:
418 SqlPacketShowTables() { buffer = NULL; bufferSize = 0; data = NULL;
419 numOfTables=0; pktType= SQL_NW_PKT_SHOWTABLES; }
420 ~SqlPacketShowTables() { free(buffer); bufferSize = 0; buffer = NULL; }
421 int numOfTables;
422 void *data;
423 DbRetVal marshall();
424 DbRetVal unmarshall();
427 class SqlPacketIsTablePresent : public BasePacket
429 public:
430 SqlPacketIsTablePresent() { buffer = NULL; bufferSize = 0;
431 tblName[0] = '\0'; pktType = SQL_NW_PKT_ISTABLEPRESENT; }
432 ~SqlPacketIsTablePresent() { free(buffer); bufferSize = 0; buffer = NULL; }
433 char tblName[IDENTIFIER_LENGTH];
434 void setTableName(char *tName) { strcpy(tblName, tName); }
435 DbRetVal marshall();
436 DbRetVal unmarshall();
439 class SqlPacketGetRecords : public BasePacket
441 public:
442 SqlPacketGetRecords() { buffer = NULL; bufferSize = 0; pages=0;
443 tblName[0] = '\0'; pktType = SQL_NW_PKT_GETRECORDS; }
444 ~SqlPacketGetRecords() { free(buffer); bufferSize = 0; buffer = NULL; }
445 char tblName[IDENTIFIER_LENGTH];
446 int pages;
447 void setTableName(char *tName) { strcpy(tblName, tName); }
448 void setPages(int pgs) { pages = pgs; }
449 DbRetVal marshall();
450 DbRetVal unmarshall();
453 class SqlPacketLoadRecords : public BasePacket
455 public:
456 SqlPacketLoadRecords() { buffer = NULL; bufferSize = 0; pages = 0;
457 pktType = SQL_NW_PKT_LOADRECORDS; }
458 ~SqlPacketLoadRecords() { free(buffer); bufferSize = 0; buffer = NULL; }
459 int pages;
460 void setPages(int pgs) { pages = pgs; }
461 DbRetVal marshall();
462 DbRetVal unmarshall();
465 class NetworkStmt
467 public:
468 int srcNetworkID;
469 int stmtID;
470 StatementType type;
471 AbsSqlStatement *stmt;
472 List paramList;
473 List projList;
474 List tableNamesList; // will be populated only for show tables query
475 NetworkStmt() { srcNetworkID=0; stmtID=0; stmt=NULL; type = UnknownStatement; }
478 class NetworkServer
480 protected:
481 int sockfd;
482 int port;
483 public:
484 NetworkServer() { port =0; sockfd= -1;}
485 void setServerPort(int p) { port = p; }
486 int getSocket(){ return sockfd; }
487 virtual DbRetVal start()=0;
488 virtual DbRetVal stop()=0;
489 virtual DbRetVal handleClient()=0;
492 class UDPServer : public NetworkServer
494 struct sockaddr_in clientAddress;
495 public:
496 UDPServer() { port = 0; sockfd = -1; }
497 DbRetVal start();
498 DbRetVal stop();
499 DbRetVal handleClient();
502 class TCPServer : public NetworkServer
504 public:
505 struct sockaddr_in clientAddress;
506 int clientfd;
507 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
508 DbRetVal start();
509 DbRetVal stop();
510 DbRetVal handleClient();
513 #endif