Its made sure that all the cases present in sqlapi/Connect worked for network
[csql.git] / include / Network.h
blob5d8b3f4256cfa95670c0bac1907284a9054fccdc
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_DISCONNECT=110,
54 class ResponsePacket
56 public:
57 ResponsePacket()
58 { stmtID = 0; retVal = 0; }
59 ~ResponsePacket() { }
60 int retVal; // will include for fetch end flag, params flag, proj flag
61 int stmtID;
62 char errorString[ERROR_STRING_LENGTH];
63 DbRetVal marshall();
64 DbRetVal unmarshall();
67 class NetworkClient {
68 protected:
69 char hostName[IDENTIFIER_LENGTH];
70 int port;
71 int networkid;
72 bool isConnectedFlag;
73 int responseTimeout; //in secs
74 int connectTimeout;
75 bool encrypt;
76 bool cacheClient;
78 public:
79 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
80 virtual DbRetVal receive()=0;
81 virtual DbRetVal connect()=0;
82 virtual DbRetVal disconnect()=0;
83 virtual void * getResponsePacket()=0;
84 virtual ~NetworkClient(){}
85 void setHost(char *host, int portno, int nwid)
87 strcpy(hostName, host);
88 port = portno;
89 networkid = nwid;
91 int getNetworkID() { return networkid; }
92 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
93 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
94 void setEntryption(bool encr) { encrypt=encr;}
95 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
96 bool isConnected() { return isConnectedFlag; }
97 void setCacheClient() { cacheClient = true; }
98 bool isCacheClient() { return cacheClient; }
100 class UDPClient : public NetworkClient{
101 public:
102 int sockfd;
103 struct sockaddr_in srvAddr;
104 struct sockaddr_in fromAddr;
105 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
106 DbRetVal send(NetworkPacketType type, char *buf, int len);
107 DbRetVal receive();
108 DbRetVal connect();
109 DbRetVal disconnect();
110 void * getResponsePacket() { return NULL; }
111 ~UDPClient();
113 class TCPClient : public NetworkClient{
114 public:
115 int sockfd;
116 struct sockaddr_in srvAddr;
117 ResponsePacket *respPkt;
118 TCPClient(){ isConnectedFlag =false; cacheClient = false; respPkt = new ResponsePacket(); }
119 DbRetVal send(NetworkPacketType type, char *buf, int len);
120 DbRetVal receive();
121 DbRetVal connect();
122 DbRetVal disconnect();
123 void * getResponsePacket() { return respPkt; }
124 ~TCPClient();
126 enum NetworkMode
128 UDP,
131 class NetworkTable
133 NetworkClient* nwClient;
134 public:
135 ~NetworkTable();
136 DbRetVal initialize();
137 void destroy(){}
138 DbRetVal readNetworkConfig();
139 NetworkClient* getNetworkClient() { return nwClient; }
140 DbRetVal connect();
141 DbRetVal disconnect();
142 DbRetVal connectIfNotConnected();
144 class NetworkFactory
146 public:
147 static NetworkClient* createClient(NetworkMode mode)
149 NetworkClient* client = NULL;
150 switch(mode)
152 case UDP:
153 client = new UDPClient();
154 break;
155 case TCP:
156 client = new TCPClient();
157 break;
159 return client;
162 class PacketHeader
164 public:
165 int packetType;
166 int packetLength;
167 int srcNetworkID;
168 int version;
170 class SqlPacketHeader
172 public:
173 int packetType;
174 int packetLength;
176 class BasePacket{
177 protected:
178 //TOTOD:: bool encrypt;
179 char *buffer;
180 int bufferSize;
181 NetworkPacketType pktType;
182 public:
183 //should be called after successful marshall call
184 char* getMarshalledBuffer(){ return buffer; }
185 int getBufferSize() { return bufferSize; }
186 void setBuffer(char *buf) { buffer = buf; }
187 void setBufferSize(int bufSize) { bufferSize = bufSize; }
189 virtual DbRetVal marshall()=0;
190 virtual DbRetVal unmarshall()=0;
191 virtual ~BasePacket(){};
193 class PacketPrepare:public BasePacket
195 public:
196 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
197 type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
198 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
199 int stmtID;
200 int syncMode;
201 int stmtLength;
202 int noParams;
203 int *type;
204 int *length;
205 char *stmtString;
206 DbRetVal marshall();
207 DbRetVal unmarshall();
209 class PacketFree : public BasePacket
211 public:
212 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
213 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
214 int stmtID;
215 DbRetVal marshall();
216 DbRetVal unmarshall();
219 class PacketExecute : public BasePacket
221 public:
222 PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
223 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
224 //TODO::need to free paramvalues based on marshall or unmarshall
226 int stmtID;
227 int noParams;
228 char **paramValues;
230 List paramList;
231 List stmtList;
233 void setStatementList(List stmtlist);
234 void setParams(List list);
236 DbRetVal marshall();
237 DbRetVal unmarshall();
239 class PacketCommit : public BasePacket
241 public:
242 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
243 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
244 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
245 int txnID;
246 int noOfStmts;
247 int *stmtBufSize;
248 char **stmtBuffer;
249 void setExecPackets(int tid, List list);
250 void getExecPacketList(List stmtList, List &list);
251 DbRetVal marshall();
252 DbRetVal unmarshall();
255 class SqlPacketConnect : public BasePacket
257 public:
258 SqlPacketConnect()
260 strcpy(userName, "");
261 strcpy(passWord, "");
262 buffer = NULL;
263 bufferSize = 0;
264 pktType = SQL_NW_PKT_CONNECT;
266 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL; }
267 char userName[IDENTIFIER_LENGTH];
268 char passWord[IDENTIFIER_LENGTH];
269 void setConnParam(char *user, char *pass)
270 { strcpy(userName, user); strcpy(passWord, pass); }
271 DbRetVal marshall();
272 DbRetVal unmarshall();
275 class SqlPacketPrepare : public BasePacket
277 public:
278 SqlPacketPrepare()
279 { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_PREPARE;}
280 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
281 int stmtLength;
282 char *stmtString;
283 DbRetVal marshall();
284 DbRetVal unmarshall();
287 class SqlPacketExecute : public BasePacket
289 public:
290 SqlPacketExecute() { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_EXECUTE;}
291 ~SqlPacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; paramValues = NULL; noParams = 0; }
292 //TODO::need to free paramvalues based on marshall or unmarshall
294 int stmtID;
295 int noParams;
296 char **paramValues;
298 List paramList;
299 List stmtList;
301 void setStatementList(List stmtlist);
302 void setParams(List list);
304 DbRetVal marshall();
305 DbRetVal unmarshall();
308 class SqlPacketParamMetadata : public BasePacket
310 public:
311 SqlPacketParamMetadata()
312 { buffer=NULL; bufferSize =0; noParams = 0;
313 type = NULL; length = NULL; pktType = SQL_NW_PKT_PARAM_METADATA;}
314 ~SqlPacketParamMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
315 int stmtID;
316 int noParams;
317 int *type;
318 int *length;
319 DbRetVal marshall();
320 DbRetVal unmarshall();
323 class SqlPacketProjMetadata : public BasePacket
325 public:
326 SqlPacketProjMetadata() { buffer=NULL; bufferSize =0; noProjs = 0;
327 type = NULL; length = NULL; pktType = SQL_NW_PKT_PROJ_METADATA; }
328 ~SqlPacketProjMetadata() { free(buffer); bufferSize = 0; buffer = NULL; }
329 int stmtID;
330 int noProjs;
331 int *type;
332 int *length;
333 DbRetVal marshall();
334 DbRetVal unmarshall();
337 class SqlPacketFetch : public BasePacket
339 public:
340 SqlPacketFetch() { buffer=NULL; bufferSize = 0;
341 pktType = SQL_NW_PKT_FETCH; }
342 ~SqlPacketFetch() { free(buffer); bufferSize = 0; buffer = NULL; }
343 int stmtID;
344 DbRetVal marshall();
345 DbRetVal unmarshall();
348 class SqlPacketResultSet : public BasePacket
350 public:
351 SqlPacketResultSet() { buffer=NULL; bufferSize = 0; noProjs = 0;
352 projValues=NULL; pktType = SQL_NW_PKT_RESULT_SET; }
353 ~SqlPacketResultSet() { free(buffer); bufferSize = 0; buffer = NULL; }
354 int stmtID;
355 int noProjs;
356 char **projValues;
357 List projList;
358 void setProjList(List list);
359 DbRetVal marshall();
360 DbRetVal unmarshall();
363 class NetworkStmt
365 public:
366 int srcNetworkID;
367 int stmtID;
368 StatementType type;
369 AbsSqlStatement *stmt;
370 List paramList;
371 List projList;
374 class NetworkServer
376 protected:
377 int sockfd;
378 int port;
379 public:
380 void setServerPort(int p) { port = p; }
381 int getSocket(){ return sockfd; }
382 virtual DbRetVal start()=0;
383 virtual DbRetVal stop()=0;
384 virtual DbRetVal handleClient()=0;
385 virtual DbRetVal send(NetworkPacketType type, char *buf, int len)=0;
388 class UDPServer : public NetworkServer
390 struct sockaddr_in clientAddress;
391 public:
392 UDPServer() { port = 0; sockfd = -1; }
393 DbRetVal start();
394 DbRetVal stop();
395 DbRetVal handleClient();
396 DbRetVal send(NetworkPacketType type, char *buf, int len) { }//dont know what to write
399 class TCPServer : public NetworkServer
401 public:
402 struct sockaddr_in clientAddress;
403 int clientfd;
404 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
405 DbRetVal start();
406 DbRetVal stop();
407 DbRetVal handleClient();
408 DbRetVal send(NetworkPacketType type, char *buf, int len);
411 #endif