Search in sources :

Example 6 with AuthenticationManager

use of de.tum.in.tumcampusapp.api.app.AuthenticationManager in project TumCampusApp by TCA-Team.

the class SendMessageService method onHandleWork.

@Override
protected void onHandleWork(@NonNull Intent intent) {
    TcaDb tcaDb = TcaDb.getInstance(this);
    final CompositeDisposable mDisposable = new CompositeDisposable();
    ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
    remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
    ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
    localRepository.setDb(tcaDb);
    ChatMessageViewModel chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, mDisposable);
    chatMessageViewModel.deleteOldEntries();
    // Get all unsent messages from database
    List<ChatMessage> unsentMsg = chatMessageViewModel.getUnsent();
    if (unsentMsg.isEmpty()) {
        return;
    }
    int numberOfAttempts = 0;
    AuthenticationManager am = new AuthenticationManager(this);
    // Try to send the message 5 times
    while (numberOfAttempts < MAX_SEND_TRIES) {
        try {
            for (ChatMessage message : unsentMsg) {
                // Generate signature and store it in the message
                message.setSignature(am.sign(message.getText()));
                // Send the message to the server
                chatMessageViewModel.sendMessage(message.getRoom(), message, this.getApplicationContext());
                Utils.logv("successfully sent message: " + message.getText());
            }
            // Exit the loop
            return;
        } catch (NoPrivateKey noPrivateKey) {
            // Nothing can be done, just exit
            return;
        } catch (Exception e) {
            Utils.log(e);
            numberOfAttempts++;
        }
        // Sleep for five seconds, maybe the server is currently really busy
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Utils.log(e);
        }
    }
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatMessageRemoteRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository) ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage) ChatMessageViewModel(de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel) TcaDb(de.tum.in.tumcampusapp.database.TcaDb) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) ChatMessageLocalRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)

Aggregations

AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)6 Intent (android.content.Intent)2 NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)2 NoPublicKey (de.tum.in.tumcampusapp.api.app.exception.NoPublicKey)2 IntentFilter (android.content.IntentFilter)1 AccessTokenManager (de.tum.in.tumcampusapp.api.tumonline.AccessTokenManager)1 ChatMessageViewModel (de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel)1 ChatRoomController (de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)1 ChatMember (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember)1 ChatMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)1 ChatRoom (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom)1 ChatMessageLocalRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)1 ChatMessageRemoteRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository)1 TcaDb (de.tum.in.tumcampusapp.database.TcaDb)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 IOException (java.io.IOException)1