TelegramClient interface

This commit is contained in:
Andrea Cavalli 2020-05-05 17:08:39 +02:00
parent abd8a6b61f
commit 6d508a4415
5 changed files with 46 additions and 4 deletions

View File

@ -28,7 +28,7 @@ import java.util.concurrent.locks.StampedLock;
/**
* Interface for interaction with TDLib.
*/
public class Client {
public class Client implements TelegramClient {
private long clientId;
private final ReentrantLock receiveLock = new ReentrantLock();
private final StampedLock executionLock = new StampedLock();
@ -56,6 +56,7 @@ public class Client {
* Sends request to TDLib. May be called from any thread.
* @param request Request to TDLib.
*/
@Override
public void send(Request request) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
@ -70,6 +71,7 @@ public class Client {
* @param eventSize Maximum number of events allowed in list.
* @return An incoming update or request response list. The object returned in the response may be an empty list if the timeout expires.
*/
@Override
public List<Response> receive(double timeout, int eventSize) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
@ -103,6 +105,7 @@ public class Client {
* @param timeout Maximum number of seconds allowed for this function to wait for new records.
* @return An incoming update or request response. The object returned in the response may be a nullptr if the timeout expires.
*/
@Override
public Response receive(double timeout) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
@ -122,6 +125,7 @@ public class Client {
* @param request Request to the TDLib.
* @return The request response.
*/
@Override
public Response execute(Request request) {
if (this.executionLock.isWriteLocked()) {
throw new IllegalStateException("ClientActor is destroyed");
@ -134,6 +138,7 @@ public class Client {
/**
* Destroys the client and TDLib instance.
*/
@Override
public void destroyClient() {
stampedLockValue = this.executionLock.writeLock();
destroyNativeClient(this.clientId);
@ -142,13 +147,15 @@ public class Client {
/**
* Destroys the client and TDLib instance.
*/
@Override
public void initializeClient() {
this.executionLock.tryUnlockWrite();
stampedLockValue = null;
this.clientId = createNativeClient();
}
public boolean isDestroyed() {
@Override
public boolean isDestroyed() {
return this.executionLock.isWriteLocked();
}
}

View File

@ -0,0 +1,21 @@
package it.ernytech.tdlib;
import java.io.IOException;
import java.util.List;
public interface TelegramClient {
void send(Request request);
List<Response> receive(double timeout, int eventSize);
Response receive(double timeout);
Response execute(Request request);
void destroyClient();
void initializeClient() throws IOException;
boolean isDestroyed();
}

View File

@ -2,7 +2,9 @@ package it.ernytech.tdlib.utils;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import org.jetbrains.annotations.Nullable;
@ -15,7 +17,17 @@ public interface BoundedExecutorService extends ExecutorService {
long keepAliveTime,
TimeUnit unit,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
return new BoundedExecutorServiceImpl(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit, queueSizeStatus);
return new BoundedExecutorServiceImpl(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit,
Executors.defaultThreadFactory(), queueSizeStatus);
}
static BoundedExecutorService create(int maxQueueSize,
int corePoolSize,
int maxPoolSize,
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
return new BoundedExecutorServiceImpl(maxQueueSize, corePoolSize, maxPoolSize, keepAliveTime, unit, threadFactory, queueSizeStatus);
}
<T> Future<T> submitButBlockIfFull(Callable<T> task) throws InterruptedException;

View File

@ -26,8 +26,9 @@ class BoundedExecutorServiceImpl extends ThreadPoolExecutor implements BoundedEx
int maxPoolSize,
long keepAliveTime,
TimeUnit unit,
ThreadFactory threadFactory,
@Nullable BiConsumer<Boolean, Integer> queueSizeStatus) {
super(corePoolSize, maxPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<>());
super(corePoolSize, maxPoolSize, keepAliveTime, unit, new LinkedBlockingQueue<>(), threadFactory);
if (maxQueueSize < 0) {
throw new IllegalArgumentException();
}

View File

@ -1,4 +1,5 @@
../../../java/it/ernytech/tdlib/Client.java
../../../java/it/ernytech/tdlib/TelegramClient.java
../../../java/it/ernytech/tdlib/utils/Init.java
../../../java/it/ernytech/tdlib/utils/LoadLibrary.java
../../../java/it/ernytech/tdlib/utils/Os.java