Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
#ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
21
#define _THRIFT_TRANSPORT_TSOCKETPOOL_H_ 1
22
 
23
#include <vector>
24
#include "TSocket.h"
25
 
26
namespace apache { namespace thrift { namespace transport {
27
 
28
 /**
29
  * Class to hold server information for TSocketPool
30
  *
31
  */
32
class TSocketPoolServer {
33
 
34
  public:
35
  /**
36
   * Default constructor for server info
37
   */
38
  TSocketPoolServer();
39
 
40
  /**
41
   * Constructor for TSocketPool server
42
   */
43
  TSocketPoolServer(const std::string &host, int port);
44
 
45
  // Host name
46
  std::string host_;
47
 
48
  // Port to connect on
49
  int port_;
50
 
51
  // Socket for the server
52
  int socket_;
53
 
54
  // Last time connecting to this server failed
55
  int lastFailTime_;
56
 
57
  // Number of consecutive times connecting to this server failed
58
  int consecutiveFailures_;
59
};
60
 
61
/**
62
 * TCP Socket implementation of the TTransport interface.
63
 *
64
 */
65
class TSocketPool : public TSocket {
66
 
67
 public:
68
 
69
   /**
70
    * Socket pool constructor
71
    */
72
   TSocketPool();
73
 
74
   /**
75
    * Socket pool constructor
76
    *
77
    * @param hosts list of host names
78
    * @param ports list of port names
79
    */
80
   TSocketPool(const std::vector<std::string> &hosts,
81
               const std::vector<int> &ports);
82
 
83
   /**
84
    * Socket pool constructor
85
    *
86
    * @param servers list of pairs of host name and port
87
    */
88
   TSocketPool(const std::vector<std::pair<std::string, int> >& servers);
89
 
90
   /**
91
    * Socket pool constructor
92
    *
93
    * @param servers list of TSocketPoolServers
94
    */
95
  TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
96
 
97
   /**
98
    * Socket pool constructor
99
    *
100
    * @param host single host
101
    * @param port single port
102
    */
103
   TSocketPool(const std::string& host, int port);
104
 
105
   /**
106
    * Destroyes the socket object, closing it if necessary.
107
    */
108
   virtual ~TSocketPool();
109
 
110
   /**
111
    * Add a server to the pool
112
    */
113
   void addServer(const std::string& host, int port);
114
 
115
   /**
116
    * Set list of servers in this pool
117
    */
118
  void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
119
 
120
   /**
121
    * Get list of servers in this pool
122
    */
123
  void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
124
 
125
   /**
126
    * Sets how many times to keep retrying a host in the connect function.
127
    */
128
   void setNumRetries(int numRetries);
129
 
130
   /**
131
    * Sets how long to wait until retrying a host if it was marked down
132
    */
133
   void setRetryInterval(int retryInterval);
134
 
135
   /**
136
    * Sets how many times to keep retrying a host before marking it as down.
137
    */
138
   void setMaxConsecutiveFailures(int maxConsecutiveFailures);
139
 
140
   /**
141
    * Turns randomization in connect order on or off.
142
    */
143
   void setRandomize(bool randomize);
144
 
145
   /**
146
    * Whether to always try the last server.
147
    */
148
   void setAlwaysTryLast(bool alwaysTryLast);
149
 
150
   /**
151
    * Creates and opens the UNIX socket.
152
    */
153
   void open();
154
 
155
   /*
156
    * Closes the UNIX socket
157
    */
158
   void close();
159
 
160
 protected:
161
 
162
  void setCurrentServer(const boost::shared_ptr<TSocketPoolServer> &server);
163
 
164
   /** List of servers to connect to */
165
  std::vector< boost::shared_ptr<TSocketPoolServer> > servers_;
166
 
167
  /** Current server */
168
  boost::shared_ptr<TSocketPoolServer> currentServer_;
169
 
170
   /** How many times to retry each host in connect */
171
   int numRetries_;
172
 
173
   /** Retry interval in seconds, how long to not try a host if it has been
174
    * marked as down.
175
    */
176
   int retryInterval_;
177
 
178
   /** Max consecutive failures before marking a host down. */
179
   int maxConsecutiveFailures_;
180
 
181
   /** Try hosts in order? or Randomized? */
182
   bool randomize_;
183
 
184
   /** Always try last host, even if marked down? */
185
   bool alwaysTryLast_;
186
};
187
 
188
}}} // apache::thrift::transport
189
 
190
#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
191