src/network/TCPClient.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 TCPClient::~TCPClient()
00024 {
00025    if (isConnectedFlag) disconnect();
00026 }
00027 DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len)
00028 {
00029     DbRetVal rv = OK;
00030     printf("NW:TCP Send\n");
00031     void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
00032     PacketHeader *hdr=  new PacketHeader();
00033     hdr->packetType = type;
00034     hdr->packetLength = len;
00035     hdr->srcNetworkID = networkid;
00036     hdr->version = 1;
00037     memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
00038     int numbytes=0;
00039     if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0)) == -1) {
00040         printError(ErrOS, "Unable to send the packet\n");
00041         return ErrOS;
00042     }
00043     printf("Sent bytes %d\n", numbytes);
00044     if ((numbytes=os::send(sockfd, buf, len, 0)) == -1) {
00045         printError(ErrOS, "Unable to send the packet\n");
00046         return ErrOS;
00047     }
00048     printf("Sent bytes %d\n", numbytes);
00049     free(totalBuffer);
00050     return rv;
00051 }
00052 DbRetVal TCPClient::receive()
00053 {
00054     DbRetVal rv = OK;
00055     printf("NW:TCP receive\n");
00056     fd_set fdset; 
00057     FD_ZERO(&fdset);
00058     FD_SET(sockfd, &fdset);
00059     struct timeval timeout;
00060     timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
00061     timeout.tv_usec = 0;
00062     int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
00063     if (ret <= 0) {
00064         printError(ErrPeerTimeOut,"Response timeout for peer site\n");
00065         return ErrPeerTimeOut;
00066     }
00067 
00068     int response =0;
00069     socklen_t len = sizeof(struct sockaddr);
00070     int numbytes = os::recv(sockfd, &response, 4, 0);
00071     if (numbytes !=4)
00072     {
00073        printf("Unable to receive response from peer\n");
00074        return ErrOS;
00075     }
00076     //printf("NW:UDP receive\n");
00077     if (response != 1) rv = ErrPeerResponse;
00078     return rv;
00079 }
00080 DbRetVal TCPClient::connect()
00081 {
00082     //printf("NW:TCP connect %s %d %d\n", hostName, port, networkid);
00083     //TODO::send endian to the peer site
00084     //do not do endian conversion here. it will be done at the server side
00085     isConnectedFlag = false;
00086     struct hostent *he;
00087     int numbytes;
00088     if ((he=gethostbyname(hostName)) == NULL) { // get the host info
00089         printError(ErrOS,"Unable to get the peer host name\n");
00090         return ErrOS;
00091     }
00092     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00093         printError(ErrOS,"Unable to create socket to peer host name\n");
00094         return ErrOS;
00095     }
00096     srvAddr.sin_family = AF_INET; // host byte order
00097     srvAddr.sin_port = htons(port); // short, network byte order
00098     srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
00099     memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
00100     if (::connect(sockfd, (struct sockaddr*) & srvAddr, sizeof(struct sockaddr))
00101          == -1)
00102     {
00103         printError(ErrOS, "Unable to connect to peer site\n");
00104         return ErrOS;
00105     }
00106     printf("NW:TCP connecting\n");
00107     fd_set fdset;
00108     FD_ZERO(&fdset);
00109     FD_SET(sockfd, &fdset);
00110     struct timeval timeout;
00111     timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
00112     timeout.tv_usec = 0;
00113     int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
00114     if (ret <= 0) {
00115         printError(ErrPeerTimeOut,"Response timeout for peer site\n");
00116         return ErrPeerTimeOut;
00117     }
00118     int response =0;
00119     socklen_t len = sizeof(struct sockaddr);
00120     numbytes = os::recv(sockfd, &response, 4, 0);
00121     if (numbytes !=4)
00122     {
00123        printf("Unable to receive response from peer\n");
00124        return ErrOS;
00125     }
00126     printf("Response from peer site is %d\n", response);
00127     if (response != 1) return ErrPeerResponse;
00128     isConnectedFlag = true;
00129     return OK;
00130 }
00131 
00132 DbRetVal TCPClient::disconnect()
00133 {
00134     if (isConnectedFlag) {
00135         printf("NW:TCP disconnect %s %d\n", hostName, port);
00136             PacketHeader *hdr=  new PacketHeader();
00137             hdr->packetType = NW_PKT_DISCONNECT;
00138             hdr->packetLength = 0;
00139             hdr->srcNetworkID =
00140             hdr->version = 1;
00141             int numbytes=0;
00142             if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0))
00143                 == -1) {
00144                 printError(ErrOS, "Unable to send the packet\n");
00145             } else
00146                 printf("Sent bytes %d\n", numbytes);
00147             close(sockfd);
00148 
00149     }
00150     isConnectedFlag = false;
00151     return OK;
00152 }

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