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; }
61 int retVal
; // will include for fetch end flag, params flag, proj flag
63 char errorString
[ERROR_STRING_LENGTH
];
65 DbRetVal
unmarshall();
70 char hostName
[IDENTIFIER_LENGTH
];
74 int responseTimeout
; //in secs
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
);
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
{
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
);
110 DbRetVal
disconnect();
111 void * getResponsePacket() { return NULL
; }
114 class TCPClient
: public NetworkClient
{
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
);
123 DbRetVal
disconnect();
124 void * getResponsePacket() { return respPkt
; }
134 NetworkClient
* nwClient
;
137 DbRetVal
initialize();
139 DbRetVal
readNetworkConfig();
140 NetworkClient
* getNetworkClient() { return nwClient
; }
142 DbRetVal
disconnect();
143 DbRetVal
connectIfNotConnected();
148 static NetworkClient
* createClient(NetworkMode mode
)
150 NetworkClient
* client
= NULL
;
154 client
= new UDPClient();
157 client
= new TCPClient();
171 class SqlPacketHeader
179 //TOTOD:: bool encrypt;
182 NetworkPacketType pktType
;
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
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
; }
208 DbRetVal
unmarshall();
210 class PacketFree
: public BasePacket
213 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
214 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
217 DbRetVal
unmarshall();
220 class PacketExecute
: public BasePacket
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
234 void setStatementList(List stmtlist
);
235 void setParams(List list
);
238 DbRetVal
unmarshall();
240 class PacketCommit
: public BasePacket
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
; }
250 void setExecPackets(int tid
, List list
);
251 void getExecPacketList(List stmtList
, List
&list
);
253 DbRetVal
unmarshall();
256 class SqlPacketConnect
: public BasePacket
261 strcpy(userName
, "");
262 strcpy(passWord
, "");
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
); }
273 DbRetVal
unmarshall();
276 class SqlPacketPrepare
: public BasePacket
280 { buffer
=NULL
; bufferSize
=0; pktType
= SQL_NW_PKT_PREPARE
;}
281 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
285 DbRetVal
unmarshall();
288 class SqlPacketExecute
: public BasePacket
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
302 void setStatementList(List stmtlist
);
303 void setParams(List list
);
306 DbRetVal
unmarshall();
309 class SqlPacketParamMetadata
: public BasePacket
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
; }
321 DbRetVal
unmarshall();
324 class SqlPacketProjMetadata
: public BasePacket
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
; }
335 DbRetVal
unmarshall();
338 class SqlPacketFetch
: public BasePacket
341 SqlPacketFetch() { buffer
=NULL
; bufferSize
= 0;
342 pktType
= SQL_NW_PKT_FETCH
; }
343 ~SqlPacketFetch() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
346 DbRetVal
unmarshall();
349 class SqlPacketFree
: public BasePacket
352 SqlPacketFree() { buffer
=NULL
; bufferSize
= 0;
353 pktType
= SQL_NW_PKT_FREE
; }
354 ~SqlPacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
357 DbRetVal
unmarshall();
360 class SqlPacketResultSet
: public BasePacket
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
; }
370 void setProjList(List list
);
372 DbRetVal
unmarshall();
381 AbsSqlStatement
*stmt
;
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
;
404 UDPServer() { port
= 0; sockfd
= -1; }
407 DbRetVal
handleClient();
408 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
) { }//dont know what to write
411 class TCPServer
: public NetworkServer
414 struct sockaddr_in clientAddress
;
416 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
419 DbRetVal
handleClient();
420 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);