Search code examples
javahttpredpitaya

Connecting to Red Pitaya application from Java


I am about to write a Java Program that connects to a redpitaya and recieves data from the websocket. I successfully can connect to the redpitaya using a HttpClient. However the connection to the websocket fails. When I start the application in a browser, the connection to the websocket succeeds. It looks like when I connect to the RP, the requested application does not start. (tested with scopegenpro and streaming_manager). Has anyone an idea what I must send to the RP so that the application starts?

package redpitaya;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.Iterator;
import java.util.List;
import java.util.zip.GZIPInputStream;


import jakarta.websocket.ContainerProvider;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.MessageHandler;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;
import jakarta.websocket.RemoteEndpoint.Basic;

public class RPClient {


    final static String hostName="192.168.1.1";
    final static int port = 9002;
    static RPClient clientEndPoint;
    Session session;
    final static String ApplicationId = "scopegenpro";
    final static String type = "run";

    public RPClient(URI uri) throws InterruptedException {

        HttpClient client = HttpClient.newHttpClient();
        try {
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(new URI("http://" + hostName + "/" + ApplicationId + "/"))
                    .GET()
                    .header("start", ApplicationId)
                    .header("type", type)
                    .build();
            System.out.println(request.uri().getPath());
            HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
            List<String> locations = response.headers().allValues("location");
            HttpRequest req = response.request();
            System.out.println(req);
            Iterator<String> locationIterator = locations.iterator();
            while (locationIterator.hasNext()) {
                System.out.println(locationIterator.next());
            }
            System.out.println(response.body());
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();  
            Endpoint endpoint = new SocketEndpoint();
            session = container.connectToServer(endpoint, uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
        while(true) {
            Thread.sleep(1000);
        }
    }

    public static void main(String[] args) throws URISyntaxException, InterruptedException {
        URI uri = new URI("ws://" + hostName + ":" + port + "/");


        clientEndPoint = new RPClient(uri);
    }

    public class SocketEndpoint extends Endpoint{

        @Override
        public void onOpen(Session session, EndpointConfig config) {

            final Basic remote = session.getBasicRemote();
            session.addMessageHandler(InputStream.class, new MessageHandler.Whole<InputStream>() {
                public void onMessage(InputStream stream) {
                    try {
                        while (stream.available() > 0) {
                            GZIPInputStream is = new GZIPInputStream(stream);
                            byte[] gzip = is.readAllBytes();
                            System.out.println("received: " + new String(gzip));
                        }
                        System.out.println();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }); 


        }

        @Override
        public void onError(Session session, Throwable t) {
            System.out.println(session.getQueryString());
            System.out.println(t.getMessage());
        }

    }

}

and the maven pom.xml for libraries:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>redpitaya</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>


<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jakartaee>9.0.0-RC3</jakartaee>
</properties>

<dependencies>
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>${jakartaee}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.tyrus.bundles</groupId>
        <artifactId>tyrus-standalone-client</artifactId>
        <version>2.0.0-M3</version>
    </dependency>
    <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <version>1.1</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
            </configuration>
        </plugin>

    </plugins>
</build>
</project>

and the stack trace:

jakarta.websocket.DeploymentException: Connection failed.
    at  org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket._connect(GrizzlyClientSocket.java:413)
    at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket.access$000(GrizzlyClientSocket.java:81)
    at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket$1.call(GrizzlyClientSocket.java:212)
    at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket$1.call(GrizzlyClientSocket.java:208)
    at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientSocket.connect(GrizzlyClientSocket.java:226)
    at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientContainer.openClientSocket(GrizzlyClientContainer.java:72)
    at org.glassfish.tyrus.client.ClientManager$3$1.run(ClientManager.java:647)
    at org.glassfish.tyrus.client.ClientManager$3.run(ClientManager.java:696)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at org.glassfish.tyrus.client.ClientManager$SameThreadExecutorService.execute(ClientManager.java:849)
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:493)
    at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:355)
    at redpitaya.RPClient.<init>(RPClient.java:57)
    at redpitaya.RPClient.main(RPClient.java:70)
Caused by: java.net.ConnectException: Connection refused: getsockopt
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
at org.glassfish.grizzly.nio.transport.TCPNIOConnectorHandler.onConnectedAsync(TCPNIOConnectorHandler.java:175)
at org.glassfish.grizzly.nio.transport.TCPNIOConnectorHandler$1.connected(TCPNIOConnectorHandler.java:132)
at org.glassfish.grizzly.nio.transport.TCPNIOConnection.onConnect(TCPNIOConnection.java:220)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:502)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:82)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:83)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.executeIoEvent(WorkerThreadIOStrategy.java:73)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.executeIoEvent(AbstractIOStrategy.java:66)
at org.glassfish.grizzly.nio.SelectorRunner.iterateKeyEvents(SelectorRunner.java:381)
at org.glassfish.grizzly.nio.SelectorRunner.iterateKeys(SelectorRunner.java:353)
at org.glassfish.grizzly.nio.SelectorRunner.doSelect(SelectorRunner.java:319)
at org.glassfish.grizzly.nio.SelectorRunner.run(SelectorRunner.java:248)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:535)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:515)
at java.base/java.lang.Thread.run(Thread.java:840)

Solution

  • I finally got it running. The correct HttpRequest must be:

                HttpRequest request = HttpRequest.newBuilder()
                    .uri(new URI("http://" + hostName + "/" + "bazaar" + "?start=" + ApplicationId + "?type=run"))
                    .GET()
                    .build();