summaryrefslogtreecommitdiffstats
path: root/rasmgr/rasmgr_comm.cc
blob: 02fffcf3455876c12d800ee97d71fa81c53148f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: rasmgr_comm.cc
 *
 * MODULE: rasmgr
 * CLASS:  HTTPComm
 *
 * PURPOSE:
 *   Performs reliable, but blocking HTTP communication. used by the slave rasmgr
 *    
 * COMMENTS:
 *   Will be removed, the plan is to have only non-blocking communication
 *
*/
 
#include "rasmgr_comm.hh"

#ifdef X86
 #define r_Socklen_t socklen_t
#endif

#ifdef AIX
 #define r_Socklen_t socklen_t
#endif

#ifdef SOLARIS
 #define r_Socklen_t socklen_t
#endif

#ifdef DECALPHA 
 #define r_Socklen_t int
#endif 

#include "debug.hh"


HTTPComm::HTTPComm()
  { //parentPID=getpid();
    listen_socket=-1;
    exitRequest=false;;
   }
HTTPComm::~HTTPComm()
  {
    ENTER("HTTPComm::~HTTPComm: enter." );
    closeListenSocket();
    LEAVE("HTTPComm::~HTTPComm: leave." );
  }
   
void HTTPComm::closeListenSocket()
  {
    ENTER("HTTPComm::closeListenSocket: enter." );

    if(listen_socket>=0) close(listen_socket);

    LEAVE("HTTPComm::closeListenSocket: leave." );
  }
   
int HTTPComm::sendAnswer(int socket,int len)
  {
    int result = 0;

    ENTER("HTTPComm::sendAnswer: enter." << std::endl );

    int write_count=writeWholeMessage(socket,outBuffer,len);

    // adapted logic to single point of return -- PB 26-may-2003
    if(write_count<0)
    {   TALK( "HTTPComm::sendAnswer: Error writing answer" );
        result = -1;
    }
    else
        result = 0;

    LEAVE("HTTPComm::sendAnswer: leave. result=" << result );
    return result;  
  }   
   
int HTTPComm::getMessage()
  {
    ENTER("HTTPComm::getMessage: enter." << std::endl );

    int socket=realGetMessage();

    if(socket>0)
      {
        header=inBuffer;
        body=strstr(inBuffer,"\r\n\r\n");
        if(body!=NULL)
          {
            *body=0;
	    body+=4;
          }
	else
	  { close(socket);
	    socket = -1;
	  }    
     }	   

    LEAVE("HTTPComm::getMessage: leave. socket=" << socket );
    return socket;
   }    


int HTTPComm::realGetMessage()   
  {  
    struct sockaddr_in clientname;
    r_Socklen_t size=sizeof(clientname);

    ENTER("HTTPComm::getRealMessage: enter." );

    int socket=accept(listen_socket,(struct sockaddr*)&clientname,&size);
    if(socket<0) 
      { TALK( "HTTPComm::realGetMessage: Error accepting connection.");
        socket = -1;	// normalize error feedback
       }	

    if (socket >= 0)	// accept() worked fine, so wa can continue
      {
        int read_count=readWholeMessage(socket,inBuffer,MAXMSG);

        if(read_count<0)
          { TALK( "HTTPComm::realGetMessage: Error reading message."<<std::endl );
            close(socket);
            socket = -1;
          }
      }
       
    LEAVE("HTTPComm::getRealMessage: leave. socket=" << socket );
    return socket;
   }
      
int HTTPComm::initListenSocket(int port)
  {
    int queuesize=SOMAXCONN; // the maximim number allowed by SO!!
    
    ENTER("HTTPComm::initListenSocket: enter. port=" << port );

    FD_ZERO(&active_fd_set);
    
    listen_socket=makeSocket(port);
    if(listen_socket<0)
    {
        TALK("HTTPComm::initListenSocket: makeSocket failed, errno=" << errno );
        exitbyerror("listen"); // it's OK to exit, we didn't start yet
    }

    if(listen(listen_socket,queuesize)<0)
    {
        TALK("HTTPComm::initListenSocket: listen failed, errno=" << errno );
        exitbyerror("listen"); // it's OK to exit, we didn't start yet
    }

    FD_SET(listen_socket,&active_fd_set);
    
    LEAVE("HTTPComm::initListenSocket: leave. socket=" << socket );
    return 0; // no error      
	       
   }

