PopBusDispatcher::ClientPtr PopBusDispatcher::GetOrCreateClient(MojObject& accountId, bool sync) { MojLogTrace(s_log); ClientPtr client; ClientMap::iterator it; it = m_clients.find(accountId); if (it == m_clients.end()) { MojLogInfo(s_log, "Creating PopClient"); shared_ptr<DatabaseInterface> databaseInterface(new MojoDatabase(m_dbClient)); client.reset(new PopClient(databaseInterface, this, accountId, &m_service)); m_clients[accountId] = client; client->CreateSession(); MojLogInfo(s_log, "Setting pop client with account id %s", AsJsonString(accountId).c_str()); } else { client = it->second; } assert(client.get()); MojObject payload; if (sync) { client->SyncAccount(payload); } return client; }
MojErr PopBusDispatcher::SyncAccount(MojServiceMessage* msg, MojObject& payload) { // cancel shut down if it is in shut down state CancelShutdown(); try { MojErr err; MojObject accountId; err = payload.getRequired("accountId", accountId); ErrorToException(err); ClientPtr client = GetOrCreateClient(accountId, false); client->SyncAccount(payload); err = msg->replySuccess(); ErrorToException(err); }catch (const std::exception& e){ MojErrThrowMsg(MojErrInternal, "%s", e.what()); } return MojErrNone; }