Update Client.java and TelegramClient.java
This commit is contained in:
parent
148a220403
commit
05f455f416
@ -20,21 +20,8 @@ public class Client extends NativeClient implements TelegramClient {
|
|||||||
/**
|
/**
|
||||||
* Creates a new TDLib client.
|
* Creates a new TDLib client.
|
||||||
*/
|
*/
|
||||||
public Client() {
|
public Client() {}
|
||||||
super();
|
|
||||||
try {
|
|
||||||
Init.start();
|
|
||||||
} catch (Throwable throwable) {
|
|
||||||
throwable.printStackTrace();
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
this.clientId = createNativeClient();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends request to TDLib. May be called from any thread.
|
|
||||||
* @param request Request to TDLib.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void send(Request request) {
|
public void send(Request request) {
|
||||||
if (this.executionLock.isWriteLocked()) {
|
if (this.executionLock.isWriteLocked()) {
|
||||||
@ -47,14 +34,8 @@ public class Client extends NativeClient implements TelegramClient {
|
|||||||
private long[] eventIds;
|
private long[] eventIds;
|
||||||
private Object[] events;
|
private Object[] events;
|
||||||
|
|
||||||
/**
|
|
||||||
* Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be called simultaneously from two different threads.
|
|
||||||
* @param timeout Maximum number of seconds allowed for this function to wait for new records.
|
|
||||||
* @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
|
@Override
|
||||||
public List<Response> receive(double timeout, int eventSize) {
|
public List<Response> receive(double timeout, int eventSize, boolean receiveResponses, boolean receiveUpdates) {
|
||||||
if (this.executionLock.isWriteLocked()) {
|
if (this.executionLock.isWriteLocked()) {
|
||||||
throw new IllegalStateException("ClientActor is destroyed");
|
throw new IllegalStateException("ClientActor is destroyed");
|
||||||
}
|
}
|
||||||
@ -77,7 +58,7 @@ public class Client extends NativeClient implements TelegramClient {
|
|||||||
int resultSize;
|
int resultSize;
|
||||||
this.receiveLock.lock();
|
this.receiveLock.lock();
|
||||||
try {
|
try {
|
||||||
resultSize = nativeClientReceive(this.clientId, eventIds, events, timeout);
|
resultSize = nativeClientReceive(this.clientId, eventIds, events, timeout, receiveResponses, receiveUpdates);
|
||||||
} finally {
|
} finally {
|
||||||
this.receiveLock.unlock();
|
this.receiveLock.unlock();
|
||||||
}
|
}
|
||||||
@ -89,18 +70,13 @@ public class Client extends NativeClient implements TelegramClient {
|
|||||||
return responseList;
|
return responseList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be called simultaneously from two different threads.
|
|
||||||
* @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
|
@Override
|
||||||
public Response receive(double timeout) {
|
public Response receive(double timeout, boolean receiveResponses, boolean receiveUpdates) {
|
||||||
if (this.executionLock.isWriteLocked()) {
|
if (this.executionLock.isWriteLocked()) {
|
||||||
throw new IllegalStateException("ClientActor is destroyed");
|
throw new IllegalStateException("ClientActor is destroyed");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Response> responseList = receive(timeout, 1);
|
List<Response> responseList = receive(timeout, 1, receiveResponses, receiveUpdates);
|
||||||
|
|
||||||
if (responseList.size() < 1) {
|
if (responseList.size() < 1) {
|
||||||
return null;
|
return null;
|
||||||
@ -109,11 +85,6 @@ public class Client extends NativeClient implements TelegramClient {
|
|||||||
return responseList.get(0);
|
return responseList.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Synchronously executes TDLib requests. Only a few requests can be executed synchronously. May be called from any thread.
|
|
||||||
* @param request Request to the TDLib.
|
|
||||||
* @return The request response.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public Response execute(Request request) {
|
public Response execute(Request request) {
|
||||||
if (this.executionLock.isWriteLocked()) {
|
if (this.executionLock.isWriteLocked()) {
|
||||||
@ -124,27 +95,22 @@ public class Client extends NativeClient implements TelegramClient {
|
|||||||
return new Response(0, object);
|
return new Response(0, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroys the client and TDLib instance.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void destroyClient() {
|
public void destroyClient() {
|
||||||
stampedLockValue = this.executionLock.tryWriteLock();
|
stampedLockValue = this.executionLock.tryWriteLock();
|
||||||
destroyNativeClient(this.clientId);
|
destroyNativeClient(this.clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Destroys the client and TDLib instance.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void initializeClient() {
|
public void initializeClient() {
|
||||||
this.executionLock.tryUnlockWrite();
|
this.executionLock.tryUnlockWrite();
|
||||||
stampedLockValue = null;
|
stampedLockValue = null;
|
||||||
|
try {
|
||||||
|
Init.start();
|
||||||
|
} catch (Throwable throwable) {
|
||||||
|
throwable.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
this.clientId = createNativeClient();
|
this.clientId = createNativeClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isDestroyed() {
|
|
||||||
return this.executionLock.isWriteLocked();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,17 +5,70 @@ import java.util.List;
|
|||||||
|
|
||||||
public interface TelegramClient {
|
public interface TelegramClient {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends request to TDLib. May be called from any thread.
|
||||||
|
* @param request Request to TDLib.
|
||||||
|
*/
|
||||||
void send(Request request);
|
void send(Request request);
|
||||||
|
|
||||||
List<Response> receive(double timeout, int eventSize);
|
/**
|
||||||
|
* Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be called simultaneously from two different threads.
|
||||||
|
* @param timeout Maximum number of seconds allowed for this function to wait for new records.
|
||||||
|
* @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.
|
||||||
|
*/
|
||||||
|
default List<Response> receive(double timeout, int eventSize) {
|
||||||
|
return receive(timeout, eventSize, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
Response receive(double timeout);
|
/**
|
||||||
|
* Receives incoming updates and/or request responses from TDLib. May be called from any thread, but shouldn't be called simultaneously from two different threads.
|
||||||
|
* @param timeout Maximum number of seconds allowed for this function to wait for new records.
|
||||||
|
* @param eventSize Maximum number of events allowed in list.
|
||||||
|
* @param receiveResponses True to receive responses.
|
||||||
|
* @param receiveUpdates True to receive updates from TDLib.
|
||||||
|
* @return An incoming update or request response list. The object returned in the response may be an empty list if the timeout expires.
|
||||||
|
*/
|
||||||
|
List<Response> receive(double timeout, int eventSize, boolean receiveResponses, boolean receiveUpdates);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives incoming updates and request responses from TDLib. May be called from any thread, but
|
||||||
|
* shouldn't be called simultaneously from two different threads.
|
||||||
|
*
|
||||||
|
* @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.
|
||||||
|
*/
|
||||||
|
default Response receive(double timeout) {
|
||||||
|
return receive(timeout, true, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives incoming updates and request responses from TDLib. May be called from any thread, but
|
||||||
|
* shouldn't be called simultaneously from two different threads.
|
||||||
|
*
|
||||||
|
* @param timeout Maximum number of seconds allowed for this function to wait for new records.
|
||||||
|
* @param receiveResponses True to receive responses.
|
||||||
|
* @param receiveUpdates True to receive updates from TDLib.
|
||||||
|
* @return An incoming update or request response. The object returned in the response may be a
|
||||||
|
* nullptr if the timeout expires.
|
||||||
|
*/
|
||||||
|
Response receive(double timeout, boolean receiveResponses, boolean receiveUpdates);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronously executes TDLib requests. Only a few requests can be executed synchronously. May be called from any thread.
|
||||||
|
* @param request Request to the TDLib.
|
||||||
|
* @return The request response.
|
||||||
|
*/
|
||||||
Response execute(Request request);
|
Response execute(Request request);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys the client and TDLib instance.
|
||||||
|
*/
|
||||||
void destroyClient();
|
void destroyClient();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the client and TDLib instance.
|
||||||
|
*/
|
||||||
void initializeClient() throws IOException;
|
void initializeClient() throws IOException;
|
||||||
|
|
||||||
boolean isDestroyed();
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user