public DisconnectionFrame(byte magic, FrameType type, byte version, byte code, byte[] data) { super(magic, type, version); this.code = code; this.data = data; if (data != null) { Assert.isTrue(data.length <= MAX_PAYLOAD_LENGTH, "The data length must be not greater than the max payload length"); } }
public Frame(byte magic, FrameType type, byte version) { Assert.isTrue(magic == MAGIC, "The protocol format error"); this.magic = magic; this.type = type; this.version = version; }
public MessageFrame(byte magic, FrameType type, byte version, boolean endStream, int streamId, boolean endFrame, byte[] data) { super(magic, type, version); this.endStream = endStream; this.streamId = streamId; this.endFrame = endFrame; this.data = data; if (data != null) { Assert.isTrue(data.length <= MAX_PAYLOAD_LENGTH, "The data length must be not greater than the max payload length"); } }
public DisconnectionFrame(byte magic, FrameType type, byte version, byte code, byte[] data) { super(magic, type, version); this.code = code; this.data = data; if (data != null) { Assert.isTrue(data.length <= MAX_PAYLOAD_LENGTH, "The data length must be not greater than the max payload length"); } }
public MessageFrame(byte magic, FrameType type, byte version, boolean endStream, int streamId, boolean endFrame, byte[] data) { super(magic, type, version); this.endStream = endStream; this.streamId = streamId; this.endFrame = endFrame; this.data = data; if (data != null) { Assert.isTrue(data.length <= MAX_PAYLOAD_LENGTH, "The data length must be not greater than the max payload length"); } }
public Frame(byte magic, FrameType type, byte version) { Assert.isTrue(magic == MAGIC, "The protocol format error"); this.magic = magic; this.type = type; this.version = version; }
/** * Assert a boolean expression, throwing {@code IllegalArgumentException} if * the test result is {@code false}. * * <pre class="code"> * Assert.isTrue(i > 0); * </pre> * * @param expression a boolean expression * @throws IllegalArgumentException if expression is {@code false} */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
/** * Assert a boolean expression, throwing {@code IllegalArgumentException} if * the test result is {@code false}. * * <pre class="code"> * Assert.isTrue(i > 0); * </pre> * * @param expression a boolean expression * @throws IllegalArgumentException if expression is {@code false} */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
/** * Assert a boolean expression, throwing {@code IllegalArgumentException} if * the test result is {@code false}. * * <pre class="code"> * Assert.isTrue(i > 0); * </pre> * * @param expression * a boolean expression * @throws IllegalArgumentException * if expression is {@code false} */ public static void isTrue(boolean expression) { isTrue(expression, "[Assertion failed] - this expression must be true"); }
/** * The exponential increase sleep time. The formula: initTime * 2 ^ ((count - 1) * (multiple - 1)). * * @param initTime The first sleep time. * @param maxTime The max sleep time. * @param timeUnit The time unit. * @param multiple The growth factor. * @param <V> The return value type. * @return The wait strategy action. */ public static <V> Action1<TaskContext<V>> exponentialWait(long initTime, long maxTime, TimeUnit timeUnit, int multiple) { Assert.isTrue(multiple > 0, "The multiple must be great than 0"); Assert.isTrue(maxTime >= initTime, "The max time must be great than or equals init time"); return ctx -> { int count = Math.max(ctx.getExecutedCount(), 1) - 1; long sleepTime = initTime << (count * (multiple - 1)); ThreadUtils.sleep(Math.min(sleepTime, maxTime), timeUnit); }; }
private void write(Path path, List<SourceCode> sourceCodes, String fileSuffix) { Assert.isTrue(Files.isDirectory(path), "The path must be a directory"); sourceCodes.forEach(s -> { Path sourcePath = Paths.get(path.toAbsolutePath().toString(), s.getName() + "." + fileSuffix); try (FileChannel channel = FileChannel.open(sourcePath, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE)) { channel.write(BufferUtils.toBuffer(s.getCodes())); } catch (IOException e) { e.printStackTrace(); } }); }
private void write(Path path, List<SourceCode> sourceCodes, String fileSuffix) { Assert.isTrue(Files.isDirectory(path), "The path must be a directory"); sourceCodes.forEach(s -> { Path sourcePath = Paths.get(path.toAbsolutePath().toString(), s.getName() + "." + fileSuffix); try (FileChannel channel = FileChannel.open(sourcePath, StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE)) { channel.write(BufferUtils.toBuffer(s.getCodes())); } catch (IOException e) { e.printStackTrace(); } }); }
/** * The exponential increase sleep time. The formula: initTime * 2 ^ ((count - 1) * (multiple - 1)). * * @param initTime The first sleep time. * @param maxTime The max sleep time. * @param timeUnit The time unit. * @param multiple The growth factor. * @param <V> The return value type. * @return The wait strategy action. */ public static <V> Action1<TaskContext<V>> exponentialWait(long initTime, long maxTime, TimeUnit timeUnit, int multiple) { Assert.isTrue(multiple > 0, "The multiple must be great than 0"); Assert.isTrue(maxTime >= initTime, "The max time must be great than or equals init time"); return ctx -> { int count = Math.max(ctx.getExecutedCount(), 1) - 1; long sleepTime = initTime << (count * (multiple - 1)); ThreadUtils.sleep(Math.min(sleepTime, maxTime), timeUnit); }; }
/** * Create a new {@code ConcurrentReferenceHashMap} instance. * * @param initialCapacity the initial capacity of the map * @param loadFactor the load factor. When the average number of references per * table exceeds this value, resize will be attempted. * @param concurrencyLevel the expected number of threads that will concurrently write to * the map * @param referenceType the reference type used for entries (soft or weak) */ @SuppressWarnings("unchecked") public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ReferenceType referenceType) { Assert.isTrue(initialCapacity >= 0, "Initial capacity must not be negative"); Assert.isTrue(loadFactor > 0f, "Load factor must be positive"); Assert.isTrue(concurrencyLevel > 0, "Concurrency level must be positive"); Assert.notNull(referenceType, "Reference type must not be null"); this.loadFactor = loadFactor; this.shift = calculateShift(concurrencyLevel, MAXIMUM_CONCURRENCY_LEVEL); int size = 1 << this.shift; this.referenceType = referenceType; int roundedUpSegmentCapacity = (int) ((initialCapacity + size - 1L) / size); this.segments = (Segment[]) Array.newInstance(Segment.class, size); for (int i = 0; i < this.segments.length; i++) { this.segments[i] = new Segment(roundedUpSegmentCapacity); } }
/** * Create a new {@code ConcurrentReferenceHashMap} instance. * * @param initialCapacity the initial capacity of the map * @param loadFactor the load factor. When the average number of references per * table exceeds this value, resize will be attempted. * @param concurrencyLevel the expected number of threads that will concurrently write to * the map * @param referenceType the reference type used for entries (soft or weak) */ @SuppressWarnings("unchecked") public ConcurrentReferenceHashMap(int initialCapacity, float loadFactor, int concurrencyLevel, ReferenceType referenceType) { Assert.isTrue(initialCapacity >= 0, "Initial capacity must not be negative"); Assert.isTrue(loadFactor > 0f, "Load factor must be positive"); Assert.isTrue(concurrencyLevel > 0, "Concurrency level must be positive"); Assert.notNull(referenceType, "Reference type must not be null"); this.loadFactor = loadFactor; this.shift = calculateShift(concurrencyLevel, MAXIMUM_CONCURRENCY_LEVEL); int size = 1 << this.shift; this.referenceType = referenceType; int roundedUpSegmentCapacity = (int) ((initialCapacity + size - 1L) / size); this.segments = (Segment[]) Array.newInstance(Segment.class, size); for (int i = 0; i < this.segments.length; i++) { this.segments[i] = new Segment(roundedUpSegmentCapacity); } }
ReferenceType referenceType) { Assert.isTrue(initialCapacity >= 0, "Initial capacity must not be negative"); Assert.isTrue(loadFactor > 0f, "Load factor must be positive"); Assert.isTrue(concurrencyLevel > 0, "Concurrency level must be positive"); Assert.notNull(referenceType, "Reference type must not be null"); this.loadFactor = loadFactor;
@Override public boolean verifyEchoString(EchoRequest request) { Assert.hasText(request.getEchostr(), "The echo string must be not empty"); Assert.hasText(request.getSignature(), "The signature string must be not empty"); Assert.hasText(request.getNonce(), "The nonce string must be not empty"); Assert.notNull(request.getTimestamp(), "The timestamp must be not null"); Assert.isTrue(request.getTimestamp() > 0, "The timestamp string must be greater than 0"); TreeSet<String> set = new TreeSet<>(Arrays.asList(request.getNonce(), request.getTimestamp().toString(), wechatToken)); StringBuilder sign = new StringBuilder(); set.forEach(sign::append); try { String originSign = sign.toString(); String hexSign = HexUtils.bytesToHex(MessageDigest.getInstance("SHA-1").digest(originSign.getBytes(StandardCharsets.US_ASCII))); log.info("verify echo string. {} | {} | {}", originSign, hexSign, request.getSignature()); return request.getSignature().equals(hexSign); } catch (Exception e) { log.error("verify echo string exception", e); return false; } }
@Override public void upgradeWebSocket(Request request, WebSocketPolicy policy, Promise<WebSocketConnection> promise, ClientHTTPHandler upgradeHandler, IncomingFrames incomingFrames) { Assert.isTrue(HttpMethod.GET.is(request.getMethod()), "The method of the request MUST be GET in the websocket handshake."); Assert.isTrue(policy.getBehavior() == WebSocketBehavior.CLIENT, "The websocket behavior MUST be client"); request.getFields().put(HttpHeader.SEC_WEBSOCKET_VERSION, String.valueOf(SPEC_VERSION)); request.getFields().put(HttpHeader.UPGRADE, "websocket"); request.getFields().put(HttpHeader.CONNECTION, "Upgrade"); request.getFields().put(HttpHeader.SEC_WEBSOCKET_KEY, genRandomKey()); webSocketConnectionPromise = promise; this.incomingFrames = incomingFrames; this.policy = policy; send(request, upgradeHandler); }
@Override public void upgradeWebSocket(Request request, WebSocketPolicy policy, Promise<WebSocketConnection> promise, ClientHTTPHandler upgradeHandler, IncomingFrames incomingFrames) { Assert.isTrue(HttpMethod.GET.is(request.getMethod()), "The method of the request MUST be GET in the websocket handshake."); Assert.isTrue(policy.getBehavior() == WebSocketBehavior.CLIENT, "The websocket behavior MUST be client"); request.getFields().put(HttpHeader.SEC_WEBSOCKET_VERSION, String.valueOf(SPEC_VERSION)); request.getFields().put(HttpHeader.UPGRADE, "websocket"); request.getFields().put(HttpHeader.CONNECTION, "Upgrade"); request.getFields().put(HttpHeader.SEC_WEBSOCKET_KEY, genRandomKey()); webSocketConnectionPromise = promise; this.incomingFrames = incomingFrames; this.policy = policy; send(request, upgradeHandler); }