~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to storage/ndb/src/cw/util/SocketRegistry.hpp

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-11-08 11:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20111108113113-3ulw01fvi4vn8m25
Tags: upstream-5.5.17
ImportĀ upstreamĀ versionĀ 5.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef SocketClientRegistry_H
 
17
#define SocketClientRegistry_H
 
18
 
 
19
#include <NdbTCP.h>
 
20
#include <NdbOut.hpp>
 
21
 
 
22
#include "SocketClient.hpp" 
 
23
 
 
24
template<class T>
 
25
class SocketRegistry {
 
26
 
 
27
public:
 
28
  SocketRegistry(Uint32 maxSocketClients);
 
29
  ~SocketRegistry();
 
30
  /**
 
31
   * creates and adds a SocketClient to m_socketClients[]
 
32
   * @param host - host name
 
33
   * @param port - port to connect to
 
34
   */
 
35
  bool createSocketClient(const char * host, const Uint16 port);
 
36
 
 
37
  /**
 
38
   * performReceive reads from sockets should do more stuff 
 
39
   */
 
40
  int performReceive(T &);
 
41
 
 
42
 
 
43
  /**
 
44
   * performReceive reads from sockets should do more stuff 
 
45
   */
 
46
  int syncPerformReceive(const char* ,T &, Uint32);
 
47
 
 
48
 
 
49
  /**
 
50
   * performSend sends a command to a host
 
51
   */
 
52
  bool performSend(const char * buf, Uint32 len, const char * remotehost);
 
53
 
 
54
  /**
 
55
   * pollSocketClients performs a select (for a max. of timeoutmillis) or
 
56
   * until there is data to be read from any SocketClient
 
57
   * @param timeOutMillis - select timeout
 
58
   */
 
59
  int pollSocketClients(Uint32 timeOutMillis);
 
60
 
 
61
  /**
 
62
   * reconnect tries to reconnect to a cpcd given its hostname
 
63
   * @param host - name of host to reconnect to.
 
64
   */
 
65
  bool reconnect(const char * host);
 
66
 
 
67
 
 
68
  /**
 
69
   * removeSocketClient
 
70
   * @param host - name of host for which to remove the SocketConnection
 
71
   */
 
72
  bool removeSocketClient(const char * host);
 
73
 
 
74
private:
 
75
  SocketClient** m_socketClients;
 
76
  Uint32 m_maxSocketClients;
 
77
  Uint32 m_nSocketClients;
 
78
  int tcpReadSelectReply;
 
79
  fd_set tcpReadset;
 
80
  
 
81
 
 
82
};
 
83
 
 
84
 
 
85
template<class T>
 
86
inline
 
87
SocketRegistry<T>::SocketRegistry(Uint32 maxSocketClients) {
 
88
  m_maxSocketClients = maxSocketClients;
 
89
  m_socketClients  = new SocketClient * [m_maxSocketClients];
 
90
  m_nSocketClients = 0;
 
91
}
 
92
 
 
93
 
 
94
template<class T>
 
95
inline
 
96
SocketRegistry<T>::~SocketRegistry() {
 
97
  delete [] m_socketClients;
 
98
}
 
99
 
 
100
template<class T>
 
101
inline
 
102
bool 
 
103
SocketRegistry<T>::createSocketClient(const char * host, Uint16 port) {
 
104
 
 
105
  if(port == 0)
 
106
    return false;
 
107
  if(host==NULL)
 
108
    return false;
 
109
  
 
110
  SocketClient * socketClient = new SocketClient(host, port);
 
111
 
 
112
  if(socketClient->openSocket() < 0 || socketClient == NULL) {
 
113
    ndbout << "could not connect" << endl;
 
114
    delete socketClient;
 
115
    return false;
 
116
  }
 
117
  else {
 
118
    m_socketClients[m_nSocketClients] = socketClient;
 
119
    m_nSocketClients++;
 
120
  }
 
121
  return true;
 
122
}
 
123
 
 
124
template<class T>
 
125
inline
 
126
int 
 
127
SocketRegistry<T>::pollSocketClients(Uint32 timeOutMillis) {
 
128
 
 
129
 
 
130
 
 
131
  // Return directly if there are no TCP transporters configured
 
132
  if (m_nSocketClients == 0){
 
133
    tcpReadSelectReply = 0;
 
134
    return 0;
 
135
  }
 
136
  struct timeval timeout;
 
137
  timeout.tv_sec  = timeOutMillis / 1000;
 
138
  timeout.tv_usec = (timeOutMillis % 1000) * 1000;
 
139
 
 
140
 
 
141
  NDB_SOCKET_TYPE maxSocketValue = 0;
 
142
  
 
143
  // Needed for TCP/IP connections
 
144
  // The read- and writeset are used by select
 
145
  
 
146
  FD_ZERO(&tcpReadset);
 
147
 
 
148
  // Prepare for sending and receiving
 
149
  for (Uint32 i = 0; i < m_nSocketClients; i++) {
 
150
    SocketClient * t = m_socketClients[i];
 
151
    
 
152
    // If the socketclient is connected
 
153
    if (t->isConnected()) {
 
154
      
 
155
      const NDB_SOCKET_TYPE socket = t->getSocket();
 
156
      // Find the highest socket value. It will be used by select
 
157
      if (socket > maxSocketValue)
 
158
        maxSocketValue = socket;
 
159
      
 
160
      // Put the connected transporters in the socket read-set 
 
161
      FD_SET(socket, &tcpReadset);
 
162
    }
 
163
  }
 
164
  
 
165
  // The highest socket value plus one
 
166
  maxSocketValue++; 
 
167
  
 
168
  tcpReadSelectReply = select(maxSocketValue, &tcpReadset, 0, 0, &timeout);  
 
169
#ifdef NDB_WIN32
 
170
  if(tcpReadSelectReply == SOCKET_ERROR)
 
171
  {
 
172
    NdbSleep_MilliSleep(timeOutMillis);
 
173
  }
 
174
#endif
 
175
 
 
176
  return tcpReadSelectReply;
 
177
 
 
178
}
 
