Commit d0c9c342 authored by armin's avatar armin
Browse files

Merge branch 'mutexProb' into 'master'

anged mutex

See merge request !117
parents 39b3b8ed 52e8b616
......@@ -168,13 +168,13 @@ void UserModel::onCurrentChannelChanged( const QString &newText )
QSharedPointer<RocketChatUser> UserModel::getUserById( const QString &pId )
{
QSharedPointer<RocketChatUser> user;
mMutex.lock();
//mMutex.lock();
if ( mAllUsers.contains( pId ) ) {
user = mAllUsers[pId];
}
mMutex.unlock();
//mMutex.unlock();
return user;
}
......
......@@ -68,7 +68,7 @@ class UserModel: public QAbstractListModel
QMap<QString, QSharedPointer<RocketChatUser>> userOfCurrentChannel;
QMap<QString, QSharedPointer<RocketChatUser>> mAllUsers;
QString current;
private:
public:
QMutex mMutex;
signals:
......
......@@ -87,7 +87,11 @@ RocketChat::RocketChat( QGuiApplication *app )
#endif
qRegisterMetaType<ConnectionState>( "ConnectionState" );
qRegisterMetaType<Qt::ApplicationState>( "Qt::ApplicationState" );
qRegisterMetaType<RocketChatServerData*>( "RocketChatServerData*" );
// qInstallMessageHandler( customOutput );
qDebug()<<"threadId ctr"<<QThread::currentThreadId();
}
RocketChat::~RocketChat()
......@@ -319,19 +323,22 @@ bool RocketChat::newServerByDomain( const QString &domain, bool pUnsecure = fals
mServerMap.clear();
if ( firstServer != nullptr ) {
connect( firstServer, &RocketChatServerData::destroyed, [ = ]() {
connect( firstServer, &RocketChatServerData::destroyed, [ =,serverptr=this ]() {
qDebug() << "domain: " << domain;
Models::resetModels();
qDebug()<<"threadId destroyed"<<QThread::currentThreadId();
Models::resetModels();
RocketChatServerData *server = new RocketChatServerData( domain, domain, pUnsecure );
RocketChatServerData *server = new RocketChatServerData( domain, domain, pUnsecure );
QMetaObject::invokeMethod(serverptr, "registerServer", Q_ARG(RocketChatServerData*, server ));
//registerServer( server );
newServerMutex.unlock();
registerServer( server );
newServerMutex.unlock();
} );
QMetaObject::invokeMethod( firstServer, "deleteLater" );
}
} else {
QString protocol = QStringLiteral( "https://" );
......@@ -601,6 +608,8 @@ void RocketChat::createPrivateGroup( const QString &pServerId, const QString &pC
void RocketChat::registerServer( RocketChatServerData *pServer )
{
qDebug()<<"threadId register"<<QThread::currentThreadId();
pServer->moveToThread( &mServerThread );
qDebug() << "add server to map";
mServerMap[pServer->getServerId()] = pServer;
......
......@@ -141,7 +141,7 @@ class RocketChat : public QObject
Q_INVOKABLE void deleteMessage(QString rid,QString id);
Q_INVOKABLE void resetConnection(void);
void registerServer( RocketChatServerData *pServer );
Q_INVOKABLE void registerServer( RocketChatServerData *pServer );
void addLoginMethod( const QMap<QString, QVariant> &pMethod );
......
......@@ -38,6 +38,18 @@ RocketChatServerData::RocketChatServerData( QString pId, QString pBaseUrl, bool
}
RocketChatServerData::~RocketChatServerData()
{
mMessageService->deleteLater();
mUnsendDdpRequests.clear();
mEmojiRepo->clear();
mChannels->clear();
mFilesRepo->clear();
delete mFileService;
delete mEmojiService;
mFileUploads.clear();
}
void RocketChatServerData::init()
{
initDb();
......@@ -45,6 +57,8 @@ void RocketChatServerData::init()
void RocketChatServerData::initDb()
{
qDebug()<<" "<<QThread::currentThreadId();
mStorage = PersistanceLayer::instance();
connect( mStorage, &PersistanceLayer::ready, this, &RocketChatServerData::initConnections, Qt::UniqueConnection );
mStorage->init();
......@@ -52,6 +66,8 @@ void RocketChatServerData::initDb()
void RocketChatServerData::initConnections()
{
qDebug()<<" "<<QThread::currentThreadId();
historyLoaded = [ = ]( QMultiMap<QString, QSharedPointer<RocketChatMessage>> *messages ) {
if ( messages ) {
mStorage->transaction();
......@@ -486,21 +502,28 @@ QString RocketChatServerData::getUserId() const
void RocketChatServerData::setUserId( const QString &userId )
{
auto users = Models::getUsersModel();
auto user = users->getUserById( userId );
//TODO: do this differently
auto users = Models::getUsersModel();
if(users){
qDebug()<<" "<<QThread::currentThreadId();
users->mMutex.lock();
auto user = users->getUserById( userId );
if ( user.isNull() && !userId.isEmpty() ) {
user = QSharedPointer<RocketChatUser>::create( userId ) ; //QSharedPointer<RocketChatUser>( new RocketChatUser( userId ) );
auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, user ) );
Q_ASSERT( result );
}
if ( user.isNull() && !userId.isEmpty() ) {
user = QSharedPointer<RocketChatUser>::create( userId ) ; //QSharedPointer<RocketChatUser>( new RocketChatUser( userId ) );
auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, user ) );
Q_ASSERT( result );
}
if ( !user.isNull() && !userId.isEmpty() ) {
mUserId = userId;
if ( !user.isNull() && !userId.isEmpty() ) {
mUserId = userId;
connect( user.data(), &RocketChatUser::statusChanged, this, [ = ]() {
emit userStatusChanged( static_cast<int>( user->getStatus() ) );
} );
}
users->mMutex.unlock();
connect( user.data(), &RocketChatUser::statusChanged, this, [ = ]() {
emit userStatusChanged( static_cast<int>( user->getStatus() ) );
} );
}
}
......@@ -1359,6 +1382,8 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage )
QString msg = pMessage[QStringLiteral( "msg" )].toString();
QJsonObject fields = pMessage["fields"].toObject();
userModel->mMutex.lock();
//race condition regarding user insertion into model, fixed for now with mutex and merge in model
auto user = userModel->getUserById( userId );
......@@ -1386,6 +1411,7 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage )
//TODO: delete user
user->setStatus( RocketChatUser::status::OFFLINE );
}
} else {
auto newUser = QSharedPointer<RocketChatUser>::create( userId );
......@@ -1412,6 +1438,8 @@ void RocketChatServerData::onDDPMessageReceived( const QJsonObject &pMessage )
auto result = QMetaObject::invokeMethod( userModel, "addUser", Q_ARG( User, newUser ) );
Q_ASSERT( result );
}
userModel->mMutex.unlock();
} else if ( pMessage[QStringLiteral( "collection" )] == QStringLiteral( "autocompleteRecords" ) ) {
......
......@@ -120,6 +120,7 @@ class RocketChatServerData : public MessageListener
RocketChatServerData( QString pId, QString pBaseUrl,
bool pUnsecure = false );
~RocketChatServerData();
void setServerId( const QString &pValue );
void persistData();
......
......@@ -51,7 +51,9 @@ void EmojiService::loadCustomEmojis( std::function<void ( QList<QSharedPointer<E
handleCustomEmojisReceived( data );
};
auto request = QSharedPointer<DDPMethodRequest>::create( QStringLiteral( "listEmojiCustom" ), params, success );
server->sendDdprequest( request );
if(server){
server->sendDdprequest( request );
}
}
/**
* Loads the Emoji Data from the databse and processes it to emoji Objects
......
......@@ -70,6 +70,7 @@ FileService::FileService( RocketChatServerData *server )
*/
bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pRequest )
{
if(mServer){
auto repo = mServer->getFiles();
if ( !pRequest.isNull() && pRequest->url.size() > 0 ) {
......@@ -137,25 +138,28 @@ bool FileService::getFileRessource( const QSharedPointer< FileRequest > &pReques
}
}
}
return false;
}
void FileService::getFilesFromDb()
{
auto files = mStorage->getFiles();
auto repo = mServer->getFiles();
if(mServer){
auto files = mStorage->getFiles();
auto repo = mServer->getFiles();
for ( const auto &file : files ) {
auto url = file["url"];
auto path = file["path"];
repo->add( url, QSharedPointer<TempFile>::create( path, url ) );
for ( const auto &file : files ) {
auto url = file["url"];
auto path = file["path"];
repo->add( url, QSharedPointer<TempFile>::create( path, url ) );
}
}
}
QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< FileRequest > &pRequest )
{
if ( !pRequest.isNull() ) {
if ( !pRequest.isNull()&&mServer ) {
auto repo = mServer->getFiles();
if ( repo == nullptr ) {
......@@ -205,7 +209,7 @@ QSharedPointer<TempFile> FileService::getFileFromCache( const QSharedPointer< Fi
void FileService::getFileFromServer( const QSharedPointer< FileRequest > &pRequest )
{
if ( Q_LIKELY( !pRequest.isNull() ) ) {
if ( Q_LIKELY( !pRequest.isNull()&&mServer ) ) {
auto baseUrl = mServer->getRestApi()->getBaseUrl();
auto fullUrl = baseUrl + pRequest->url ;
......
......@@ -110,6 +110,7 @@ void MessageService::persistMessages( const MessageList &pMessage )
void MessageService::persist()
{
if(mServer){
mPersistanceLayer->transaction();
auto channels = mServer->getChannels();
......@@ -126,11 +127,13 @@ void MessageService::persist()
}
mPersistanceLayer->askForcommit();
}
}
QSharedPointer<RocketChatMessage> MessageService::parseMessage( const QJsonObject &pMessageData,
bool linkify )
{
if(mServer){
ChatMessage message( nullptr );
QString userId = mServer->getUserId();
bool blocked = false;
......@@ -241,7 +244,7 @@ QSharedPointer<RocketChatMessage> MessageService::parseMessage( const QJsonObjec
if ( !blocked ) {
return message;
}
}
return nullptr;
}
......@@ -255,14 +258,17 @@ void MessageService::addUserToBlockList( const QString &pId )
void MessageService::addChannelToBlockList( const QString &pId )
{
if(mServer){
QString roomId = pId;
QString userId = mServer->getUserId();
QString otherUserId = roomId.replace( userId, "" );
addUserToBlockList( otherUserId );
}
}
void MessageService::searchMessage( const QString &pTerm, const QString &pRoom )
{
if(mServer){
DdpCallback ddpSuccess = [ = ]( const QJsonObject & pResponse,
MeteorDDP * pDdp ) {
Q_UNUSED( pDdp )
......@@ -290,11 +296,12 @@ void MessageService::searchMessage( const QString &pTerm, const QString &pRoom )
request->setSuccess( ddpSuccess );
mServer->sendDdprequest( request );
}
}
void MessageService::loadHistoryFromServer( const QSharedPointer<LoadHistoryRequestContainer> &pContainer )
{
if ( !pContainer.isNull() ) {
if ( !pContainer.isNull()&&mServer ) {
auto requests = pContainer->getRequests();
pContainer->mAlredayReceived = 0;
......@@ -572,6 +579,7 @@ void MessageService::deleteMessagesNotInList(MessageMap *pMessages,QString pChan
{
QMap<qint64,QSharedPointer<RocketChatMessage>> timeIndex;
QList<QString> ids;
if(mServer){
auto channel = mServer->getChannels()->get(pChannelId);
for(const auto message : pMessages->values(pChannelId)){
......@@ -588,4 +596,5 @@ void MessageService::deleteMessagesNotInList(MessageMap *pMessages,QString pChan
for(QString messageId : blacklist){
mPersistanceLayer->deleteMessage(messageId);
}
}
}
......@@ -38,6 +38,7 @@ RocketChatChannelService::RocketChatChannelService( QObject *parent, RocketChatS
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, bool insertIntoRepo )
{
if(mServer){
auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType );
if ( Q_LIKELY( mChannels ) ) {
......@@ -49,10 +50,14 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject(
fillChannelWithMessages( ptr );
return ptr;
}else{
return nullptr;
}
}
QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject( const QString &pRoomId, const QString &pName, const QString &pType, const QString &pUsername, bool insertIntoRepo )
{
if(mServer){
auto ptr = QSharedPointer<RocketChatChannel>::create( mServer, mMessageService, pRoomId, pName, pType );
if ( pType == "d" ) {
......@@ -77,6 +82,9 @@ QSharedPointer<RocketChatChannel> RocketChatChannelService::createChannelObject(
fillChannelWithMessages( ptr );
return ptr;
}else{
return nullptr;
}
}
......@@ -84,6 +92,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha
{
QVector<QSharedPointer<RocketChatChannel> > vec;
if(mServer){
std::tuple<QString, QString> openChannelTupel = mStorage->getCurrentChannel();
QString openChannel = std::get<0>( openChannelTupel );
mStorage->transaction();
......@@ -207,7 +216,7 @@ QVector<QSharedPointer<RocketChatChannel> > RocketChatChannelService::processCha
}
mStorage->askForcommit();
}
return vec;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment