Testing Commit in autocommit mode
[csql.git] / include / Network.h
blob5e1ff30ea9527c0cea26892277ae318b9a15c228
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; }
60 ~ResponsePacket() { }
61 int retVal; // will include for fetch end flag, params flag, proj flag
62 int stmtID;
63 char errorString[ERROR_STRING_LENGTH];
64 DbRetVal marshall();
65 DbRetVal unmarshall();
68 class NetworkClient {
69 protected:
70 char hostName[IDENTIFIER_LENGTH];
71 int port;
72 int networkid;
73 bool isConnectedFlag;
74 int responseTimeout; //in secs
75 int connectTimeout;
76 bool encrypt;
77 bool cacheClient;
79 public:
80 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
81 virtual DbRetVal receive()=0;
82 virtual DbRetVal connect()=0;
83 virtual DbRetVal disconnect()=0;
84 virtual void * getResponsePacket()=0;
85 virtual ~NetworkClient(){}
86 void setHost(char *host, int portno, int nwid)
88 strcpy(hostName, host);
89 port = portno;
90 networkid = nwid;
92 int getNetworkID() { return networkid; }
93 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
94 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
95 void setEntryption(bool encr) { encrypt=encr;}
96 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
97 bool isConnected() { return isConnectedFlag; }
98 void setCacheClient() { cacheClient = true; }
99 bool isCacheClient() { return cacheClient; }
101 class UDPClient : public NetworkClient{
102 public:
103 int sockfd;
104 struct sockaddr_in srvAddr;
105 struct sockaddr_in fromAddr;
106 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
107 DbRetVal send(NetworkPacketType type, char *buf, int len);
108 DbRetVal receive();
109 DbRetVal connect();
110 DbRetVal disconnect();
111 void * getResponsePacket() { return NULL; }
112 ~UDPClient();
114 class TCPClient : public NetworkClient{
115 public:
116 int sockfd;
117 struct sockaddr_in srvAddr;
118 ResponsePacket *respPkt;
119 TCPClient(){ isConnectedFlag =false; cacheClient = false; respPkt = new ResponsePacket(); }
120 DbRetVal send(NetworkPacketType type, char *buf, int len);
121 DbRetVal receive();
122 DbRetVal connect();
123 DbRetVal disconnect();
124 void * getResponsePacket() { return respPkt; }
125 ~TCPClient();
127 enum NetworkMode
129 UDP,
132 class NetworkTable
134 NetworkClient* nwClient;
135 public:
136 ~NetworkTable();
137 DbRetVal initialize();
138 void destroy(){}
139 DbRetVal readNetworkConfig();
140 NetworkClient* getNetworkClient() { return nwClient; }
141 DbRetVal connect();
142 DbRetVal disconnect();
143 DbRetVal connectIfNotConnected();
145 class NetworkFactory
147 public:
148 static NetworkClient* createClient(NetworkMode mode)
150 NetworkClient* client = NULL;
151 switch(mode)
153 case UDP:
154 client = new UDPClient();
155 break;
156 case TCP:
157 client = new TCPClient();
158 break;
160 return client;
163 class PacketHeader
165 public:
166 int packetType;
167 int packetLength;
168 int srcNetworkID;
169 int version;
171 class SqlPacketHeader
173 public:
174 int packetType;
175 int packetLength;
177 class BasePacket{
178 protected:
179 //TOTOD:: bool encrypt;
180 char *buffer;
181 int bufferSize;
182 NetworkPacketType pktType;
183 public:
184 //should be called after successful marshall call
185 char* getMarshalledBuffer(){ return buffer; }
186 int getBufferSize() { return bufferSize; }
187 void setBuffer(char *buf) { buffer = buf; }
188 void setBufferSize(int bufSize) { bufferSize = bufSize; }
190 virtual DbRetVal marshall()=0;
191 virtual DbRetVal unmarshall()=0;
192 virtual ~BasePacket(){};
194 class PacketPrepare:public BasePacket
196 public:
197 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
198 type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
199 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
200 int stmtID;
201 int syncMode;
202 int stmtLength;
203 int noParams;
204 int *type;
205 int *length;
206 char *stmtString;
207 DbRetVal marshall();
208 DbRetVal unmarshall();
210 class PacketFree : public BasePacket
212 public:
213 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
214 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
215 int stmtID;
216 DbRetVal marshall();
217 DbRetVal unmarshall();
220 class PacketExecute : public BasePacket
222 public:
223 PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
224 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
225 //TODO::need to free paramvalues based on marshall or unmarshall
227 int stmtID;
228 int noParams;
229 char **paramValues;
231 List paramList;
232 List stmtList;
234 void setStatementList(List stmtlist);
235 void setParams(List list);
237 DbRetVal marshall();
238 DbRetVal unmarshall();
240 class PacketCommit : public BasePacket
242 public:
243 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
244 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
245 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
246 int txnID;
247 int noOfStmts;
248 int *stmtBufSize;
249 char **stmtBuffer;
250 void setExecPackets(int tid, List list);
251 void getExecPacketList(List stmtList, List &list);
252 DbRetVal marshall();
253 DbRetVal unmarshall();
256 class SqlPacketConnect : public BasePacket
258 public:
259 SqlPacketConnect()
261 strcpy(userName, "");
262 strcpy(passWord, "");
263 buffer = NULL;
264 bufferSize = 0;
265 pktType = SQL_NW_PKT_CONNECT;
267 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL; }
268 char userName[IDENTIFIER_LENGTH];
269 char passWord[IDENTIFIER_LENGTH];
270 void setConnParam(char *user, char *pass)
271 { strcpy(userName, user); strcpy(passWord, pass); }
272 DbRetVal marshall();
273 DbRetVal unmarshall();
276 class SqlPacketPrepare : public BasePacket
278 public:
279 SqlPacketPrepare()
280 { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_PREPARE;}
281 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
282 int stmtLength;
283 char *stmtString;
284 DbRetVal marshall();
285 DbRetVal unmarshall();
288 class SqlPacketExecute : public BasePacket
290 public:
291 SqlPacketExecute() { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_EXECUTE;}
292 ~SqlPacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; paramValues = NULL; noParams = 0; }
293 //TODO::need to free paramvalues based on marshall or unmarshall
295 int stmtID;
296 int noParams;
297 char **paramValues;
299 List paramList;
300 List stmtList;
302 void setStatementList(List stmtlist);
303 void setParams(List list);
305 DbRetVal marshall();
306 DbRetVal unmarshall();
309 class SqlPacketParamMetadata : public BasePacket
311 public:
312 SqlPacketParamMetadata()
313 { buffer=NULL; bufferSize =0; noParams = 0;
314 type = NULL; length = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
315 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
316 int stmtID;
317 int noParams;
318 int *type;
319 int *length;
320 DbRetVal marshall();
321 DbRetVal unmarshall();
324 class SqlPacketProjMetadata : public BasePacket
326 public:
327 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
328 type = NULL; length = NULL; pktType = SQL_NW_PKT_PROJ_METADATA; }
329 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
330 int stmtID;
331 int noProjs;
332 int *type;
333 int *length;
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