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,
59 { stmtID
= 0; retVal
= 0; isSelect
= false; rows
=0; }
61 int retVal
; // will include for fetch end flag, params flag, proj flag
65 char errorString
[ERROR_STRING_LENGTH
];
67 DbRetVal
unmarshall();
72 char hostName
[IDENTIFIER_LENGTH
];
76 int responseTimeout
; //in secs
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
);
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
{
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
);
112 DbRetVal
disconnect();
113 void * getResponsePacket() { return NULL
; }
116 class TCPClient
: public NetworkClient
{
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
);
125 DbRetVal
disconnect();
126 void * getResponsePacket() { return respPkt
; }
136 NetworkClient
* nwClient
;
139 DbRetVal
initialize();
141 DbRetVal
readNetworkConfig();
142 NetworkClient
* getNetworkClient() { return nwClient
; }
144 DbRetVal
disconnect();
145 DbRetVal
connectIfNotConnected();
150 static NetworkClient
* createClient(NetworkMode mode
)
152 NetworkClient
* client
= NULL
;
156 client
= new UDPClient();
159 client
= new TCPClient();
173 class SqlPacketHeader
181 //TOTOD:: bool encrypt;
184 NetworkPacketType pktType
;
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
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
; }
210 DbRetVal
unmarshall();
212 class PacketFree
: public BasePacket
215 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
216 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
219 DbRetVal
unmarshall();
222 class PacketExecute
: public BasePacket
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
236 void setStatementList(List stmtlist
);
237 void setParams(List list
);
240 DbRetVal
unmarshall();
242 class PacketCommit
: public BasePacket
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
; }
252 void setExecPackets(int tid
, List list
);
253 void getExecPacketList(List stmtList
, List
&list
);
255 DbRetVal
unmarshall();
258 class SqlPacketConnect
: public BasePacket
263 strcpy(userName
, "");
264 strcpy(passWord
, "");
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
); }
275 DbRetVal
unmarshall();
278 class SqlPacketPrepare
: public BasePacket
282 { buffer
=NULL
; bufferSize
=0; pktType
= SQL_NW_PKT_PREPARE
;}
283 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
287 DbRetVal
unmarshall();
290 class SqlPacketExecute
: public BasePacket
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
304 void setStatementList(List stmtlist
);
305 void setParams(List list
);
308 DbRetVal
unmarshall();
311 class SqlPacketParamMetadata
: public BasePacket
314 SqlPacketParamMetadata()
315 { buffer
=NULL
; bufferSize
=0; noParams
= 0;
316 type
= NULL
; length
= NULL
; pktType
= SQL_NW_PKT_PARAM_METADATA
;}
317 ~SqlPacketParamMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
323 DbRetVal
unmarshall();
326 class SqlPacketProjMetadata
: public BasePacket
329 SqlPacketProjMetadata() { buffer
=NULL
; bufferSize
=0; noProjs
= 0;
330 type
= NULL
; length
= NULL
; pktType
= SQL_NW_PKT_PROJ_METADATA
; }
331 ~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();
383 AbsSqlStatement
*stmt
;
394 void setServerPort(int p
) { port
= p
; }
395 int getSocket(){ return sockfd
; }
396 virtual DbRetVal
start()=0;
397 virtual DbRetVal
stop()=0;
398 virtual DbRetVal
handleClient()=0;
399 virtual DbRetVal
send(NetworkPacketType type
, char *buf
, int len
)=0;
402 class UDPServer
: public NetworkServer
404 struct sockaddr_in clientAddress
;
406 UDPServer() { port
= 0; sockfd
= -1; }
409 DbRetVal
handleClient();
410 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
) { }//dont know what to write
413 class TCPServer
: public NetworkServer
416 struct sockaddr_in clientAddress
;
418 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
421 DbRetVal
handleClient();
422 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);