rasdaman complete source
rasmgr_comm_nb.hh
Go to the documentation of this file.
1 /*
2 * This file is part of rasdaman community.
3 *
4 * Rasdaman community is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Rasdaman community is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with rasdaman community. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
18 rasdaman GmbH.
19 *
20 * For more information please see <http://www.rasdaman.org>
21 * or contact Peter Baumann via <baumann@rasdaman.com>.
22 /
36 #ifndef RASMGR_COMM_NB_HH
37 #define RASMGR_COMM_NB_HH
38 
39 #include "rasmgr_comm.hh"
40 
41 // this is the NON-BLOCKING version, which will replace the other one
42 
43 // maximum number of slaves a master mgr can handle
44 // #define MAXJOBSMASTER 50
45 #define MAXJOBSMASTER 1
46 
47 
51 class IOSelector
52 {
53 public:
54  IOSelector();
55  void setTimeout(int sec,int milisec);
56  void disableTimeout();
57  void setReadSocket(int socket);
58  void clearReadSocket(int socket);
59  void setWriteSocket(int socket);
60  void clearWriteSocket(int socket);
61 
62  int waitForRequest();
63  int someWaitingSocket();
64 
65  bool isReadSocket(int socket);
66  bool isWriteSocket(int socket);
67 
68  void closeForcedAllSockets(); // useful for childs which don't have to inherit this sockets
69 private:
70  fd_set watchReadFdSet;
71  fd_set watchWriteFdSet;
72  fd_set watchExceptFdSet; // unused but ...
73 
74  fd_set resultReadFdSet;
75  fd_set resultWriteFdSet;
76  fd_set resultExceptFdSet; // unused but ...
77 
78  struct timeval tvinit;
79  struct timeval tv;
80  timeval *tvptr; // set to &tv... for timeout, NULL for no timeout
81 
82 };
83 
87 class NbJob
88 {
89 public:
90  NbJob();
91  void init(IOSelector *pselector,int maxInputBuffer);
92 
93  enum acceptStatus
94  {
95  acs_nopending = 0,
96  acs_Iambusy = 1,
97  acs_accepted = 2,
98  acs_outofmem = 3,
99  acs_invalidsocket = 4
100  };
101  acceptStatus acceptConnection(int listenSocket);
102 
103  bool readPartialMessage();
104  bool isMessageOK();
105  const char *getMessage();
106 
107  bool initSendAnswer(const char*);
108  bool writePartialMessage();
109  bool isOperationPending();
110 
111  int getSocket();
112  const char *getRequestor(); // added -- PB 2004-jul-16
113 
114  bool wasError();
115  void closeConnection();
116  void closeSocket();
117  bool cleanUpIfTimeout();
118  bool processJobTimeout();
119  void printStatus();
120  // void reset(); replaced by closeConnection() -- PB 2003-jun-04
121  void clearConnection();
122 private:
123  void clearInputBuffer();
124  void clearOutputBuffer();
125  int socket;
126  IOSelector *pselector;
127 
128  // reading
129  char *inputBuffer;
130  int nextReadPos;
131  int maxInputLength;
132  char messageTerminator;
133  // writing
134  char *outputBuffer;
135  int answerLength;
136  int nextWritePos;
137  // errors
138  bool bigError;
139 
140  // timing
141  time_t lastActionTime;
142  time_t messageReadyTime;
143  void markAction();
144  // public:
145  static time_t timeOutInterv;
146  static time_t currentTime;
147 
148 };
149 
150 //###################
151 
155 class NbServerComm
156 {
157 public:
158  NbServerComm();
159  ~NbServerComm();
160 
161  // void work();
162  void shouldExit();
163  void closeForcedAllSockets(); // useful for children which don't have to inherit these sockets
164  void printStatus();
165 protected:
166  void initJobs(int maxJobs);
167  bool initListenSocket(int port);
168  void closeListenSocket();
169 
170  // void itsRinging(); doesn't exit -- PB 2003-may-04
171  void dispatchReadRequest();
172  void dispatchWriteRequest();
173 
174  void connectNewClients();
175 
176  void lookForTimeout();
177  int listenSocket;
178 
179  NbJob *job;
180  int maxJobs;
181 
182  volatile bool exitRequest;
183  bool mayExit();
184 
185  IOSelector selector;
186  pid_t mypid;
187 
188 };
189 
190 #ifdef X86
191 #define r_Socklen_t socklen_t
192 #endif
193 
194 #ifdef AIX
195 #define r_Socklen_t socklen_t
196 #endif
197 
198 #ifdef SOLARIS
199 #define r_Socklen_t socklen_t
200 #endif
201 
202 #ifdef DECALPHA
203 #define r_Socklen_t int
204 #endif
205 
206 #endif