1501526 Composite primary keys
[csql.git] / src / network / TCPClient.cxx
blobf6f70b0ad5fea37c820a4a670791129b8069ab76
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>
23 TCPClient::~TCPClient()
25 if (isConnectedFlag) disconnect();
27 DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len)
29 DbRetVal rv = OK;
30 printf("NW:TCP Send\n");
31 void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
32 PacketHeader *hdr= new PacketHeader();
33 hdr->packetType = type;
34 hdr->packetLength = len;
35 hdr->srcNetworkID = networkid;
36 hdr->version = 1;
37 memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
38 int numbytes=0;
39 if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0)) == -1) {
40 printError(ErrOS, "Unable to send the packet\n");
41 return ErrOS;
43 printf("Sent bytes %d\n", numbytes);
44 if ((numbytes=os::send(sockfd, buf, len, 0)) == -1) {
45 printError(ErrOS, "Unable to send the packet\n");
46 return ErrOS;
48 printf("Sent bytes %d\n", numbytes);
49 free(totalBuffer);
50 return rv;
52 DbRetVal TCPClient::receive()
54 DbRetVal rv = OK;
55 printf("NW:TCP receive\n");
56 fd_set fdset;
57 FD_ZERO(&fdset);
58 FD_SET(sockfd, &fdset);
59 struct timeval timeout;
60 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
61 timeout.tv_usec = 0;
62 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
63 if (ret <= 0) {
64 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
65 return ErrPeerTimeOut;
68 int response =0;
69 socklen_t len = sizeof(struct sockaddr);
70 int numbytes = os::recv(sockfd, &response, 4, 0);
71 if (numbytes !=4)
73 printf("Unable to receive response from peer\n");
74 return ErrOS;
76 //printf("NW:UDP receive\n");
77 if (response != 1) rv = ErrPeerResponse;
78 return rv;
80 DbRetVal TCPClient::connect()
82 //printf("NW:TCP connect %s %d %d\n", hostName, port, networkid);
83 //TODO::send endian to the peer site
84 //do not do endian conversion here. it will be done at the server side
85 isConnectedFlag = false;
86 struct hostent *he;
87 int numbytes;
88 if ((he=gethostbyname(hostName)) == NULL) { // get the host info
89 printError(ErrOS,"Unable to get the peer host name\n");
90 return ErrOS;
92 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
93 printError(ErrOS,"Unable to create socket to peer host name\n");
94 return ErrOS;
96 srvAddr.sin_family = AF_INET; // host byte order
97 srvAddr.sin_port = htons(port); // short, network byte order
98 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
99 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
100 if (::connect(sockfd, (struct sockaddr*) & srvAddr, sizeof(struct sockaddr))
101 == -1)
103 printError(ErrOS, "Unable to connect to peer site\n");
104 return ErrOS;
106 printf("NW:TCP connecting\n");
107 fd_set fdset;
108 FD_ZERO(&fdset);
109 FD_SET(sockfd, &fdset);
110 struct timeval timeout;
111 timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
112 timeout.tv_usec = 0;
113 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
114 if (ret <= 0) {
115 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
116 return ErrPeerTimeOut;
118 int response =0;
119 socklen_t len = sizeof(struct sockaddr);
120 numbytes = os::recv(sockfd, &response, 4, 0);
121 if (numbytes !=4)
123 printf("Unable to receive response from peer\n");
124 return ErrOS;
126 printf("Response from peer site is %d\n", response);
127 if (response != 1) return ErrPeerResponse;
128 isConnectedFlag = true;
129 return OK;
132 DbRetVal TCPClient::disconnect()
134 if (isConnectedFlag) {
135 printf("NW:TCP disconnect %s %d\n", hostName, port);
136 PacketHeader *hdr= new PacketHeader();
137 hdr->packetType = NW_PKT_DISCONNECT;
138 hdr->packetLength = 0;
139 hdr->srcNetworkID =
140 hdr->version = 1;
141 int numbytes=0;
142 if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0))
143 == -1) {
144 printError(ErrOS, "Unable to send the packet\n");
145 } else
146 printf("Sent bytes %d\n", numbytes);
147 close(sockfd);
150 isConnectedFlag = false;
151 return OK;