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>
35 enum NetworkPacketType
43 SQL_NW_PKT_EXECDIRECT
=100,
44 SQL_NW_PKT_CONNECT
=101,
45 SQL_NW_PKT_PREPARE
=102,
46 SQL_NW_PKT_PARAM_METADATA
=103,
47 SQL_NW_PKT_PROJ_METADATA
=104,
48 SQL_NW_PKT_EXECUTE
=105,
50 SQL_NW_PKT_RESULT_SET
=107,
51 SQL_NW_PKT_COMMIT
=108,
52 SQL_NW_PKT_ROLLBACK
=109,
54 SQL_NW_PKT_DISCONNECT
=111,
55 SQL_NW_PKT_SHOWTABLES
=112,
56 SQL_NW_PKT_ISTABLEPRESENT
=113,
57 SQL_NW_PKT_GETRECORDS
=114,
58 SQL_NW_PKT_LOADRECORDS
=115,
66 stmtID
= 0; retVal
= 0; errRetVal
= OK
; isSelect
= false; rows
=0;
67 errorString
[0] = '\0';
70 int retVal
; // will include for fetch end flag, params flag, proj flag
75 char errorString
[ERROR_STRING_LENGTH
];
77 DbRetVal
unmarshall();
82 char hostName
[IDENTIFIER_LENGTH
];
86 int responseTimeout
; //in secs
92 virtual DbRetVal
send( NetworkPacketType type
) =0;
93 virtual DbRetVal
send( NetworkPacketType type
, int stmtid
)=0;
94 virtual DbRetVal
send( NetworkPacketType type
, char *buf
, int len
)=0;
95 virtual DbRetVal
receive()=0;
96 virtual DbRetVal
connect()=0;
97 virtual DbRetVal
disconnect()=0;
98 virtual void * getResponsePacket()=0;
99 virtual ~NetworkClient(){}
100 void setHost(char *host
, int portno
, int nwid
)
102 strcpy(hostName
, host
);
106 int getNetworkID() { return networkid
; }
107 void setConnectionTimeout(int timeout
) { connectTimeout
=timeout
;}
108 void setResponseTimeout(int timeout
) { responseTimeout
=timeout
;}
109 void setEntryption(bool encr
) { encrypt
=encr
;}
110 void setConnectFlag(bool flag
) { isConnectedFlag
=flag
;}
111 bool isConnected() { return isConnectedFlag
; }
112 void setIsConnectedFlag(bool fl
) { isConnectedFlag
= fl
; }
113 void setCacheClient() { cacheClient
= true; }
114 bool isCacheClient() { return cacheClient
; }
116 class UDPClient
: public NetworkClient
{
119 struct sockaddr_in srvAddr
;
120 struct sockaddr_in fromAddr
;
121 UDPClient(){ isConnectedFlag
=false; cacheClient
= false;}
122 DbRetVal
send(NetworkPacketType type
);
123 DbRetVal
send(NetworkPacketType type
,int stmtid
);
124 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
127 DbRetVal
disconnect();
128 void * getResponsePacket() { return NULL
; }
132 class TCPClient
: public NetworkClient
{
135 struct sockaddr_in srvAddr
;
136 ResponsePacket
*respPkt
;
137 PacketHeader
*pktHdr
;
139 DbRetVal
send(NetworkPacketType type
);
140 DbRetVal
send(NetworkPacketType type
, int stmtid
);
141 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
144 DbRetVal
disconnect();
145 void * getResponsePacket() { return respPkt
; }
155 NetworkClient
* nwClient
;
157 NetworkTable() { nwClient
= NULL
; }
159 DbRetVal
initialize();
161 DbRetVal
readNetworkConfig();
162 NetworkClient
* getNetworkClient() { return nwClient
; }
164 DbRetVal
disconnect();
165 DbRetVal
connectIfNotConnected();
170 static NetworkClient
* createClient(NetworkMode mode
)
172 NetworkClient
* client
= NULL
;
176 client
= new UDPClient();
179 client
= new TCPClient();
194 /*class SqlPacketHeader
202 //TOTOD:: bool encrypt;
205 NetworkPacketType pktType
;
207 //should be called after successful marshall call
208 char* getMarshalledBuffer(){ return buffer
; }
209 int getBufferSize() { return bufferSize
; }
210 void setBuffer(char *buf
) { buffer
= buf
; }
211 void setBufferSize(int bufSize
) { bufferSize
= bufSize
; }
213 virtual DbRetVal
marshall()=0;
214 virtual DbRetVal
unmarshall()=0;
215 virtual ~BasePacket(){};
217 class PacketPrepare
:public BasePacket
220 PacketPrepare() { buffer
=NULL
; bufferSize
=0; noParams
= 0; stmtString
= NULL
;
221 stmtID
= 0; stmtLength
= 0; type
= NULL
; length
= NULL
;
222 pktType
= NW_PKT_PREPARE
;}
223 ~PacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
232 DbRetVal
unmarshall();
234 class PacketFree
: public BasePacket
237 PacketFree() { buffer
=NULL
; bufferSize
=0; stmtID
= 0; pktType
= NW_PKT_FREE
;}
238 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
241 DbRetVal
unmarshall();
244 class PacketExecute
: public BasePacket
247 PacketExecute() { buffer
=NULL
; bufferSize
=0;
248 stmtID
= 0; noParams
= 0; paramValues
= NULL
;
249 pktType
= NW_PKT_EXECUTE
;}
250 ~PacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
251 //TODO::need to free paramvalues based on marshall or unmarshall
260 void setStatementList(List stmtlist
);
261 void setParams(List list
);
264 DbRetVal
unmarshall();
266 class PacketCommit
: public BasePacket
269 PacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
270 buffer
= NULL
; bufferSize
= 0; pktType
= NW_PKT_COMMIT
; }
271 ~PacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
276 void setExecPackets(int tid
, List list
);
277 void getExecPacketList(List stmtList
, List
&list
);
279 DbRetVal
unmarshall();
282 class SqlPacketConnect
: public BasePacket
287 strcpy(userName
, "");
288 strcpy(passWord
, "");
291 pktType
= SQL_NW_PKT_CONNECT
;
293 ~SqlPacketConnect() { free(buffer
); bufferSize
= 0; buffer
= NULL
;
295 char userName
[IDENTIFIER_LENGTH
];
296 char passWord
[IDENTIFIER_LENGTH
];
298 void setConnParam(char *user
, char *pass
, char tp
)
300 strcpy(userName
, user
);
301 strcpy(passWord
, pass
);
305 DbRetVal
unmarshall();
308 class SqlPacketPrepare
: public BasePacket
312 { buffer
=NULL
; bufferSize
=0;
313 stmtString
= NULL
; stmtLength
= 0; pktType
= SQL_NW_PKT_PREPARE
; }
314 ~SqlPacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
318 DbRetVal
unmarshall();
321 class SqlPacketExecute
: public BasePacket
326 //TODO::need to free paramvalues based on marshall or unmarshall
335 char *localBuf
[10]; //to store paramValues if noParams <10
337 void setStatementList(List stmtlist
);
338 void setParams(List list
);
339 void setNullInfo(char *nInfo
) { nullInfo
= nInfo
; }
340 char *getNullInfo() { return nullInfo
; }
342 DbRetVal
unmarshall();
345 class SqlPacketParamMetadata
: public BasePacket
348 SqlPacketParamMetadata()
349 { buffer
=NULL
; bufferSize
=0; noParams
= 0;
350 stmtID
= 0; data
= NULL
; pktType
= SQL_NW_PKT_PARAM_METADATA
;}
351 ~SqlPacketParamMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
356 DbRetVal
unmarshall();
359 class SqlPacketProjMetadata
: public BasePacket
362 SqlPacketProjMetadata() { buffer
=NULL
; bufferSize
=0; noProjs
= 0;
363 data
= NULL
; stmtID
= 0; pktType
= SQL_NW_PKT_PROJ_METADATA
; }
364 ~SqlPacketProjMetadata() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
369 DbRetVal
unmarshall();
372 class SqlPacketFetch
: public BasePacket
375 SqlPacketFetch() { buffer
=NULL
; bufferSize
= 0;
376 stmtID
= 0; pktType
= SQL_NW_PKT_FETCH
; }
377 ~SqlPacketFetch() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
380 DbRetVal
unmarshall();
383 class SqlPacketFree
: public BasePacket
386 SqlPacketFree() { buffer
=NULL
; stmtID
=0;bufferSize
= 0;
387 pktType
= SQL_NW_PKT_FREE
; }
388 ~SqlPacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
391 DbRetVal
unmarshall();
394 class SqlPacketResultSet
: public BasePacket
397 SqlPacketResultSet() { buffer
=NULL
; bufferSize
= 0; noProjs
= 0;
398 nullInfo
= NULL
; projValues
=NULL
;
399 hasData
=0; nullInfoLen
=0;
400 pktType
= SQL_NW_PKT_RESULT_SET
; }
401 ~SqlPacketResultSet() { free(buffer
);
402 bufferSize
= 0; buffer
= NULL
;
403 if (projValues
) delete[] projValues
;
411 void setNullInfo(char *info
){ nullInfo
= info
; }
412 void setProjList(List list
);
414 DbRetVal
unmarshall();
417 class SqlPacketShowTables
: public BasePacket
420 SqlPacketShowTables() { buffer
= NULL
; bufferSize
= 0; data
= NULL
;
421 numOfTables
=0; pktType
= SQL_NW_PKT_SHOWTABLES
; }
422 ~SqlPacketShowTables() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
426 DbRetVal
unmarshall();
429 class SqlPacketIsTablePresent
: public BasePacket
432 SqlPacketIsTablePresent() { buffer
= NULL
; bufferSize
= 0;
433 tblName
[0] = '\0'; pktType
= SQL_NW_PKT_ISTABLEPRESENT
; }
434 ~SqlPacketIsTablePresent() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
435 char tblName
[IDENTIFIER_LENGTH
];
436 void setTableName(char *tName
) { strcpy(tblName
, tName
); }
438 DbRetVal
unmarshall();
441 class SqlPacketGetRecords
: public BasePacket
444 SqlPacketGetRecords() { buffer
= NULL
; bufferSize
= 0; pages
=0;
445 tblName
[0] = '\0'; pktType
= SQL_NW_PKT_GETRECORDS
; }
446 ~SqlPacketGetRecords() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
447 char tblName
[IDENTIFIER_LENGTH
];
449 void setTableName(char *tName
) { strcpy(tblName
, tName
); }
450 void setPages(int pgs
) { pages
= pgs
; }
452 DbRetVal
unmarshall();
455 class SqlPacketLoadRecords
: public BasePacket
458 SqlPacketLoadRecords() { buffer
= NULL
; bufferSize
= 0; pages
= 0;
459 pktType
= SQL_NW_PKT_LOADRECORDS
; }
460 ~SqlPacketLoadRecords() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
462 void setPages(int pgs
) { pages
= pgs
; }
464 DbRetVal
unmarshall();
473 AbsSqlStatement
*stmt
;
476 List tableNamesList
; // will be populated only for show tables query
477 NetworkStmt() { srcNetworkID
=0; stmtID
=0; stmt
=NULL
; type
= UnknownStatement
; }
486 NetworkServer() { port
=0; sockfd
= -1;}
487 void setServerPort(int p
) { port
= p
; }
488 int getSocket(){ return sockfd
; }
489 virtual DbRetVal
start()=0;
490 virtual DbRetVal
stop()=0;
491 virtual DbRetVal
handleClient()=0;
494 class UDPServer
: public NetworkServer
496 struct sockaddr_in clientAddress
;
498 UDPServer() { port
= 0; sockfd
= -1; }
501 DbRetVal
handleClient();
504 class TCPServer
: public NetworkServer
507 struct sockaddr_in clientAddress
;
509 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
512 DbRetVal
handleClient();