int HTTPComm::makeSocket(int port)
  {
    int sock;
    struct sockaddr_in name;
    struct protoent *getprotoptr;

    ENTER("HTTPComm::makeSocket: enter. port=" << port );

    getprotoptr=getprotobyname("tcp");
    sock=socket(PF_INET,SOCK_STREAM,getprotoptr->p_proto);
    if(sock<0)
      {
        TALK("HTTPComm::makeSocket: socket failed. errno=" << errno );
        exitbyerror("make socket");
      }
                
    name.sin_family=AF_INET;
    name.sin_port=htons(port);
    name.sin_addr.s_addr=htonl(INADDR_ANY);

#ifdef SO_REUSEADDR
    int val = 1;
    int len = sizeof( val );
    if(setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, (char*)&val, len ))
      {
        TALK( "HTTPComm::makeSocket: Can't set address reusable: "<< strerror(errno) );
      }
#endif    

    int sockResult = bind(sock,(sockaddr*)&name,sizeof(name));
    TALK( "HTTPComm::makeSocket: bind() with socket=" << sock << ", name.sin_port="<< name.sin_port << " returned " << sockResult );
    if(sockResult < 0)
      {
        TALK( "HTTPComm::makeSocket: bind failed: "<< strerror(errno) );
        exitbyerror("bind");
        // This is OK to exit, program just starts and we can't have an address
      }
	
    ENTER("HTTPComm::makeSocket: leave. socket=" << sock << std::endl );
    return sock;
   }

void HTTPComm::shouldExit()
  {
    exitRequest=true;
  }

bool HTTPComm::isMessage(const char *messageStart)
  {
    ENTER("HTTPComm::isMessage: enter. messageStarte=" << messageStart );

    bool rasp= (strncasecmp(header,messageStart,strlen(messageStart))==0) ? true:false;
    if(rasp)
      {
        TALK("HTTPComm::isMessage: (b) Message=" << messageStart );
      }

    LEAVE("HTTPComm::isMessage: leave. result=" << rasp );
    return rasp;
  }
   
//
int readWholeMessage(int socket,char *destBuffer,int buffSize)
  {
    ENTER("HTTPComm::readWholeMessage: enter. socket=" << socket << ", destBuffer=" << destBuffer << ", buffSize=" << buffSize );

    // we read what is comming in until we encounter a '\0'
    // this is our end-sign. 
    int totalLength=0;
    int redNow;
    while(1)
      {
        redNow = read(socket,destBuffer+totalLength,buffSize-totalLength);
	if(redNow == -1)
	  { if(errno == EINTR) continue; // read was interrupted by signal
    
            TALK("HTTPComm::readWholeMessage: read error. errno=" << errno );
	    return -1; // another error
	  }
        totalLength+=redNow;
	
	if(destBuffer[totalLength-1]==0) break; // THE END	    
      }

    LEAVE("HTTPComm::readWholeMessage: leave. totalLength=" << totalLength );
    return totalLength;
   }

int writeWholeMessage(int socket,char *destBuffer,int buffSize)
  {
    ENTER("HTTPComm::writeWholeMessage: enter. socket=" << socket << ", destBuffer=" << destBuffer << ", buffSize=" << buffSize );

    // we write the whole message, including the ending '\0', which is already in
    // the buffSize provided by the caller
    int totalLength=0;
    int writeNow;
    while(1)
      {
        writeNow = write(socket,destBuffer+totalLength,buffSize-totalLength);
	if(writeNow == -1)
	  { if(errno == EINTR) continue; // read was interrupted by signal
	    
            TALK("HTTPComm::writeWholeMessage: read error. errno=" << errno );
	    return -1; // another error
	   }
        totalLength+=writeNow;
	
	if( totalLength==buffSize ) break; // THE END	    
      }

    LEAVE("HTTPComm::writeWholeMessage: leave. totalLength=" << totalLength );
    return totalLength;
  }