Change errors behavior

This commit is contained in:
Andrea Cavalli 2020-10-13 23:22:21 +02:00
parent 9dd625a1d7
commit 11ce142716
9 changed files with 9 additions and 251 deletions

View File

@ -1,116 +0,0 @@
package it.tdlight.common;
import java.util.StringJoiner;
public class ClientState {
private boolean hasClientId;
private long clientId;
private boolean initialized;
private boolean readyToReceive;
private boolean readyToSend;
private ClientState(boolean hasClientId, long clientId, boolean initialized, boolean readyToReceive, boolean readyToSend) {
this.hasClientId = hasClientId;
this.clientId = clientId;
this.initialized = initialized;
this.readyToReceive = readyToReceive;
this.readyToSend = readyToSend;
}
public static ClientState of(boolean hasClientId, long clientId, boolean initialized, boolean readyToReceive, boolean readyToSend) {
return new ClientState(hasClientId, clientId, initialized, readyToReceive, readyToSend);
}
public boolean hasClientId() {
return hasClientId;
}
public long getClientId() {
return clientId;
}
public boolean isInitialized() {
return initialized;
}
public boolean isReadyToReceive() {
return readyToReceive;
}
public boolean isReadyToSend() {
return readyToSend;
}
public ClientState setHasClientId(boolean hasClientId) {
this.hasClientId = hasClientId;
return this;
}
public ClientState setClientId(long clientId) {
this.clientId = clientId;
return this;
}
public ClientState setInitialized(boolean initialized) {
this.initialized = initialized;
return this;
}
public ClientState setReadyToReceive(boolean readyToReceive) {
this.readyToReceive = readyToReceive;
return this;
}
public ClientState setReadyToSend(boolean readyToSend) {
this.readyToSend = readyToSend;
return this;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ClientState that = (ClientState) o;
if (hasClientId != that.hasClientId) {
return false;
}
if (clientId != that.clientId) {
return false;
}
if (initialized != that.initialized) {
return false;
}
if (readyToReceive != that.readyToReceive) {
return false;
}
return readyToSend == that.readyToSend;
}
@Override
public int hashCode() {
int result = (hasClientId ? 1 : 0);
result = 31 * result + (int) (clientId ^ (clientId >>> 32));
result = 31 * result + (initialized ? 1 : 0);
result = 31 * result + (readyToReceive ? 1 : 0);
result = 31 * result + (readyToSend ? 1 : 0);
return result;
}
@Override
public String toString() {
return new StringJoiner(", ", ClientState.class.getSimpleName() + "[", "]")
.add("hasClientId=" + hasClientId)
.add("clientId=" + clientId)
.add("initialized=" + initialized)
.add("readyToReceive=" + readyToReceive)
.add("readyToSend=" + readyToSend)
.toString();
}
}

View File

@ -1,9 +1,7 @@
package it.tdlight.common;
import it.tdlight.jni.TdApi.Error;
import it.tdlight.jni.TdApi.Function;
import it.tdlight.jni.TdApi.Object;
import it.tdlight.jni.TdApi.Update;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.concurrent.ConcurrentHashMap;
@ -82,21 +80,8 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
}
}
eventsToFilter.removeIf(event -> {
if (event instanceof Error) {
handleException(updatesHandler.getExceptionHandler(), new TDLibException((Error) event));
return true;
}
return false;
});
ObjectArrayList<Update> updates = new ObjectArrayList<>(eventsToFilter.size());
for (Object object : eventsToFilter) {
updates.add((Update) object);
}
try {
updatesHandler.getUpdatesHandler().onUpdates(updates);
updatesHandler.getUpdatesHandler().onUpdates(eventsToFilter);
} catch (Throwable cause) {
handleException(updatesHandler.getExceptionHandler(), cause);
}
@ -117,11 +102,7 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
private void handleResponse(long eventId, Object event, Handler handler) {
if (handler != null) {
try {
if (event instanceof Error) {
handleException(handler.getExceptionHandler(), new TDLibException((Error) event));
} else {
handler.getResultHandler().onResult(event);
}
handler.getResultHandler().onResult(event);
} catch (Throwable cause) {
handleException(handler.getExceptionHandler(), cause);
}

View File

@ -1,54 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.common;
import it.tdlight.jni.TdApi.Function;
/**
* A request to the TDLib.
*/
public class Request {
private long id;
private Function function;
/**
* Creates a request with eventId and function.
* @param id Request identifier. Responses to TDLib requests will have the same id as the corresponding request. Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0.
* @param function TDLib API function representing a request to TDLib.
*/
public Request(long id, Function function) {
this.id = id;
this.function = function;
}
/**
* Get request identifier.
* @return Request identifier. Responses to TDLib requests will have the same id as the corresponding request. Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0.
*/
public long getId() {
return this.id;
}
/**
* Get TDLib API function.
* @return TDLib API function representing a request to TDLib.
*/
public Function getFunction() {
return this.function;
}
}

View File

@ -1,33 +0,0 @@
/*
* Copyright (c) 2018. Ernesto Castellotti <erny.castell@gmail.com>
* This file is part of JTdlib.
*
* JTdlib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* JTdlib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.common;
/**
* An array of incoming updates from TDLib.
*/
public class ResponseArray {
private Response[] responses;
public ResponseArray(Response[] responses) {
this.responses = responses;
}
public Response[] getResponses() {
return responses;
}
}

View File

@ -2,6 +2,7 @@ package it.tdlight.common;
import static it.tdlight.common.InternalClient.clientInitializationLock;
import it.tdlight.common.utils.IntSwapper;
import it.tdlight.jni.TdApi;
import it.tdlight.jni.TdApi.Object;
import java.util.ArrayList;

View File

@ -1,21 +0,0 @@
package it.tdlight.common;
import it.tdlight.jni.TdApi.Error;
public class TDLibException extends RuntimeException {
private final Error event;
public TDLibException(Error event) {
super(event.code + ": " + event.message);
this.event = event;
}
public int getErrorCode() {
return event.code;
}
public String getErrorMessage() {
return event.message;
}
}

View File

@ -35,7 +35,7 @@ public interface TelegramClient {
* Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously.
*
* @param query Object representing a query to the TDLib.
* @return request result.
* @return request result or {@link TdApi.Error}.
* @throws NullPointerException if query is null.
*/
TdApi.Object execute(TdApi.Function query);

View File

@ -1,6 +1,6 @@
package it.tdlight.common;
import it.tdlight.jni.TdApi.Update;
import it.tdlight.jni.TdApi.Object;
import java.util.List;
/**
@ -11,7 +11,7 @@ public interface UpdatesHandler {
/**
* Callback called on incoming update from TDLib.
*
* @param object Updates of type TdApi.Update about new events.
* @param object Updates of type {@link it.tdlight.jni.TdApi.Update} about new events, or {@link it.tdlight.jni.TdApi.Error}.
*/
void onUpdates(List<Update> object);
void onUpdates(List<Object> object);
}

View File

@ -1,8 +1,8 @@
package it.tdlight.common;
package it.tdlight.common.utils;
import it.unimi.dsi.fastutil.Swapper;
class IntSwapper implements Swapper {
public class IntSwapper implements Swapper {
private final int[] array;
int tmp;