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>
31 enum NetworkPacketType
41 char hostName
[IDENTIFIER_LENGTH
];
45 int responseTimeout
; //in secs
51 virtual DbRetVal
send( NetworkPacketType type
, char *buf
, int len
)=0;
52 virtual DbRetVal
receive()=0;
53 virtual DbRetVal
connect()=0;
54 virtual DbRetVal
disconnect()=0;
55 virtual ~NetworkClient(){}
56 void setHost(char *host
, int portno
, int nwid
)
58 strcpy(hostName
, host
);
62 int getNetworkID() { return networkid
; }
63 void setConnectionTimeout(int timeout
) { connectTimeout
=timeout
;}
64 void setResponseTimeout(int timeout
) { responseTimeout
=timeout
;}
65 void setEntryption(bool encr
) { encrypt
=encr
;}
66 void setConnectFlag(bool flag
) { isConnectedFlag
=flag
;}
67 bool isConnected() { return isConnectedFlag
; }
68 void setCacheClient() { cacheClient
= true; }
69 bool isCacheClient() { return cacheClient
; }
71 class UDPClient
: public NetworkClient
{
74 struct sockaddr_in srvAddr
;
75 struct sockaddr_in fromAddr
;
76 UDPClient(){ isConnectedFlag
=false; cacheClient
= false;}
77 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
80 DbRetVal
disconnect();
83 class TCPClient
: public NetworkClient
{
85 TCPClient(){ isConnectedFlag
=false; cacheClient
= false;}
86 DbRetVal
send(NetworkPacketType type
, char *buf
, int len
);
89 DbRetVal
disconnect();
99 NetworkClient
* nwClient
;
102 DbRetVal
initialize();
104 DbRetVal
readNetworkConfig();
105 NetworkClient
* getNetworkClient() { return nwClient
; }
108 DbRetVal
connectIfNotConnected();
113 static NetworkClient
* createClient(NetworkMode mode
)
115 NetworkClient
* client
= NULL
;
119 client
= new UDPClient();
122 client
= new TCPClient();
138 //TOTOD:: bool encrypt;
141 NetworkPacketType pktType
;
143 //should be called after successful marshall call
144 char* getMarshalledBuffer(){ return buffer
; }
145 int getBufferSize() { return bufferSize
; }
146 void setBuffer(char *buf
) { buffer
= buf
; }
147 void setBufferSize(int bufSize
) { bufferSize
= bufSize
; }
149 virtual DbRetVal
marshall()=0;
150 virtual DbRetVal
unmarshall()=0;
151 virtual ~BasePacket(){};
153 class PacketPrepare
:public BasePacket
156 PacketPrepare() { buffer
=NULL
; bufferSize
=0; noParams
= 0;
157 type
= NULL
; length
= NULL
; pktType
= NW_PKT_PREPARE
;}
158 ~PacketPrepare() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
167 DbRetVal
unmarshall();
169 class PacketFree
: public BasePacket
172 PacketFree() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_FREE
;}
173 ~PacketFree() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
176 DbRetVal
unmarshall();
179 class PacketExecute
: public BasePacket
182 PacketExecute() { buffer
=NULL
; bufferSize
=0; pktType
= NW_PKT_EXECUTE
;}
183 ~PacketExecute() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
184 //TODO::need to free paramvalues based on marshall or unmarshall
193 void setStatementList(List stmtlist
);
194 void setParams(List list
);
197 DbRetVal
unmarshall();
199 class PacketCommit
: public BasePacket
202 PacketCommit() { txnID
=0; noOfStmts
= 0; stmtBufSize
= NULL
; stmtBuffer
= NULL
;
203 buffer
= NULL
; bufferSize
= 0; pktType
= NW_PKT_COMMIT
; }
204 ~PacketCommit() { free(buffer
); bufferSize
= 0; buffer
= NULL
; }
209 void setExecPackets(int tid
, List list
);
210 void getExecPacketList(List stmtList
, List
&list
);
212 DbRetVal
unmarshall();
219 AbsSqlStatement
*stmt
;
229 void setServerPort(int p
) { port
= p
; }
230 int getSocket(){ return sockfd
; }
231 virtual DbRetVal
start()=0;
232 virtual DbRetVal
stop()=0;
233 virtual DbRetVal
handleClient()=0;
236 class UDPServer
: public NetworkServer
238 struct sockaddr_in clientAddress
;
240 UDPServer() { port
= 0; sockfd
= -1; }
243 DbRetVal
handleClient();
245 class TCPServer
: public NetworkServer
248 TCPServer() { port
= 0; sockfd
= -1; }
251 DbRetVal
handleClient();