src/network/UDPClient.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <CSql.h>
00021 #include <Network.h>
00022 
00023 UDPClient::~UDPClient()
00024 {
00025    if (isConnectedFlag) disconnect();
00026 }
00027 DbRetVal UDPClient::send(NetworkPacketType type, char *buf, int len)
00028 {
00029     DbRetVal rv = OK;
00030     //printf("NW:UDP Send\n");
00031     void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
00032     //PacketHeader *hdr= (PacketHeader*) totalBuffer;
00033     PacketHeader *hdr=  new PacketHeader();
00034     hdr->packetType = type;
00035     hdr->packetLength = len;
00036     hdr->srcNetworkID = networkid;
00037     hdr->version = 1;
00038     memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
00039 
00040 /*
00041     int numbytes =sendto(sockfd, totalBuffer, sizeof(PacketHeader)+ len, 0,
00042                 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr));
00043 
00044     if (numbytes== -1) {
00045         printError(ErrOS,"Unable to send the packet\n");
00046         return ErrOS;
00047     }
00048 */
00049     int numbytes=0;
00050     if ((numbytes=sendto(sockfd, hdr, sizeof(PacketHeader), 0,
00051            (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
00052         printError(ErrOS, "Unable to send the packet\n");
00053         return ErrOS;
00054     }
00055     //printf("Sent bytes %d\n", numbytes);
00056     if ((numbytes=sendto(sockfd, buf, len, 0,
00057            (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
00058         printError(ErrOS, "Unable to send the packet\n");
00059         return ErrOS;
00060     }
00061     //printf("Sent bytes %d\n", numbytes);
00062     free(totalBuffer);
00063     return rv;
00064     
00065 }
00066 DbRetVal UDPClient::receive()
00067 {
00068     //TODO:: resoibse timeout during socket read
00069     DbRetVal rv = OK;
00070     fd_set fdset; //TODO::Move it to UDPClient class
00071     FD_ZERO(&fdset);
00072     FD_SET(sockfd, &fdset);
00073     struct timeval timeout;
00074     timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
00075     timeout.tv_usec = 0;
00076     int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
00077     if (ret <= 0) {
00078         printError(ErrPeerTimeOut,"Response timeout for peer site\n");
00079         return ErrPeerTimeOut;
00080     }
00081 
00082     int response =0;
00083     socklen_t len = sizeof(struct sockaddr);
00084     int numbytes = recvfrom(sockfd, &response, 4, 0,
00085                    (struct sockaddr *)&fromAddr, &len);
00086     if (numbytes !=4)
00087     {
00088        printf("Unable to receive response from peer\n");
00089        return ErrOS;
00090     }
00091     //printf("NW:UDP receive\n");
00092     if (response != 1) rv = ErrPeerResponse;
00093     if(srvAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
00094     {
00095         printf("Packet received from unknown source\n");
00096         rv = ErrPeerResponse;
00097     }
00098     //printf("Response rv %d\n", rv);
00099     return rv;
00100 }
00101 DbRetVal UDPClient::connect()
00102 {
00103     printf("NW:UDP connect %s %d %d\n", hostName, port, networkid);
00104 
00105     //TODO::send endian to the peer site
00106     //do not do endian conversion here. it will be done at the server side
00107     DbRetVal rv = OK; 
00108     isConnectedFlag = false;
00109     struct hostent *he;
00110     int numbytes;
00111     if ((he=gethostbyname(hostName)) == NULL) { // get the host info
00112         printError(ErrOS,"Unable to get the peer host name\n");
00113         return ErrOS;
00114     }
00115     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
00116         printError(ErrOS,"Unable to create socket to peer host name\n");
00117         return ErrOS;
00118     }
00119     srvAddr.sin_family = AF_INET; // host byte order
00120     srvAddr.sin_port = htons(port); // short, network byte order
00121     srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
00122     memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
00123     printf("NW:UDP connecting\n");
00124     PacketHeader *hdr=  new PacketHeader();
00125     hdr->packetType = NW_PKT_CONNECT;
00126     hdr->packetLength = 0;
00127     hdr->srcNetworkID = 
00128     hdr->version = 1;
00129     if ((numbytes=sendto(sockfd, hdr, sizeof(PacketHeader), 0,
00130            (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
00131         printError(ErrOS, "Unable to send the packet\n");
00132         return ErrOS;
00133     }
00134     printf("Sent bytes %d\n", numbytes);
00135 
00136     fd_set fdset; 
00137     FD_ZERO(&fdset);
00138     FD_SET(sockfd, &fdset);
00139     struct timeval timeout;
00140     timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
00141     timeout.tv_usec = 0;
00142     int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
00143     if (ret <= 0) {
00144         printError(ErrPeerTimeOut,"Response timeout for peer site\n");
00145         return ErrPeerTimeOut;
00146     }
00147 
00148     int response =0;
00149     socklen_t len = sizeof(struct sockaddr);
00150     numbytes = recvfrom(sockfd, &response, 4, 0,
00151                    (struct sockaddr *)&fromAddr, &len);
00152     if (numbytes !=4)
00153     {
00154        printf("Unable to receive response from peer\n");
00155        return ErrOS;
00156     }
00157     if (response != 1) return ErrPeerResponse;
00158     isConnectedFlag = true;
00159     printf("NW:UDP connected\n");
00160     return OK;
00161 }
00162 
00163 DbRetVal UDPClient::disconnect()
00164 {
00165     if (isConnectedFlag)
00166        printf("NW:UDP disconnect\n");
00167 
00168     isConnectedFlag = false;
00169     return OK;
00170 }

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7