1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
24 #include <AbsSqlStatement.h>
34 enum NetworkPacketType
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,
48 SQL_NW_PKT_RESULT_SET
=107,
49 SQL_NW_PKT_COMMIT
=108,
50 SQL_NW_PKT_ROLLBACK
=109,
52 SQL_NW_PKT_DISCONNECT
=111,
53 SQL_NW_PKT_SHOWTABLES
=112,
60 { stmtID
= 0; retVal
= 0; isSelect
= false; rows
=0; }
62 int retVal
; // will include for fetch end flag, params flag, proj flag
66 char errorString
[ERROR_STRING_LENGTH
];
68 DbRetVal
unmarshall();
73 char hostName
[IDENTIFIER_LENGTH
];
77 int responseTimeout
; //in secs
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
);
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
{
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
);
113 DbRetVal
disconnect();
114 void * getResponsePacket() { return NULL
; }
117 class TCPClient
: public NetworkClient
{
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
);
126 DbRetVal
disconnect();
127 void * getResponsePacket() { return respPkt
; }
137 NetworkClient
* nwClient
;
140 DbRetVal
initialize();
142 DbRetVal
readNetworkConfig();
143 NetworkClient
* getNetworkClient() { return nwClient
; }
145 DbRetVal
disconnect();
146 DbRetVal
connectIfNotConnected();
151 static NetworkClient
* createClient(NetworkMode mode
)
153 NetworkClient
* client
= NULL
;
157 client
= new UDPClient();
160 client
= new TCPClient();
174 class SqlPacketHeader
182 //TOTOD:: bool encrypt;
185 NetworkPacketType pktType
;
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
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
; }
211 DbRetVal
unmarshall();
213 class PacketFree
: public BasePacket
216 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
217 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
220 DbRetVal
unmarshall();
223 class PacketExecute
: public BasePacket
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
237 void setStatementList(List stmtlist
);
238 void setParams(List list
);
241 DbRetVal
unmarshall();
243 class PacketCommit
: public BasePacket
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
; }
253 void setExecPackets(int tid
, List list
);
254 void getExecPacketList(List stmtList
, List
&list
);
256 DbRetVal
unmarshall();
259 class SqlPacketConnect
: public BasePacket
264 strcpy(userName
, "");
265 strcpy(passWord
, "");
268 pktType
= SQL_NW_PKT_CONNECT
;
270 ~SqlPacketConnect() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
271 char userName
[IDENTIFIER_LENGTH
];
272 char passWord
[IDENTIFIER_LENGTH
];
274 void setConnParam(char *user
, char *pass
, char tp
)
275 { strcpy(userName
, user
); strcpy(passWord
, pass
); sqlApiImplType
= tp
; }
277 DbRetVal
unmarshall();
280 class SqlPacketPrepare
: public BasePacket
284 { buffer
=NULL
; bufferSize
=0; pktType
= SQL_NW_PKT_PREPARE
;}
285 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
289 DbRetVal
unmarshall();
292 class SqlPacketExecute
: public BasePacket
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
306 void setStatementList(List stmtlist
);
307 void setParams(List list
);
310 DbRetVal
unmarshall();
313 class SqlPacketParamMetadata
: public BasePacket
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
; }
324 DbRetVal
unmarshall();
327 class SqlPacketProjMetadata
: public BasePacket
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
; }
337 DbRetVal
unmarshall();
340 class SqlPacketFetch
: public BasePacket
343 SqlPacketFetch() { buffer
=NULL
; bufferSize
= 0;
344 pktType
= SQL_NW_PKT_FETCH
; }
345 ~SqlPacketFetch() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
348 DbRetVal
unmarshall();
351 class SqlPacketFree
: public BasePacket
354 SqlPacketFree() { buffer
=NULL
; bufferSize
= 0;
355 pktType
= SQL_NW_PKT_FREE
; }
356 ~SqlPacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
359 DbRetVal
unmarshall();
362 class SqlPacketResultSet
: public BasePacket
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
; }
372 void setProjList(List list
);
374 DbRetVal
unmarshall();
377 class SqlPacketShowTables
: public BasePacket
380 SqlPacketShowTables() { buffer
= NULL
; bufferSize
= 0; data
= NULL
;
381 pktType
= SQL_NW_PKT_SHOWTABLES
; }
382 ~SqlPacketShowTables() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
387 DbRetVal
unmarshall();
396 AbsSqlStatement
*stmt
;
399 List tableNamesList
; // will be populated only for show tables query
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
;
420 UDPServer() { port
= 0; sockfd
= -1; }
423 DbRetVal
handleClient();
424 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
) { }//dont know what to write
427 class TCPServer
: public NetworkServer
430 struct sockaddr_in clientAddress
;
432 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
435 DbRetVal
handleClient();
436 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);