Fixing failed test cache/DDL/test001.ksh
[csql.git] / include / Network.h
blob68d066a53cf4dc8b85ff5827dd26308dc1ba2d2d
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 OSYNC=1,
29 TSYNC=1,
30 ASYNC=2
33 enum NetworkPacketType
35 NW_PKT_PREPARE =1,
36 NW_PKT_EXECUTE =2,
37 NW_PKT_COMMIT =3,
38 NW_PKT_FREE =4,
39 NW_PKT_CONNECT =5,
40 NW_PKT_DISCONNECT =6,
41 SQL_NW_PKT_CONNECT=101,
42 SQL_NW_PKT_PREPARE=102,
43 SQL_NW_PKT_COMMIT=103,
44 SQL_NW_PKT_DISCONNECT=104,
46 class NetworkClient {
47 protected:
48 char hostName[IDENTIFIER_LENGTH];
49 int port;
50 int networkid;
51 bool isConnectedFlag;
52 int responseTimeout; //in secs
53 int connectTimeout;
54 bool encrypt;
55 bool cacheClient;
57 public:
58 virtual DbRetVal send( NetworkPacketType type, char *buf, int len)=0;
59 virtual DbRetVal receive()=0;
60 virtual DbRetVal connect()=0;
61 virtual DbRetVal disconnect()=0;
62 virtual ~NetworkClient(){}
63 void setHost(char *host, int portno, int nwid)
65 strcpy(hostName, host);
66 port = portno;
67 networkid = nwid;
69 int getNetworkID() { return networkid; }
70 void setConnectionTimeout(int timeout) { connectTimeout=timeout;}
71 void setResponseTimeout(int timeout) { responseTimeout=timeout;}
72 void setEntryption(bool encr) { encrypt=encr;}
73 void setConnectFlag(bool flag) { isConnectedFlag=flag;}
74 bool isConnected() { return isConnectedFlag; }
75 void setCacheClient() { cacheClient = true; }
76 bool isCacheClient() { return cacheClient; }
78 class UDPClient : public NetworkClient{
79 public:
80 int sockfd;
81 struct sockaddr_in srvAddr;
82 struct sockaddr_in fromAddr;
83 UDPClient(){ isConnectedFlag =false; cacheClient = false;}
84 DbRetVal send(NetworkPacketType type, char *buf, int len);
85 DbRetVal receive();
86 DbRetVal connect();
87 DbRetVal disconnect();
88 ~UDPClient();
90 class TCPClient : public NetworkClient{
91 public:
92 int sockfd;
93 struct sockaddr_in srvAddr;
94 TCPClient(){ isConnectedFlag =false; cacheClient = false;}
95 DbRetVal send(NetworkPacketType type, char *buf, int len);
96 DbRetVal receive();
97 DbRetVal connect();
98 DbRetVal disconnect();
99 ~TCPClient();
101 enum NetworkMode
103 UDP,
106 class NetworkTable
108 NetworkClient* nwClient;
109 public:
110 ~NetworkTable();
111 DbRetVal initialize();
112 void destroy(){}
113 DbRetVal readNetworkConfig();
114 NetworkClient* getNetworkClient() { return nwClient; }
115 DbRetVal connect();
116 DbRetVal disconnect();
117 DbRetVal connectIfNotConnected();
119 class NetworkFactory
121 public:
122 static NetworkClient* createClient(NetworkMode mode)
124 NetworkClient* client = NULL;
125 switch(mode)
127 case UDP:
128 client = new UDPClient();
129 break;
130 case TCP:
131 client = new TCPClient();
132 break;
134 return client;
137 class PacketHeader
139 public:
140 int packetType;
141 int packetLength;
142 int srcNetworkID;
143 int version;
145 class SqlPacketHeader
147 public:
148 int packetType;
149 int packetLength;
151 class BasePacket{
152 protected:
153 //TOTOD:: bool encrypt;
154 char *buffer;
155 int bufferSize;
156 NetworkPacketType pktType;
157 public:
158 //should be called after successful marshall call
159 char* getMarshalledBuffer(){ return buffer; }
160 int getBufferSize() { return bufferSize; }
161 void setBuffer(char *buf) { buffer = buf; }
162 void setBufferSize(int bufSize) { bufferSize = bufSize; }
164 virtual DbRetVal marshall()=0;
165 virtual DbRetVal unmarshall()=0;
166 virtual ~BasePacket(){};
168 class PacketPrepare:public BasePacket
170 public:
171 PacketPrepare() { buffer=NULL; bufferSize =0; noParams = 0;
172 type = NULL; length = NULL; pktType = NW_PKT_PREPARE;}
173 ~PacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
174 int stmtID;
175 int syncMode;
176 int stmtLength;
177 int noParams;
178 int *type;
179 int *length;
180 char *stmtString;
181 DbRetVal marshall();
182 DbRetVal unmarshall();
184 class PacketFree : public BasePacket
186 public:
187 PacketFree() { buffer=NULL; bufferSize =0; pktType = NW_PKT_FREE;}
188 ~PacketFree() { free(buffer); bufferSize = 0; buffer = NULL; }
189 int stmtID;
190 DbRetVal marshall();
191 DbRetVal unmarshall();
194 class PacketExecute : public BasePacket
196 public:
197 PacketExecute() { buffer=NULL; bufferSize =0; pktType = NW_PKT_EXECUTE;}
198 ~PacketExecute() { free(buffer); bufferSize = 0; buffer = NULL; }
199 //TODO::need to free paramvalues based on marshall or unmarshall
201 int stmtID;
202 int noParams;
203 char **paramValues;
205 List paramList;
206 List stmtList;
208 void setStatementList(List stmtlist);
209 void setParams(List list);
211 DbRetVal marshall();
212 DbRetVal unmarshall();
214 class PacketCommit : public BasePacket
216 public:
217 PacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
218 buffer = NULL; bufferSize = 0; pktType = NW_PKT_COMMIT; }
219 ~PacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
220 int txnID;
221 int noOfStmts;
222 int *stmtBufSize;
223 char **stmtBuffer;
224 void setExecPackets(int tid, List list);
225 void getExecPacketList(List stmtList, List &list);
226 DbRetVal marshall();
227 DbRetVal unmarshall();
230 class SqlPacketConnect : public BasePacket
232 public:
233 SqlPacketConnect()
235 strcpy(userName, "");
236 strcpy(passWord, "");
237 buffer = NULL;
238 bufferSize = 0;
239 pktType = SQL_NW_PKT_CONNECT;
241 ~SqlPacketConnect() { free(buffer); bufferSize = 0; buffer = NULL; }
242 char userName[IDENTIFIER_LENGTH];
243 char passWord[IDENTIFIER_LENGTH];
244 void setConnParam(char *user, char *pass)
245 { strcpy(userName, user); strcpy(passWord, pass); }
246 DbRetVal marshall();
247 DbRetVal unmarshall();
250 class SqlPacketPrepare : public BasePacket
252 public:
253 SqlPacketPrepare()
254 { buffer=NULL; bufferSize =0; noParams = 0;
255 type = NULL; length = NULL; pktType = SQL_NW_PKT_PREPARE;}
256 ~SqlPacketPrepare() { free(buffer); bufferSize = 0; buffer = NULL; }
257 int stmtID;
258 int syncMode;
259 int stmtLength;
260 int noParams;
261 int *type;
262 int *length;
263 char *stmtString;
264 DbRetVal marshall();
265 DbRetVal unmarshall();
268 class SqlPacketCommit : public BasePacket
270 public:
271 SqlPacketCommit() { txnID =0; noOfStmts = 0; stmtBufSize = NULL; stmtBuffer = NULL;
272 buffer = NULL; bufferSize = 0; pktType = SQL_NW_PKT_COMMIT; }
273 ~SqlPacketCommit() { free(buffer); bufferSize = 0; buffer = NULL; }
274 int txnID;
275 int noOfStmts;
276 int *stmtBufSize;
277 char **stmtBuffer;
278 void setExecPackets(int tid, List list);
279 void getExecPacketList(List stmtList, List &list);
280 DbRetVal marshall();
281 DbRetVal unmarshall();
284 class NetworkStmt
286 public:
287 int srcNetworkID;
288 int stmtID;
289 AbsSqlStatement *stmt;
290 List paramList;
293 class NetworkServer
295 protected:
296 int sockfd;
297 int port;
298 public:
299 void setServerPort(int p) { port = p; }
300 int getSocket(){ return sockfd; }
301 virtual DbRetVal start()=0;
302 virtual DbRetVal stop()=0;
303 virtual DbRetVal handleClient()=0;
306 class UDPServer : public NetworkServer
308 struct sockaddr_in clientAddress;
309 public:
310 UDPServer() { port = 0; sockfd = -1; }
311 DbRetVal start();
312 DbRetVal stop();
313 DbRetVal handleClient();
315 class TCPServer : public NetworkServer
317 public:
318 struct sockaddr_in clientAddress;
319 int clientfd;
320 TCPServer() { port = 0; sockfd = -1; clientfd = -1;}
321 DbRetVal start();
322 DbRetVal stop();
323 DbRetVal handleClient();
326 #endif