Making JDBCBench generic so as to run csql and mysql benchmark
[csql.git] / src / network / UDPClient.cxx
blobd54ceb9c65c0d34bfd82655588d3b19b4256fad3
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <CSql.h>
21 #include <Network.h>
23 UDPClient::~UDPClient()
25 if (isConnectedFlag) disconnect();
27 DbRetVal UDPClient::send(NetworkPacketType type, char *buf, int len)
29 DbRetVal rv = OK;
30 //printf("NW:UDP Send\n");
31 void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
32 //PacketHeader *hdr= (PacketHeader*) totalBuffer;
33 PacketHeader *hdr= new PacketHeader();
34 hdr->packetType = type;
35 hdr->packetLength = len;
36 hdr->srcNetworkID = networkid;
37 hdr->version = 1;
38 memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
41 int numbytes =sendto(sockfd, totalBuffer, sizeof(PacketHeader)+ len, 0,
42 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr));
44 if (numbytes== -1) {
45 printError(ErrOS,"Unable to send the packet\n");
46 return ErrOS;
49 int numbytes=0;
50 if ((numbytes=sendto(sockfd, hdr, sizeof(PacketHeader), 0,
51 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
52 printError(ErrOS, "Unable to send the packet\n");
53 return ErrOS;
55 //printf("Sent bytes %d\n", numbytes);
56 if ((numbytes=sendto(sockfd, buf, len, 0,
57 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
58 printError(ErrOS, "Unable to send the packet\n");
59 return ErrOS;
61 //printf("Sent bytes %d\n", numbytes);
62 free(totalBuffer);
63 return rv;
66 DbRetVal UDPClient::receive()
68 //TODO:: resoibse timeout during socket read
69 DbRetVal rv = OK;
70 fd_set fdset; //TODO::Move it to UDPClient class
71 FD_ZERO(&fdset);
72 FD_SET(sockfd, &fdset);
73 struct timeval timeout;
74 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
75 timeout.tv_usec = 0;
76 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
77 if (ret <= 0) {
78 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
79 return ErrPeerTimeOut;
82 int response =0;
83 socklen_t len = sizeof(struct sockaddr);
84 int numbytes = recvfrom(sockfd, &response, 4, 0,
85 (struct sockaddr *)&fromAddr, &len);
86 if (numbytes !=4)
88 printf("Unable to receive response from peer\n");
89 return ErrOS;
91 //printf("NW:UDP receive\n");
92 if (response != 1) rv = ErrPeerResponse;
93 if(srvAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
95 printf("Packet received from unknown source\n");
96 rv = ErrPeerResponse;
98 //printf("Response rv %d\n", rv);
99 return rv;
101 DbRetVal UDPClient::connect()
103 printf("NW:UDP connect %s %d %d\n", hostName, port, networkid);
105 //TODO::send endian to the peer site
106 //do not do endian conversion here. it will be done at the server side
107 DbRetVal rv = OK;
108 isConnectedFlag = false;
109 struct hostent *he;
110 int numbytes;
111 if ((he=gethostbyname(hostName)) == NULL) { // get the host info
112 printError(ErrOS,"Unable to get the peer host name\n");
113 return ErrOS;
115 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
116 printError(ErrOS,"Unable to create socket to peer host name\n");
117 return ErrOS;
119 srvAddr.sin_family = AF_INET; // host byte order
120 srvAddr.sin_port = htons(port); // short, network byte order
121 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
122 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
123 printf("NW:UDP connecting\n");
124 PacketHeader *hdr= new PacketHeader();
125 hdr->packetType = NW_PKT_CONNECT;
126 hdr->packetLength = 0;
127 hdr->srcNetworkID =
128 hdr->version = 1;
129 if ((numbytes=sendto(sockfd, hdr, sizeof(PacketHeader), 0,
130 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
131 printError(ErrOS, "Unable to send the packet\n");
132 return ErrOS;
134 printf("Sent bytes %d\n", numbytes);
136 fd_set fdset;
137 FD_ZERO(&fdset);
138 FD_SET(sockfd, &fdset);
139 struct timeval timeout;
140 timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
141 timeout.tv_usec = 0;
142 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
143 if (ret <= 0) {
144 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
145 return ErrPeerTimeOut;
148 int response =0;
149 socklen_t len = sizeof(struct sockaddr);
150 numbytes = recvfrom(sockfd, &response, 4, 0,
151 (struct sockaddr *)&fromAddr, &len);
152 if (numbytes !=4)
154 printf("Unable to receive response from peer\n");
155 return ErrOS;
157 if (response != 1) return ErrPeerResponse;
158 isConnectedFlag = true;
159 printf("NW:UDP connected\n");
160 return OK;
163 DbRetVal UDPClient::disconnect()
165 if (isConnectedFlag)
166 printf("NW:UDP disconnect\n");
168 isConnectedFlag = false;
169 return OK;