Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Java Client API review cleanup #2956

Merged
merged 3 commits into from Sep 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,9 +5,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Collections;

class CallbackMap {
private ConcurrentHashMap<String, List<InvocationHandler>> handlers = new ConcurrentHashMap<>();
Expand Down
Expand Up @@ -249,6 +249,24 @@ public void send(String method, Object... args) throws Exception {
transport.send(message);
}

/**
* Removes all handlers associated with the method with the specified method name.
*
* @param name The name of the hub method from which handlers are being removed.
*/
public void remove(String name) {
handlers.remove(name);
logger.log(LogLevel.Trace, "Removing handlers for client method %s", name);
}

public void onClosed(Consumer<Exception> callback) {
if (onClosedCallbackList == null) {
onClosedCallbackList = new ArrayList<>();
}

onClosedCallbackList.add(callback);
}

/**
* Registers a handler that will be invoked when the hub method with the specified method name is invoked.
*
Expand Down Expand Up @@ -515,24 +533,6 @@ public <T1, T2, T3, T4, T5, T6, T7, T8> Subscription on(String target, Action8<T
return new Subscription(handlers, handler, target);
}

/**
* Removes all handlers associated with the method with the specified method name.
*
* @param name The name of the hub method from which handlers are being removed.
*/
public void remove(String name) {
handlers.remove(name);
logger.log(LogLevel.Trace, "Removing handlers for client method %s", name);
}

public void onClosed(Consumer<Exception> callback) {
if (onClosedCallbackList == null) {
onClosedCallbackList = new ArrayList<>();
}

onClosedCallbackList.add(callback);
}

private class ConnectionState implements InvocationBinder {
HubConnection connection;

Expand Down
Expand Up @@ -3,7 +3,6 @@

package com.microsoft.aspnet.signalr;

import java.io.IOException;

/**
* A protocol abstraction for communicating with SignalR hubs.
Expand Down
Expand Up @@ -3,7 +3,6 @@

package com.microsoft.aspnet.signalr;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -12,7 +11,6 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;

class JsonHubProtocol implements HubProtocol {
private final JsonParser jsonParser = new JsonParser();
Expand All @@ -31,7 +29,7 @@ public int getVersion() {

@Override
public TransferFormat getTransferFormat() {
return TransferFormat.Text;
return TransferFormat.TEXT;
}

@Override
Expand Down Expand Up @@ -120,7 +118,7 @@ public HubMessage[] parseMessages(String payload, InvocationBinder binder) throw
case CANCEL_INVOCATION:
throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType));
case PING:
hubMessages.add(new PingMessage());
hubMessages.add(PingMessage.getInstance());
break;
case CLOSE:
if (error != null) {
Expand Down
Expand Up @@ -3,12 +3,18 @@

package com.microsoft.aspnet.signalr;

class PingMessage extends HubMessage {
class PingMessage extends HubMessage
{
private static PingMessage instance = new PingMessage();

int type = HubMessageType.PING.value;
private PingMessage()
{
}

public static PingMessage getInstance() {return instance;}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: formatting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we always want a new line after a bracket? I'll add a spotless rule

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more about the spaces here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I get that, but it's easier to normalize and write rules for if we just put newlines


@Override
public HubMessageType getMessageType() {
return HubMessageType.PING;
}
}
}
Expand Up @@ -4,6 +4,6 @@
package com.microsoft.aspnet.signalr;

public enum TransferFormat {
Text,
Binary
TEXT,
BINARY
}
Expand Up @@ -8,13 +8,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.google.gson.JsonArray;

public class JsonHubProtocolTest {
private JsonHubProtocol jsonHubProtocol = new JsonHubProtocol();
Expand All @@ -31,7 +29,7 @@ public void checkVersionNumber() {

@Test
public void checkTransferFormat() {
assertEquals(TransferFormat.Text, jsonHubProtocol.getTransferFormat());
assertEquals(TransferFormat.TEXT, jsonHubProtocol.getTransferFormat());
}

@Test
Expand All @@ -45,7 +43,7 @@ public void verifyWriteMessage() {
@Test
public void parsePingMessage() throws Exception {
String stringifiedMessage = "{\"type\":6}\u001E";
TestBinder binder = new TestBinder(new PingMessage());
TestBinder binder = new TestBinder(PingMessage.getInstance());

HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage, binder);

Expand Down