summaryrefslogtreecommitdiffstats
path: root/rasmgr/rasmgr_comm_nb.hh
blob: 7db5ed184282934427b3ea0c5f1316b349c5f43f (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
/*
* 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_nb.hh
 *
 * MODULE: rasmgr
 * CLASS:  <many>
 *
 * PURPOSE:
 *   Performs reliable, non-blocking HTTP communication. used by the master rasmgr
 *    
 * COMMENTS:
 *   none
 *
*/
#ifndef RASMGR_COMM_NB_HH
#define RASMGR_COMM_NB_HH

#include "rasmgr_comm.hh"

// this is the NON-BLOCKING version, which will replace the other one

// maximum number of slaves a master mgr can handle
// #define MAXJOBSMASTER 50
#define MAXJOBSMASTER 1


/**
  * \ingroup Rasmgrs
  */
class IOSelector
  {
    public:
      IOSelector();
      void setTimeout(int sec,int milisec);
      void disableTimeout();
      void setReadSocket(int socket);
      void clearReadSocket(int socket);
      void setWriteSocket(int socket);
      void clearWriteSocket(int socket);
    
      int waitForRequest();
      int someWaitingSocket();

      bool isReadSocket(int socket);
      bool isWriteSocket(int socket);
      
      void closeForcedAllSockets(); // useful for childs which don't have to inherit this sockets
    private:
      fd_set watchReadFdSet;
      fd_set watchWriteFdSet;  
      fd_set watchExceptFdSet; // unused but ...
    
      fd_set resultReadFdSet;
      fd_set resultWriteFdSet; 
      fd_set resultExceptFdSet; // unused but ...
    
      struct timeval tvinit;
      struct timeval tv;
      timeval *tvptr;		// set to &tv... for timeout, NULL for no timeout
      
   };

/**
  * \ingroup Rasmgrs
  */
class NbJob
  {
    public:
      NbJob();
      void init(IOSelector *pselector,int maxInputBuffer);
      
      enum acceptStatus
        { acs_nopending = 0,
	  acs_Iambusy   = 1,
	  acs_accepted  = 2,
	  acs_outofmem  = 3,
	  acs_invalidsocket = 4
	 };
      acceptStatus acceptConnection(int listenSocket);

      bool readPartialMessage();
      bool isMessageOK();
      const char *getMessage();
      
      bool initSendAnswer(const char*);
      bool writePartialMessage();
      bool isOperationPending();
      
      int  getSocket();
      const char *getRequestor();	// added -- PB 2004-jul-16

      bool wasError();
      void closeConnection();
      void closeSocket();
      bool cleanUpIfTimeout();
      bool processJobTimeout();
      void printStatus();
      // void reset(); replaced by closeConnection() -- PB 2003-jun-04
      void clearConnection();
    private:
      void clearInputBuffer();
      void clearOutputBuffer();
      int socket;
      IOSelector *pselector;
      
      // reading
      char *inputBuffer;
      int   nextReadPos;
      int   maxInputLength;
      char  messageTerminator;
      // writing
      char *outputBuffer;
      int  answerLength;
      int  nextWritePos;
      // errors
      bool bigError;

      // timing
      time_t lastActionTime;
      time_t messageReadyTime;
      void   markAction();
    // public:  
      static time_t timeOutInterv;
      static time_t currentTime;
      
   };
   
//###################   

/**
  * \ingroup Rasmgrs
  */         
class NbServerComm
  {
    public:
      NbServerComm();
      ~NbServerComm();
      
     // void work();
      void shouldExit();
      void closeForcedAllSockets(); // useful for children which don't have to inherit these sockets
      void printStatus(); 
    protected:
       void initJobs(int maxJobs);
       bool initListenSocket(int port);
       void closeListenSocket();
       
       // void itsRinging();		doesn't exit -- PB 2003-may-04
       void dispatchReadRequest();
       void dispatchWriteRequest();
      
       void connectNewClients();
       
       void lookForTimeout();
       int listenSocket;
      
       NbJob *job;
       int   maxJobs;
      
       volatile bool exitRequest;
       bool mayExit();
      
       IOSelector selector;
       pid_t mypid;
       
   };

#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 

#endif