179
 
 
180
template<class T>
 
181
inline
 
182
bool 
 
183
SocketRegistry<T>::performSend(const char * buf, Uint32 len, const char * remotehost)
 
184
{
 
185
  SocketClient * socketClient;
 
186
  for(Uint32 i=0; i < m_nSocketClients; i++) {
 
187
    socketClient = m_socketClients[i];
 
188
    if(strcmp(socketClient->gethostname(), remotehost)==0) {
 
189
      if(socketClient->isConnected()) {
 
190
        if(socketClient->writeSocket(buf, len)>0)
 
191
          return true;
 
192
        else
 
193
          return false;
 
194
      }
 
195
    }
 
196
  }
 
197
  return false;
 
198
}
 
199
 
 
200
template<class T>
 
201
inline
 
202
int 
 
203
SocketRegistry<T>::performReceive(T & t) {  
 
204
  char buf[255] ; //temp. just for testing. must fix better
 
205
 
 
206
  if(tcpReadSelectReply > 0){
 
207
    for (Uint32 i=0; i<m_nSocketClients; i++) {
 
208
      SocketClient *sc = m_socketClients[i];
 
209
      const NDB_SOCKET_TYPE socket    = sc->getSocket();
 
210
      if(sc->isConnected() && FD_ISSET(socket, &tcpReadset)) {
 
211
        t->runSession(socket,t);
 
212
      }
 
213
    }
 
214
    return 1;
 
215
  }
 
216
  return 0;
 
217
  
 
218
}
 
219
 
 
220
 
 
221
 
 
222
template<class T>
 
223
inline
 
224
int 
 
225
SocketRegistry<T>::syncPerformReceive(const char * remotehost,
 
226
                                      T & t,
 
227
                                      Uint32 timeOutMillis) {  
 
228
  char buf[255] ; //temp. just for testing. must fix better
 
229
  struct timeval timeout;
 
230
  timeout.tv_sec  = timeOutMillis / 1000;
 
231
  timeout.tv_usec = (timeOutMillis % 1000) * 1000;
 
232
  int reply;
 
233
  SocketClient * sc;
 
234
  for(Uint32 i=0; i < m_nSocketClients; i++) {
 
235
    sc = m_socketClients[i];
 
236
    if(strcmp(sc->gethostname(), remotehost)==0) {
 
237
      if(sc->isConnected()) {
 
238
        /*FD_ZERO(&tcpReadset);
 
239
          reply = select(sc->getSocket()+1, 0, 0, 0, &timeout);
 
240
        reply=1;
 
241
        if(reply > 0) {*/
 
242
          t.runSession(sc->getSocket(), t);
 
243
          //}
 
244
      }
 
245
      
 
246
    }
 
247
  }  
 
248
}
 
249
 
 
250
 
 
251
 
 
252
template<class T>
 
253
inline
 
254
bool 
 
255
SocketRegistry<T>::reconnect(const char * host){
 
256
  for(Uint32 i=0; i < m_nSocketClients; i++) {
 
257
    SocketClient * socketClient = m_socketClients[i];
 
258
    if(strcmp(socketClient->gethostname(), host)==0) {
 
259
      if(!socketClient->isConnected()) {
 
260
        if(socketClient->openSocket() > 0)
 
261
          return true;
 
262
        else return false;
 
263
      }
 
264
    }
 
265
  }
 
266
  return false;
 
267
}
 
268
 
 
269
template<class T>
 
270
inline
 
271
bool 
 
272
SocketRegistry<T>::removeSocketClient(const char * host){
 
273
  for(Uint32 i=0; i < m_nSocketClients; i++) {
 
274
    SocketClient * socketClient = m_socketClients[i];
 
275
    if(strcmp(socketClient->gethostname(), host)==0) {
 
276
      if(!socketClient->isConnected()) {
 
277
        if(socketClient->closeSocket() > 0) {
 
278
          delete socketClient;
 
279
          return true;
 
280
        }
 
281
        else return false;
 
282
      }
 
283
    }
 
284
  }
 
285
  return false;
 
286
}
 
287
 
 
288
 
 
289
#endif // Define of SocketRegistry