Java Client API review cleanup (#2956)

This commit is contained in:
Mikael Mengistu 2018-09-15 16:43:16 -04:00 committed by GitHub
parent 48b3f18642
commit 529cfeeff8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 33 deletions

View File

@ -5,9 +5,9 @@ package com.microsoft.aspnet.signalr;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.Collections;
class CallbackMap { class CallbackMap {
private ConcurrentHashMap<String, List<InvocationHandler>> handlers = new ConcurrentHashMap<>(); private ConcurrentHashMap<String, List<InvocationHandler>> handlers = new ConcurrentHashMap<>();

View File

@ -249,6 +249,24 @@ public class HubConnection {
transport.send(message); 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. * Registers a handler that will be invoked when the hub method with the specified method name is invoked.
* *
@ -515,24 +533,6 @@ public class HubConnection {
return new Subscription(handlers, handler, target); 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 { private class ConnectionState implements InvocationBinder {
HubConnection connection; HubConnection connection;

View File

@ -3,7 +3,6 @@
package com.microsoft.aspnet.signalr; package com.microsoft.aspnet.signalr;
import java.io.IOException;
/** /**
* A protocol abstraction for communicating with SignalR hubs. * A protocol abstraction for communicating with SignalR hubs.

View File

@ -3,7 +3,6 @@
package com.microsoft.aspnet.signalr; package com.microsoft.aspnet.signalr;
import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -12,7 +11,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
class JsonHubProtocol implements HubProtocol { class JsonHubProtocol implements HubProtocol {
private final JsonParser jsonParser = new JsonParser(); private final JsonParser jsonParser = new JsonParser();
@ -31,7 +29,7 @@ class JsonHubProtocol implements HubProtocol {
@Override @Override
public TransferFormat getTransferFormat() { public TransferFormat getTransferFormat() {
return TransferFormat.Text; return TransferFormat.TEXT;
} }
@Override @Override
@ -120,7 +118,7 @@ class JsonHubProtocol implements HubProtocol {
case CANCEL_INVOCATION: case CANCEL_INVOCATION:
throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType)); throw new UnsupportedOperationException(String.format("The message type %s is not supported yet.", messageType));
case PING: case PING:
hubMessages.add(new PingMessage()); hubMessages.add(PingMessage.getInstance());
break; break;
case CLOSE: case CLOSE:
if (error != null) { if (error != null) {

View File

@ -3,12 +3,18 @@
package com.microsoft.aspnet.signalr; 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;}
@Override @Override
public HubMessageType getMessageType() { public HubMessageType getMessageType() {
return HubMessageType.PING; return HubMessageType.PING;
} }
} }

View File

@ -4,6 +4,6 @@
package com.microsoft.aspnet.signalr; package com.microsoft.aspnet.signalr;
public enum TransferFormat { public enum TransferFormat {
Text, TEXT,
Binary BINARY
} }

View File

@ -8,13 +8,11 @@ import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import com.google.gson.JsonArray;
public class JsonHubProtocolTest { public class JsonHubProtocolTest {
private JsonHubProtocol jsonHubProtocol = new JsonHubProtocol(); private JsonHubProtocol jsonHubProtocol = new JsonHubProtocol();
@ -31,7 +29,7 @@ public class JsonHubProtocolTest {
@Test @Test
public void checkTransferFormat() { public void checkTransferFormat() {
assertEquals(TransferFormat.Text, jsonHubProtocol.getTransferFormat()); assertEquals(TransferFormat.TEXT, jsonHubProtocol.getTransferFormat());
} }
@Test @Test
@ -45,7 +43,7 @@ public class JsonHubProtocolTest {
@Test @Test
public void parsePingMessage() throws Exception { public void parsePingMessage() throws Exception {
String stringifiedMessage = "{\"type\":6}\u001E"; String stringifiedMessage = "{\"type\":6}\u001E";
TestBinder binder = new TestBinder(new PingMessage()); TestBinder binder = new TestBinder(PingMessage.getInstance());
HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage, binder); HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage, binder);