Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

And nullable/nonnullable annotation to improve Kotlin API #54

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions centrifuge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.protobuf:protobuf-javalite:3.20.1'
implementation 'net.sourceforge.streamsupport:streamsupport-cfuture:1.7.0'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
FZambia marked this conversation as resolved.
Show resolved Hide resolved

testImplementation 'junit:junit:4.13.2'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;
FZambia marked this conversation as resolved.
Show resolved Hide resolved

import io.github.centrifugal.centrifuge.internal.backoff.Backoff;
import io.github.centrifugal.centrifuge.internal.protocol.Protocol;

Expand Down Expand Up @@ -390,7 +392,7 @@ private void handleConnectionOpen() {
this.ws.close(NORMAL_CLOSURE_STATUS, "");
return;
}
if (token.equals("")) {
if (token == null || token.equals("")) {
Client.this.failUnauthorized();
return;
}
Expand Down Expand Up @@ -737,7 +739,7 @@ private void sendRefresh() {
);
return;
}
if (token.equals("")) {
if (token == null || token.equals("")) {
this.failUnauthorized();
return;
}
Expand All @@ -746,31 +748,22 @@ private void sendRefresh() {
if (Client.this.getState() != ClientState.CONNECTED) {
return;
}
if (error != null) {
FZambia marked this conversation as resolved.
Show resolved Hide resolved
Client.this.listener.onError(Client.this, new ErrorEvent(new RefreshError(error)));
if (error instanceof ReplyError) {
ReplyError e;
e = (ReplyError) error;
if (e.isTemporary()) {
Client.this.refreshTask = Client.this.scheduler.schedule(
Client.this::sendRefresh,
Client.this.backoff.duration(0, 10000, 20000),
TimeUnit.MILLISECONDS
);
} else {
Client.this.processDisconnect(e.getCode(), e.getMessage(), false);
}
return;
} else {
Client.this.listener.onError(Client.this, new ErrorEvent(new RefreshError(error)));
if (error instanceof ReplyError) {
ReplyError e;
e = (ReplyError) error;
if (e.isTemporary()) {
Client.this.refreshTask = Client.this.scheduler.schedule(
Client.this::sendRefresh,
Client.this.backoff.duration(0, 10000, 20000),
TimeUnit.MILLISECONDS
);
} else {
Client.this.processDisconnect(e.getCode(), e.getMessage(), false);
}
return;
}
if (result.getExpires()) {
if (result != null && result.getExpires()) {
int ttl = result.getTtl();
Client.this.refreshTask = Client.this.scheduler.schedule(Client.this::sendRefresh, ttl, TimeUnit.SECONDS);
}
Expand Down Expand Up @@ -1045,11 +1038,11 @@ private void cleanCommandFuture(Protocol.Command cmd) {
* @param data: a custom payload for RPC call.
* @param cb: will be called as soon as rpc response received or error happened.
*/
public void rpc(String method, byte[] data, ResultCallback<RPCResult> cb) {
public void rpc(@Nullable String method, byte[] data, ResultCallback<RPCResult> cb) {
FZambia marked this conversation as resolved.
Show resolved Hide resolved
this.executor.submit(() -> Client.this.rpcSynchronized(method, data, cb));
}

private void rpcSynchronized(String method, byte[] data, ResultCallback<RPCResult> cb) {
private void rpcSynchronized(@Nullable String method, byte[] data, ResultCallback<RPCResult> cb) {
Protocol.RPCRequest.Builder builder = Protocol.RPCRequest.newBuilder()
.setData(com.google.protobuf.ByteString.copyFrom(data));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public interface CompletionCallback {
/**
* Called when operation done. Caller must check possible error (it's null in case of success).
*/
void onDone(Throwable e);
void onDone(@Nullable Throwable e);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public class RefreshError extends Throwable {
private final Throwable error;

RefreshError(Throwable error) {
RefreshError(@Nullable Throwable error) {
this.error = error;
}

public Throwable getError() {
public @Nullable Throwable getError() {
return error;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public interface ResultCallback<T> {
void onDone(Throwable e, T result);
void onDone(@Nullable Throwable e, @Nullable T result);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public class ServerSubscribedEvent {
ServerSubscribedEvent(String channel, Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, StreamPosition streamPosition, byte[] data) {
ServerSubscribedEvent(String channel, Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, @Nullable StreamPosition streamPosition, @Nullable byte[] data) {
this.channel = channel;
this.wasRecovering = wasRecovering;
this.recovered = recovered;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public class SubscribedEvent {
SubscribedEvent(Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, StreamPosition streamPosition, byte[] data) {
SubscribedEvent(Boolean wasRecovering, Boolean recovered, Boolean positioned, Boolean recoverable, @Nullable StreamPosition streamPosition, @Nullable byte[] data) {
this.wasRecovering = wasRecovering;
this.recovered = recovered;
this.data = data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void sendRefresh() {
);
return;
}
if (token.equals("")) {
if (token == null || token.equals("")) {
this.failUnauthorized(true);
return;
}
Expand All @@ -110,8 +110,9 @@ void sendRefresh() {
if (Subscription.this.getState() != SubscriptionState.SUBSCRIBED) {
return;
}
if (error != null) {
Subscription.this.listener.onError(Subscription.this, new SubscriptionErrorEvent(new SubscriptionRefreshError(error)));
Throwable errorOrNull = error != null ? error : (result == null ? new NullPointerException() : null);
if (errorOrNull != null) {
Subscription.this.listener.onError(Subscription.this, new SubscriptionErrorEvent(new SubscriptionRefreshError(errorOrNull)));
if (error instanceof ReplyError) {
ReplyError e;
e = (ReplyError) error;
Expand Down Expand Up @@ -273,7 +274,7 @@ void sendSubscribe() {
Subscription.this.scheduleResubscribe();
return;
}
if (token.equals("")) {
if (token == null || token.equals("")) {
Subscription.this.failUnauthorized(false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.centrifugal.centrifuge;

import javax.annotation.Nullable;

public interface TokenCallback {
/* Call this with exception or token. If called with null error and empty token then client considers access unauthorized */
void Done(Throwable e, String token);
void Done(@Nullable Throwable e, @Nullable String token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* File: package-info.java
* Make all method parameters @NonNull by default
*/
@ParametersAreNonnullByDefault
FZambia marked this conversation as resolved.
Show resolved Hide resolved
package io.github.centrifugal.centrifuge;

import javax.annotation.ParametersAreNonnullByDefault;