Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License (MIT)
* Copyright (c) 2017 [email protected]
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package us.levk.rserve.client.websocket;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static java.util.Optional.ofNullable;
import static java.util.stream.IntStream.range;
import static us.levk.rserve.client.protocol.Qap.packet;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import com.fasterxml.jackson.databind.ObjectMapper;
import us.levk.rserve.client.Client;
import us.levk.rserve.client.protocol.commands.Command;
/**
* Websocket client endpoint
*
* @author levk
*/
@ClientEndpoint
public class Endpoint implements Client {
/**
* Mapper
*/
private final ObjectMapper mapper;
/**
* Async provider
*/
private final ExecutorService executor;
/**
* Websocket session
*/
private Session session;
/**
* Current command
*/
private Command > current;
/**
* Receiver for the current command result
*/
private final AtomicReference > receiver =
new AtomicReference <> (new CompletableFuture ());
/**
* Command queue
*/
private final AtomicReference > queue = new AtomicReference <> (receiver.get ());
/**
* @param m
* mapper
* @param e
* executor
*/
public Endpoint (ObjectMapper m, ExecutorService e) {
mapper = m;
executor = e;
}
/**
* @param s
* websocket session
*/
@OnOpen
public void connect (Session s) {
session = s;
}
/**
* @param w
* welcome wagon
* @throws IOException
* on unsupported welcome wagon
*/
@OnMessage
public void handshake (String w) throws IOException {
if (!HANDSHAKE_PATTERN.matcher (w).matches ()) throw new IOException ("Unsupported handshake wagon " + w);
receiver.getAndSet (null).complete (null);
}
/**
* @param i
* input
* @throws IOException
* on read or decoding failure
*/
@SuppressWarnings ("unchecked")
@OnMessage
public void receive (ByteBuffer i) throws IOException {
Object v = current.decode (packet (i.order (LITTLE_ENDIAN)), mapper);
((CompletableFuture