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
44 char hostName
[IDENTIFIER_LENGTH
];
48 int responseTimeout
; //in secs
54 virtual DbRetVal
send( NetworkPacketType type
, char *buf
, int len
)=0;
55 virtual DbRetVal
receive()=0;
56 virtual DbRetVal
connect()=0;
57 virtual DbRetVal
disconnect()=0;
58 virtual ~NetworkClient(){}
59 void setHost(char *host
, int portno
, int nwid
)
61 strcpy(hostName
, host
);
65 int getNetworkID() { return networkid
; }
66 void setConnectionTimeout(int timeout
) { connectTimeout
=timeout
;}
67 void setResponseTimeout(int timeout
) { responseTimeout
=timeout
;}
68 void setEntryption(bool encr
) { encrypt
=encr
;}
69 void setConnectFlag(bool flag
) { isConnectedFlag
=flag
;}
70 bool isConnected() { return isConnectedFlag
; }
71 void setCacheClient() { cacheClient
= true; }
72 bool isCacheClient() { return cacheClient
; }
74 class UDPClient
: public NetworkClient
{
77 struct sockaddr_in srvAddr
;
78 struct sockaddr_in fromAddr
;
79 UDPClient(){ isConnectedFlag
=false; cacheClient
= false;}
80 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
83 DbRetVal
disconnect();
86 class TCPClient
: public NetworkClient
{
89 struct sockaddr_in srvAddr
;
90 TCPClient(){ isConnectedFlag
=false; cacheClient
= false;}
91 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
94 DbRetVal
disconnect();
104 NetworkClient
* nwClient
;
107 DbRetVal
initialize();
109 DbRetVal
readNetworkConfig();
110 NetworkClient
* getNetworkClient() { return nwClient
; }
112 DbRetVal
disconnect();
113 DbRetVal
connectIfNotConnected();
118 static NetworkClient
* createClient(NetworkMode mode
)
120 NetworkClient
* client
= NULL
;
124 client
= new UDPClient();
127 client
= new TCPClient();
143 //TOTOD:: bool encrypt;
146 NetworkPacketType pktType
;
148 //should be called after successful marshall call
149 char* getMarshalledBuffer(){ return buffer
; }
150 int getBufferSize() { return bufferSize
; }
151 void setBuffer(char *buf
) { buffer
= buf
; }
152 void setBufferSize(int bufSize
) { bufferSize
= bufSize
; }
154 virtual DbRetVal
marshall()=0;
155 virtual DbRetVal
unmarshall()=0;
156 virtual ~BasePacket(){};
158 class PacketPrepare
:public BasePacket
161 PacketPrepare() { buffer
=NULL
; bufferSize
=0; noParams
= 0;
162 type
= NULL
; length
= NULL
; pktType
= NW_PKT_PREPARE
;}
163 ~PacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
172 DbRetVal
unmarshall();
174 class PacketFree
: public BasePacket
177 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
178 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
181 DbRetVal
unmarshall();
184 class PacketExecute
: public BasePacket
187 PacketExecute() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_EXECUTE
;}
188 ~PacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
189 //TODO::need to free paramvalues based on marshall or unmarshall
198 void setStatementList(List stmtlist
);
199 void setParams(List list
);
202 DbRetVal
unmarshall();
204 class PacketCommit
: public BasePacket
207 PacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
208 buffer
= NULL
; bufferSize
= 0; pktType
= NW_PKT_COMMIT
; }
209 ~PacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
214 void setExecPackets(int tid
, List list
);
215 void getExecPacketList(List stmtList
, List
&list
);
217 DbRetVal
unmarshall();
224 AbsSqlStatement
*stmt
;
234 void setServerPort(int p
) { port
= p
; }
235 int getSocket(){ return sockfd
; }
236 virtual DbRetVal
start()=0;
237 virtual DbRetVal
stop()=0;
238 virtual DbRetVal
handleClient()=0;
241 class UDPServer
: public NetworkServer
243 struct sockaddr_in clientAddress
;
245 UDPServer() { port
= 0; sockfd
= -1; }
248 DbRetVal
handleClient();
250 class TCPServer
: public NetworkServer
253 struct sockaddr_in clientAddress
;
255 TCPServer() { port
= 0; sockfd
= -1; clientfd
= -1;}
258 DbRetVal
handleClient();