Remove nwmode in csqlnw.conf file and making UDPClient and UDPServer as
[csql.git] / include / Network.h
blobaf3fb0367b95a78023d8ba072c137e9a43fd40f1
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
16 #ifndef NETWORK_H
17 #define NETWORK_H
18 #include <CSql.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <AbsSqlStatement.h>
26 enum DataSyncMode {
27 NOSYNC=0,
28 TSYNC=1,
29 ASYNC=2
31 enum NetworkPacketType
33 NW_PKT_PREPARE =1,
34 NW_PKT_EXECUTE =2,
35 NW_PKT_COMMIT =3,
36 NW_PKT_FREE =4,
37 NW_PKT_CONNECT =5
39 class NetworkClient {
40 protected:
41 char hostName[IDENTIFIER_LENGTH];
42 int port;
43 int networkid;
44 bool isConnectedFlag;
45 int responseTimeout; //in secs
46 int connectTimeout;
47 bool encrypt;
48 bool cacheClient;
50 public:
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);
59 port = portno;
60 networkid = nwid;
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{
72 public:
73 int sockfd;
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);
78 DbRetVal receive();
79 DbRetVal connect();
80 DbRetVal disconnect();
81 ~UDPClient();
83 class TCPClient : public NetworkClient{
84 public:
85 TCPClient(){ isConnectedFlag =false; cacheClient = false;}
86 DbRetVal send(NetworkPacketType type, char *buf, int len);
87 DbRetVal receive();
88 DbRetVal connect();
89 DbRetVal disconnect();
90 ~TCPClient();
92 enum NetworkMode
94 UDP,
95 TCP
97 class NetworkTable
99 NetworkClient* nwClient;
100 public:
101 ~NetworkTable();
102 DbRetVal initialize();
103 void destroy(){}
104 DbRetVal readNetworkConfig();
105 NetworkClient* getNetworkClient() { return nwClient; }
106 void connect();
107 void disconnect();
108 DbRetVal connectIfNotConnected();
110 class NetworkFactory
112 public:
113 static NetworkClient* createClient(NetworkMode mode)
115 NetworkClient* client = NULL;
116 switch(mode)
118 case UDP:
119 client = new UDPClient();
120 break;
121 case TCP:
122 client = new TCPClient();
123 break;
125 return client;
128 class PacketHeader
130 public:
131 int packetType;
132 int packetLength;
133 int srcNetworkID;
134 int version;
136 class BasePacket{
137 protected:
138 //TOTOD:: bool encrypt;
139 char *buffer;
140 int bufferSize;
141 NetworkPacketType pktType;
142 public:
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
155 public:
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; }
159 int stmtID;
160 int syncMode;
161 int stmtLength;
162 int noParams;
163 int *type;
164 int *length;
165 char *stmtString;
166 DbRetVal marshall();
167 DbRetVal unmarshall();
169 class PacketFree : public BasePacket
171 public:
172 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
173 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
174 int stmtID;
175 DbRetVal marshall();
176 DbRetVal unmarshall();
179 class PacketExecute : public BasePacket
181 public:
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
186 int stmtID;
187 int noParams;
188 char **paramValues;
190 List paramList;
191 List stmtList;
193 void setStatementList(List stmtlist);
194 void setParams(List list);
196 DbRetVal marshall();
197 DbRetVal unmarshall();
199 class PacketCommit : public BasePacket
201 public:
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; }
205 int txnID;
206 int noOfStmts;
207 int *stmtBufSize;
208 char **stmtBuffer;
209 void setExecPackets(int tid, List list);
210 void getExecPacketList(List stmtList, List &list);
211 DbRetVal marshall();
212 DbRetVal unmarshall();
214 class NetworkStmt
216 public:
217 int srcNetworkID;
218 int stmtID;
219 AbsSqlStatement *stmt;
220 List paramList;
223 class NetworkServer
225 protected:
226 int sockfd;
227 int port;
228 public:
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;
239 public:
240 UDPServer() { port = 0; sockfd = -1; }
241 DbRetVal start();
242 DbRetVal stop();
243 DbRetVal handleClient();
245 class TCPServer : public NetworkServer
247 public:
248 TCPServer() { port = 0; sockfd = -1; }
249 DbRetVal start();
250 DbRetVal stop();
251 DbRetVal handleClient();
254 #endif