getParamInfo and getProjInfo implemented.
[csql.git] / include / Network.h
blob834924abfef6e0f73b0e8168de0e15e850bf2da0
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 data = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
317 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
318 int stmtID;
319 int noParams;
320 void *data;
321 DbRetVal marshall();
322 DbRetVal unmarshall();
325 class SqlPacketProjMetadata : public BasePacket
327 public:
328 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
329 data = NULL; pktType = SQL_NW_PKT_PROJ_METADATA; }
330 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
331 int stmtID;
332 int noProjs;
333 void *data;
334 DbRetVal marshall();
335 DbRetVal unmarshall();
338 class SqlPacketFetch : public BasePacket
340 public:
341 SqlPacketFetch() { buffer=NULL; bufferSize = 0;
342 pktType = SQL_NW_PKT_FETCH; }
343 ~SqlPacketFetch() { free(buffer); bufferSize = 0; buffer = NULL; }
344 int stmtID;
345 DbRetVal marshall();
346 DbRetVal unmarshall();
349 class SqlPacketFree : public BasePacket
351 public:
352 SqlPacketFree() { buffer=NULL; bufferSize = 0;
353 pktType = SQL_NW_PKT_FREE; }
354 ~SqlPacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
355 int stmtID;
356 DbRetVal marshall();
357 DbRetVal unmarshall();
360 class SqlPacketResultSet : public BasePacket
362 public:
363 SqlPacketResultSet() { buffer=NULL; bufferSize = 0; noProjs = 0;
364 projValues=NULL; pktType = SQL_NW_PKT_RESULT_SET; }
365 ~SqlPacketResultSet() { free(buffer); bufferSize = 0; buffer = NULL; }
366 int stmtID;
367 int noProjs;
368 char **projValues;
369 List projList;
370 void setProjList(List list);
371 DbRetVal marshall();
372 DbRetVal unmarshall();
375 class NetworkStmt
377 public:
378 int srcNetworkID;
379 int stmtID;
380 StatementType type;
381 AbsSqlStatement *stmt;
382 List paramList;
383 List projList;
386 class NetworkServer
388 protected:
389 int sockfd;
390 int port;
391 public:
392 void setServerPort(int p) { port = p; }
393 int getSocket(){ return sockfd; }
394 virtual DbRetVal start()=0;
395 virtual DbRetVal stop()=0;
396 virtual DbRetVal handleClient()=0;
397 virtual DbRetVal send(NetworkPacketType type, char *buf, int len)=0;
400 class UDPServer : public NetworkServer
402 struct sockaddr_in clientAddress;
403 public:
404 UDPServer() { port = 0; sockfd = -1; }
405 DbRetVal start();
406 DbRetVal stop();
407 DbRetVal handleClient();
408 DbRetVal send(NetworkPacketType type, char *buf, int len) { }//dont know what to write
411 class TCPServer : public NetworkServer
413 public:
414 struct sockaddr_in clientAddress;
415 int clientfd;
416 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
417 DbRetVal start();
418 DbRetVal stop();
419 DbRetVal handleClient();
420 DbRetVal send(NetworkPacketType type, char *buf, int len);
423 #endif