|
| 1 | +package com.microsoft.signalr; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Base64; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import org.msgpack.core.MessageInsufficientBufferException; |
| 15 | +import org.msgpack.core.MessagePack; |
| 16 | +import org.msgpack.core.MessagePackException; |
| 17 | +import org.msgpack.core.MessagePacker; |
| 18 | +import org.msgpack.core.MessageUnpacker; |
| 19 | + |
| 20 | +public class testStuff { |
| 21 | + |
| 22 | + public TestBinder binder; |
| 23 | + |
| 24 | + public testStuff(HubMessage hm) { |
| 25 | + binder = new TestBinder(hm); |
| 26 | + } |
| 27 | + |
| 28 | + public static void main(String[] args) throws IOException { |
| 29 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 30 | + MessagePacker packer = MessagePack.newDefaultPacker(out); |
| 31 | + |
| 32 | + MessagePackHubProtocol prot = new MessagePackHubProtocol(); |
| 33 | + |
| 34 | + System.out.println(prot.getName()); |
| 35 | + System.out.println(prot.getVersion()); |
| 36 | + System.out.println(prot.getTransferFormat()); |
| 37 | + |
| 38 | + Map<String, String> headers = new HashMap<String, String>(); |
| 39 | + headers.put("key", "value"); |
| 40 | + |
| 41 | + Map<String, List<String>> map = new HashMap<String, List<String>>(); |
| 42 | + List<String> list = new ArrayList<String>(); |
| 43 | + list.add("abc"); |
| 44 | + map.put("ding", list); |
| 45 | + InvocationMessage invocationMessage = new InvocationMessage(null, "1", "test", new Object[] {42, map}, null); |
| 46 | + StreamItem streamItem = new StreamItem(headers, "test", 69); |
| 47 | + |
| 48 | + String result = prot.writeMessage(invocationMessage); |
| 49 | + String itemResult = prot.writeMessage(streamItem); |
| 50 | + byte[] bytes = result.getBytes(StandardCharsets.ISO_8859_1); |
| 51 | + for (byte b: bytes) { |
| 52 | + System.out.printf("0x%02X ", b); |
| 53 | + } |
| 54 | + System.out.println(); |
| 55 | + byte[] itemBytes = itemResult.getBytes(StandardCharsets.ISO_8859_1); |
| 56 | + for (byte b: itemBytes) { |
| 57 | + System.out.printf("0x%02X ", b); |
| 58 | + } |
| 59 | + System.out.println(); |
| 60 | + |
| 61 | + testStuff ts = new testStuff(invocationMessage); |
| 62 | + |
| 63 | + HubMessage[] messages = prot.parseMessages(result + itemResult, ts.binder); |
| 64 | + |
| 65 | + InvocationMessage im = (InvocationMessage) messages[0]; |
| 66 | + System.out.println(im.getInvocationId()); |
| 67 | + System.out.println(im.getTarget()); |
| 68 | + System.out.println(im.getArguments()[0]); |
| 69 | + System.out.println(im.getArguments()[1]); |
| 70 | + System.out.println(im.getHeaders()); |
| 71 | + System.out.println(im.getStreamIds()); |
| 72 | + |
| 73 | + System.out.println(); |
| 74 | + StreamItem sm = (StreamItem) messages[1]; |
| 75 | + System.out.println(sm.getInvocationId()); |
| 76 | + System.out.println(sm.getItem()); |
| 77 | + System.out.println(sm.getHeaders()); |
| 78 | + |
| 79 | + System.out.println(); |
| 80 | + |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + public static Object get(int i) { |
| 85 | + if (i % 4 == 0) { |
| 86 | + return 2; |
| 87 | + } if (i % 4 == 1) { |
| 88 | + return false; |
| 89 | + } else if (i % 4 == 2) { |
| 90 | + return "butter"; |
| 91 | + } else { |
| 92 | + List<Object> objects = new ArrayList<Object>(); |
| 93 | + objects.add(get(2)); |
| 94 | + return objects; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private class TestBinder implements InvocationBinder { |
| 99 | + private Class<?>[] paramTypes = null; |
| 100 | + private Class<?> returnType = Integer.class; |
| 101 | + |
| 102 | + public TestBinder(HubMessage expectedMessage) { |
| 103 | + if (expectedMessage == null) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + switch (expectedMessage.getMessageType()) { |
| 108 | + case STREAM_INVOCATION: |
| 109 | + ArrayList<Class<?>> streamTypes = new ArrayList<>(); |
| 110 | + for (Object obj : ((StreamInvocationMessage) expectedMessage).getArguments()) { |
| 111 | + streamTypes.add(obj.getClass()); |
| 112 | + } |
| 113 | + paramTypes = streamTypes.toArray(new Class<?>[streamTypes.size()]); |
| 114 | + break; |
| 115 | + case INVOCATION: |
| 116 | + ArrayList<Class<?>> types = new ArrayList<>(); |
| 117 | + for (Object obj : ((InvocationMessage) expectedMessage).getArguments()) { |
| 118 | + types.add(obj.getClass()); |
| 119 | + } |
| 120 | + paramTypes = types.toArray(new Class<?>[types.size()]); |
| 121 | + break; |
| 122 | + case STREAM_ITEM: |
| 123 | + break; |
| 124 | + case COMPLETION: |
| 125 | + returnType = ((CompletionMessage)expectedMessage).getResult().getClass(); |
| 126 | + break; |
| 127 | + default: |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public Class<?> getReturnType(String invocationId) { |
| 134 | + return returnType; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public List<Class<?>> getParameterTypes(String methodName) { |
| 139 | + if (paramTypes == null) { |
| 140 | + return new ArrayList<>(); |
| 141 | + } |
| 142 | + return new ArrayList<Class<?>>(Arrays.asList(paramTypes)); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments