diff --git a/.gitignore b/.gitignore
index ac264bf..2fb81a2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -95,7 +95,4 @@ parent/.classpath
tdlight-java/target-snapshot/
tdlight-java-8/target-snapshot/
-/example-tdlight-session/data/db.sqlite
-/example-tdlight-session/data/db.sqlite-shm
-/example-tdlight-session/data/db.sqlite-wal
-/example-tdlight-session/data/td.binlog
+/example-tdlight-session/
diff --git a/example/pom.xml b/example/pom.xml
index 84dbde5..9792abf 100644
--- a/example/pom.xml
+++ b/example/pom.xml
@@ -26,7 +26,7 @@
it.tdlight
tdlight-java-bom
- 3.0.0.0-SNAPSHOT
+ 3.0.2+td.1.18.4
pom
import
@@ -54,7 +54,7 @@
it.tdlight
- osx_amd64
+ macos_amd64
-->
diff --git a/example/src/main/java/it/tdlight/example/Example.java b/example/src/main/java/it/tdlight/example/Example.java
index 043226f..141a403 100644
--- a/example/src/main/java/it/tdlight/example/Example.java
+++ b/example/src/main/java/it/tdlight/example/Example.java
@@ -44,6 +44,8 @@ public final class Example {
// Configure the client
TDLibSettings settings = TDLibSettings.create(apiToken);
+ // This is an example, remove this line to use the real telegram datacenters!
+ settings.setUseTestDatacenter(true);
// Configure the session directory
Path sessionPath = Paths.get("example-tdlight-session");
@@ -54,7 +56,7 @@ public final class Example {
SimpleTelegramClientBuilder clientBuilder = clientFactory.builder(settings);
// Configure the authentication info
- ConsoleInteractiveAuthenticationData authenticationData = AuthenticationSupplier.consoleLogin();
+ SimpleAuthenticationSupplier> authenticationData = AuthenticationSupplier.testUser(3210); // Replace with AuthenticationSupplier.consoleLogin(), or .user(xxx), or .bot(xxx);
// Add an example update handler that prints when the bot is started
clientBuilder.addUpdateHandler(TdApi.UpdateAuthorizationState.class, Example::onUpdateAuthorizationState);
diff --git a/tdlight-java/src/main/java/it/tdlight/ClientFactory.java b/tdlight-java/src/main/java/it/tdlight/ClientFactory.java
index 5d91152..a6c73ea 100644
--- a/tdlight-java/src/main/java/it/tdlight/ClientFactory.java
+++ b/tdlight-java/src/main/java/it/tdlight/ClientFactory.java
@@ -8,6 +8,7 @@ import it.tdlight.util.CleanSupport.CleanableSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
+import java.util.concurrent.locks.StampedLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -92,9 +93,9 @@ public class ClientFactory implements AutoCloseable {
TdApi.Object[] clientEvents,
int arrayOffset,
int arrayLength) {
- var eventsHandlingLock = state.getEventsHandlingLock();
+ StampedLock eventsHandlingLock = state.getEventsHandlingLock();
boolean closeWriteLockAcquisitionFailed = false;
- var stamp = eventsHandlingLock.readLock();
+ long stamp = eventsHandlingLock.readLock();
try {
ClientEventsHandler handler = state.getClientEventsHandler(clientId);
@@ -119,7 +120,7 @@ public class ClientFactory implements AutoCloseable {
}
if (isClosed) {
- var writeLockStamp = eventsHandlingLock.tryConvertToWriteLock(stamp);
+ long writeLockStamp = eventsHandlingLock.tryConvertToWriteLock(stamp);
if (writeLockStamp != 0L) {
stamp = writeLockStamp;
removeClientEventHandlers(clientId);
diff --git a/tdlight-java/src/main/java/it/tdlight/InternalClient.java b/tdlight-java/src/main/java/it/tdlight/InternalClient.java
index 8a43a6a..2ed39e2 100644
--- a/tdlight-java/src/main/java/it/tdlight/InternalClient.java
+++ b/tdlight-java/src/main/java/it/tdlight/InternalClient.java
@@ -152,7 +152,7 @@ final class InternalClient implements ClientEventsHandler, TelegramClient {
private void createAndRegisterClient() {
InternalClientsState clientManagerState = this.clientManagerState;
final StampedLock eventsHandlingLock = clientManagerState.getEventsHandlingLock();
- var stamp = eventsHandlingLock.writeLock();
+ long stamp = eventsHandlingLock.writeLock();
try {
if (clientId != null) {
throw new UnsupportedOperationException("Can't initialize the same client twice!");
diff --git a/tdlight-java/src/main/java/it/tdlight/InternalReactiveClient.java b/tdlight-java/src/main/java/it/tdlight/InternalReactiveClient.java
index 940592f..0018714 100644
--- a/tdlight-java/src/main/java/it/tdlight/InternalReactiveClient.java
+++ b/tdlight-java/src/main/java/it/tdlight/InternalReactiveClient.java
@@ -15,6 +15,7 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.locks.StampedLock;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
@@ -165,8 +166,8 @@ final class InternalReactiveClient implements ClientEventsHandler, ReactiveTeleg
}
logger.debug(TG_MARKER, "Creating new client");
clientId = NativeClientAccess.create();
- var eventsHandlingLock = clientManagerState.getEventsHandlingLock();
- var stamp = eventsHandlingLock.writeLock();
+ StampedLock eventsHandlingLock = clientManagerState.getEventsHandlingLock();
+ long stamp = eventsHandlingLock.writeLock();
try {
logger.debug(TG_MARKER, "Registering new client {}", clientId);
clientManagerState.registerClient(clientId, this);
diff --git a/tdlight-java/src/main/java/it/tdlight/client/AuthenticationDataImpl.java b/tdlight-java/src/main/java/it/tdlight/client/AuthenticationDataImpl.java
index 4264637..b725170 100644
--- a/tdlight-java/src/main/java/it/tdlight/client/AuthenticationDataImpl.java
+++ b/tdlight-java/src/main/java/it/tdlight/client/AuthenticationDataImpl.java
@@ -10,12 +10,13 @@ final class AuthenticationDataImpl implements SimpleAuthenticationSupplier 0) {
@@ -48,6 +50,10 @@ final class AuthenticationDataImpl implements SimpleAuthenticationSupplier {
+ /**
+ * User used in Telegram Test Servers
+ * @param value any number from 0001 to 9999
+ */
+ static SimpleAuthenticationSupplier> testUser(int value) {
+ if (value < 1) {
+ throw new IllegalArgumentException("value must be greater than 0");
+ }
+ if (value > 9999) {
+ throw new IllegalArgumentException("value must be lower than 10000");
+ }
+ return new AuthenticationDataImpl("999664" + value, null, true);
+ }
+
CompletableFuture get();
static SimpleAuthenticationSupplier> qrCode() {
@@ -19,11 +33,11 @@ public interface AuthenticationSupplier {
}
static SimpleAuthenticationSupplier> user(String userPhoneNumber) {
- return new AuthenticationDataImpl(userPhoneNumber, null);
+ return new AuthenticationDataImpl(userPhoneNumber, null, false);
}
static SimpleAuthenticationSupplier> bot(String botToken) {
- return new AuthenticationDataImpl(null, botToken);
+ return new AuthenticationDataImpl(null, botToken, false);
}
static ConsoleInteractiveAuthenticationData consoleLogin() {
diff --git a/tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitCodeHandler.java b/tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitCodeHandler.java
index 1cb860d..fbcdc49 100644
--- a/tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitCodeHandler.java
+++ b/tdlight-java/src/main/java/it/tdlight/client/AuthorizationStateWaitCodeHandler.java
@@ -11,13 +11,16 @@ final class AuthorizationStateWaitCodeHandler implements GenericUpdateHandler {
- if (ex != null) {
- exceptionHandler.onException(ex);
- return;
- }
- CheckAuthenticationCode response = new CheckAuthenticationCode(code);
- client.send(response, ok -> {
- if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
- throw new TelegramError((TdApi.Error) ok);
+ if (testCode != null) {
+ sendCode(testCode);
+ } else {
+ ParameterInfo parameterInfo = new ParameterInfoCode(authorizationState.codeInfo.phoneNumber,
+ authorizationState.codeInfo.nextType,
+ authorizationState.codeInfo.timeout,
+ authorizationState.codeInfo.type
+ );
+ clientInteraction.onParameterRequest(InputParameter.ASK_CODE, parameterInfo).whenComplete((code, ex) -> {
+ if (ex != null) {
+ exceptionHandler.onException(ex);
+ return;
}
- }, exceptionHandler);
- });
+ sendCode(code);
+ });
+ }
}
}
+
+ private void sendCode(String code) {
+ CheckAuthenticationCode response = new CheckAuthenticationCode(code);
+ client.send(response, ok -> {
+ if (ok.getConstructor() == TdApi.Error.CONSTRUCTOR) {
+ throw new TelegramError((TdApi.Error) ok);
+ }
+ }, exceptionHandler);
+ }
}
diff --git a/tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClient.java b/tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClient.java
index f4304c5..0b8dc2b 100644
--- a/tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClient.java
+++ b/tdlight-java/src/main/java/it/tdlight/client/SimpleTelegramClient.java
@@ -105,6 +105,7 @@ public final class SimpleTelegramClient implements Authenticable, MutableTelegra
this.addUpdateHandler(TdApi.UpdateAuthorizationState.class,
new AuthorizationStateWaitCodeHandler(client,
new SimpleTelegramClientInteraction(),
+ getTestCode(authenticationData),
this::handleDefaultException
)
);
@@ -119,6 +120,18 @@ public final class SimpleTelegramClient implements Authenticable, MutableTelegra
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
}
+ private String getTestCode(AuthenticationSupplier> authenticationData) {
+ if (authenticationData instanceof AuthenticationDataImpl) {
+ if (!((AuthenticationDataImpl) authenticationData).isBot()
+ && ((AuthenticationDataImpl) authenticationData).isTest()) {
+ String phoneNumber = ((AuthenticationDataImpl) authenticationData).getUserPhoneNumber();
+ String substring = phoneNumber.substring(5, 6);
+ return substring + substring + substring + substring;
+ }
+ }
+ return null;
+ }
+
private void handleUpdate(TdApi.Object update) {
boolean handled = false;
for (ResultHandler updateHandler : updateHandlers.keySet()) {