change close to closeScan
[csql.git] / include / Network.h
blobc36d097f796d81ebee34d2752fe17751061c7ce8
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,
55 class ResponsePacket
57 public:
58 ResponsePacket()
59 { stmtID = 0; retVal = 0; isSelect = false; rows=0; }
60 ~ResponsePacket() { }
61 int retVal; // will include for fetch end flag, params flag, proj flag
62 int stmtID;
63 int rows;
64 bool isSelect;
65 char errorString[ERROR_STRING_LENGTH];
66 DbRetVal marshall();
67 DbRetVal unmarshall();
70 class NetworkClient {
71 protected:
72 char hostName[IDENTIFIER_LENGTH];
73 int port;
74 int networkid;
75 bool isConnectedFlag;
76 int responseTimeout; //in secs
77 int connectTimeout;
78 bool encrypt;
79 bool cacheClient;
81 public:
82 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
83 virtual DbRetVal receive()=0;
84 virtual DbRetVal connect()=0;
85 virtual DbRetVal disconnect()=0;
86 virtual void * getResponsePacket()=0;
87 virtual ~NetworkClient(){}
88 void setHost(char *host, int portno, int nwid)
90 strcpy(hostName, host);
91 port = portno;
92 networkid = nwid;
94 int getNetworkID() { return networkid; }
95 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
96 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
97 void setEntryption(bool encr) { encrypt=encr;}
98 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
99 bool isConnected() { return isConnectedFlag; }
100 void setCacheClient() { cacheClient = true; }
101 bool isCacheClient() { return cacheClient; }
103 class UDPClient : public NetworkClient{
104 public:
105 int sockfd;
106 struct sockaddr_in srvAddr;
107 struct sockaddr_in fromAddr;
108 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
109 DbRetVal send(NetworkPacketType type, char *buf, int len);
110 DbRetVal receive();
111 DbRetVal connect();
112 DbRetVal disconnect();
113 void * getResponsePacket() { return NULL; }
114 ~UDPClient();
116 class TCPClient : public NetworkClient{
117 public:
118 int sockfd;
119 struct sockaddr_in srvAddr;
120 ResponsePacket *respPkt;
121 TCPClient(){ isConnectedFlag =false; cacheClient = false; respPkt = new ResponsePacket(); }
122 DbRetVal send(NetworkPacketType type, char *buf, int len);
123 DbRetVal receive();
124 DbRetVal connect();
125 DbRetVal disconnect();
126 void * getResponsePacket() { return respPkt; }
127 ~TCPClient();
129 enum NetworkMode
131 UDP,
134 class NetworkTable
136 NetworkClient* nwClient;
137 public:
138 ~NetworkTable();
139 DbRetVal initialize();
140 void destroy(){}
141 DbRetVal readNetworkConfig();
142 NetworkClient* getNetworkClient() { return nwClient; }
143 DbRetVal connect();
144 DbRetVal disconnect();
145 DbRetVal connectIfNotConnected();
147 class NetworkFactory
149 public:
150 static NetworkClient* createClient(NetworkMode mode)
152 NetworkClient* client = NULL;
153 switch(mode)
155 case UDP:
156 client = new UDPClient();
157 break;
158 case TCP:
159 client = new TCPClient();
160 break;
162 return client;
165 class PacketHeader
167 public:
168 int packetType;
169 int packetLength;
170 int srcNetworkID;
171 int version;
173 class SqlPacketHeader
175 public:
176 int packetType;
177 int packetLength;
179 class BasePacket{
180 protected:
181 //TOTOD:: bool encrypt;
182 char *buffer;
183 int bufferSize;
184 NetworkPacketType pktType;
185 public:
186 //should be called after successful marshall call
187 char* getMarshalledBuffer(){ return buffer; }
188 int getBufferSize() { return bufferSize; }
189 void setBuffer(char *buf) { buffer = buf; }
190 void setBufferSize(int bufSize) { bufferSize = bufSize; }
192 virtual DbRetVal marshall()=0;
193 virtual DbRetVal unmarshall()=0;
194 virtual ~BasePacket(){};
196 class PacketPrepare:public BasePacket
198 public:
199 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
200 type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
201 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
202 int stmtID;
203 int syncMode;
204 int stmtLength;
205 int noParams;
206 int *type;
207 int *length;
208 char *stmtString;
209 DbRetVal marshall();
210 DbRetVal unmarshall();
212 class PacketFree : public BasePacket
214 public:
215 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
216 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
217 int stmtID;
218 DbRetVal marshall();
219 DbRetVal unmarshall();
222 class PacketExecute : public BasePacket
224 public:
225 PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
226 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
227 //TODO::need to free paramvalues based on marshall or unmarshall
229 int stmtID;
230 int noParams;
231 char **paramValues;
233 List paramList;
234 List stmtList;
236 void setStatementList(List stmtlist);
237 void setParams(List list);
239 DbRetVal marshall();
240 DbRetVal unmarshall();
242 class PacketCommit : public BasePacket
244 public:
245 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
246 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
247 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
248 int txnID;
249 int noOfStmts;
250 int *stmtBufSize;
251 char **stmtBuffer;
252 void setExecPackets(int tid, List list);
253 void getExecPacketList(List stmtList, List &list);
254 DbRetVal marshall();
255 DbRetVal unmarshall();
258 class SqlPacketConnect : public BasePacket
260 public:
261 SqlPacketConnect()
263 strcpy(userName, "");
264 strcpy(passWord, "");
265 buffer = NULL;
266 bufferSize = 0;
267 pktType = SQL_NW_PKT_CONNECT;
269 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL; }
270 char userName[IDENTIFIER_LENGTH];
271 char passWord[IDENTIFIER_LENGTH];
272 void setConnParam(char *user, char *pass)
273 { strcpy(userName, user); strcpy(passWord, pass); }
274 DbRetVal marshall();
275 DbRetVal unmarshall();
278 class SqlPacketPrepare : public BasePacket
280 public:
281 SqlPacketPrepare()
282 { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_PREPARE;}
283 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
284 int stmtLength;
285 char *stmtString;
286 DbRetVal marshall();
287 DbRetVal unmarshall();
290 class SqlPacketExecute : public BasePacket
292 public:
293 SqlPacketExecute() { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_EXECUTE;}
294 ~SqlPacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; paramValues = NULL; noParams = 0; }
295 //TODO::need to free paramvalues based on marshall or unmarshall
297 int stmtID;
298 int noParams;
299 char **paramValues;
301 List paramList;
302 List stmtList;
304 void setStatementList(List stmtlist);
305 void setParams(List list);
307 DbRetVal marshall();
308 DbRetVal unmarshall();
311 class SqlPacketParamMetadata : public BasePacket
313 public:
314 SqlPacketParamMetadata()
315 { buffer=NULL; bufferSize =0; noParams = 0;
316 type = NULL; length = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
317 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
318 int stmtID;
319 int noParams;
320 int *type;
321 int *length;
322 DbRetVal marshall();
323 DbRetVal unmarshall();
326 class SqlPacketProjMetadata : public BasePacket
328 public:
329 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
330 type = NULL; length = NULL; pktType = SQL_NW_PKT_PROJ_METADATA; }
331 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
332 int stmtID;
333 int noProjs;
334 int *type;
335 int *length;
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 NetworkStmt
379 public:
380 int srcNetworkID;
381 int stmtID;
382 StatementType type;
383 AbsSqlStatement *stmt;
384 List paramList;
385 List projList;
388 class NetworkServer
390 protected:
391 int sockfd;
392 int port;
393 public:
394 void setServerPort(int p) { port = p; }
395 int getSocket(){ return sockfd; }
396 virtual DbRetVal start()=0;
397 virtual DbRetVal stop()=0;
398 virtual DbRetVal handleClient()=0;
399 virtual DbRetVal send(NetworkPacketType type, char *buf, int len)=0;
402 class UDPServer : public NetworkServer
404 struct sockaddr_in clientAddress;
405 public:
406 UDPServer() { port = 0; sockfd = -1; }
407 DbRetVal start();
408 DbRetVal stop();
409 DbRetVal handleClient();
410 DbRetVal send(NetworkPacketType type, char *buf, int len) { }//dont know what to write
413 class TCPServer : public NetworkServer
415 public:
416 struct sockaddr_in clientAddress;
417 int clientfd;
418 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
419 DbRetVal start();
420 DbRetVal stop();
421 DbRetVal handleClient();
422 DbRetVal send(NetworkPacketType type, char *buf, int len);
425 #endif