*** empty log message ***
[csql.git] / include / Network.h
blob7a3fadf34e4882912a81bf2fb29fe2964f223853
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 bool isSelect;
72 char errorString[ERROR_STRING_LENGTH];
73 long lastAutoIncVal;
74 long lastAutoIncVal1; //Added for VC Windows to work. For long long , it pads another 4 bytes to structure
75 DbRetVal marshall();
76 DbRetVal unmarshall();
79 class NetworkClient {
80 protected:
81 char hostName[IDENTIFIER_LENGTH];
82 int port;
83 int networkid;
84 bool isConnectedFlag;
85 int responseTimeout; //in secs
86 int connectTimeout;
87 bool encrypt;
88 bool cacheClient;
90 public:
91 virtual DbRetVal send( NetworkPacketType type) =0;
92 virtual DbRetVal send( NetworkPacketType type, int stmtid)=0;
93 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
94 virtual DbRetVal receive()=0;
95 virtual DbRetVal connect()=0;
96 virtual DbRetVal disconnect()=0;
97 virtual void * getResponsePacket()=0;
98 virtual ~NetworkClient(){}
99 void setHost(char *host, int portno, int nwid)
101 strcpy(hostName, host);
102 port = portno;
103 networkid = nwid;
105 int getNetworkID() { return networkid; }
106 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
107 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
108 void setEntryption(bool encr) { encrypt=encr;}
109 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
110 bool isConnected() { return isConnectedFlag; }
111 void setIsConnectedFlag(bool fl) { isConnectedFlag = fl; }
112 void setCacheClient() { cacheClient = true; }
113 bool isCacheClient() { return cacheClient; }
115 class UDPClient : public NetworkClient{
116 public:
117 int sockfd;
118 struct sockaddr_in srvAddr;
119 struct sockaddr_in fromAddr;
120 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
121 DbRetVal send(NetworkPacketType type);
122 DbRetVal send(NetworkPacketType type,int stmtid);
123 DbRetVal send(NetworkPacketType type, char *buf, int len);
124 DbRetVal receive();
125 DbRetVal connect();
126 DbRetVal disconnect();
127 void * getResponsePacket() { return NULL; }
128 ~UDPClient();
130 struct PacketHeader;
131 class TCPClient : public NetworkClient{
132 public:
133 #ifdef WINNT
134 WSADATA wsaData;
135 #endif
136 int sockfd;
137 struct sockaddr_in srvAddr;
138 ResponsePacket *respPkt;
139 PacketHeader *pktHdr;
140 TCPClient();
141 DbRetVal send(NetworkPacketType type);
142 DbRetVal send(NetworkPacketType type, int stmtid);
143 DbRetVal send(NetworkPacketType type, char *buf, int len);
144 DbRetVal receive();
145 DbRetVal connect();
146 DbRetVal disconnect();
147 void * getResponsePacket() { return respPkt; }
148 ~TCPClient();
150 enum NetworkMode
152 UDP,
155 class NetworkTable
157 NetworkClient* nwClient;
158 public:
159 NetworkTable() { nwClient = NULL; }
160 ~NetworkTable();
161 DbRetVal initialize();
162 void destroy(){}
163 DbRetVal readNetworkConfig();
164 NetworkClient* getetworkClient() { return nwClient; }
165 DbRetVal connect();
166 DbRetVal disconnect();
167 DbRetVal connectIfNotConnected();
169 class NetworkFactory
171 public:
172 static NetworkClient* createClient(NetworkMode mode)
174 NetworkClient* client = NULL;
175 switch(mode)
177 case UDP:
178 client = new UDPClient();
179 break;
180 case TCP:
181 client = new TCPClient();
182 break;
184 return client;
187 class PacketHeader
189 public:
190 int packetType;
191 int packetLength;
192 int srcNetworkID;
193 int stmtID;
194 int version;
196 /*class SqlPacketHeader
198 public:
199 int packetType;
200 int packetLength;
201 };*/
202 class BasePacket{
203 protected:
204 //TOTOD:: bool encrypt;
205 char *buffer;
206 int bufferSize;
207 NetworkPacketType pktType;
208 public:
209 //should be called after successful marshall call
210 char* getMarshalledBuffer(){ return buffer; }
211 int getBufferSize() { return bufferSize; }
212 void setBuffer(char *buf) { buffer = buf; }
213 void setBufferSize(int bufSize) { bufferSize = bufSize; }
215 virtual DbRetVal marshall()=0;
216 virtual DbRetVal unmarshall()=0;
217 virtual ~BasePacket(){};
219 class PacketPrepare:public BasePacket
221 public:
222 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0; stmtString = NULL;
223 stmtID = 0; stmtLength = 0; type = NULL; length = NULL;
224 pktType = NW_PKT_PREPARE;}
225 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
226 int stmtID;
227 int syncMode;
228 int stmtLength;
229 int noParams;
230 int *type;
231 int *length;
232 char *stmtString;
233 DbRetVal marshall();
234 DbRetVal unmarshall();
236 class PacketFree : public BasePacket
238 public:
239 PacketFree() { buffer=NULL; bufferSize =0; stmtID = 0; pktType = NW_PKT_FREE;}
240 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
241 int stmtID;
242 DbRetVal marshall();
243 DbRetVal unmarshall();
246 class PacketExecute : public BasePacket
248 public:
249 PacketExecute() { buffer=NULL; bufferSize =0;
250 stmtID= 0; noParams= 0; paramValues = NULL;
251 pktType = NW_PKT_EXECUTE;}
252 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
253 //TODO::need to free paramvalues based on marshall or unmarshall
255 int stmtID;
256 int noParams;
257 char **paramValues;
259 List paramList;
260 List stmtList;
262 void setStatementList(List stmtlist);
263 void setParams(List list);
265 DbRetVal marshall();
266 DbRetVal unmarshall();
268 class PacketCommit : public BasePacket
270 public:
271 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
272 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
273 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
274 int txnID;
275 int noOfStmts;
276 int *stmtBufSize;
277 char **stmtBuffer;
278 void setExecPackets(int tid, List list);
279 void getExecPacketList(List stmtList, List &list);
280 DbRetVal marshall();
281 DbRetVal unmarshall();
284 class SqlPacketConnect : public BasePacket
286 public:
287 SqlPacketConnect()
289 strcpy(userName, "");
290 strcpy(passWord, "");
291 buffer = NULL;
292 bufferSize = 0;
293 pktType = SQL_NW_PKT_CONNECT;
295 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL;
297 char userName[IDENTIFIER_LENGTH];
298 char passWord[IDENTIFIER_LENGTH];
299 char sqlApiImplType;
300 void setConnParam(char *user, char *pass, char tp)
302 strcpy(userName, user);
303 strcpy(passWord, pass);
304 sqlApiImplType = tp;
306 DbRetVal marshall();
307 DbRetVal unmarshall();
310 class SqlPacketPrepare : public BasePacket
312 public:
313 SqlPacketPrepare()
314 { buffer=NULL; bufferSize =0;
315 stmtString = NULL; stmtLength = 0; pktType = SQL_NW_PKT_PREPARE; }
316 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
317 int stmtLength;
318 char *stmtString;
319 DbRetVal marshall();
320 DbRetVal unmarshall();
323 class SqlPacketExecute : public BasePacket
325 public:
326 SqlPacketExecute();
327 ~SqlPacketExecute();
328 //TODO::need to free paramvalues based on marshall or unmarshall
330 int stmtID;
331 int noParams;
332 char *nullInfo;
333 char **paramValues;
335 List paramList;
336 List stmtList;
337 char *localBuf[10]; //to store paramValues if noParams <10
339 void setStatementList(List stmtlist);
340 void setParams(List list);
341 void setNullInfo(char *nInfo) { nullInfo = nInfo; }
342 char *getNullInfo() { return nullInfo; }
343 DbRetVal marshall();
344 DbRetVal unmarshall();
347 class SqlPacketParamMetadata : public BasePacket
349 public:
350 SqlPacketParamMetadata()
351 { buffer=NULL; bufferSize =0; noParams = 0;
352 stmtID= 0; data = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
353 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
354 int stmtID;
355 int noParams;
356 void *data;
357 DbRetVal marshall();
358 DbRetVal unmarshall();
361 class SqlPacketProjMetadata : public BasePacket
363 public:
364 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
365 data = NULL; stmtID= 0; pktType = SQL_NW_PKT_PROJ_METADATA; }
366 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
367 int stmtID;
368 int noProjs;
369 void *data;
370 DbRetVal marshall();
371 DbRetVal unmarshall();
374 class SqlPacketFetch : public BasePacket
376 public:
377 SqlPacketFetch() { buffer=NULL; bufferSize = 0;
378 stmtID= 0; pktType = SQL_NW_PKT_FETCH; }
379 ~SqlPacketFetch() { free(buffer); bufferSize = 0; buffer = NULL; }
380 int stmtID;
381 DbRetVal marshall();
382 DbRetVal unmarshall();
385 class SqlPacketFree : public BasePacket
387 public:
388 SqlPacketFree() { buffer=NULL; stmtID=0;bufferSize = 0;
389 pktType = SQL_NW_PKT_FREE; }
390 ~SqlPacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
391 int stmtID;
392 DbRetVal marshall();
393 DbRetVal unmarshall();
396 class SqlPacketResultSet : public BasePacket
398 public:
399 SqlPacketResultSet() { buffer=NULL; bufferSize = 0; noProjs = 0;
400 nullInfo = NULL; projValues=NULL;
401 hasData=0; nullInfoLen=0;
402 pktType = SQL_NW_PKT_RESULT_SET; }
403 ~SqlPacketResultSet() { free(buffer);
404 bufferSize = 0; buffer = NULL;
405 if (projValues) delete[] projValues;
406 nullInfo = NULL; }
407 int hasData;
408 int noProjs;
409 int nullInfoLen;
410 void *nullInfo;
411 char **projValues;
412 List projList;
413 void setNullInfo(char *info){ nullInfo = info; }
414 void setProjList(List list);
415 DbRetVal marshall();
416 DbRetVal unmarshall();
419 class SqlPacketShowTables : public BasePacket
421 public:
422 SqlPacketShowTables() { buffer = NULL; bufferSize = 0; data = NULL;
423 numOfTables=0; pktType= SQL_NW_PKT_SHOWTABLES; }
424 ~SqlPacketShowTables() { free(buffer); bufferSize = 0; buffer = NULL; }
425 int numOfTables;
426 void *data;
427 DbRetVal marshall();
428 DbRetVal unmarshall();
431 class SqlPacketIsTablePresent : public BasePacket
433 public:
434 SqlPacketIsTablePresent() { buffer = NULL; bufferSize = 0;
435 tblName[0] = '\0'; pktType = SQL_NW_PKT_ISTABLEPRESENT; }
436 ~SqlPacketIsTablePresent() { free(buffer); bufferSize = 0; buffer = NULL; }
437 char tblName[IDENTIFIER_LENGTH];
438 void setTableName(char *tName) { strcpy(tblName, tName); }
439 DbRetVal marshall();
440 DbRetVal unmarshall();
443 class SqlPacketGetRecords : public BasePacket
445 public:
446 SqlPacketGetRecords() { buffer = NULL; bufferSize = 0; pages=0;
447 tblName[0] = '\0'; pktType = SQL_NW_PKT_GETRECORDS; }
448 ~SqlPacketGetRecords() { free(buffer); bufferSize = 0; buffer = NULL; }
449 char tblName[IDENTIFIER_LENGTH];
450 int pages;
451 void setTableName(char *tName) { strcpy(tblName, tName); }
452 void setPages(int pgs) { pages = pgs; }
453 DbRetVal marshall();
454 DbRetVal unmarshall();
457 class SqlPacketLoadRecords : public BasePacket
459 public:
460 SqlPacketLoadRecords() { buffer = NULL; bufferSize = 0; pages = 0;
461 pktType = SQL_NW_PKT_LOADRECORDS; }
462 ~SqlPacketLoadRecords() { free(buffer); bufferSize = 0; buffer = NULL; }
463 int pages;
464 void setPages(int pgs) { pages = pgs; }
465 DbRetVal marshall();
466 DbRetVal unmarshall();
469 class NetworkStmt
471 public:
472 int srcNetworkID;
473 int stmtID;
474 StatementType type;
475 AbsSqlStatement *stmt;
476 List paramList;
477 List projList;
478 List tableNamesList; // will be populated only for show tables query
479 NetworkStmt() { srcNetworkID=0; stmtID=0; stmt=NULL; type = UnknownStatement; }
482 class NetworkServer
484 protected:
485 int sockfd;
486 int port;
487 public:
488 NetworkServer() { port =0; sockfd= -1;}
489 void setServerPort(int p) { port = p; }
490 int getSocket(){ return sockfd; }
491 virtual DbRetVal start()=0;
492 virtual DbRetVal stop()=0;
493 virtual DbRetVal handleClient()=0;
496 class UDPServer : public NetworkServer
498 struct sockaddr_in clientAddress;
499 public:
500 UDPServer() { port = 0; sockfd = -1; }
501 DbRetVal start();
502 DbRetVal stop();
503 DbRetVal handleClient();
506 class TCPServer : public NetworkServer
508 public:
509 struct sockaddr_in clientAddress;
510 int clientfd;
511 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
512 DbRetVal start();
513 DbRetVal stop();
514 DbRetVal handleClient();
517 #endif