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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <Eris/Account.h>
#include <Eris/Connection.h>
#include <Eris/LogStream.h>
#include <Eris/Exceptions.h>
#include <Eris/Avatar.h>
#include <Eris/Router.h>
#include <Eris/Response.h>
#include <Eris/DeleteLater.h>
#include <Atlas/Objects/Entity.h>
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Anonymous.h>
#include <algorithm>
#include <cassert>
using Atlas::Objects::Root;
using namespace Atlas::Objects::Operation;
using Atlas::Objects::Entity::RootEntity;
using Atlas::Objects::Entity::Anonymous;
typedef Atlas::Objects::Entity::Account AtlasAccount;
using Atlas::Objects::smart_dynamic_cast;
namespace Eris {
class AccountRouter : public Router
{
public:
AccountRouter(Account* pl) :
m_account(pl)
{
m_account->getConnection()->setDefaultRouter(this);
}
virtual ~AccountRouter()
{
m_account->getConnection()->clearDefaultRouter();
}
virtual RouterResult handleOperation(const RootOperation& op)
{
// logout
if (op->getClassNo() == LOGOUT_NO) {
debug() << "Account reciev forced logout from server";
m_account->internalLogout(false);
return HANDLED;
}
return IGNORED;
}
private:
Account* m_account;
};
#pragma mark -
Account::Account(Connection *con) :
m_con(con),
m_status(DISCONNECTED),
m_doingCharacterRefresh(false)
{
if (!m_con) throw InvalidOperation("invalid Connection passed to Account");
m_router = new AccountRouter(this);
m_con->Connected.connect(sigc::mem_fun(this, &Account::netConnected));
m_con->Failure.connect(sigc::mem_fun(this, &Account::netFailure));
}
Account::~Account()
{
ActiveCharacterMap::iterator it;
for (it = m_activeCharacters.begin(); it != m_activeCharacters.end(); )
{
ActiveCharacterMap::iterator cur = it++;
// cur gets invalidated by deactivateCharacter
delete cur->second;
}
delete m_router;
}
Result Account::login(const std::string &uname, const std::string &password)
{
if (!m_con->isConnected()) {
error() << "called login on unconnected Connection";
return NOT_CONNECTED;
}
if (m_status != DISCONNECTED) {
error() << "called login, but state is not currently disconnected";
return ALREADY_LOGGED_IN;
}
return internalLogin(uname, password);
}
Result Account::createAccount(const std::string &uname,
const std::string &fullName,
const std::string &pwd)
{
if (!m_con->isConnected()) return NOT_CONNECTED;
if (m_status != DISCONNECTED) return ALREADY_LOGGED_IN;
m_status = LOGGING_IN;
// okay, build and send the create(account) op
AtlasAccount account;
account->setPassword(pwd);
account->setName(fullName);
account->setUsername(uname);
Create c;
c->setSerialno(getNewSerialno());
c->setArgs1(account);
m_con->getResponder()->await(c->getSerialno(), this, &Account::loginResponse);
m_con->send(c);
// store for re-logins
m_username = uname;
m_pass = pwd;
m_timeout.reset(new Timeout(5000));
m_timeout->Expired.connect(sigc::mem_fun(this, &Account::handleLoginTimeout));
return NO_ERR;
}
Result Account::logout()
{
if (!m_con->isConnected()) {
error() << "called logout on bad connection ignoring";
return NOT_CONNECTED;
}
if (m_status != LOGGED_IN) {
error() << "called logout on non-logged-in Account";
return NOT_LOGGED_IN;
}
m_status = LOGGING_OUT;
Logout l;
l->setId(m_accountId);
l->setSerialno(getNewSerialno());
l->setFrom(m_accountId);
m_con->getResponder()->await(l->getSerialno(), this, &Account::logoutResponse);
m_con->send(l);
m_timeout.reset(new Timeout(5000));
m_timeout->Expired.connect(sigc::mem_fun(this, &Account::handleLogoutTimeout));
return NO_ERR;
}
const std::vector< std::string > & Account::getCharacterTypes(void) const
{
return m_characterTypes;
}
const CharacterMap& Account::getCharacters()
{
if (m_status != LOGGED_IN)
error() << "Not logged into an account : getCharacter returning empty dictionary";
if (m_doingCharacterRefresh)
warning() << "client retrieving partial / incomplete character dictionary";
return _characters;
}
Result Account::refreshCharacterInfo()
{
if (!m_con->isConnected()) return NOT_CONNECTED;
if (m_status != LOGGED_IN) return NOT_LOGGED_IN;
// silently ignore overlapping refreshes
if (m_doingCharacterRefresh) return NO_ERR;
_characters.clear();
if (m_characterIds.empty())
{
GotAllCharacters.emit(); // we must emit the done signal
return NO_ERR;
}
// okay, now we know we have at least one character to lookup, set the flag
m_doingCharacterRefresh = true;
Look lk;
Anonymous obj;
lk->setFrom(m_accountId);
for (StringSet::iterator I=m_characterIds.begin(); I!=m_characterIds.end(); ++I)
{
obj->setId(*I);
lk->setArgs1(obj);
lk->setSerialno(getNewSerialno());
m_con->getResponder()->await(lk->getSerialno(), this, &Account::sightCharacter);
m_con->send(lk);
}
return NO_ERR;
}
Result Account::createCharacter(const Atlas::Objects::Entity::RootEntity &ent)
{
if (!m_con->isConnected()) return NOT_CONNECTED;
if (m_status != LOGGED_IN) {
if ((m_status == CREATING_CHAR) || (m_status == TAKING_CHAR)) {
error() << "duplicate char creation / take";
return DUPLICATE_CHAR_ACTIVE;
} else {
error() << "called createCharacter on unconnected Account, ignoring";
return NOT_LOGGED_IN;
}
}
Create c;
c->setArgs1(ent);
c->setFrom(m_accountId);
c->setSerialno(getNewSerialno());
m_con->send(c);
m_con->getResponder()->await(c->getSerialno(), this, &Account::avatarResponse);
m_status = CREATING_CHAR;
return NO_ERR;
}
/*
void Account::createCharacter()
{
if (!_lobby || _lobby->getAccountID().empty())
throw InvalidOperation("no account exists!");
if (!_con->isConnected())
throw InvalidOperation("Not connected to server");
throw InvalidOperation("No UserInterface handler defined");
// FIXME look up the dialog, create the instance,
// hook in a slot to feed the serialno of any Create op
// the dialog passes back to createCharacterHandler()
}
void Account::createCharacterHandler(long serialno)
{
if (serialno)
NewCharacter((new World(this, _con))->createAvatar(serialno));
}
*/
Result Account::takeCharacter(const std::string &id)
{
if (m_characterIds.count(id) == 0) {
error() << "Character '" << id << "' not owned by Account " << m_username;
return BAD_CHARACTER_ID;
}
if (!m_con->isConnected()) return NOT_CONNECTED;
if (m_status != LOGGED_IN) {
if ((m_status == CREATING_CHAR) || (m_status == TAKING_CHAR)) {
error() << "duplicate char creation / take";
return DUPLICATE_CHAR_ACTIVE;
} else {
error() << "called createCharacter on unconnected Account, ignoring";
return NOT_LOGGED_IN;
}
}
Anonymous what;
what->setId(id);
Look l;
l->setFrom(id); // should this be m_accountId?
l->setArgs1(what);
l->setSerialno(getNewSerialno());
m_con->send(l);
m_con->getResponder()->await(l->getSerialno(), this, &Account::avatarResponse);
m_status = TAKING_CHAR;
return NO_ERR;
}
bool Account::isLoggedIn() const
{
return ((m_status == LOGGED_IN) ||
(m_status == TAKING_CHAR) || (m_status == CREATING_CHAR));
}
#pragma mark -
Result Account::internalLogin(const std::string &uname, const std::string &pwd)
{
assert(m_status == DISCONNECTED);
m_status = LOGGING_IN;
m_username = uname; // store for posterity
AtlasAccount account;
account->setPassword(pwd);
account->setUsername(uname);
Login l;
l->setArgs1(account);
l->setSerialno(getNewSerialno());
m_con->getResponder()->await(l->getSerialno(), this, &Account::loginResponse);
m_con->send(l);
m_timeout.reset(new Timeout(5000));
m_timeout->Expired.connect(sigc::mem_fun(this, &Account::handleLoginTimeout));
return NO_ERR;
}
void Account::logoutResponse(const RootOperation& op)
{
if (!op->instanceOf(INFO_NO))
warning() << "received a logout response that is not an INFO";
internalLogout(true);
}
void Account::internalLogout(bool clean)
{
if (clean) {
if (m_status != LOGGING_OUT)
error() << "got clean logout, but not logging out already";
} else {
if ((m_status != LOGGED_IN) && (m_status != TAKING_CHAR) && (m_status != CREATING_CHAR))
error() << "got forced logout, but not currently logged in";
}
m_status = DISCONNECTED;
m_timeout.reset();
if (m_con->getStatus() == BaseConnection::DISCONNECTING) {
m_con->unlock();
} else {
// note this will cause netDisconnect to run, which is why we set
// status to DISCONNECT already
m_con->disconnect();
LogoutComplete.emit(clean);
}
}
void Account::loginResponse(const RootOperation& op)
{
if (op->instanceOf(ERROR_NO)) {
loginError(smart_dynamic_cast<Error>(op));
} else if (op->instanceOf(INFO_NO)) {
const std::vector<Root>& args = op->getArgs();
loginComplete(smart_dynamic_cast<AtlasAccount>(args.front()));
} else
warning() << "received malformed login response: " << op->getClassNo();
}
void Account::loginComplete(const AtlasAccount &p)
{
if (m_status != LOGGING_IN)
error() << "got loginComplete, but not currently logging in!";
if (p->getUsername() != m_username)
error() << "missing or incorrect username on login INFO";
m_status = LOGGED_IN;
m_accountId = p->getId();
if(p->hasAttr("character_types") == true)
{
Atlas::Message::Element CharacterTypes(p->getAttr("character_types"));
if(CharacterTypes.isList() == true)
{
const Atlas::Message::ListType & CharacterTypesList(CharacterTypes.asList());
Atlas::Message::ListType::const_iterator iCharacterType(CharacterTypesList.begin());
Atlas::Message::ListType::const_iterator iEnd(CharacterTypesList.end());
m_characterTypes.reserve(CharacterTypesList.size());
while(iCharacterType != iEnd)
{
if(iCharacterType->isString() == true)
{
m_characterTypes.push_back(iCharacterType->asString());
}
else
{
error() << "An element of the \"character_types\" list is not a String.";
}
++iCharacterType;
}
}
else
{
error() << "Account has attribute \"character_types\" which is not of type List.";
}
}
m_characterIds = StringSet(p->getCharacters().begin(), p->getCharacters().end());
// notify an people watching us
LoginSuccess.emit();
m_con->Disconnecting.connect(sigc::mem_fun(this, &Account::netDisconnecting));
m_timeout.reset();
}
void Account::loginError(const Error& err)
{
assert(err.isValid());
if (m_status != LOGGING_IN) {
error() << "got loginError while not logging in";
}
const std::vector<Root>& args = err->getArgs();
std::string msg = args[0]->getAttr("message").asString();
// update state before emitting signal
m_status = DISCONNECTED;
m_timeout.reset();
LoginFailure.emit(msg);
}
void Account::handleLoginTimeout()
{
m_status = DISCONNECTED;
deleteLater(m_timeout.release());
LoginFailure.emit("timed out waiting for server response");
}
void Account::avatarResponse(const RootOperation& op)
{
if (op->instanceOf(ERROR_NO)) {
const std::vector<Root>& args = op->getArgs();
std::string msg = args[0]->getAttr("message").asString();
// creating or taking a character failed for some reason
AvatarFailure(msg);
m_status = Account::LOGGED_IN;
} else if (op->instanceOf(INFO_NO)) {
const std::vector<Root>& args = op->getArgs();
RootEntity ent = smart_dynamic_cast<RootEntity>(args.front());
if (!ent.isValid()) {
warning() << "malformed character create/take response";
return;
}
Avatar* av = new Avatar(this, ent->getId());
AvatarSuccess.emit(av);
m_status = Account::LOGGED_IN;
assert(m_activeCharacters.count(av->getId()) == 0);
m_activeCharacters[av->getId()] = av;
// expect another op with the same refno
m_con->getResponder()->ignore(op->getRefno());
} else
warning() << "received malformed avatar take response";
}
void Account::deactivateCharacter(Avatar* av)
{
assert(m_activeCharacters.count(av->getId()) == 1);
m_activeCharacters.erase(av->getId());
}
void Account::sightCharacter(const RootOperation& op)
{
if (!m_doingCharacterRefresh) {
error() << "got sight of character outside a refresh, ignoring";
return;
}
const std::vector<Root>& args = op->getArgs();
assert(!args.empty());
RootEntity ge = smart_dynamic_cast<RootEntity>(args.front());
assert(ge.isValid());
CharacterMap::iterator C = _characters.find(ge->getId());
if (C != _characters.end()) {
error() << "duplicate sight of character " << ge->getId();
return;
}
// okay, we can now add it to our map
_characters.insert(C, CharacterMap::value_type(ge->getId(), ge));
GotCharacterInfo.emit(ge);
// check if we're done
if (_characters.size() == m_characterIds.size()) {
m_doingCharacterRefresh = false;
GotAllCharacters.emit();
}
}
/* this will only ever get encountered after the connection is initally up;
thus we use it to trigger a reconnection. Note that some actions by
Lobby and World are also required to get everything back into the correct
state */
void Account::netConnected()
{
// re-connection
if (!m_username.empty() && !m_pass.empty() && (m_status == DISCONNECTED)) {
debug() << "Account " << m_username << " got netConnected, doing reconnect";
internalLogin(m_username, m_pass);
}
}
bool Account::netDisconnecting()
{
if (m_status == LOGGED_IN) {
m_con->lock();
logout();
return false;
} else
return true;
}
void Account::netFailure(const std::string& /*msg*/)
{
}
void Account::handleLogoutTimeout()
{
error() << "LOGOUT timed out waiting for response";
m_status = DISCONNECTED;
deleteLater(m_timeout.release());
LogoutComplete.emit(false);
}
} // of namespace Eris
|