csqlserver starts csql network daemon for network support
[csql.git] / src / network / TCPClient.cxx
blob03843333943d2a39631b57a7d57293b5a2ca89ef
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 <Parser.h>
24 TCPClient::~TCPClient()
26 if (isConnectedFlag) disconnect();
27 delete respPkt;
29 DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len)
31 DbRetVal rv = OK;
32 // printf("NW:TCP Send\n");
33 void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
34 PacketHeader *hdr= new PacketHeader();
35 hdr->packetType = type;
36 hdr->packetLength = len;
37 hdr->srcNetworkID = networkid;
38 hdr->version = 1;
39 memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
40 int numbytes=0;
41 if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0)) == -1) {
42 printError(ErrOS, "Unable to send the packet\n");
43 return ErrOS;
45 // printf("Sent bytes %d\n", numbytes);
46 if ((numbytes=os::send(sockfd, buf, len, 0)) == -1) {
47 printError(ErrOS, "Unable to send the packet\n");
48 return ErrOS;
50 // printf("Sent bytes %d\n", numbytes);
51 free(totalBuffer);
52 delete hdr;
53 return rv;
55 DbRetVal TCPClient::receive()
57 DbRetVal rv = OK;
58 // printf("NW:TCP receive\n");
59 fd_set fdset;
60 FD_ZERO(&fdset);
61 FD_SET(sockfd, &fdset);
62 struct timeval timeout;
63 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
64 timeout.tv_usec = 0;
65 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
66 if (ret <= 0) {
67 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
68 return ErrPeerTimeOut;
70 socklen_t len = sizeof(struct sockaddr);
71 int numbytes = os::recv(sockfd, respPkt, sizeof(ResponsePacket), 0);
72 if (numbytes == -1)
74 printf("Unable to receive response from peer\n");
75 return ErrOS;
77 char *response = (char *) &respPkt->retVal;
78 // if (*response != 1) rv = ErrPeerResponse;
79 return rv;
81 DbRetVal TCPClient::connect()
83 //printf("NW:TCP connect %s %d %d\n", hostName, port, networkid);
84 //TODO::send endian to the peer site
85 //do not do endian conversion here. it will be done at the server side
86 isConnectedFlag = false;
87 struct hostent *he;
88 int numbytes;
89 if ((he=gethostbyname(hostName)) == NULL) { // get the host info
90 printError(ErrOS,"Unable to get the peer host name\n");
91 return ErrOS;
93 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
94 printError(ErrOS,"Unable to create socket to peer host name\n");
95 return ErrOS;
97 srvAddr.sin_family = AF_INET; // host byte order
98 srvAddr.sin_port = htons(port); // short, network byte order
99 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
100 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
101 if (::connect(sockfd, (struct sockaddr*) & srvAddr, sizeof(struct sockaddr))
102 == -1)
104 printError(ErrOS, "Unable to connect to peer site\n");
105 return ErrOS;
107 // printf("NW:TCP connecting\n");
108 fd_set fdset;
109 FD_ZERO(&fdset);
110 FD_SET(sockfd, &fdset);
111 struct timeval timeout;
112 timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
113 timeout.tv_usec = 0;
114 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
115 if (ret <= 0) {
116 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
117 return ErrPeerTimeOut;
119 int response=0;
120 socklen_t len = sizeof(struct sockaddr);
121 numbytes = os::recv(sockfd, &response, 4, 0);
122 if (numbytes !=4)
124 printf("Unable to receive response from peer\n");
125 return ErrOS;
127 // printf("Response from peer site is %d\n", response);
128 // if (response != 1) return ErrPeerResponse;
129 isConnectedFlag = true;
130 return OK;
133 DbRetVal TCPClient::disconnect()
135 if (isConnectedFlag) {
136 // printf("NW:TCP disconnect %s %d\n", hostName, port);
137 PacketHeader *hdr= new PacketHeader();
138 hdr->packetType = SQL_NW_PKT_DISCONNECT;
139 hdr->packetLength = 0;
140 hdr->srcNetworkID =
141 hdr->version = 1;
142 int numbytes=0;
143 if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0)) == -1) {
144 printError(ErrOS, "Unable to send the packet\n");
145 return ErrOS;
146 } else {
147 // printf("Sent bytes %d\n", numbytes);
148 DbRetVal rv = receive();
149 close(sockfd);
150 delete hdr;
153 isConnectedFlag = false;
154 return OK;