fix for trie index
[csql.git] / src / network / UDPClient.cxx
blob476c5de85614c7b0391003464ff723137a163e08
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 <os.h>
21 #include <CSql.h>
22 #include <Network.h>
24 UDPClient::~UDPClient()
26 if (isConnectedFlag) disconnect();
28 DbRetVal UDPClient::send(NetworkPacketType type)
30 return send(type, NULL, 0);
32 DbRetVal UDPClient::send(NetworkPacketType type, int stmtid)
34 //TODO:
35 return send(type, NULL, 0);
37 DbRetVal UDPClient::send(NetworkPacketType type, char *buf, int len)
39 DbRetVal rv = OK;
40 //printf("NW:UDP Send\n");
41 void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
42 //PacketHeader *hdr= (PacketHeader*) totalBuffer;
43 PacketHeader *hdr= new PacketHeader();
44 hdr->packetType = type;
45 hdr->packetLength = len;
46 hdr->srcNetworkID = networkid;
47 hdr->version = 1;
48 memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
51 int numbytes =sendto(sockfd, totalBuffer, sizeof(PacketHeader)+ len, 0,
52 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr));
54 if (numbytes== -1) {
55 printError(ErrOS,"Unable to send the packet\n");
56 return ErrOS;
59 int numbytes=0;
60 if ((numbytes=sendto(sockfd, (const char*)hdr, sizeof(PacketHeader), 0,
61 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
62 printError(ErrOS, "Unable to send the packet\n");
63 return ErrOS;
65 //printf("Sent bytes %d\n", numbytes);
66 if ((numbytes=sendto(sockfd, buf, len, 0,
67 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
68 printError(ErrOS, "Unable to send the packet\n");
69 return ErrOS;
71 //printf("Sent bytes %d\n", numbytes);
72 free(totalBuffer);
73 return rv;
76 DbRetVal UDPClient::receive()
78 //TODO:: resoibse timeout during socket read
79 DbRetVal rv = OK;
80 fd_set fdset; //TODO::Move it to UDPClient class
81 FD_ZERO(&fdset);
82 FD_SET(sockfd, &fdset);
83 struct timeval timeout;
84 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
85 timeout.tv_usec = 0;
86 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
87 if (ret <= 0) {
88 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
89 return ErrPeerTimeOut;
92 int response =0;
93 socklen_t len = sizeof(struct sockaddr);
94 int numbytes = recvfrom(sockfd, (char*)&response, 4, 0,
95 (struct sockaddr *)&fromAddr, &len);
96 if (numbytes !=4)
98 printf("Unable to receive response from peer\n");
99 return ErrOS;
101 //printf("NW:UDP receive\n");
102 if (response != 1) rv = ErrPeerResponse;
103 if(srvAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
105 printf("Packet received from unknown source\n");
106 rv = ErrPeerResponse;
108 //printf("Response rv %d\n", rv);
109 return rv;
111 DbRetVal UDPClient::connect()
113 printf("NW:UDP connect %s %d %d\n", hostName, port, networkid);
115 //TODO::send endian to the peer site
116 //do not do endian conversion here. it will be done at the server side
117 DbRetVal rv = OK;
118 isConnectedFlag = false;
119 struct hostent *he;
120 int numbytes;
121 if ((he=gethostbyname(hostName)) == NULL) { // get the host info
122 printError(ErrOS,"Unable to get the peer host name\n");
123 return ErrOS;
125 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
126 printError(ErrOS,"Unable to create socket to peer host name\n");
127 return ErrOS;
129 srvAddr.sin_family = AF_INET; // host byte order
130 srvAddr.sin_port = htons(port); // short, network byte order
131 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
132 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
133 printf("NW:UDP connecting\n");
134 PacketHeader *hdr= new PacketHeader();
135 hdr->packetType = NW_PKT_CONNECT;
136 hdr->packetLength = 0;
137 hdr->srcNetworkID =
138 hdr->version = 1;
139 if ((numbytes=sendto(sockfd, (const char*) hdr, sizeof(PacketHeader), 0,
140 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
141 printError(ErrOS, "Unable to send the packet\n");
142 return ErrOS;
144 printf("Sent bytes %d\n", numbytes);
146 fd_set fdset;
147 FD_ZERO(&fdset);
148 FD_SET(sockfd, &fdset);
149 struct timeval timeout;
150 timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
151 timeout.tv_usec = 0;
152 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
153 if (ret <= 0) {
154 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
155 return ErrPeerTimeOut;
158 int response =0;
159 socklen_t len = sizeof(struct sockaddr);
160 numbytes = recvfrom(sockfd, (char*)&response, 4, 0,
161 (struct sockaddr *)&fromAddr, &len);
162 if (numbytes !=4)
164 printf("Unable to receive response from peer\n");
165 return ErrOS;
167 if (response != 1) return ErrPeerResponse;
168 isConnectedFlag = true;
169 printf("NW:UDP connected\n");
170 return OK;
173 DbRetVal UDPClient::disconnect()
175 if (isConnectedFlag)
176 printf("NW:UDP disconnect\n");
178 isConnectedFlag = false;
179 return OK;