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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Eris/BaseConnection.h>
#include <Eris/Exceptions.h>
#include <Eris/Timeout.h>
#include <Eris/Poll.h>
#include <Eris/Log.h>
#include <Atlas/Codec.h>
#include <Atlas/Net/Stream.h>
#include <Atlas/Objects/Encoder.h>
#include <skstream/skstream.h>
#include <cassert>
namespace Eris {
////////////////////////////////////////////////////////////////////////////////////////////////////////
BaseConnection::BaseConnection(const std::string &cnm,
const std::string &id,
Atlas::Bridge *br) :
_sc(NULL),
_status(DISCONNECTED),
_id(id),
_stream(NULL),
_clientName(cnm),
_bridge(br),
_timeout(NULL),
_host(""),
_port(0)
{
assert(_bridge);
//_stream = new tcp_socket_stream();
}
BaseConnection::~BaseConnection()
{
if (_status != DISCONNECTED) {
hardDisconnect(true);
}
//delete _stream;
}
void BaseConnection::connect(const std::string &host, short port)
{
if (_stream != NULL) {
log(LOG_WARNING, "in base connection :: connect, had existing stream, discarding it");
hardDisconnect(true);
}
_host = host;
_port = port;
// start timeout
_timeout = new Timeout("connect_" + _id, this, 5000);
bindTimeout(*_timeout, CONNECTING);
setStatus(CONNECTING);
_stream = new tcp_socket_stream(host, port, true);
Poll::instance().addStream(_stream, Poll::WRITE);
log(LOG_DEBUG, "Stream added to poller");
}
void BaseConnection::hardDisconnect(bool emit)
{
if (!_stream) {
log(LOG_WARNING, "in baseConnection::hardDisconnect with a NULL stream!");
} else {
// okay, tear it down
if ((_status == CONNECTED) || (_status == DISCONNECTING)){
_codec->streamEnd();
(*_stream) << std::flush;
delete _codec;
delete _encode;
delete _msgEncode;
} else if (_status == NEGOTIATE) {
delete _sc;
_sc = NULL;
} else if (_status == CONNECTING){
// nothing to be done, but can happen
} else
throw InvalidOperation("Bad connection state for disconnection");
delete _timeout;
_timeout = NULL;
Poll::instance().removeStream(_stream);
delete _stream;
_stream = NULL;
}
if (emit) {
Disconnected.emit();
setStatus(DISCONNECTED);
} else
_status = DISCONNECTED;
}
void BaseConnection::recv()
{
int err = 0;
assert(_status != DISCONNECTED);
assert(_stream);
if (_stream->getSocket() == INVALID_SOCKET) {
handleFailure("Connection stream closed unexpectedly");
hardDisconnect(false);
} else {
switch (_status) {
case CONNECTING:
nonblockingConnect();
break;
case NEGOTIATE:
pollNegotiation();
break;
case CONNECTED:
case DISCONNECTING:
_codec->poll();
break;
default:
throw InvalidOperation("Unexpected connection status in poll()");
}
}
// another paranoid check
if (_stream && (err = _stream->getLastError()) != 0) {
char msgBuf[128];
::snprintf(msgBuf, 128, "recv() got stream failure, error %d", _stream->getLastError());
handleFailure(msgBuf);
hardDisconnect(false);
}
}
void BaseConnection::nonblockingConnect()
{
assert(_stream);
if (!_stream->isReady())
return;
if(!_stream->is_open()) {
handleFailure("Failed to connect to " + _host);
hardDisconnect(false);
return;
}
Poll::instance().changeStream(_stream, Poll::READ);
// negotiation timeout
delete _timeout;
_timeout = new Timeout("negotiate_" + _id, this, 5000);
bindTimeout(*_timeout, NEGOTIATE);
_sc = new Atlas::Net::StreamConnect(_clientName,
*_stream, _bridge);
setStatus(NEGOTIATE);
}
void BaseConnection::pollNegotiation()
{
if (!_sc || (_status != NEGOTIATE)) {
log(LOG_DEBUG, "pollNegotiation: unexpected connection status");
throw InvalidOperation("pollNegotiation: unexpected connection status");
}
_sc->poll();
if (_sc->getState() == Atlas::Negotiate<std::iostream>::IN_PROGRESS)
// more negotiation to do once more netwrok data arrives
return;
if (_sc->getState() == Atlas::Negotiate<std::iostream>::SUCCEEDED) {
log(LOG_DEBUG, "Negotiation Success");
_codec = _sc->getCodec();
_encode = new Atlas::Objects::Encoder(_codec);
_codec->streamBegin();
_msgEncode = new Atlas::Message::Encoder(_codec);
// clean up
delete _sc;
_sc = NULL;
delete _timeout;
_timeout = NULL;
setStatus(CONNECTED);
onConnect();
} else {
// assume it all went wrong
handleFailure("Atlas negotiation failed");
hardDisconnect(false);
}
}
void BaseConnection::onConnect()
{
// tell anyone who cares with a signal
Connected.emit();
}
void BaseConnection::setStatus(Status sc)
{
_status = sc;
}
int BaseConnection::getFileDescriptor()
{
if (!_stream)
throw InvalidOperation("Not connected, hence no FD");
return _stream->getSocket();
}
} // of namespace
|