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>
33 enum NetworkPacketType
41 SQL_NW_PKT_CONNECT
=101,
42 SQL_NW_PKT_PREPARE
=102,
43 SQL_NW_PKT_COMMIT
=103,
44 SQL_NW_PKT_DISCONNECT
=104,
48 char hostName
[IDENTIFIER_LENGTH
];
52 int responseTimeout
; //in secs
58 virtual DbRetVal
send( NetworkPacketType type
, char *buf
, int len
)=0;
59 virtual DbRetVal
receive()=0;
60 virtual DbRetVal
connect()=0;
61 virtual DbRetVal
disconnect()=0;
62 virtual ~NetworkClient(){}
63 void setHost(char *host
, int portno
, int nwid
)
65 strcpy(hostName
, host
);
69 int getNetworkID() { return networkid
; }
70 void setConnectionTimeout(int timeout
) { connectTimeout
=timeout
;}
71 void setResponseTimeout(int timeout
) { responseTimeout
=timeout
;}
72 void setEntryption(bool encr
) { encrypt
=encr
;}
73 void setConnectFlag(bool flag
) { isConnectedFlag
=flag
;}
74 bool isConnected() { return isConnectedFlag
; }
75 void setCacheClient() { cacheClient
= true; }
76 bool isCacheClient() { return cacheClient
; }
78 class UDPClient
: public NetworkClient
{
81 struct sockaddr_in srvAddr
;
82 struct sockaddr_in fromAddr
;
83 UDPClient(){ isConnectedFlag
=false; cacheClient
= false;}
84 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
87 DbRetVal
disconnect();
90 class TCPClient
: public NetworkClient
{
93 struct sockaddr_in srvAddr
;
94 TCPClient(){ isConnectedFlag
=false; cacheClient
= false;}
95 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
98 DbRetVal
disconnect();
108 NetworkClient
* nwClient
;
111 DbRetVal
initialize();
113 DbRetVal
readNetworkConfig();
114 NetworkClient
* getNetworkClient() { return nwClient
; }
116 DbRetVal
disconnect();
117 DbRetVal
connectIfNotConnected();
122 static NetworkClient
* createClient(NetworkMode mode
)
124 NetworkClient
* client
= NULL
;
128 client
= new UDPClient();
131 client
= new TCPClient();
145 class SqlPacketHeader
153 //TOTOD:: bool encrypt;
156 NetworkPacketType pktType
;
158 //should be called after successful marshall call
159 char* getMarshalledBuffer(){ return buffer
; }
160 int getBufferSize() { return bufferSize
; }
161 void setBuffer(char *buf
) { buffer
= buf
; }
162 void setBufferSize(int bufSize
) { bufferSize
= bufSize
; }
164 virtual DbRetVal
marshall()=0;
165 virtual DbRetVal
unmarshall()=0;
166 virtual ~BasePacket(){};
168 class PacketPrepare
:public BasePacket
171 PacketPrepare() { buffer
=NULL
; bufferSize
=0; noParams
= 0;
172 type
= NULL
; length
= NULL
; pktType
= NW_PKT_PREPARE
;}
173 ~PacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
182 DbRetVal
unmarshall();
184 class PacketFree
: public BasePacket
187 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
188 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
191 DbRetVal
unmarshall();
194 class PacketExecute
: public BasePacket
197 PacketExecute() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_EXECUTE
;}
198 ~PacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
199 //TODO::need to free paramvalues based on marshall or unmarshall
208 void setStatementList(List stmtlist
);
209 void setParams(List list
);
212 DbRetVal
unmarshall();
214 class PacketCommit
: public BasePacket
217 PacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
218 buffer
= NULL
; bufferSize
= 0; pktType
= NW_PKT_COMMIT
; }
219 ~PacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
224 void setExecPackets(int tid
, List list
);
225 void getExecPacketList(List stmtList
, List
&list
);
227 DbRetVal
unmarshall();
230 class SqlPacketConnect
: public BasePacket
235 strcpy(userName
, "");
236 strcpy(passWord
, "");
239 pktType
= SQL_NW_PKT_CONNECT
;
241 ~SqlPacketConnect() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
242 char userName
[IDENTIFIER_LENGTH
];
243 char passWord
[IDENTIFIER_LENGTH
];
244 void setConnParam(char *user
, char *pass
)
245 { strcpy(userName
, user
); strcpy(passWord
, pass
); }
247 DbRetVal
unmarshall();
250 class SqlPacketPrepare
: public BasePacket
254 { buffer
=NULL
; bufferSize
=0; noParams
= 0;
255 type
= NULL
; length
= NULL
; pktType
= SQL_NW_PKT_PREPARE
;}
256 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
265 DbRetVal
unmarshall();
268 class SqlPacketCommit
: public BasePacket
271 SqlPacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
272 buffer
= NULL
; bufferSize
= 0; pktType
= SQL_NW_PKT_COMMIT
; }
273 ~SqlPacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
278 void setExecPackets(int tid
, List list
);
279 void getExecPacketList(List stmtList
, List
&list
);
281 DbRetVal
unmarshall();
289 AbsSqlStatement
*stmt
;
299 void setServerPort(int p
) { port
= p
; }
300 int getSocket(){ return sockfd
; }
301 virtual DbRetVal
start()=0;
302 virtual DbRetVal
stop()=0;
303 virtual DbRetVal
handleClient()=0;
306 class UDPServer
: public NetworkServer
308 struct sockaddr_in clientAddress
;
310 UDPServer() { port
= 0; sockfd
= -1; }
313 DbRetVal
handleClient();
315 class TCPServer
: public NetworkServer
318 struct sockaddr_in clientAddress
;
320 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
323 DbRetVal
handleClient();