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,
51 SQL_NW_PKT_DISCONNECT
=110,
58 { stmtID
= 0; retVal
= 0; }
60 int retVal
; // will include for fetch end flag, params flag, proj flag
62 char errorString
[ERROR_STRING_LENGTH
];
64 DbRetVal
unmarshall();
69 char hostName
[IDENTIFIER_LENGTH
];
73 int responseTimeout
; //in secs
79 virtual DbRetVal
send( NetworkPacketType type
, char *buf
, int len
)=0;
80 virtual DbRetVal
receive()=0;
81 virtual DbRetVal
connect()=0;
82 virtual DbRetVal
disconnect()=0;
83 virtual void * getResponsePacket()=0;
84 virtual ~NetworkClient(){}
85 void setHost(char *host
, int portno
, int nwid
)
87 strcpy(hostName
, host
);
91 int getNetworkID() { return networkid
; }
92 void setConnectionTimeout(int timeout
) { connectTimeout
=timeout
;}
93 void setResponseTimeout(int timeout
) { responseTimeout
=timeout
;}
94 void setEntryption(bool encr
) { encrypt
=encr
;}
95 void setConnectFlag(bool flag
) { isConnectedFlag
=flag
;}
96 bool isConnected() { return isConnectedFlag
; }
97 void setCacheClient() { cacheClient
= true; }
98 bool isCacheClient() { return cacheClient
; }
100 class UDPClient
: public NetworkClient
{
103 struct sockaddr_in srvAddr
;
104 struct sockaddr_in fromAddr
;
105 UDPClient(){ isConnectedFlag
=false; cacheClient
= false;}
106 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
109 DbRetVal
disconnect();
110 void * getResponsePacket() { return NULL
; }
113 class TCPClient
: public NetworkClient
{
116 struct sockaddr_in srvAddr
;
117 ResponsePacket
*respPkt
;
118 TCPClient(){ isConnectedFlag
=false; cacheClient
= false; respPkt
= new ResponsePacket(); }
119 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
122 DbRetVal
disconnect();
123 void * getResponsePacket() { return respPkt
; }
133 NetworkClient
* nwClient
;
136 DbRetVal
initialize();
138 DbRetVal
readNetworkConfig();
139 NetworkClient
* getNetworkClient() { return nwClient
; }
141 DbRetVal
disconnect();
142 DbRetVal
connectIfNotConnected();
147 static NetworkClient
* createClient(NetworkMode mode
)
149 NetworkClient
* client
= NULL
;
153 client
= new UDPClient();
156 client
= new TCPClient();
170 class SqlPacketHeader
178 //TOTOD:: bool encrypt;
181 NetworkPacketType pktType
;
183 //should be called after successful marshall call
184 char* getMarshalledBuffer(){ return buffer
; }
185 int getBufferSize() { return bufferSize
; }
186 void setBuffer(char *buf
) { buffer
= buf
; }
187 void setBufferSize(int bufSize
) { bufferSize
= bufSize
; }
189 virtual DbRetVal
marshall()=0;
190 virtual DbRetVal
unmarshall()=0;
191 virtual ~BasePacket(){};
193 class PacketPrepare
:public BasePacket
196 PacketPrepare() { buffer
=NULL
; bufferSize
=0; noParams
= 0;
197 type
= NULL
; length
= NULL
; pktType
= NW_PKT_PREPARE
;}
198 ~PacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
207 DbRetVal
unmarshall();
209 class PacketFree
: public BasePacket
212 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
213 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
216 DbRetVal
unmarshall();
219 class PacketExecute
: public BasePacket
222 PacketExecute() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_EXECUTE
;}
223 ~PacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
224 //TODO::need to free paramvalues based on marshall or unmarshall
233 void setStatementList(List stmtlist
);
234 void setParams(List list
);
237 DbRetVal
unmarshall();
239 class PacketCommit
: public BasePacket
242 PacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
243 buffer
= NULL
; bufferSize
= 0; pktType
= NW_PKT_COMMIT
; }
244 ~PacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
249 void setExecPackets(int tid
, List list
);
250 void getExecPacketList(List stmtList
, List
&list
);
252 DbRetVal
unmarshall();
255 class SqlPacketConnect
: public BasePacket
260 strcpy(userName
, "");
261 strcpy(passWord
, "");
264 pktType
= SQL_NW_PKT_CONNECT
;
266 ~SqlPacketConnect() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
267 char userName
[IDENTIFIER_LENGTH
];
268 char passWord
[IDENTIFIER_LENGTH
];
269 void setConnParam(char *user
, char *pass
)
270 { strcpy(userName
, user
); strcpy(passWord
, pass
); }
272 DbRetVal
unmarshall();
275 class SqlPacketPrepare
: public BasePacket
279 { buffer
=NULL
; bufferSize
=0; pktType
= SQL_NW_PKT_PREPARE
;}
280 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
284 DbRetVal
unmarshall();
287 class SqlPacketExecute
: public BasePacket
290 SqlPacketExecute() { buffer
=NULL
; bufferSize
=0; pktType
= SQL_NW_PKT_EXECUTE
;}
291 ~SqlPacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; paramValues
= NULL
; noParams
= 0; }
292 //TODO::need to free paramvalues based on marshall or unmarshall
301 void setStatementList(List stmtlist
);
302 void setParams(List list
);
305 DbRetVal
unmarshall();
308 class SqlPacketParamMetadata
: public BasePacket
311 SqlPacketParamMetadata()
312 { buffer
=NULL
; bufferSize
=0; noParams
= 0;
313 type
= NULL
; length
= NULL
; pktType
= SQL_NW_PKT_PARAM_METADATA
;}
314 ~SqlPacketParamMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
320 DbRetVal
unmarshall();
323 class SqlPacketProjMetadata
: public BasePacket
326 SqlPacketProjMetadata() { buffer
=NULL
; bufferSize
=0; noProjs
= 0;
327 type
= NULL
; length
= NULL
; pktType
= SQL_NW_PKT_PROJ_METADATA
; }
328 ~SqlPacketProjMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
334 DbRetVal
unmarshall();
337 class SqlPacketFetch
: public BasePacket
340 SqlPacketFetch() { buffer
=NULL
; bufferSize
= 0;
341 pktType
= SQL_NW_PKT_FETCH
; }
342 ~SqlPacketFetch() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
345 DbRetVal
unmarshall();
348 class SqlPacketResultSet
: public BasePacket
351 SqlPacketResultSet() { buffer
=NULL
; bufferSize
= 0; noProjs
= 0;
352 projValues
=NULL
; pktType
= SQL_NW_PKT_RESULT_SET
; }
353 ~SqlPacketResultSet() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
358 void setProjList(List list
);
360 DbRetVal
unmarshall();
369 AbsSqlStatement
*stmt
;
380 void setServerPort(int p
) { port
= p
; }
381 int getSocket(){ return sockfd
; }
382 virtual DbRetVal
start()=0;
383 virtual DbRetVal
stop()=0;
384 virtual DbRetVal
handleClient()=0;
385 virtual DbRetVal
send(NetworkPacketType type
, char *buf
, int len
)=0;
388 class UDPServer
: public NetworkServer
390 struct sockaddr_in clientAddress
;
392 UDPServer() { port
= 0; sockfd
= -1; }
395 DbRetVal
handleClient();
396 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
) { }//dont know what to write
399 class TCPServer
: public NetworkServer
402 struct sockaddr_in clientAddress
;
404 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
407 DbRetVal
handleClient();
408 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);