Compare commits

...
8 Commits
14 changed files with 253 additions and 158 deletions

No files matched your search

+6
View File
@@ -5,7 +5,13 @@ All notable changes to this project are documented below.
The format is based on [keep a changelog](http://keepachangelog.com/) and this project uses [semantic versioning](http://semver.org/).
## [Unreleased]
## [2.1.3] - 2021-3-16
### Fixed
- Dispatch error callback when socket connect fails.
## [2.1.2] - 2021-3-16
### Fixed
- Properly enable protobuf-javalite for Android builds.
## [2.1.1] - 2021-02-17
+4 -4
View File
@@ -69,6 +69,9 @@ configurations {
dependencies {
// syntax sugaring for data models
annotationProcessor 'org.projectlombok:lombok:1.18.12'
api 'io.grpc:grpc-protobuf-lite:1.31.1'
// listenable future is part of the SDK public API
api 'com.google.guava:guava:29.0-android'
implementation "org.projectlombok:lombok:1.18.12"
implementation 'com.google.code.gson:gson:2.8.6'
@@ -80,15 +83,12 @@ dependencies {
implementation 'com.squareup.okio:okio:2.7.0'
implementation 'com.squareup.okhttp3:okhttp:4.8.1'
implementation 'io.grpc:grpc-protobuf-lite:1.31.1'
implementation 'io.grpc:grpc-stub:1.31.1'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
protobuf 'com.google.api.grpc:proto-google-common-protos:2.1.0'
implementation 'org.slf4j:slf4j-api:1.7.30'
api 'com.google.guava:guava:29.0-android' // listenable future is part of the SDK public API
protobuf 'com.google.api.grpc:proto-google-common-protos:2.1.0'
testCompile 'junit:junit:4.12'
}
+1 -1
View File
@@ -49,7 +49,7 @@ android {
ext{
versionFirebaseCrashlytics = "17.3.0"
versionFirebaseBom = "26.2.0"
versionNakama = "76b4af4d149f715eed491052f3fa9963e0b0d781"
versionNakama = "08587dcdecc18efce0a0592451f81b31af59bb42"
versionProtobuf = "3.13.0"
versionPlayAuth = "19.0.0"
}
@@ -18,7 +18,11 @@ package com.example.nakama_java_test_android;
import java.util.concurrent.*;
import com.google.common.util.concurrent.*;
import com.heroiclabs.nakama.*;
import com.heroiclabs.nakama.DefaultClient;
import com.heroiclabs.nakama.Session;
import com.heroiclabs.nakama.api.Account;
import android.app.*;
import android.os.Bundle;
import android.util.*;
@@ -32,35 +36,41 @@ public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final DefaultClient client = new DefaultClient("defaultkey", "10.0.2.2", 7349, false);
final ExecutorService executor = Executors.newSingleThreadExecutor();
try {
String email = "[email protected]";
String password = "batsignal";
Futures.addCallback(client.authenticateEmail(email, password), new FutureCallback<Session>() {
@Override
public void onSuccess(final Session result) {
Log.i("auth","got session: " + result.getAuthToken());
client.createSocket();
executor.shutdown();
}
@Override
public void onFailure(final Throwable throwable) {
Log.e("auth", throwable.getMessage());
executor.shutdown();
}
}, executor);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
String email = "[email protected]";
String password = "batsignal";
ListenableFuture<Session> authFuture = client.authenticateEmail(email, password);
AsyncFunction<Session, Account> accountFunction = new AsyncFunction<Session, Account>() {
public ListenableFuture<Account> apply(Session session) {
return client.getAccount(session);
}
};
ListenableFuture<Account> accountFuture = Futures.transformAsync(authFuture, accountFunction, executor);
Futures.addCallback(accountFuture, new FutureCallback<Account>() {
@Override
public void onSuccess(Account account) {
System.out.println("Got account: " + account.getUser().getId());
executor.shutdown();
}
@Override
public void onFailure(Throwable e) {
System.out.println(e.getMessage());
executor.shutdown();
}
}, executor);
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
+1
View File
@@ -0,0 +1 @@
To run this example, run ./gradlew run.
+1 -1
View File
@@ -35,7 +35,7 @@ repositories {
}
dependencies {
implementation 'com.github.heroiclabs.nakama-java:nakama-java:76b4af4d149f715eed491052f3fa9963e0b0d781'
implementation 'com.github.heroiclabs.nakama-java:nakama-java:08587dcdecc18efce0a0592451f81b31af59bb42'
// fat Jar
// implementation 'com.github.heroiclabs.nakama-java:nakama-java-all:dde7330adfa1867881dc6c6c6e27c1176512c916'
+32 -23
View File
@@ -15,40 +15,49 @@
*/
import java.util.concurrent.*;
import com.google.common.util.concurrent.*;
import com.heroiclabs.nakama.DefaultClient;
import com.heroiclabs.nakama.Session;
import com.heroiclabs.nakama.api.Account;
// fat jar
// import nakama.com.google.common.util.concurrent.*;
import com.google.common.util.concurrent.*;
import com.heroiclabs.nakama.*;
public class Main {
public static void main(String[] args) {
DefaultClient client = new DefaultClient("defaultkey", "127.0.0.1", 7349, false);
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
String email = "[email protected]";
String password = "batsignal";
Futures.addCallback(client.authenticateEmail(email, password), new FutureCallback<Session>() {
@Override
public void onSuccess(final Session result) {
System.out.println("got session: " + result.getAuthToken());
executor.shutdown();
}
@Override
public void onFailure(final Throwable throwable) {
System.out.println(throwable.getMessage());
executor.shutdown();
}
}, executor);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
String email = "[email protected]";
String password = "batsignal";
ListenableFuture<Session> authFuture = client.authenticateEmail(email, password);
AsyncFunction<Session, Account> accountFunction = new AsyncFunction<Session, Account>() {
public ListenableFuture<Account> apply(Session session) {
return client.getAccount(session);
}
};
ListenableFuture<Account> accountFuture = Futures.transformAsync(authFuture, accountFunction, executor);
Futures.addCallback(accountFuture, new FutureCallback<Account>() {
@Override
public void onSuccess(Account account) {
System.out.println("Got account: " + account.getUser().getId());
executor.shutdown();
}
@Override
public void onFailure(Throwable e) {
System.out.println(e.getMessage());
executor.shutdown();
}
}, executor);
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
+1
View File
@@ -0,0 +1 @@
To run this example, run `mvn compile` and then `mvn exec:java`.
+1 -34
View File
@@ -38,16 +38,10 @@
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.heroiclabs.nakama-java</groupId>
<artifactId>nakama-java</artifactId>
<version>dde7330adfa1867881dc6c6c6e27c1176512c916</version>
<version>08587dcdecc18efce0a0592451f81b31af59bb42</version>
<type>jar</type>
</dependency>
<!-- fat jar
@@ -61,7 +55,6 @@
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
@@ -76,19 +69,10 @@
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
@@ -102,23 +86,6 @@
</configuration>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
@@ -16,9 +16,12 @@
import java.util.concurrent.*;
import com.google.common.util.concurrent.*;
import com.heroiclabs.nakama.DefaultClient;
import com.heroiclabs.nakama.Session;
import com.heroiclabs.nakama.api.Account;
// fat jar
// import nakama.com.google.common.util.concurrent.*;
import com.heroiclabs.nakama.*;
public class Main {
@@ -26,28 +29,35 @@ public class Main {
DefaultClient client = new DefaultClient("defaultkey", "127.0.0.1", 7349, false);
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
String email = "[email protected]";
String password = "batsignal";
Futures.addCallback(client.authenticateEmail(email, password), new FutureCallback<Session>() {
@Override
public void onSuccess(final Session result) {
System.out.println("got session: " + result.getAuthToken());
executor.shutdown();
}
@Override
public void onFailure(final Throwable throwable) {
System.out.println(throwable.getMessage());
executor.shutdown();
}
}, executor);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
String email = "[email protected]";
String password = "batsignal";
ListenableFuture<Session> authFuture = client.authenticateEmail(email, password);
AsyncFunction<Session, Account> accountFunction = new AsyncFunction<Session, Account>() {
public ListenableFuture<Account> apply(Session session) {
return client.getAccount(session);
}
};
ListenableFuture<Account> accountFuture = Futures.transformAsync(authFuture, accountFunction, executor);
Futures.addCallback(accountFuture, new FutureCallback<Account>() {
@Override
public void onSuccess(Account account) {
System.out.println("Got account: " + account.getUser().getId());
executor.shutdown();
}
@Override
public void onFailure(Throwable e) {
System.out.println(e.getMessage());
executor.shutdown();
}
}, executor);
try {
executor.awaitTermination(5, TimeUnit.SECONDS);
executor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
@@ -1,36 +0,0 @@
/*
* Copyright 2020 The Nakama Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.heroiclabs;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.heroiclabs.nakama.*;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
@@ -126,14 +126,14 @@ public class WebSocketClient implements SocketClient {
}
private ListenableFuture<Session> createWebsocket(@NonNull final Session session, @NonNull final SocketListener listener, @NonNull final Request request) {
final SettableFuture<Session> future = SettableFuture.create();
final SettableFuture<Session> connectFuture = SettableFuture.create();
final Object lock = this;
socket = client.newWebSocket(request, new WebSocketListener() {
@Override
public void onOpen(final WebSocket webSocket, final Response response) {
super.onOpen(webSocket, response);
// Notify the deferred caller that the client has connected and is ready to use.
future.set(session);
connectFuture.set(session);
}
@Override
@@ -234,14 +234,14 @@ public class WebSocketClient implements SocketClient {
}
});
} else {
final SettableFuture future = collationIds.remove(collationId);
if (future == null) {
final SettableFuture cidFuture = collationIds.remove(collationId);
if (cidFuture == null) {
log.warn("No matching future for incoming collation ID: " + collationId);
return;
}
if (env.getError() != null) {
future.setException(new DefaultError(collationId, env.getError()));
cidFuture.setException(new DefaultError(collationId, env.getError()));
} else if (env.getRpc() != null) {
final com.heroiclabs.nakama.api.Rpc.Builder apiRpcBuilder = com.heroiclabs.nakama.api.Rpc.newBuilder();
if (env.getRpc().getId() != null) {
@@ -250,19 +250,19 @@ public class WebSocketClient implements SocketClient {
if (env.getRpc().getPayload() != null) {
apiRpcBuilder.setPayload(env.getRpc().getPayload());
}
future.set(apiRpcBuilder.build());
cidFuture.set(apiRpcBuilder.build());
} else if (env.getChannel() != null) {
future.set(env.getChannel());
cidFuture.set(env.getChannel());
} else if (env.getChannelMessageAck() != null) {
future.set(env.getChannelMessageAck());
cidFuture.set(env.getChannelMessageAck());
} else if (env.getMatch() != null) {
future.set(env.getMatch());
cidFuture.set(env.getMatch());
} else if (env.getMatchmakerTicket() != null) {
future.set(env.getMatchmakerTicket());
cidFuture.set(env.getMatchmakerTicket());
} else if (env.getStatus() != null) {
future.set(env.getStatus());
cidFuture.set(env.getStatus());
} else {
future.set(null);
cidFuture.set(null);
}
}
}
@@ -279,8 +279,11 @@ public class WebSocketClient implements SocketClient {
// Graceful socket disconnect is complete, clean up.
synchronized (lock) {
socket = null;
// TODO callback any leftover deferred items with a disconnect error message?
collationIds.clear();
if (!connectFuture.isCancelled() && !connectFuture.isDone()) {
connectFuture.setException(new Throwable("Socket closed."));
}
}
listener.onDisconnect(null);
}
@@ -291,14 +294,17 @@ public class WebSocketClient implements SocketClient {
// Socket has failed and is no longer connected, clean up.
synchronized (lock) {
socket = null;
// TODO callback any leftover deferred items with a disconnect error message?
collationIds.clear();
if (!connectFuture.isCancelled() && !connectFuture.isDone()) {
connectFuture.setException(t);
}
}
listener.onDisconnect(t);
}
});
return future;
return connectFuture;
}
@Override
@@ -0,0 +1,69 @@
/*
* Copyright 2020 The Nakama Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.heroiclabs.nakama;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
class InvalidSession implements Session {
@Override
public String getAuthToken() {
return "badtoken";
}
@Override
public boolean isCreated() {
return true;
}
@Override
public long getCreateTime() {
return new Date().getTime();
}
@Override
public long getExpireTime() {
return new Date().getTime() + 5000L;
}
@Override
public boolean IsExpired() {
return false;
}
@Override
public String getUsername() {
return "invalid_username";
}
@Override
public String getUserId() {
return "invalid_user";
}
@Override
public boolean isExpired(Date dateTime) {
return false;
}
@Override
public Map<String, String> getVars() {
return new Hashtable<String, String>();
}
}
@@ -22,6 +22,12 @@ import org.junit.Before;
import org.junit.Test;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
public class SocketTest {
private Session session;
@@ -41,7 +47,6 @@ public class SocketTest {
@Test(expected = Error.class)
public void sendNotConnectedUnhandled() throws Throwable {
// socket.connect(session, new AbstractSocketListener() {}).get();
try {
socket.createMatch().get();
} catch (Exception e) {
@@ -60,4 +65,51 @@ public class SocketTest {
e.printStackTrace();
}
}
class TestSocketConnectCallback implements FutureCallback<Session> {
public boolean failureDispatched = false;
public boolean successDispatched = false;
@Override
public void onSuccess(@org.jetbrains.annotations.Nullable Session result) {
this.successDispatched = true;
}
@Override
public void onFailure(Throwable t) {
this.failureDispatched = true;
}
}
@Test
public void sendConnectedFailureHandled() {
final ExecutorService executor = Executors.newSingleThreadExecutor();
final TestSocketConnectCallback connectCallback = new TestSocketConnectCallback();
Futures.addCallback(socket.connect(new InvalidSession(), new AbstractSocketListener(){}), connectCallback, executor);
try {
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Assert.fail("Current thread interrupted.");
}
Assert.assertTrue("Failure callback was not dispatched.", connectCallback.failureDispatched);
}
@Test
public void sendConnectedSuccessHandled() {
final ExecutorService executor = Executors.newSingleThreadExecutor();
final TestSocketConnectCallback connectCallback = new TestSocketConnectCallback();
Futures.addCallback(socket.connect(session, new AbstractSocketListener(){}), connectCallback, executor);
try {
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Assert.fail("Current thread interrupted.");
}
Assert.assertTrue("Success callback was not dispatched.", connectCallback.successDispatched);
}
}