1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
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. *
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. *
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 ***************************************************************************/
24 TCPClient::~TCPClient()
26 if (isConnectedFlag
) disconnect();
29 DbRetVal
TCPClient::send(NetworkPacketType type
, char *buf
, int len
)
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
;
39 memcpy(((char*)totalBuffer
) + sizeof(PacketHeader
) , buf
, len
);
41 if ((numbytes
=os::send(sockfd
, hdr
, sizeof(PacketHeader
), 0)) == -1) {
42 printError(ErrOS
, "Unable to send the packet\n");
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");
50 // printf("Sent bytes %d\n", numbytes);
55 DbRetVal
TCPClient::receive()
58 // printf("NW:TCP receive\n");
61 FD_SET(sockfd
, &fdset
);
62 struct timeval timeout
;
63 timeout
.tv_sec
= Conf::config
.getNetworkResponseTimeout();
65 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
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);
74 printf("Unable to receive response from peer\n");
77 char *response
= (char *) &respPkt
->retVal
;
78 // if (*response != 1) rv = ErrPeerResponse;
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;
89 if ((he
=gethostbyname(hostName
)) == NULL
) { // get the host info
90 printError(ErrOS
,"Unable to get the peer host name\n");
93 if ((sockfd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
94 printError(ErrOS
,"Unable to create socket to peer host name\n");
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
))
104 printError(ErrOS
, "Unable to connect to peer site\n");
107 // printf("NW:TCP connecting\n");
110 FD_SET(sockfd
, &fdset
);
111 struct timeval timeout
;
112 timeout
.tv_sec
= Conf::config
.getNetworkConnectTimeout();
114 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
116 printError(ErrPeerTimeOut
,"Response timeout for peer site\n");
117 return ErrPeerTimeOut
;
120 socklen_t len
= sizeof(struct sockaddr
);
121 numbytes
= os::recv(sockfd
, &response
, 4, 0);
124 printf("Unable to receive response from peer\n");
127 // printf("Response from peer site is %d\n", response);
128 // if (response != 1) return ErrPeerResponse;
129 isConnectedFlag
= true;
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;
143 if ((numbytes
=os::send(sockfd
, hdr
, sizeof(PacketHeader
), 0)) == -1) {
144 printError(ErrOS
, "Unable to send the packet\n");
147 // printf("Sent bytes %d\n", numbytes);
148 DbRetVal rv
= receive();
153 isConnectedFlag
= false;