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);
}
}
}
Aggregations