core reorg
[csql.git] / src / network / UDPServer.cxx
blob43b0ad017fe386212aa739d33933bfb36eb96430
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 <os.h>
21 #include <CSql.h>
22 #include <Network.h>
23 #include <SqlNetworkHandler.h>
25 DbRetVal UDPServer::start()
27 DbRetVal rv = OK;
28 if (port == 0 )
30 printError(ErrBadArg, "Set the port first before starting\n");
31 return ErrBadArg;
33 struct sockaddr_in my_addr;
34 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
35 printError(ErrOS, "Unable to create socket\n");
36 return ErrOS;
38 my_addr.sin_family = AF_INET;
39 my_addr.sin_port = htons(port);
40 my_addr.sin_addr.s_addr = INADDR_ANY;
41 memset(&(my_addr.sin_zero), '\0', 8);
42 if (bind(sockfd, (struct sockaddr *)&my_addr,
43 sizeof(struct sockaddr)) == -1) {
44 return ErrOS;
46 return rv;
49 DbRetVal UDPServer::stop()
51 //TODO:: response timeout during socket read
52 DbRetVal rv = OK;
53 close(sockfd);
54 return rv;
56 DbRetVal UDPServer::handleClient()
58 DbRetVal rv = OK;
59 PacketHeader header;
60 socklen_t addressLen = sizeof(struct sockaddr);
61 //printf("UDP Server receives packet\n");
62 int numbytes = recvfrom(sockfd, (char*) &header, sizeof(PacketHeader), 0,
63 (struct sockaddr*) &clientAddress, &addressLen);
64 if (numbytes == -1)
66 printf("Error reading from socket\n");
67 return ErrOS;
69 //printf("HEADERINFO %d %d %d %d\n", header.packetType, header.packetLength,
70 // header.srcNetworkID, header.version);
71 if (header.packetType == NW_PKT_CONNECT)
73 int response =1;
74 numbytes = sendto(sockfd, (const char*) &response, 4, 0,
75 (struct sockaddr*) &clientAddress, addressLen);
76 if (numbytes != 4)
78 printf("Error writing to socket\n");
79 return ErrOS;
81 return OK;
83 char *buffer = (char*) malloc(header.packetLength);
84 fd_set fdset; //TODO::Move it to UDPClient class
85 FD_ZERO(&fdset);
86 FD_SET(sockfd, &fdset);
87 struct timeval timeout;
88 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
89 timeout.tv_usec = 0;
90 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
91 if (ret <= 0) {
92 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
93 return ErrPeerTimeOut;
96 numbytes = recvfrom(sockfd, buffer, header.packetLength, 0,
97 (struct sockaddr*) &clientAddress, &addressLen);
99 //printf("Bytes read %d\n", numbytes);
100 if (numbytes == -1)
102 printf("Error reading from socket\n");
103 return ErrOS;
105 SqlNetworkHandler handler;
106 // following line is changed may not work
107 void * response = (void *) handler.process(header, buffer);
108 numbytes = ::sendto(sockfd, (const char*) &response, 4, 0,
109 (struct sockaddr*) &clientAddress, addressLen);
110 if (numbytes != 4)
112 printf("Error writing to socket\n");
113 return ErrOS;
115 return OK;