Fixing Build failure.
[csql.git] / include / Network.h
blob7be6dfb4b182132b41de4ddae92bfed2ab28cf5f
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 int stmtID;
64 int rows;
65 bool isSelect;
66 char errorString[ERROR_STRING_LENGTH];
67 DbRetVal marshall();
68 DbRetVal unmarshall();
71 class NetworkClient {
72 protected:
73 char hostName[IDENTIFIER_LENGTH];
74 int port;
75 int networkid;
76 bool isConnectedFlag;
77 int responseTimeout; //in secs
78 int connectTimeout;
79 bool encrypt;
80 bool cacheClient;
82 public:
83 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
84 virtual DbRetVal receive()=0;
85 virtual DbRetVal connect()=0;
86 virtual DbRetVal disconnect()=0;
87 virtual void * getResponsePacket()=0;
88 virtual ~NetworkClient(){}
89 void setHost(char *host, int portno, int nwid)
91 strcpy(hostName, host);
92 port = portno;
93 networkid = nwid;
95 int getNetworkID() { return networkid; }
96 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
97 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
98 void setEntryption(bool encr) { encrypt=encr;}
99 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
100 bool isConnected() { return isConnectedFlag; }
101 void setCacheClient() { cacheClient = true; }
102 bool isCacheClient() { return cacheClient; }
104 class UDPClient : public NetworkClient{
105 public:
106 int sockfd;
107 struct sockaddr_in srvAddr;
108 struct sockaddr_in fromAddr;
109 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
110 DbRetVal send(NetworkPacketType type, char *buf, int len);
111 DbRetVal receive();
112 DbRetVal connect();
113 DbRetVal disconnect();
114 void * getResponsePacket() { return NULL; }
115 ~UDPClient();
117 class TCPClient : public NetworkClient{
118 public:
119 int sockfd;
120 struct sockaddr_in srvAddr;
121 ResponsePacket *respPkt;
122 TCPClient(){ isConnectedFlag =false; cacheClient = false; respPkt = new ResponsePacket(); }
123 DbRetVal send(NetworkPacketType type, char *buf, int len);
124 DbRetVal receive();
125 DbRetVal connect();
126 DbRetVal disconnect();
127 void * getResponsePacket() { return respPkt; }
128 ~TCPClient();
130 enum NetworkMode
132 UDP,
135 class NetworkTable
137 NetworkClient* nwClient;
138 public:
139 ~NetworkTable();
140 DbRetVal initialize();
141 void destroy(){}
142 DbRetVal readNetworkConfig();
143 NetworkClient* getNetworkClient() { return nwClient; }
144 DbRetVal connect();
145 DbRetVal disconnect();
146 DbRetVal connectIfNotConnected();
148 class NetworkFactory
150 public:
151 static NetworkClient* createClient(NetworkMode mode)
153 NetworkClient* client = NULL;
154 switch(mode)
156 case UDP:
157 client = new UDPClient();
158 break;
159 case TCP:
160 client = new TCPClient();
161 break;
163 return client;
166 class PacketHeader
168 public:
169 int packetType;
170 int packetLength;
171 int srcNetworkID;
172 int version;
174 class SqlPacketHeader
176 public:
177 int packetType;
178 int packetLength;
180 class BasePacket{
181 protected:
182 //TOTOD:: bool encrypt;
183 char *buffer;
184 int bufferSize;
185 NetworkPacketType pktType;
186 public:
187 //should be called after successful marshall call
188 char* getMarshalledBuffer(){ return buffer; }
189 int getBufferSize() { return bufferSize; }
190 void setBuffer(char *buf) { buffer = buf; }
191 void setBufferSize(int bufSize) { bufferSize = bufSize; }
193 virtual DbRetVal marshall()=0;
194 virtual DbRetVal unmarshall()=0;
195 virtual ~BasePacket(){};
197 class PacketPrepare:public BasePacket
199 public:
200 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
201 type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
202 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
203 int stmtID;
204 int syncMode;
205 int stmtLength;
206 int noParams;
207 int *type;
208 int *length;
209 char *stmtString;
210 DbRetVal marshall();
211 DbRetVal unmarshall();
213 class PacketFree : public BasePacket
215 public:
216 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
217 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
218 int stmtID;
219 DbRetVal marshall();
220 DbRetVal unmarshall();
223 class PacketExecute : public BasePacket
225 public:
226 PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
227 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
228 //TODO::need to free paramvalues based on marshall or unmarshall
230 int stmtID;
231 int noParams;
232 char **paramValues;
234 List paramList;
235 List stmtList;
237 void setStatementList(List stmtlist);
238 void setParams(List list);
240 DbRetVal marshall();
241 DbRetVal unmarshall();
243 class PacketCommit : public BasePacket
245 public:
246 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
247 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
248 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
249 int txnID;
250 int noOfStmts;
251 int *stmtBufSize;
252 char **stmtBuffer;
253 void setExecPackets(int tid, List list);
254 void getExecPacketList(List stmtList, List &list);
255 DbRetVal marshall();
256 DbRetVal unmarshall();
259 class SqlPacketConnect : public BasePacket
261 public:
262 SqlPacketConnect()
264 strcpy(userName, "");
265 strcpy(passWord, "");
266 buffer = NULL;
267 bufferSize = 0;
268 pktType = SQL_NW_PKT_CONNECT;
270 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL; }
271 char userName[IDENTIFIER_LENGTH];
272 char passWord[IDENTIFIER_LENGTH];
273 char sqlApiImplType;
274 void setConnParam(char *user, char *pass, char tp)
275 { strcpy(userName, user); strcpy(passWord, pass); sqlApiImplType = tp; }
276 DbRetVal marshall();
277 DbRetVal unmarshall();
280 class SqlPacketPrepare : public BasePacket
282 public:
283 SqlPacketPrepare()
284 { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_PREPARE;}
285 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
286 int stmtLength;
287 char *stmtString;
288 DbRetVal marshall();
289 DbRetVal unmarshall();
292 class SqlPacketExecute : public BasePacket
294 public:
295 SqlPacketExecute() { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_EXECUTE;}
296 ~SqlPacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; paramValues = NULL; noParams = 0; }
297 //TODO::need to free paramvalues based on marshall or unmarshall
299 int stmtID;
300 int noParams;
301 char **paramValues;
303 List paramList;
304 List stmtList;
306 void setStatementList(List stmtlist);
307 void setParams(List list);
309 DbRetVal marshall();
310 DbRetVal unmarshall();
313 class SqlPacketParamMetadata : public BasePacket
315 public:
316 SqlPacketParamMetadata()
317 { buffer=NULL; bufferSize =0; noParams = 0;
318 data = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
319 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
320 int stmtID;
321 int noParams;
322 void *data;
323 DbRetVal marshall();
324 DbRetVal unmarshall();
327 class SqlPacketProjMetadata : public BasePacket
329 public:
330 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
331 data = NULL; pktType = SQL_NW_PKT_PROJ_METADATA; }
332 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
333 int stmtID;
334 int noProjs;
335 void *data;
336 DbRetVal marshall();
337 DbRetVal unmarshall();
340 class SqlPacketFetch : public BasePacket
342 public:
343 SqlPacketFetch() { buffer=NULL; bufferSize = 0;
344 pktType = SQL_NW_PKT_FETCH; }
345 ~SqlPacketFetch() { free(buffer); bufferSize = 0; buffer = NULL; }
346 int stmtID;
347 DbRetVal marshall();
348 DbRetVal unmarshall();
351 class SqlPacketFree : public BasePacket
353 public:
354 SqlPacketFree() { buffer=NULL; bufferSize = 0;
355 pktType = SQL_NW_PKT_FREE; }
356 ~SqlPacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
357 int stmtID;
358 DbRetVal marshall();
359 DbRetVal unmarshall();
362 class SqlPacketResultSet : public BasePacket
364 public:
365 SqlPacketResultSet() { buffer=NULL; bufferSize = 0; noProjs = 0;
366 projValues=NULL; pktType = SQL_NW_PKT_RESULT_SET; }
367 ~SqlPacketResultSet() { free(buffer); bufferSize = 0; buffer = NULL; }
368 int stmtID;
369 int noProjs;
370 char **projValues;
371 List projList;
372 void setProjList(List list);
373 DbRetVal marshall();
374 DbRetVal unmarshall();
377 class SqlPacketShowTables : public BasePacket
379 public:
380 SqlPacketShowTables() { buffer = NULL; bufferSize = 0; data = NULL;
381 pktType= SQL_NW_PKT_SHOWTABLES; }
382 ~SqlPacketShowTables() { free(buffer); bufferSize = 0; buffer = NULL; }
383 int stmtID;
384 int numOfTables;
385 void *data;
386 DbRetVal marshall();
387 DbRetVal unmarshall();
390 class NetworkStmt
392 public:
393 int srcNetworkID;
394 int stmtID;
395 StatementType type;
396 AbsSqlStatement *stmt;
397 List paramList;
398 List projList;
399 List tableNamesList; // will be populated only for show tables query
402 class NetworkServer
404 protected:
405 int sockfd;
406 int port;
407 public:
408 void setServerPort(int p) { port = p; }
409 int getSocket(){ return sockfd; }
410 virtual DbRetVal start()=0;
411 virtual DbRetVal stop()=0;
412 virtual DbRetVal handleClient()=0;
413 virtual DbRetVal send(NetworkPacketType type, char *buf, int len)=0;
416 class UDPServer : public NetworkServer
418 struct sockaddr_in clientAddress;
419 public:
420 UDPServer() { port = 0; sockfd = -1; }
421 DbRetVal start();
422 DbRetVal stop();
423 DbRetVal handleClient();
424 DbRetVal send(NetworkPacketType type, char *buf, int len) { }//dont know what to write
427 class TCPServer : public NetworkServer
429 public:
430 struct sockaddr_in clientAddress;
431 int clientfd;
432 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
433 DbRetVal start();
434 DbRetVal stop();
435 DbRetVal handleClient();
436 DbRetVal send(NetworkPacketType type, char *buf, int len);
439 #endif