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
67 char errorString
[ERROR_STRING_LENGTH
];
69 DbRetVal
unmarshall();
74 char hostName
[IDENTIFIER_LENGTH
];
78 int responseTimeout
; //in secs
84 virtual DbRetVal
send( NetworkPacketType type
) =0;
85 virtual DbRetVal
send( NetworkPacketType type
, int stmtid
)=0;
86 virtual DbRetVal
send( NetworkPacketType type
, char *buf
, int len
)=0;
87 virtual DbRetVal
receive()=0;
88 virtual DbRetVal
connect()=0;
89 virtual DbRetVal
disconnect()=0;
90 virtual void * getResponsePacket()=0;
91 virtual ~NetworkClient(){}
92 void setHost(char *host
, int portno
, int nwid
)
94 strcpy(hostName
, host
);
98 int getNetworkID() { return networkid
; }
99 void setConnectionTimeout(int timeout
) { connectTimeout
=timeout
;}
100 void setResponseTimeout(int timeout
) { responseTimeout
=timeout
;}
101 void setEntryption(bool encr
) { encrypt
=encr
;}
102 void setConnectFlag(bool flag
) { isConnectedFlag
=flag
;}
103 bool isConnected() { return isConnectedFlag
; }
104 void setCacheClient() { cacheClient
= true; }
105 bool isCacheClient() { return cacheClient
; }
107 class UDPClient
: public NetworkClient
{
110 struct sockaddr_in srvAddr
;
111 struct sockaddr_in fromAddr
;
112 UDPClient(){ isConnectedFlag
=false; cacheClient
= false;}
113 DbRetVal
send(NetworkPacketType type
);
114 DbRetVal
send(NetworkPacketType type
,int stmtid
);
115 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
118 DbRetVal
disconnect();
119 void * getResponsePacket() { return NULL
; }
123 class TCPClient
: public NetworkClient
{
126 struct sockaddr_in srvAddr
;
127 ResponsePacket
*respPkt
;
128 PacketHeader
*pktHdr
;
130 DbRetVal
send(NetworkPacketType type
);
131 DbRetVal
send(NetworkPacketType type
, int stmtid
);
132 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
135 DbRetVal
disconnect();
136 void * getResponsePacket() { return respPkt
; }
146 NetworkClient
* nwClient
;
149 DbRetVal
initialize();
151 DbRetVal
readNetworkConfig();
152 NetworkClient
* getNetworkClient() { return nwClient
; }
154 DbRetVal
disconnect();
155 DbRetVal
connectIfNotConnected();
160 static NetworkClient
* createClient(NetworkMode mode
)
162 NetworkClient
* client
= NULL
;
166 client
= new UDPClient();
169 client
= new TCPClient();
184 /*class SqlPacketHeader
192 //TOTOD:: bool encrypt;
195 NetworkPacketType pktType
;
197 //should be called after successful marshall call
198 char* getMarshalledBuffer(){ return buffer
; }
199 int getBufferSize() { return bufferSize
; }
200 void setBuffer(char *buf
) { buffer
= buf
; }
201 void setBufferSize(int bufSize
) { bufferSize
= bufSize
; }
203 virtual DbRetVal
marshall()=0;
204 virtual DbRetVal
unmarshall()=0;
205 virtual ~BasePacket(){};
207 class PacketPrepare
:public BasePacket
210 PacketPrepare() { buffer
=NULL
; bufferSize
=0; noParams
= 0;
211 type
= NULL
; length
= NULL
; pktType
= NW_PKT_PREPARE
;}
212 ~PacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
221 DbRetVal
unmarshall();
223 class PacketFree
: public BasePacket
226 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
227 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
230 DbRetVal
unmarshall();
233 class PacketExecute
: public BasePacket
236 PacketExecute() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_EXECUTE
;}
237 ~PacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
238 //TODO::need to free paramvalues based on marshall or unmarshall
247 void setStatementList(List stmtlist
);
248 void setParams(List list
);
251 DbRetVal
unmarshall();
253 class PacketCommit
: public BasePacket
256 PacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
257 buffer
= NULL
; bufferSize
= 0; pktType
= NW_PKT_COMMIT
; }
258 ~PacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
263 void setExecPackets(int tid
, List list
);
264 void getExecPacketList(List stmtList
, List
&list
);
266 DbRetVal
unmarshall();
269 class SqlPacketConnect
: public BasePacket
274 strcpy(userName
, "");
275 strcpy(passWord
, "");
278 pktType
= SQL_NW_PKT_CONNECT
;
280 ~SqlPacketConnect() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
281 char userName
[IDENTIFIER_LENGTH
];
282 char passWord
[IDENTIFIER_LENGTH
];
284 void setConnParam(char *user
, char *pass
, char tp
)
285 { strcpy(userName
, user
); strcpy(passWord
, pass
); sqlApiImplType
= tp
; }
287 DbRetVal
unmarshall();
290 class SqlPacketPrepare
: public BasePacket
294 { buffer
=NULL
; bufferSize
=0; pktType
= SQL_NW_PKT_PREPARE
;}
295 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
299 DbRetVal
unmarshall();
302 class SqlPacketExecute
: public BasePacket
305 SqlPacketExecute();// { buffer=NULL; bufferSize =0; pktType = SQL_NW_PKT_EXECUTE;}
306 ~SqlPacketExecute();// { free(buffer); bufferSize = 0; buffer = NULL; paramValues = NULL; noParams = 0; }
307 //TODO::need to free paramvalues based on marshall or unmarshall
316 char *localBuf
[10]; //to store paramValues if noParams <10
318 void setStatementList(List stmtlist
);
319 void setParams(List list
);
320 void setNullInfo(char *nInfo
) { nullInfo
= nInfo
; }
321 char *getNullInfo() { return nullInfo
; }
323 DbRetVal
unmarshall();
326 class SqlPacketParamMetadata
: public BasePacket
329 SqlPacketParamMetadata()
330 { buffer
=NULL
; bufferSize
=0; noParams
= 0;
331 data
= NULL
; pktType
= SQL_NW_PKT_PARAM_METADATA
;}
332 ~SqlPacketParamMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
337 DbRetVal
unmarshall();
340 class SqlPacketProjMetadata
: public BasePacket
343 SqlPacketProjMetadata() { buffer
=NULL
; bufferSize
=0; noProjs
= 0;
344 data
= NULL
; pktType
= SQL_NW_PKT_PROJ_METADATA
; }
345 ~SqlPacketProjMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
350 DbRetVal
unmarshall();
353 class SqlPacketFetch
: public BasePacket
356 SqlPacketFetch() { buffer
=NULL
; bufferSize
= 0;
357 pktType
= SQL_NW_PKT_FETCH
; }
358 ~SqlPacketFetch() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
361 DbRetVal
unmarshall();
364 class SqlPacketFree
: public BasePacket
367 SqlPacketFree() { buffer
=NULL
; bufferSize
= 0;
368 pktType
= SQL_NW_PKT_FREE
; }
369 ~SqlPacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
372 DbRetVal
unmarshall();
375 class SqlPacketResultSet
: public BasePacket
378 SqlPacketResultSet() { buffer
=NULL
; bufferSize
= 0; noProjs
= 0;
379 nullInfo
= NULL
; projValues
=NULL
;
380 pktType
= SQL_NW_PKT_RESULT_SET
; }
381 ~SqlPacketResultSet() { free(buffer
); bufferSize
= 0; buffer
= NULL
;
389 void setNullInfo(char *info
){ nullInfo
= info
; }
390 void setProjList(List list
);
392 DbRetVal
unmarshall();
395 class SqlPacketShowTables
: public BasePacket
398 SqlPacketShowTables() { buffer
= NULL
; bufferSize
= 0; data
= NULL
;
399 pktType
= SQL_NW_PKT_SHOWTABLES
; }
400 ~SqlPacketShowTables() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
404 DbRetVal
unmarshall();
413 AbsSqlStatement
*stmt
;
416 List tableNamesList
; // will be populated only for show tables query
425 void setServerPort(int p
) { port
= p
; }
426 int getSocket(){ return sockfd
; }
427 virtual DbRetVal
start()=0;
428 virtual DbRetVal
stop()=0;
429 virtual DbRetVal
handleClient()=0;
430 virtual DbRetVal
send(NetworkPacketType type
, char *buf
, int len
)=0;
433 class UDPServer
: public NetworkServer
435 struct sockaddr_in clientAddress
;
437 UDPServer() { port
= 0; sockfd
= -1; }
440 DbRetVal
handleClient();
441 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
) { }//dont know what to write
444 class TCPServer
: public NetworkServer
447 struct sockaddr_in clientAddress
;
449 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
452 DbRetVal
handleClient();
453 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);