Making JDBCBench generic so as to run csql and mysql benchmark
[csql.git] / src / network / UDPServer.cxx
blobd1f92147f58f4a3b4473b104f7f7db96f4b4eeaf
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>
22 #include <SqlNetworkHandler.h>
24 DbRetVal UDPServer::start()
26 DbRetVal rv = OK;
27 if (port == 0 )
29 printError(ErrBadArg, "Set the port first before starting\n");
30 return ErrBadArg;
32 struct sockaddr_in my_addr;
33 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
34 printError(ErrOS, "Unable to create socket\n");
35 return ErrOS;
37 my_addr.sin_family = AF_INET;
38 my_addr.sin_port = htons(port);
39 my_addr.sin_addr.s_addr = INADDR_ANY;
40 memset(&(my_addr.sin_zero), '\0', 8);
41 if (bind(sockfd, (struct sockaddr *)&my_addr,
42 sizeof(struct sockaddr)) == -1) {
43 return ErrOS;
45 return rv;
48 DbRetVal UDPServer::stop()
50 //TODO:: response timeout during socket read
51 DbRetVal rv = OK;
52 close(sockfd);
53 return rv;
55 DbRetVal UDPServer::handleClient()
57 DbRetVal rv = OK;
58 PacketHeader header;
59 socklen_t addressLen = sizeof(struct sockaddr);
60 //printf("UDP Server receives packet\n");
61 int numbytes = recvfrom(sockfd, &header, sizeof(PacketHeader), 0,
62 (struct sockaddr*) &clientAddress, &addressLen);
63 if (numbytes == -1)
65 printf("Error reading from socket\n");
66 return ErrOS;
68 //printf("HEADERINFO %d %d %d %d\n", header.packetType, header.packetLength,
69 // header.srcNetworkID, header.version);
70 if (header.packetType == NW_PKT_CONNECT)
72 int response =1;
73 numbytes = sendto(sockfd, &response, 4, 0,
74 (struct sockaddr*) &clientAddress, addressLen);
75 if (numbytes != 4)
77 printf("Error writing to socket\n");
78 return ErrOS;
80 return OK;
82 char *buffer = (char*) malloc(header.packetLength);
83 fd_set fdset; //TODO::Move it to UDPClient class
84 FD_ZERO(&fdset);
85 FD_SET(sockfd, &fdset);
86 struct timeval timeout;
87 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
88 timeout.tv_usec = 0;
89 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
90 if (ret <= 0) {
91 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
92 return ErrPeerTimeOut;
95 numbytes = recvfrom(sockfd, buffer, header.packetLength, 0,
96 (struct sockaddr*) &clientAddress, &addressLen);
98 //printf("Bytes read %d\n", numbytes);
99 if (numbytes == -1)
101 printf("Error reading from socket\n");
102 return ErrOS;
104 SqlNetworkHandler handler;
105 // following line is changed may not work
106 void * response = (void *) handler.process(header, buffer);
107 numbytes = sendto(sockfd, &response, 4, 0,
108 (struct sockaddr*) &clientAddress, addressLen);
109 if (numbytes != 4)
111 printf("Error writing to socket\n");
112 return ErrOS;
114 return OK;