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 295 296 297 298 299 300
|
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (c) 2010 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _PASSENGER_MESSAGE_CLIENT_H_
#define _PASSENGER_MESSAGE_CLIENT_H_
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <string>
#include "StaticString.h"
#include "Exceptions.h"
#include "Utils/MessageIO.h"
#include "Utils/IOUtils.h"
#include "Utils/ScopeGuard.h"
namespace Passenger {
using namespace std;
using namespace boost;
class MessageClient {
protected:
FileDescriptor fd;
bool shouldAutoDisconnect;
/* sendUsername() and sendPassword() exist and are virtual in order to facilitate unit testing. */
virtual void sendUsername(int fd, const StaticString &username, unsigned long long *timeout) {
writeScalarMessage(fd, username);
}
virtual void sendPassword(int fd, const StaticString &userSuppliedPassword, unsigned long long *timeout) {
writeScalarMessage(fd, userSuppliedPassword);
}
/**
* Authenticate to the server with the given username and password.
*
* @throws SystemException An error occurred while reading data from or sending data to the server.
* @throws IOException An error occurred while reading data from or sending data to the server.
* @throws SecurityException The server denied authentication.
* @throws boost::thread_interrupted
* @pre <tt>channel</tt> is connected.
*/
void authenticate(const StaticString &username, const StaticString &userSuppliedPassword,
unsigned long long *timeout)
{
vector<string> args;
sendUsername(fd, username, timeout);
sendPassword(fd, userSuppliedPassword, timeout);
if (!readArrayMessage(fd, args, timeout)) {
throw IOException("The message server did not send an authentication response.");
} else if (args.size() != 1) {
throw IOException("The authentication response that the message server sent is not valid.");
} else if (args[0] != "ok") {
throw SecurityException("The message server denied authentication: " + args[0]);
}
}
void checkConnection() {
if (!connected()) {
throw IOException("Not connected");
}
}
void autoDisconnect() {
if (shouldAutoDisconnect) {
fd.close(false);
}
}
public:
/**
* Create a new MessageClient object. It doesn't actually connect to the server until
* you call connect().
*/
MessageClient() {
/* The reason why we don't connect right away is because we want to make
* certain methods virtual for unit testing purposes. We can't call
* virtual methods in the constructor. :-(
*/
shouldAutoDisconnect = true;
}
virtual ~MessageClient() { }
/**
* Connect to the given MessageServer. If a connection was already established,
* then the old connection will be closed and a new connection will be established.
*
* If this MessageClient was in a connected state, and this method throws an exception,
* then old connection will be broken.
*
* @param serverAddress The address of the server to connect to, in the format
* as specified by getSocketAddressType().
* @param username The username to use for authenticating with the server.
* @param userSuppliedPassword The password to use for authenticating with the server.
* @return this
* @throws SystemException Something went wrong while connecting to the server.
* @throws IOException Something went wrong while connecting to the server.
* @throws RuntimeException Something went wrong.
* @throws SecurityException Unable to authenticate to the server with the given username and password.
* You may call connect() again with a different username/password.
* @throws boost::thread_interrupted
* @post connected()
*/
MessageClient *connect(const string &serverAddress, const StaticString &username,
const StaticString &userSuppliedPassword)
{
TRACE_POINT();
ScopeGuard g(boost::bind(&MessageClient::autoDisconnect, this));
fd = connectToServer(serverAddress.c_str());
vector<string> args;
if (!readArrayMessage(fd, args)) {
throw IOException("The message server closed the connection before sending a version identifier.");
}
if (args.size() != 2 || args[0] != "version") {
throw IOException("The message server didn't sent a valid version identifier.");
}
if (args[1] != "1") {
string message = string("Unsupported message server protocol version ") +
args[1] + ".";
throw IOException(message);
}
authenticate(username, userSuppliedPassword, NULL);
g.clear();
return this;
}
void disconnect() {
fd.close();
}
bool connected() const {
return fd != -1;
}
void setAutoDisconnect(bool value) {
shouldAutoDisconnect = value;
}
FileDescriptor getConnection() const {
return fd;
}
/**
* @throws SystemException
* @throws TimeoutException
* @throws boost::thread_interrupted
*/
bool read(vector<string> &args, unsigned long long *timeout = NULL) {
return readArray(args);
}
/**
* @throws SystemException
* @throws TimeoutException
* @throws boost::thread_interrupted
*/
bool readArray(vector<string> &args, unsigned long long *timeout = NULL) {
checkConnection();
ScopeGuard g(boost::bind(&MessageClient::autoDisconnect, this));
bool result = readArrayMessage(fd, args, timeout);
g.clear();
return result;
}
/**
* @throws SystemException
* @throws SecurityException
* @throws TimeoutException
* @throws boost::thread_interrupted
*/
bool readScalar(string &output, unsigned int maxSize = 0, unsigned long long *timeout = NULL) {
checkConnection();
ScopeGuard g(boost::bind(&MessageClient::autoDisconnect, this));
try {
output = readScalarMessage(fd, maxSize, timeout);
g.clear();
return true;
} catch (const EOFException &) {
g.clear();
return false;
}
}
/**
* @throws SystemExeption
* @throws IOException
* @throws boost::thread_interrupted
*/
int readFileDescriptor(bool negotiate = true) {
checkConnection();
ScopeGuard g(boost::bind(&MessageClient::autoDisconnect, this));
int result;
if (negotiate) {
result = Passenger::readFileDescriptorWithNegotiation(fd);
} else {
result = Passenger::readFileDescriptor(fd);
}
g.clear();
return result;
}
/**
* @throws SystemException
* @throws boost::thread_interrupted
*/
void write(const char *name, ...) {
checkConnection();
va_list ap;
va_start(ap, name);
try {
try {
writeArrayMessage(fd, name, ap);
} catch (const SystemException &) {
autoDisconnect();
throw;
}
va_end(ap);
} catch (...) {
va_end(ap);
throw;
}
}
/**
* @throws SystemException
* @throws TimeoutException
* @throws boost::thread_interrupted
*/
void writeScalar(const char *data, unsigned int size, unsigned long long *timeout = NULL) {
checkConnection();
ScopeGuard g(boost::bind(&MessageClient::autoDisconnect, this));
writeScalarMessage(fd, data, size, timeout);
g.clear();
}
/**
* @throws SystemException
* @throws TimeoutException
* @throws boost::thread_interrupted
*/
void writeScalar(const StaticString &data, unsigned long long *timeout = NULL) {
checkConnection();
ScopeGuard g(boost::bind(&MessageClient::autoDisconnect, this));
writeScalarMessage(fd, data, timeout);
g.clear();
}
/**
* @throws SystemException
* @throws boost::thread_interrupted
*/
void writeFileDescriptor(int fileDescriptor, bool negotiate = true) {
checkConnection();
ScopeGuard g(boost::bind(&MessageClient::autoDisconnect, this));
if (negotiate) {
Passenger::writeFileDescriptorWithNegotiation(fd, fileDescriptor);
} else {
Passenger::writeFileDescriptor(fd, fileDescriptor);
}
g.clear();
}
};
typedef shared_ptr<MessageClient> MessageClientPtr;
} // namespace Passenger
#endif /* _PASSENGER_MESSAGE_CLIENT_H_ */
|